javascript - How does JUMflot redraw points/choose the item? Trying to add items into an array and redraw them without affecting the original item -
essentially i'm trying have button .push line array , want jumflot maintain redrawing not impact line beingness pushed in.
what had done have (the generator id button , options defined not relevant question didn't include them):
var genvector=[[0,1],[6,6]]; //line beingness pushed in var info = []; $("#generator").click(function(){ data.push({data: genvector, editable: true}); var p = $.plot($("#graph"),data,options); }); $("#graph").bind("datadrop", function(event,pos,item) { data[item.seriesindex].data[item.dataindex] = [math.round(pos.x1),math.round(pos.y1)]; p = $.plot($("#graph"), data, options); };
what realized end happening, every time chose move point genvector coordinates change. work around did following:
var vectorcounter=0; var genvector=[[0,1],[6,6]]; var info = []; var vectorarray=[]; $("#generator").click(function(){ vectorarray.push(genvector); data.push({data: vectorarray[vectorcounter], editable: true}); vectorcounter++; var p = $.plot($("#graph"),data,options); }); $("#graph").bind("datadrop", function(event,pos,item) { data[item.seriesindex].data[item.dataindex] = [math.round(pos.x1),math.round(pos.y1)]; p = $.plot($("#graph"), data, options); };
i thought adding vector separate array , modifying there leave genvector alone. did not.
i'm assuming has how jumflot code written? if not, how can accomplish i'm doing? in advance!
this has nil jumflot rather question on object references.
when force genvector vectorarray:
vectorarray.push(genvector);
this not create new array within vector array, rather vectorarray
holds reference genvector
.
so when force vectorarray[0]
, data
getting reference orginal genvector
. illustrated by:
> data[0].data[0] = "mark awesome"; "mark awesome" > genvector ["mark awesome", array[2] ]
this whole concept can more shown with:
> x = [1,2,3]; [1, 2, 3] > y = x [1, 2, 3] > y[0] = "mark awesome" "mark awesome" > x ["mark awesome", 2, 3]
so question becomes, how re-create or clone array? easiest using slice trick:
> x = [1,2,3]; [1, 2, 3] > y = x.slice(0); [1, 2, 3] > y[0] = "mark awesome" "mark awesome" > x [1, 2, 3]
this shallow copy, though, if array contains object reference, won't clone it:
> x = [{'a':'b'},2,3]; [object, 2, 3] > y = x.slice(0); [object, 2, 3] > y[0]['a'] = "mark awesome" "mark awesome" > x [object a: "mark awesome" __proto__: object , 2, 3]
if want clone everything, need utilize deep copy. won't go details on i'd duplicating first-class give-and-take here.
javascript jquery arrays logic flot
No comments:
Post a Comment