[iOS] How to update SChartDataPoint


#1

Hi everybody,
I use SChartLineSeries for draw chart and use crosshairMovedToXValue function for move crosshair.
I want to update SChartDataPoint of line chart when moving crosshair.
I don’t know how to update datapoint for chart.


Please help me resolve this issue?
Thanks guys!


#2

Hi there!

This sounds like the feature we are currently working on.

We are currently implementing a per point animations feature that will allow users to animate a data point from it’s current value to a new one.

Until we release this feature you will have to do this manually by reloading your chart and update your data source with new data.

You will have to force a redraw after you’ve told your chart to reload it’s data.

Let me know if you have any questions.

Kind regards,
Andrew Polkinghorn.


#3

I have updated old data by new data and then reloadData, but it’s very slow.
By away, i want to it’s reload data than faster?
This is my soure code:

-(void)sChart:(ShinobiChart *)chart crosshairMovedToXValue:(id)x andYValue:(id)y {
    SChartDataPoint *dataPoint = [chart.datasource sChart:chart dataPointAtIndex:[x integerValue] forSeriesAtIndex:0];
    dataPoint.yValue = @([y doubleValue]);
    [self.arrs replaceObjectAtIndex:[x integerValue] withObject:dataPoint];
    [chart reloadData];
}

I have to touch screen again so it’s reloadData, if i don’t touch it’s don’t reloadData.
Please help me resolve this issue.
Thanks!


#4

Hi there!

Using the SChartDelegate method crosshairMovedTo… is not the correct place to be implementing this feature.

As your current implementation would require a call to ShinobiChart’s redrawChart method, which would create an infinite loop.

I would recommend adding your own gesture recogniser to your chart that uses the SChartAxis’s dataValueForPixelValue: method to translate pixel values given when the user pans into data values.

Then you can update your data points and reload and redraw your chart. You could also show or move your crosshair using the SChartCrosshair protocol methods to keep your crosshair updated.

Kind regards,
Andrew Polkinghorn.


#5
// Add a pan gesture recogniser for dragging the chart
    UIPanGestureRecognizer *gestureRecogniser = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [_chart addGestureRecognizer:gestureRecogniser];

- (void)handlePan:(UIPanGestureRecognizer*)recogniser
{
    // What's the pixel location of the touch?
    CGPoint currentTouchPoint = [recogniser locationInView:_chart.canvas];
   
     [_chart.yAxis dataValueForPixelValue:currentTouchPoint.y];

    // Move the annotation to the correct location
    [_chart redrawChart];
}

Sorry, is this correct?


#6

Hi there,

It looks like you are heading in the right direction.

To move the data point you just need to update your data point’s value with the value returned from the dataValueForPixelValue: method then call a reloadData before you call redrawChart.

Then if you want the crosshair to update, use the SChartCrosshair protocol methods to show or move your crosshair.

Kind regards,
Andrew Polkinghorn.


#7

Thanks guy,

- (void)handlePan:(UIPanGestureRecognizer*)recogniser
{
    // What's the pixel location of the touch?
    CGPoint currentTouchPoint = [recogniser locationInView:_chart];
    // Create the range
    double x = [[_chart.xAxis dataValueForPixelValue:currentTouchPoint.x] doubleValue];
    double y = [[_chart.yAxis dataValueForPixelValue:currentTouchPoint.y] doubleValue];
    SChartDataPoint *data = [[SChartDataPoint alloc] initWithXValue:@(x) yValue:@(y)];
    [self.arrs replaceObjectAtIndex:HOW_TO_GET_INDEX withObject:data];
    [_chart reloadData];
    [_chart redrawChart];
}

So how to get HOW_TO_GET_INDEX and then replace datapoint?
Thanks!


#8

Hey,

When your gesture handler is first triggered you will have to loop through your data points comparing each point’s X value with the pixel point’s data X value to find the index of the closest point.

You can then store the index and use it every time handlePan is called mid gesture.

Kind regards,
Andrew Polkinghorn.


#9

Mr APolk, Thanks for support! I resolved this issue!