ios - SHGraphLineView remove a plot -
i'm using class called shlinegraphview
, it's working good, have problem didn't implemented methods removing plot. have it, can't quite figure right way do.
this how drawing plot:
- (void)addplot:(shplot *)newplot; { if(nil == newplot) { return; } if(_plots == nil){ _plots = [nsmutablearray array]; } [_plots addobject:newplot]; } - (void)drawplot:(shplot *)plot { nsdictionary *theme = plot.plotthemeattributes; // cashapelayer *backgroundlayer = [cashapelayer layer]; backgroundlayer.frame = self.bounds; backgroundlayer.fillcolor = ((uicolor *)theme[kplotfillcolorkey]).cgcolor; backgroundlayer.backgroundcolor = [uicolor clearcolor].cgcolor; [backgroundlayer setstrokecolor:[uicolor clearcolor].cgcolor]; [backgroundlayer setlinewidth:((nsnumber *)theme[kplotstrokewidthkey]).intvalue]; cgmutablepathref backgroundpath = cgpathcreatemutable(); // cashapelayer *circlelayer = [cashapelayer layer]; circlelayer.frame = self.bounds; circlelayer.fillcolor = ((uicolor *)theme[kplotpointfillcolorkey]).cgcolor; circlelayer.backgroundcolor = [uicolor clearcolor].cgcolor; [circlelayer setstrokecolor:((uicolor *)theme[kplotpointfillcolorkey]).cgcolor]; [circlelayer setlinewidth:((nsnumber *)theme[kplotstrokewidthkey]).intvalue]; cgmutablepathref circlepath = cgpathcreatemutable(); // cashapelayer *graphlayer = [cashapelayer layer]; graphlayer.frame = self.bounds; graphlayer.fillcolor = [uicolor clearcolor].cgcolor; graphlayer.backgroundcolor = [uicolor clearcolor].cgcolor; [graphlayer setstrokecolor:((uicolor *)theme[kplotstrokecolorkey]).cgcolor]; [graphlayer setlinewidth:((nsnumber *)theme[kplotstrokewidthkey]).intvalue]; cgmutablepathref graphpath = cgpathcreatemutable(); double yrange = [_yaxisrange doublevalue]; // value in dollars double yintervalvalue = yrange / interval_count; //logic fill graph path, ciricle path, background path. [plot.plottingvalues enumerateobjectsusingblock:^(id obj, nsuinteger idx, bool *stop) { nsdictionary *dic = (nsdictionary *)obj; __block nsnumber *_key = nil; __block nsnumber *_value = nil; [dic enumeratekeysandobjectsusingblock:^(id key, id obj, bool *stop) { _key = (nsnumber *)key; _value = (nsnumber *)obj; }]; int xindex = [self getindexforvalue:_key forplot:plot]; //x value double height = self.bounds.size.height - bottom_margin_to_leave; double y = height - ((height / ([_yaxisrange doublevalue] + yintervalvalue)) * [_value doublevalue]); (plot.xpoints[xindex]).x = ceil((plot.xpoints[xindex]).x); (plot.xpoints[xindex]).y = ceil(y); }]; //move initial point path , background. cgpathmovetopoint(graphpath, null, _leftmargintoleave, plot.xpoints[0].y); cgpathmovetopoint(backgroundpath, null, _leftmargintoleave, plot.xpoints[0].y); int count = _xaxisvalues.count; for(int i=0; i< count; i++){ cgpoint point = plot.xpoints[i]; cgpathaddlinetopoint(graphpath, null, point.x, point.y); cgpathaddlinetopoint(backgroundpath, null, point.x, point.y); cgpathaddellipseinrect(circlepath, null, cgrectmake(point.x - 2.5, point.y - 2.5, 5, 5)); } //move initial point path , background. cgpathaddlinetopoint(graphpath, null, _leftmargintoleave + plot_width, plot.xpoints[count -1].y); cgpathaddlinetopoint(backgroundpath, null, _leftmargintoleave + plot_width, plot.xpoints[count - 1].y); //additional points background. cgpathaddlinetopoint(backgroundpath, null, _leftmargintoleave + plot_width, self.bounds.size.height - bottom_margin_to_leave); cgpathaddlinetopoint(backgroundpath, null, _leftmargintoleave, self.bounds.size.height - bottom_margin_to_leave); cgpathclosesubpath(backgroundpath); backgroundlayer.path = backgroundpath; graphlayer.path = graphpath; circlelayer.path = circlepath; //animation cabasicanimation *animation = [cabasicanimation animationwithkeypath:@"strokeend"]; animation.duration = 1; animation.fromvalue = @(0.0); animation.tovalue = @(1.0); [graphlayer addanimation:animation forkey:@"strokeend"]; backgroundlayer.zposition = 0; graphlayer.zposition = 1; circlelayer.zposition = 2; [self.layer addsublayer:graphlayer]; [self.layer addsublayer:circlelayer]; [self.layer addsublayer:backgroundlayer]; nsuinteger count2 = _xaxisvalues.count; for(int i=0; i< count2; i++){ cgpoint point = plot.xpoints[i]; uibutton *btn = [uibutton buttonwithtype:uibuttontypecustom]; btn.backgroundcolor = [uicolor clearcolor]; btn.tag = i; btn.frame = cgrectmake(point.x - 20, point.y - 20, 40, 40); [btn addtarget:self action:@selector(clicked:) forcontrolevents:uicontroleventtouchupinside]; objc_setassociatedobject(btn, kassociatedplotobject, plot, objc_association_retain_nonatomic); [self addsubview:btn]; } }
and i've been added, can't figure out how remove plot:
- (void)removeplot:(shplot *)plot { if (nil == plot) { return; } if (_plots == nil) { return; } [_plots removeobject:plot]; [self removeplotfromview:plot]; }
this need add in shlinegraphview.m file. method remove plots added on graphview.
- (void)removeallplots { (uiview *view in self.subviews) { view.layer.sublayers = nil; [view removefromsuperview]; } self.layer.sublayers = nil; }
for removing specific plot, first need give unique tag graphlayer, circlelayer , backgroundlayer.
you can adding following code in drawplot: method.
[graphlayer setvalue:[nsnumber numberwithinteger:[_plots indexofobject:plot]] forkey:@"pltag"]; [circlelayer.frame setvalue:[nsnumber numberwithinteger:[_plots indexofobject:plot]] forkey:@"pltag"]; [backgroundlayer setvalue:[nsnumber numberwithinteger:[_plots indexofobject:plot]] forkey:@"pltag"];
then, add method remove specific plot:
- (void)removeplot:(shplot *)plot { if (plot == nil) return; else if (_plots == nil) return; (calayer *layer in self.layer.sublayers) { if ([[layer valueforkey:@"pltag"] isequaltonumber:[nsnumber numberwithinteger:[_plots indexofobject:plot]]]) { [layer removefromsuperlayer]; } } }
hope helps you.
Comments
Post a Comment