i have points (x1,y1), (x2,y2), (x3,y3)...
now want draw chart smooth curve?
i'm trying draw below
-(void)drawprices { nsinteger count = self.prices.count; uibezierpath *path = [uibezierpath bezierpath]; path.linecapstyle = kcglinecapround; for(int i=0; i<count-1; i++) { cgpoint controlpoint[2]; cgpoint p = [self pointwithindex:i indata:self.prices]; if(i==0) { [path movetopoint:p]; } cgpoint nextpoint, previouspoint, m; nextpoint = [self pointwithindex:i+1 indata:self.prices]; previouspoint = [self pointwithindex:i-1 indata:self.prices]; if(i > 0) { m.x = (nextpoint.x - previouspoint.x) / 2; m.y = (nextpoint.y - previouspoint.y) / 2; } else { m.x = (nextpoint.x - p.x) / 2; m.y = (nextpoint.y - p.y) / 2; } controlpoint[0].x = p.x + m.x * 0.2; controlpoint[0].y = p.y + m.y * 0.2; // second control point nextpoint = [self pointwithindex:i+2 indata:self.prices]; previouspoint = [self pointwithindex:i indata:self.prices]; p = [self pointwithindex:i + 1 indata:self.prices]; m = zeropoint; if(i < self.prices.count - 2) { m.x = (nextpoint.x - previouspoint.x) / 2; m.y = (nextpoint.y - previouspoint.y) / 2; } else { m.x = (p.x - previouspoint.x) / 2; m.y = (p.y - previouspoint.y) / 2; } controlpoint[1].x = p.x - m.x * 0.2; controlpoint[1].y = p.y - m.y * 0.2; [path addcurvetopoint:p controlpoint1:controlpoint[0] controlpoint2:controlpoint[1]]; } cashapelayer *linelayer = [cashapelayer layer]; linelayer.path = path.cgpath; linelayer.linewidth = line_width; linelayer.strokecolor = _pricecolor.cgcolor; linelayer.fillcolor = [uicolor clearcolor].cgcolor; [self.layer addsublayer:linelayer]; }
but in situation, line "go back"
is there better way that?
i find answer @ draw graph curves uibezierpath
and try implement code
+ (uibezierpath *)quadcurvedpathwithpoints:(nsarray *)points { uibezierpath *path = [uibezierpath bezierpath]; nsvalue *value = points[0]; cgpoint p1 = [value cgpointvalue]; [path movetopoint:p1]; if (points.count == 2) { value = points[1]; cgpoint p2 = [value cgpointvalue]; [path addlinetopoint:p2]; return path; } (nsuinteger = 1; < points.count; i++) { value = points[i]; cgpoint p2 = [value cgpointvalue]; cgpoint midpoint = midpointforpoints(p1, p2); [path addquadcurvetopoint:midpoint controlpoint:controlpointforpoints(midpoint, p1)]; [path addquadcurvetopoint:p2 controlpoint:controlpointforpoints(midpoint, p2)]; p1 = p2; } return path; } static cgpoint midpointforpoints(cgpoint p1, cgpoint p2) { return cgpointmake((p1.x + p2.x) / 2, (p1.y + p2.y) / 2); } static cgpoint controlpointforpoints(cgpoint p1, cgpoint p2) { cgpoint controlpoint = midpointforpoints(p1, p2); cgfloat diffy = abs(p2.y - controlpoint.y); if (p1.y < p2.y) controlpoint.y += diffy; else if (p1.y > p2.y) controlpoint.y -= diffy; return controlpoint; }
thanks user1244109 ^_^.
Comments
Post a Comment