ios - Core Plot doesn't draw the axes, why? -
i'm learning core plot , i've come draw following graph of sinus function core plot 1.2:
as see, axes aren't drawn, if configure axes in following snippet.
cptmutablelinestyle *axislinestyle = [cptmutablelinestyle linestyle]; axislinestyle.linewidth = 2.0f; axislinestyle.linecolor = [cptcolor blackcolor]; cptxyaxisset *axisset = (cptxyaxisset *)self.graph.axisset; axisset.xaxis.axislinestyle = axislinestyle; axisset.yaxis.axislinestyle = axislinestyle;
my question is: why axes aren't shown? how can force coreplot draw them?.
here's full relevant code:
-(void)viewdidload { [super viewdidload]; self.graph = [[cptxygraph alloc] initwithframe:self.view.frame]; cptgraphhostingview *hostingview = (cptgraphhostingview *)self.view; hostingview.hostedgraph = self.graph; cptxyplotspace *plotspace = (cptxyplotspace *)self.graph.defaultplotspace; plotspace.xrange = [cptplotrange plotrangewithlocation:cptdecimalfromfloat(-4) length:cptdecimalfromfloat(8)]; plotspace.yrange = [cptplotrange plotrangewithlocation:cptdecimalfromfloat(-2.0) length:cptdecimalfromfloat(4.0)]; cptscatterplot *sinusplot = [[cptscatterplot alloc] initwithframe:self.graph.frame]; sinusplot.datasource = self; sinusplot.backgroundcolor = [cptcolor whitecolor].cgcolor; cptmutablelinestyle *axislinestyle = [cptmutablelinestyle linestyle]; axislinestyle.linewidth = 2.0f; axislinestyle.linecolor = [cptcolor blackcolor]; cptxyaxisset *axisset = (cptxyaxisset *)self.graph.axisset; axisset.xaxis.axislinestyle = axislinestyle; axisset.yaxis.axislinestyle = axislinestyle; [self.graph addplot:sinusplot]; } -(nsuinteger)numberofrecordsforplot:(cptplot *)plot { return 101; } -(nsnumber *)numberforplot:(cptplot *)plot field:(nsuinteger)fieldenum recordindex:(nsuinteger)idx { static double pi = 3.14159; double x = pi*((((double)idx) - 51.0)/50.0); if (fieldenum == cptscatterplotfieldx) return [nsnumber numberwithdouble:x]; else return [nsnumber numberwithdouble:sin(x + ((x<0)?2*pi:0))]; }
adding following line:
self.graph.backgroundcolor = [cptcolor whitecolor].cgcolor;
to viewdidload
solved issue.
my explanation axis set property of cptgraphhostingview
(here self.graph
), , not of plot. changing plot's layer background color instead of changing graph's layer background color. since axes still had same color background of containing layer, weren't visible.
for future reference, simple working version of viewdidload
, following:
-(void)viewdidload { [super viewdidload]; self.graph = [[cptxygraph alloc] initwithframe:self.view.frame]; cptgraphhostingview *hostingview = (cptgraphhostingview *)self.view; hostingview.hostedgraph = self.graph; cptxyplotspace *plotspace = (cptxyplotspace *)self.graph.defaultplotspace; plotspace.xrange = [cptplotrange plotrangewithlocation:cptdecimalfromfloat(-4) length:cptdecimalfromfloat(8)]; plotspace.yrange = [cptplotrange plotrangewithlocation:cptdecimalfromfloat(-2.0) length:cptdecimalfromfloat(4.0)]; cptscatterplot *sinusplot = [[cptscatterplot alloc] initwithframe:self.graph.frame]; sinusplot.datasource = self; self.graph.backgroundcolor = [cptcolor whitecolor].cgcolor; [self.graph addplot:sinusplot]; }
i hope coreplot rookies current self.
Comments
Post a Comment