I had an iPhone application in which i have to calculate the values for x axis and y axis with some intervals as xaxis array and Yaxis array
I had fixed values.
Then i need to draw 3 line graph with their on x values and Y
values.then i need to plot scatter points on that graph itself with
different (x,y) coordinates.
I am using IOs charts library.But when giving different x values to
the line graph the X axis values are also changing, I am trying in
this way
` chartview.descriptionText = #"";
chartview.noDataTextDescription = #"You need to provide data for the chart.";
chartview.drawGridBackgroundEnabled = NO;
chartview.drawBarShadowEnabled = NO;
chartview.highlightFullBarEnabled = NO;
chartview.rightAxis.enabled=NO;
chartview.drawOrder = #[#(CombinedChartDrawOrderLine),
#(CombinedChartDrawOrderScatter),
];
ChartLegend *l = chartview.legend;
l.enabled = false;
//chartview.xAxisRenderer
ChartYAxis *leftAxis = chartview.leftAxis;
leftAxis.axisLineWidth=2.0f;
leftAxis.axisLineColor =[UIColor whiteColor];
leftAxis.drawGridLinesEnabled = YES;
leftAxis.gridColor=[UIColor whiteColor];
leftAxis.gridLineDashLengths = #[#5.f, #5.f];
NSLog(#"%#",yaxispoints);
NSLog(#"%f",[[yaxispoints firstObject] floatValue]);
NSLog(#"%f",[[yaxispoints lastObject] floatValue]);
leftAxis.axisMinimum = [[yaxispoints firstObject] floatValue]; // this replaces startAtZero = YES
leftAxis.axisMaximum = [[yaxispoints lastObject] floatValue];
leftAxis.labelCount = [yaxispoints count];
leftAxis.spaceTop = 40.0;
leftAxis.labelTextColor=[UIColor whiteColor];
ChartXAxis *xAxis = chartview.xAxis;
[xAxis setDrawAxisLineEnabled:YES];
xAxis.axisLineWidth=2.0f;
xAxis.granularityEnabled=YES;
xAxis.axisLineColor =[UIColor whiteColor];
xAxis.labelPosition = XAxisLabelPositionBottom;
xAxis.axisMinimum = [[months firstObject] floatValue]; // this replaces startAtZero = YES
xAxis.axisMaximum = [[months lastObject] floatValue];
xAxis.labelCount = [months count];
xAxis.forceLabelsEnabled=YES;
xAxis.drawGridLinesEnabled = YES;
xAxis.gridColor=[UIColor whiteColor];
xAxis.gridLineDashLengths = #[#5.f, #5.f];
xAxis.valueFormatter = self;
xAxis.labelTextColor=[UIColor whiteColor];
[self updateChartData];
[chartview animateWithXAxisDuration:1.5 yAxisDuration:1.5];`
- (void)updateChartData
{
[self setChartData];
}
- (void)setChartData
{
CombinedChartData *data = [[CombinedChartData alloc] init];
data.lineData = [self generateLineData];
data.scatterData = [self generateScatterData];
chartview.xAxis.axisMaximum = data.xMax + 5.0;
chartview.data = data;
}
- (LineChartData *)generateLineData
{
LineChartData *d = [[LineChartData alloc] init];
NSMutableArray *entries = [[NSMutableArray alloc] init];
NSArray *scxaxis = #[#"55.987", #"75.976", #"85.976"
];
NSArray *scyaxis = #[#"85.987", #"25.976", #"95.976"
];
for (int index = 0; index < [scxaxis count]; index++)
{
[entries addObject:[[ChartDataEntry alloc] initWithX:[[scxaxis objectAtIndex:index] doubleValue] y:[[scyaxis objectAtIndex:index] doubleValue]]];
}
LineChartDataSet *set = [[LineChartDataSet alloc] initWithValues:entries label:#"Line DataSet"];
[set setColor:[UIColor greenColor]];
set.lineWidth = 3.0;
// [set setCircleColor:[UIColor redColor]];
//set.circleRadius = 0.0;
// set.circleHoleRadius = 0.0;
set .drawCirclesEnabled=NO;
set.fillColor = [UIColor clearColor];
set.mode = LineChartModeLinear;
set.drawValuesEnabled = NO;
set.valueFont = [UIFont systemFontOfSize:14.f];
set.valueTextColor = [UIColor whiteColor];
set.axisDependency = AxisDependencyLeft;
[d addDataSet:set];
return d;
}
I am trying like this,But when giving different x values to each data set in line graph the x axis is completely going wrong according to it. i want the x axis to be fetched from different array and x,y points of line graph to be from different array, Can anybody help me on achieving this ?
Maybe the problem is
chartview.xAxis.axisMaximum = data.xMax + 5.0;
Cause you are using CombinedChartData,if the you should call calcMinMax() at first.
And accord to your code ,the x axis should be in [0,90.976].What your graph shows?
Related
In my objective c project I need to Migrating MPChart from 2.2 to 3.0 for supporting my project in Xcode 9.2. but sum code is changed now.
view DidLoad
xAxis.valueFormatter = self;
chart update function
- (void)setDataCount:(int)count range:(double)range
{
xVals = [[NSMutableArray alloc] init];
for (int i = 0; i < dateAll.count; i++)
{
[xVals addObject:[dateAll objectAtIndex:i]];
}
NSLog(#"xVals %#",xVals);
NSMutableArray *yVals = [[NSMutableArray alloc] init];
for (int i = 0; i < arrtymHr.count; i++)
{
double vals = [[NSString stringWithFormat:#"%#.%#",[arrtymHr objectAtIndex:i],[arrtymMnt objectAtIndex:i]] doubleValue];
[yVals addObject:[[ChartDataEntry alloc] initWithX:i y:vals]];
}
NSLog(#"yVals %#",yVals);
LineChartDataSet *set1 = [[LineChartDataSet alloc] initWithValues:yVals label:#"DataSet 1"];
set1.axisDependency = AxisDependencyRight;
[set1 setColor:[UIColor colorWithRed:51/255.f green:181/255.f blue:229/255.f alpha:1.f]];
[set1 setCircleColor:[UIColor clearColor]];
set1.lineWidth = 2.0;
set1.circleRadius = 3.0;
set1.fillAlpha = 0.70;
set1.fillColor = [UIColor colorWithRed:51/255.f green:181/255.f blue:229/255.f alpha:1.f];
set1.highlightColor = [UIColor colorWithRed:244/255.f green:117/255.f blue:117/255.f alpha:1.f];
set1.drawCircleHoleEnabled = YES;
set1.drawFilledEnabled = !set1.isDrawFilledEnabled;
NSMutableArray *yVals2 = [[NSMutableArray alloc] init];
for (int i = 0; i < Allpost.count; i++)
{
double vals = [[Allpost objectAtIndex:i]doubleValue];
[yVals2 addObject:[[ChartDataEntry alloc] initWithX:i y:vals]];
}
NSLog(#"yVals 2 %#",yVals2);
LineChartDataSet *set2 = [[LineChartDataSet alloc] initWithValues:yVals2 label:#"DataSet 2"];
set2.axisDependency = AxisDependencyLeft;
[set2 setColor:[UIColor colorWithRed:245/255.f green:130/255.f blue:32/255.f alpha:1.f]];
[set2 setCircleColor:[UIColor clearColor]];
set2.lineWidth = 3.0;
set2.circleRadius = 3.0;
set2.fillAlpha = 1.0;
set2.fillColor = UIColor.redColor;
set2.highlightColor = [UIColor colorWithRed:244/255.f green:117/255.f blue:117/255.f alpha:1.f];
set2.drawCircleHoleEnabled = YES;
NSMutableArray *dataSets = [[NSMutableArray alloc] init];
[dataSets addObject:set1];
[dataSets addObject:set2];
LineChartData *data = [[LineChartData alloc] initWithDataSets:dataSets];
[data setValueTextColor:[UIColor clearColor]];
[data setValueFont:[UIFont systemFontOfSize:9.f]];
_chartView.data = data;
}
formatter function
- (NSString *)stringForValue:(double)value
axis:(ChartAxisBase *)axis
{
return [xVals objectAtIndex:value];
}
but the problem is the array only have one value but it displays more than one.
Printed Array
xVals (
"03/08",
"03/16",
"03/16",
"03/16"
)
yVals (
"ChartDataEntry, x: 0.0, y 0.0",
"ChartDataEntry, x: 1.0, y 0.0",
"ChartDataEntry, x: 2.0, y 0.0",
"ChartDataEntry, x: 3.0, y 0.0"
)
yVals 2 (
"ChartDataEntry, x: 0.0, y 5.5",
"ChartDataEntry, x: 1.0, y 7.0",
"ChartDataEntry, x: 2.0, y 7.0",
"ChartDataEntry, x: 3.0, y 3.0"
)
As per new Charts update you need to create ChartDataEntry in which you can add both X & Y value and than add it into LineChartDataSet
Please check below code :
//Create values array
NSMutableArray *values = [[NSMutableArray alloc] init];
//Create
[yValArray enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
[values addObject:[[ChartDataEntry alloc] initWithX:idx y:obj];
}];
In above code we are adding index in DataEntry form 0 .... N.
//Add dataEntry Object into values array
[values addObject:dataEntry];
//Create DataSet with values array
LineChartDataSet *set1 = [[LineChartDataSet alloc] initWithValues:values label:#"your_label_for_data_set"];
//Add Line Chart Data inro Chart.
LineChartData *data = [[LineChartData alloc] initWithDataSet:set1];
//Set Data to Line Chart
_lineChartTrend.data = data;
Now use your Formatter and return value based on index like below
- (NSString *)stringForValue:(double)value
axis:(ChartAxisBase *)axis
{
if (yourLineChart.xAxis == axis) {
return [xValArray objectAtIndex:value];
}
}
Where value is your index which we have added in DataEntry.
By Doing this you will get your X & Y value on your line Chart.
Edit:
Based on your comment and Question updation you need to add one more property for your XAxis so that it will not repeat XAxis value and will remain same as per your array, set below code and you will get expected output on your chart.
ChartXAxis *xAxis = self.lineChart.xAxis;
xAxis.granularity = 1.0;
Try this and let me know is it working or not.
Try This
#interface ViewController ()<IChartAxisValueFormatter>
viewDidLoad
xAxis.axisRange = 1;
xAxis.granularity = 1;
xAxis.valueFormatter =self;
#pragma mark - IAxisValueFormatter
- (NSString * _Nonnull)stringForValue:(double)value axis:(ChartAxisBase * _Nullable)axis
{
NSString *xAxisStringValue = #"";
int myInt = (int)value;
if(xVals.count > myInt)
{
xAxisStringValue = [xVals objectAtIndex:myInt];
return xAxisStringValue;
}
else
{
return 0;
}
}
I am developing a project using Core-Plot with Objective-C. The client asks to create a line chart (using Scatter Plot for this) where plots the number of ocurrencies x the time.
Well, after study for almost a week, this isn't working. My dots aren't connected and I don't know why.
This blue line should've been connected, since it is one plot.
Here is my code:
-(void)renderInLayer:(CPTGraphHostingView *)layerHostingView withTheme:(CPTTheme *)theme animated:(BOOL)animated{
CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:bounds];
[self addGraph:graph toHostingView:layerHostingView];
[self applyTheme:theme toGraph:graph withDefault:[CPTTheme themeNamed:kCPTDarkGradientTheme]];
graph.paddingTop = 20.0;
graph.paddingRight = 20.0;
graph.paddingLeft = 20.0;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.delegate = self;
CPTMutableLineStyle *axesLineStyle = [CPTMutableLineStyle lineStyle];
axesLineStyle.lineWidth = 1.0f;
axesLineStyle.miterLimit = 1.0f;
axesLineStyle.lineColor = [CPTColor grayColor];
CPTTextStyle *labelTextStyle = [CPTTextStyle textStyleWithAttributes:#{ NSForegroundColorAttributeName: BoardGraphValueColor, NSFontAttributeName: BoardGraphValueFont}];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
NSNumberFormatter * axisFormat = [[NSNumberFormatter alloc] init];
[axisFormat setNumberStyle:NSNumberFormatterNoStyle];
CPTXYAxis *x = axisSet.xAxis;{
x.visibleRange = [CPTPlotRange plotRangeWithLocation:#(-1.0) length:#(100)];
x.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
x.majorIntervalLength = #2;
x.minorTicksPerInterval = 1;
x.majorTickLineStyle = axesLineStyle;
x.minorTickLineStyle = axesLineStyle;
x.axisLineStyle = axesLineStyle;
x.plotSpace = plotSpace;
x.labelTextStyle = labelTextStyle;
x.labelFormatter = axisFormat;
x.labelOffset = 5.0;
}
CPTXYAxis *y = axisSet.yAxis;{
y.visibleRange = [CPTPlotRange plotRangeWithLocation:#(-1.0) length:#(200)];
y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
y.majorIntervalLength = #30;
y.minorTicksPerInterval = 6;
y.majorTickLineStyle = axesLineStyle;
y.minorTickLineStyle = axesLineStyle;
y.axisLineStyle = axesLineStyle;
y.plotSpace = plotSpace;
y.labelTextStyle = labelTextStyle;
y.labelFormatter = axisFormat;
y.labelOffset = 5.0;
}
graph.axisSet.axes = #[x, y];
CPTMutableLineStyle *lineStyle = [[CPTMutableLineStyle alloc] init];
lineStyle.lineWidth = 2.0;
CPTScatterPlot *scatterLinePlot = [[CPTScatterPlot alloc] init];
scatterLinePlot.areaBaseValue = #(0.0);
scatterLinePlot.identifier = #(1);
scatterLinePlot.title = #"Title";
lineStyle.lineColor = [CPTColor blueColor];
scatterLinePlot.dataLineStyle = lineStyle;
scatterLinePlot.histogramOption = self.histogramOption;
scatterLinePlot.areaBaseValue = #(0);
scatterLinePlot.delegate = self;
scatterLinePlot.plotSymbolMarginForHitDetection = 5.0;
scatterLinePlot.dataSource = self;
[graph addPlot:scatterLinePlot];
CPTPlotRange *globalYRange = [CPTPlotRange plotRangeWithLocation:#0.0
length:#12.0];
plotSpace.globalYRange = globalYRange;
[plotSpace scaleToFitEntirePlots:[graph allPlots]];}
This is the code for create just one plot, but I need to create six. I did a loop but it is working that way too.
Here's the datasource:
-(NSUInteger)numberOfRecordsForPlot:(nonnull CPTPlot *)plot{
return [self.plotData objectAtIndex:0].count;}
-(NSNumber *)numberForPlot:(nonnull CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index{
NSNumber *num = nil;
NSString *key;
NSArray *currentDataPoint;
NSString *plotIdentifier = (NSString *)plot.identifier;
if ([plotIdentifier isEqual:#(1)]){
currentDataPoint = [self.plotData objectAtIndex:0];
} else if ([plotIdentifier isEqual:#(2)]){
currentDataPoint = [self.plotData objectAtIndex:1];
} else if ([plotIdentifier isEqual:#(3)]){
currentDataPoint = [self.plotData objectAtIndex:2];
} else if ([plotIdentifier isEqual:#(4)]){
currentDataPoint = [self.plotData objectAtIndex:3];
} else if ([plotIdentifier isEqual:#(5)]){
currentDataPoint = [self.plotData objectAtIndex:4];
} else if ([plotIdentifier isEqual:#(6)]){
currentDataPoint = [self.plotData objectAtIndex:5];
}
if (fieldEnum == CPTScatterPlotFieldX) {
return #(index);
} else if (fieldEnum == CPTScatterPlotFieldY) {
key = #"count";
int number = [[[currentDataPoint objectAtIndex:index] valueForKey:key] intValue];
NSLog(#"Y value for index %lu is %d", index, number);
num = [[currentDataPoint objectAtIndex:index] valueForKey:key];
return num;}
return num;
}
What am I doing wrong?
PS: Well, another bug is the color of the bar chart. Each of this should have a different color, but it doesn't work too. The colors provided is that for the legends.
Thanks in advance.
To change the bar color for each bar, implement the -barFillForBarPlot:recordIndex: method in the datasource and return the correct fill for each index. You can also set the border line style for each bar (e.g., to use a different color) with the -barLineStyleForBarPlot:recordIndex: datasource method.
It looks like the data line is getting clipped outside the visible area. Make sure the plot data is available from the datasource when you call -scaleToFitEntirePlots:.
I'm using this library to make charts. Everything works fine if there are several bars but when I only have one, it seems that the value cannot be shown. This is the chart if there is only one bar (image)
But when I put two bars the values are visible.
My code is this:
-(void)setupChart{
myQuota = [[DataManager shared] myQuota];
[self setupBarLineChartView:_chartView];
_chartView.delegate = self;
_chartView.drawBarShadowEnabled = NO;
_chartView.drawValueAboveBarEnabled = YES;
_chartView.maxVisibleValueCount = [myQuota.quota doubleValue] * 2;
ChartXAxis *xAxis = _chartView.xAxis;
xAxis.labelPosition = XAxisLabelPositionBottom;
xAxis.labelFont = [UIFont systemFontOfSize:10.f];
xAxis.drawAxisLineEnabled = YES;
xAxis.drawGridLinesEnabled = YES;
xAxis.gridLineWidth = .3;
ChartYAxis *leftAxis = _chartView.leftAxis;
leftAxis.labelFont = [UIFont systemFontOfSize:10.f];
leftAxis.drawAxisLineEnabled = YES;
leftAxis.drawGridLinesEnabled = YES;
leftAxis.drawLabelsEnabled = NO; // don't write label values
leftAxis.gridLineWidth = .3;
leftAxis.axisMinValue = 0.0; // this replaces startAtZero = YES
ChartYAxis *rightAxis = _chartView.rightAxis;
rightAxis.enabled = YES;
rightAxis.labelFont = [UIFont systemFontOfSize:10.f];
rightAxis.drawAxisLineEnabled = YES;
rightAxis.drawGridLinesEnabled = NO;
rightAxis.axisMinValue = 0.0; // this replaces startAtZero = YES
rightAxis.yOffset = 1.0;
rightAxis.axisMaxValue = [myQuota.quota doubleValue] * 2;
[rightAxis removeAllLimitLines];
ChartLimitLine *limit = [[ChartLimitLine alloc] initWithLimit:[myQuota.quota doubleValue] label:[NSString stringWithFormat:#"%d",[myQuota.quota integerValue] ]];
[rightAxis addLimitLine:limit];
rightAxis.valueFormatter = [[NSNumberFormatter alloc] init];
rightAxis.valueFormatter.minimumFractionDigits = 0;
_chartView.doubleTapToZoomEnabled = NO;
_chartView.highlightPerTapEnabled = NO;
_chartView.legend.enabled = NO;
[_chartView animateWithYAxisDuration:1.5];
[self setDataCount:(1) range: ([myQuota.quota doubleValue]*2)];
}
- (void)setDataCount:(int)count range:(double)range
{
NSMutableArray *xVals = [[NSMutableArray alloc] init];
[xVals addObject:myQuota.rowName];
[xVals addObject:myQuota.rowName];
NSMutableArray *yVals = [[NSMutableArray alloc] init];
double val = [myQuota.quotaReached doubleValue];
[yVals addObject:[[BarChartDataEntry alloc] initWithValue:([myQuota.quota doubleValue]*2) xIndex:0]];
BarChartDataSet *set1 = nil;
if (_chartView.data.dataSetCount > 0)
{
set1 = (BarChartDataSet *)_chartView.data.dataSets[0];
set1.yVals = yVals;
_chartView.data.xValsObjc = xVals;
[_chartView notifyDataSetChanged];
}
else
{
set1 = [[BarChartDataSet alloc] initWithYVals:yVals label:#""];
set1.barSpace = 0.35;
set1.colors = [NSArray arrayWithObjects:[UIColor greenColor], nil]; // set color
NSMutableArray *dataSets = [[NSMutableArray alloc] init];
[dataSets addObject:set1];
BarChartData *data = [[BarChartData alloc] initWithXVals:xVals dataSets:dataSets];
[data setValueFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:10.f]];
//_chartView.xAxis.labelPosition = Top
_chartView.data = data;
}
}
I've used the ios-charts project on github and used the HorizontalCharts example. It works good in the example but when I try to set the horizontal bar value, the bar just gets too long. Example visible here:
My code is this:
-(void)setupChart{
myQuota = [[DataManager shared] myQuota];
[self setupBarLineChartView:_chartView];
_chartView.delegate = self;
_chartView.drawBarShadowEnabled = NO;
_chartView.drawValueAboveBarEnabled = YES;
_chartView.maxVisibleValueCount = 60;
ChartXAxis *xAxis = _chartView.xAxis;
xAxis.labelPosition = XAxisLabelPositionBottom;
xAxis.labelFont = [UIFont systemFontOfSize:10.f];
xAxis.drawAxisLineEnabled = YES;
xAxis.drawGridLinesEnabled = YES;
xAxis.gridLineWidth = .3;
ChartYAxis *leftAxis = _chartView.leftAxis;
leftAxis.labelFont = [UIFont systemFontOfSize:10.f];
leftAxis.drawAxisLineEnabled = YES;
leftAxis.drawGridLinesEnabled = YES;
leftAxis.drawLabelsEnabled = NO;
leftAxis.gridLineWidth = .3;
leftAxis.axisMinValue = 0.0;
ChartYAxis *rightAxis = _chartView.rightAxis;
rightAxis.enabled = YES;
rightAxis.labelFont = [UIFont systemFontOfSize:10.f];
rightAxis.drawAxisLineEnabled = YES;
rightAxis.drawGridLinesEnabled = NO;
rightAxis.axisMinValue = 0.0;
rightAxis.yOffset = 1.0;
rightAxis.axisMaxValue = [myQuota.quota doubleValue]; // here is 10
_chartView.doubleTapToZoomEnabled = NO;
_chartView.highlightPerTapEnabled = NO;
_chartView.legend.enabled = NO;
[_chartView animateWithYAxisDuration:2.5];
[self updateChartData];
}
- (void)updateChartData
{
if (self.shouldHideData)
{
_chartView.data = nil;
return;
}
if(myQuota != nil && myQuota.quota != nil)
{
[self setDataCount:(1) range: [myQuota.quota doubleValue]];
}
}
- (void)setDataCount:(int)count range:(double)range
{
NSMutableArray *xVals = [[NSMutableArray alloc] init];
[xVals addObject:myQuota.rowName];
NSMutableArray *yVals = [[NSMutableArray alloc] init];
double val = [myQuota.quotaReached doubleValue]; // here is 2
[yVals addObject:[[BarChartDataEntry alloc] initWithValue:val xIndex:0]];
BarChartDataSet *set1 = nil;
if (_chartView.data.dataSetCount > 0)
{
set1 = (BarChartDataSet *)_chartView.data.dataSets[0];
set1.yVals = yVals;
_chartView.data.xValsObjc = xVals;
[_chartView notifyDataSetChanged];
}
else
{
set1 = [[BarChartDataSet alloc] initWithYVals:yVals label:#""];
set1.barSpace = 0.35;
set1.colors = [NSArray arrayWithObjects:[UIColor greenColor], nil];
NSMutableArray *dataSets = [[NSMutableArray alloc] init];
[dataSets addObject:set1];
BarChartData *data = [[BarChartData alloc] initWithXVals:xVals dataSets:dataSets];
[data setValueFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:10.f]];
_chartView.data = data;
}
}
Why is the bar so long?
Okay, since nobody offered an answer I tried a HACK. I've added an invisible bar/row for which I set the value to the maximum of the bar chart. This way the axis shows the maximum even though the bar is invisible.
I welcome anybody who knows a better way but until then I'll do it like this:
- (void)setDataCount:(int)count range:(double)range{
MY_MAX_VALUE = 10; // put HERE your MAX value for the axis
NSMutableArray *xVals = [[NSMutableArray alloc] init];
[xVals addObject:myQuota.rowName];
NSMutableArray *yVals = [[NSMutableArray alloc] init];
double val = [myQuota.quotaReached doubleValue]; // here is 2
[yVals addObject:[[BarChartDataEntry alloc] initWithValue:val xIndex:0]];
[yVals addObject:[[BarChartDataEntry alloc] initWithValue: MY_MAX_VALUE xIndex:1]];
}
I have successfully selected a point, but I hope at this point when selected, the point of color set to different from other points to distinguish,how to implement,Thanks a lot
How to set up only this one in red.
-(void)scatterPlot:(CPTScatterPlot *)plot plotSymbolWasSelectedAtRecordIndex:(NSUInteger)index{
CPTXYGraph graph = (CPTXYGraph)_graph;
if ( _symbolTextAnnotation ){
[graph.plotAreaFrame.plotArea removeAnnotation:_symbolTextAnnotation];
_symbolTextAnnotation = nil;
}
// Setup a style for the annotation
CPTMutableTextStyle *hitAnnotationTextStyle = [CPTMutableTextStyle textStyle];
hitAnnotationTextStyle.color = [CPTColor whiteColor];
hitAnnotationTextStyle.fontSize = 16.0;
hitAnnotationTextStyle.fontName = #"Helvetica-Bold";
// Determine point of symbol in plot coordinates
NSDictionary *dataPoint = _plotData[index];
NSNumber *x = dataPoint[#"x"];
NSNumber *y = dataPoint[#"y"];
NSArray *anchorPoint = #[x, y];
float yValue = [y floatValue];
NSString *yString = [NSString stringWithFormat:#"%.1f",yValue];
CPTTextLayer *textLayer = [[CPTTextLayer alloc] initWithText:yString style:hitAnnotationTextStyle];
CPTImage *background = [CPTImage imageWithCGImage:[UIImage imageNamed:#"bg_red"].CGImage];
textLayer.fill = [CPTFill fillWithImage:background];
textLayer.paddingLeft = 2.0;
textLayer.paddingTop = 2.0;
textLayer.paddingRight = 2.0;
textLayer.paddingBottom = 2.0;
self.symbolTextAnnotation = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:graph.defaultPlotSpace anchorPlotPoint:anchorPoint];
_symbolTextAnnotation.contentLayer = textLayer;
_symbolTextAnnotation.contentAnchorPoint = CGPointMake(0.5, 0.0);
_symbolTextAnnotation.displacement = CGPointMake(0.0, 10.0);
[graph.plotAreaFrame.plotArea addAnnotation:_symbolTextAnnotation];
}
Implement the -symbolForScatterPlot:recordIndex: datasource method. Return nil to use the default plotSymbol, [NSNull null] to show no symbol, or a CPTPlotSymbol to show that symbol at the given data index.