我在Raphael.js上工作过一段时间,发现最有效的方法是将绘图逻辑与数据结构分离。我无法在此处完全编写代码(太长了),但可以通过一些代码片段给您一些想法,这可能会有所帮助。
function DrawingPanelStructure(Type, Attributes){
this.Type = Type;
this.Attributes = Attributes;
}
function PanelLineAttribute(Color,CurveDataX, CurveDataY)
{
this.Color = Color;
this.CurveDataX = CurveDataX;
this.CurveDataY = CurveDataY;
}
_drawingPanelStructure = new Object();
var ElementDrawnNumber = 0;
_drawingPanelStructure[ElementDrawnNumber] = new DrawingPanelStructure("Line",new PanelLineAttribute("Red",[1,5,6,7,5], [5,1,8,6,8]));
ElementDrawnNumber = ElementDrawnNumber + 1;
var XData = [];
var YData =[];
XData.push(_drawingPanelStructure[index].Attributes.CurveDataX);
YData.push(_drawingPanelStructure[index].Attributes.CurveDataY);
var LineChart = Paper.linechart(0, 0, DrawinPanelWidth, DrawingPanelHeight, 0),
XData, YData, {}
);
LineChart.lines[0].attr({ "stroke": _drawingPanelStructure[index].Attributes.Color});
在绘制元素的同时为其指定唯一的 ID,这也是有帮助的。
ElementDrawn.id = "Element_" + ElementDrawnNumber;
那样你就可以确定Element_3是指_drawingPanelStructure中第三个索引处的元素。
因此,将绘图逻辑与数据结构分离,即填充数据结构,然后将数据结构传递给某个函数,该函数将在面板上执行所有绘图操作。