How to select only datapoints in SChart Line Series?


#1

I am using SChart Line Series in my graph, to show the data points, upon selection. But when i select a datapoint is gets selected and displays the value, and it also selects the the line in between two data points and displays the x axis and y axis points, which i do not want to.
I just want to display the data points when selected and not other places, on the line.
How could i do this ?
I have attached two images here showing how it is displaying.

Though it points on other places on line, it should display data for only the data points and not other places on the line.
And is it possible to display float value on the label when data point is selected ?
How can this be done.

I’m currently using the below code.

**-(SChartSeries )sChart:(ShinobiChart )chart seriesAtIndex:(NSInteger)index {


** SChartLineSeries lineSeries = [SChartLineSeries new];*
** lineSeries.selectionMode = SChartSelectionPoint;**

** lineSeries.crosshairEnabled = YES;**
** lineSeries.style.showFill = NO;**
** lineSeries.style.pointStyle.showPoints = YES;**
** ChannelDOH model = self.displayModels[index][0];*
** lineSeries.title = model.channel;**


** return lineSeries;**
}
*-(void)sChart:(ShinobiChart )chart crosshairMovedToXValue:(id)x andYValue:(id)y {


** NSString tooltipText = self.customCrosshair.tooltip.label.text;*
** int index = (int) [tooltipText rangeOfString:@" " options:NSBackwardsSearch].location;**
** NSDate dateValue = (NSDate )x;
** NSString timeText = [dateValue stringWithFormat:@“M/d/yy”];*
** NSString valueText = [tooltipText substringFromIndex:index+1];*
** valueText = [valueText stringByReplacingOccurrencesOfString:@"," withString:@""];**
** valueText = [CommonUtil stringFromNumber:@([valueText floatValue]) returnCurrencySymbol:NO];**
** tooltipText = [NSString stringWithFormat:@"%@,%@", timeText, y];**
** self.customCrosshair.tooltip.label.text = tooltipText;**

}

#pragma mark - SChartDelegate

**- (void)sChart:(ShinobiChart *)chart alterTickMark:(SChartTickMark )tickMark beforeAddingToAxis:(SChartAxis )axis {


** if (axis == chart.xAxis) {**
** return;**
** }**
** tickMark.tickLabel.text = [CommonUtil stringFromNumber:@(tickMark.value) returnCurrencySymbol:NO];**
}

Can I know how to fix my issues plz …



#2

Hi skr,

If I understand it correctly, your issue is about crosshair tracking rather than about point selection: is that right?

It looks like you’re trying to achieve behaviour like that of SChartCrosshair when interpolatePoints is set to NO, i.e. that the crosshair snaps to the nearest data point when it is dragged. Unfortunately this is not an option for the SChartSeriesCrosshair, which always moves smoothly between the points.

There are two possible ways to achieve the effect you’re looking for:

  1. Subclass SChartCrosshair to use a custom lineDrawer and a custom tooltip which look like those of the SChartSeriesCrosshair.

  2. Subclass SChartSeriesCrosshair and override moveToPoint:(CGPoint)pointInChart inChart:(ShinobiChart *)chart with an implementation that finds the closest data point to the pointInChart (making use of SChartPixelToPointMapper) and moves the crosshair to that point.

I hope this helps.

Kind regards,

Alison


#3

Thanks Alison.

I’ll try this way and let you if every thing goes fine.

Regards,
Kaushal


#4

1 st option is working.but initially it is not showing cross hair at certain points.but if we do zooming or panning it will work fine.if change the legend style its working good.(as i mentioned in other topic to you) leend style is top middle which is not working fine.that is the issue for 1 st option.
2 nd option how to override the pointInchart mapper (some code base require if possible for moving only data points in line series ).

thank you for your guidance.it is very helpful


#5

Hi skr,

Regarding the first option, I’ll reply to your other post separately.

Regarding the second option, you need to do something like this to find the CGPoint to move to:

First, use SChartPixelToPointMapper to find the nearest point in the chart:

SChartPixelToPointMapper *mapper = [SChartPixelToPointMapper new];
SChartPixelToPointMapping *mapping = [mapper mappingForPoint:pointInChart
                                              seriesToSearch:@[series]
                                                     onChart:chart];

The mapping object contains a data point. You can’t use this directly as its values will be the interpolated ones, but you can use its sChartDataPointIndex property to find its index, and ask your data source for the value you need:

id<SChartData> dp = [datasource sChart:chart 
                      dataPointAtIndex:mapping.dataPoint.sChartDataPointIndex 
                      forSeriesAtIndex:0];

Finally you can use the chart’s axes to calculate the pixel values for each data value, making sure you take into account the plot area’s origin:

double xValue = [chart.xAxis pixelValueForDataValue:dp.sChartXValue] + chart.plotAreaFrame.origin.x;
double yValue = [chart.yAxis pixelValueForDataValue:dp.sChartYValue] + chart.plotAreaFrame.origin.y;
CGPoint newPoint = CGPointMake(xValue, yValue);

I hope that helps.

Alison


#6

Thanks Alison.
I’ll try implementing this and get back to you on this with the update.

Regards,
SKR