Change the selected state of a series


#1

Hi,

I have a chart with 200 StepLineSeries. For particular reasons of my layout, the delegate method toggledSelectionForSeries is not called when the right series whenever I tap in one of them. Thus, I want to manually select and deselect the series.

I have an algorith that based on the location of the user’s touch determines which series correspond to that datapoint. However, when it comes to apply the selectedStyle, nothings happens. When I create my series I define the style and selectedStyle properties with the right values. When I detect the touch, changing the selected property of the series to YES does nothing. I have also tried by creating a new SChartLineSeriesStyle, setting that object as the style property of the series and calling [ShinobiChart redrawChart], but again nothing changes.

I have to say that I have the selectionMode of my series set to SChartSelectionNone. Does it mean that series can never be selected?

As I said, setting the mode to SChartSelectionSeries makes the delegate select the wrong series. My workaround was to keep track of the selectedSeries that I calculate and have this as the implementation of the delegate method:

- (void)sChart:(ShinobiChart *)chart toggledSelectionForSeries:(SChartSeries *)series nearPoint:(SChartDataPoint *)dataPoint atPixelCoordinate:(CGPoint)pixelPoint
{
    series.selected = NO;
    self.selectedSeries.selected = YES;
}

But for some reason that introduces a very large lag between the tapping and actually the changing in the selected series style.

Is there any way to accomplish what I am trying to do?

Thanks.


#2

I managed to programatically select a line series by doing the following (in the 2.3 framework released recently):

// Set series 0 as selected. (Here you could get the series object you know should be selected.)
    [_chart.series[0] setSelected:YES];  
    // Force the chart to redraw its plot area.
    [_chart redrawChartIncludePlotArea:YES];

You must get your series from this _chart.series array (unless you have implemented some kind of cache in your datasource) so that you are changing the selected property on the SChartSeries object that your chart has a reference to, the one it is currently using. 

The selectionMode property on your chart should only affect the selection gesture recognizers, you should still be able to programatically select series & points on your chart regardless.

I hope the above helps.

Jan


#3

It works pretty well and the series update much faster than using the delegate. Thanks a lot!

Out of curiosity, why the change of selected states occur much faster with this method rather than when the selectionMode is set to SChartSelectionSeries?

Just a final question. In the implementation suggested, the whole data series is redrawn. In my case, I only have around 500 data points but I guess that the whole redraw would be an issue if I would have like 100k dataPoints, wouldn’t it? 


#4

A redraw should be super fast - the chart actually redraws multiple times a second when you pan/zoom. A chart reload would be more noticably affected by the number of datapoints on your chart. This is because the chart has to query your datasource for each data point. 

As for the delay, it’s hard to say without looking at a project. I’m actually a little confused as to how your previous method worked. From what I understood you are doing the following:

  • Adding a gesture recognizer to the chart.
  • Using this gesture recognizer’s touch point to calculate which series should be selected (using your patented algorithm :laughing:).
  • Storing the series as an ivar.
  • Setting the series to be selected when the toggle delegate method is called.

I would have thought that your gesture recognizer would have swallowed the tap gesture causing the delegate method not to fire? 


#5

Totally true about the chart redraws.

What you understood from my approach is correct. I set the cancelsTouchesInView property to NO and conforms to the UIGestureRecognizerDelegate protocol to always return YES in gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: and that makes it work well.

About ‘my algorithm’ there is nothing magical there  :rolling_eyes:. I am using the approach I described in here to simulate columns of different width. Thus, I think the reason why the delegate is not returning always the right series is because not always the closest point belongs to the area of the series I am tapping, particularly when I click on a pixel near the X axis (not very sure about this, to be honest).

Again, thanks for your help!