Toggle line series on/off


#1

I have a lineseries chart that has the usual x,y,z values resuling in three lines on the chart.  I have each linecolor as red, green, blue respectively and have also added three switches for each axis to allow the user to turn off any they are not interested in.

I’m have bother trying to implement this by getting the data to match up on the required line.  Is there an inbuilt way that I can use to achive this, as with the points and grid which can be toggled dynamically?

Thanks


#2

Hi Paul,

Are you using ShinobiCharts for Android or iOS?

Thanks,

Kai.


#3

Sorry, should have mentioned that!  I am using IOS.

I have actually coded my app now to do just the thing I’m after and that is toggle any or all the lines on or off.

Is there an inbuilt feature for this or shall I post my solution…?   :kissing_heart:


#4

Glad you got this sorted! I was going to suggest altering each line series’ selected style to be hidden, and then hooking your switches up to programmatically toggle the series’ selected property. 

It might be interesting for other users (and for the Shinobi team) to see how you’ve achieved this.


#5

I have not looked at your suggestion yet, but will do, might me far easier than my er insane way of doing it…but insane or no it does work  :grin:

This is in the VC that has the switches to turn the different x,y,z series on or off…

-(IBAction)didSelectAxisSwitch:(id)sender {
    
    // _graphView uses the bit value of v to determine which data is displayed as well as line colours
    int v = 0;
    
    if (self.xLine.on) {
        
        v++;
    }
    if (self.yLine.on) {
        
        v += 2;
    }
    if (self.zLine.on) {
        
        v += 4;
    }

    [_graphView axisValuesChangedTo:v];
}

And this is what is in my class that is the chart view:

-(void)axisValuesChangedTo:(int)value {
    
    /*
     00000000	all off 0
     00000001	x on 1
     00000010	y on 2
     00000011	x,y on	3
     00000100 z on 4
     00000101 x,z on 5
     00000110 y,z on 6
     00000111	x,y,z on	7
    */
   
    // dependant on value
    //
    // reset the number of series to process
    // point the _data[n] array to point to the required data source
    // change the line colours to reflect the change
    
    switch (value) {
        
        case 0: // all axis switches off
            
            _numberOfSeries = 0;
           
            break;
        
        case 1: // just x axis switch on
            
            _numberOfSeries = 1;
            
            _data[0] = _xDataStore;
            
            _xLineColour = [UIColor redColor];
            
            break;
        
        case 2: // just y axis switch on
            
            _numberOfSeries = 1;
            
            _data[0] = _yDataStore;
            
            _xLineColour = [UIColor greenColor];
            
            break;
            
        case 3: // x and y axis switches on
            
            _numberOfSeries = 2;
            
            _data[0] = _xDataStore;
            _data[1] = _yDataStore;
            
            _xLineColour = [UIColor redColor];
            _yLineColour = [UIColor greenColor];
            
            break;
        
        case 4: // just z axis switch on
            
            _numberOfSeries = 1;
            
            _data[0] = _zDataStore;
            
            _xLineColour = [UIColor blueColor];
            
            break;
        
        case 5: // x and z axis switches on
            
            _numberOfSeries = 2;
            
            _data[0] = _xDataStore;
            _data[1] = _zDataStore;
            
            _xLineColour = [UIColor redColor];
            _yLineColour = [UIColor blueColor];
            
            break;
            
        case 6: // y and z axis switches on
            
            _numberOfSeries = 2;
            
            _data[0] = _yDataStore;
            _data[1] = _zDataStore;
            
            _xLineColour = [UIColor greenColor];
            _yLineColour = [UIColor blueColor];
            
            break;
            
        case 7: // all axis switches on
            
            _numberOfSeries = 3;
            
            _data[0] = _xDataStore;
            _data[1] = _yDataStore;
            _data[2] = _zDataStore;
            
            _xLineColour = [UIColor redColor];
            _yLineColour = [UIColor greenColor];
            _zLineColour = [UIColor blueColor];
            
            break;
            
        default:
            break;
    }

    // have to apply the theme to get chart to redraw
    [_chart applyTheme:[self theme]];

    // reload with revised data
    [_chart reloadData];
}

It may not be perfect or even the right way to do it, would appreciate feedback as it this…but it works perfect…