Well, I did a bit of work on Raphael.js, and the most efficient way I found was to separate the drawing logic from the data structure. I can't completely write the code here (too long = (), but it can give you some idea using code snippets that may be useful.
// Create Data Structure to keep seperate track of Elements and its attribute (assuming a Drawing Panel and only Line Type here) function DrawingPanelStructure(Type, Attributes){ this.Type = Type; this.Attributes = Attributes; } function PanelLineAttribute(Color,CurveDataX, CurveDataY) { this.Color = Color; this.CurveDataX = CurveDataX; this.CurveDataY = CurveDataY; } // Make Global Variables _drawingPanelStructure = new Object(); var ElementDrawnNumber = 0; // Keeping Track of Number of elements drawn // Then when Drawing the element, populate the Data Structure inside a function as _drawingPanelStructure[ElementDrawnNumber] = new DrawingPanelStructure("Line",new PanelLineAttribute("Red",[1,5,6,7,5], [5,1,8,6,8])); ElementDrawnNumber = ElementDrawnNumber + 1; // Then you can call a function to draw the Element at specific index as 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, {} ); // Since in this example there is only 1 line drawn on LineChart I'll use LineChart.lines[0] LineChart.lines[0].attr({ "stroke": _drawingPanelStructure[index].Attributes.Color});
This is also useful in the sense that when drawing an element, you can give it a unique identifier.
ElementDrawn.id = "Element_" + ElementDrawnNumber;
This way you will be sure that Element_3 means the element in the third _drawingPanelStructure index.
Therefore, separate the drawing logic from the data structure, i.e. fill in the data structure, and then pass the data structure to some function, which then makes the entire drawing in the panel.