We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to get a Series by ID or Name Instead of by Index

I know that I can get a chart series with code like this:

     var chart = document.getElementById("container").ej2_instances[0];
     chart.series[3].visible = document.getElementById("chkTempLow").checked;

But my chart has a dynamic number of series so I don't know the index of the series I want.

Is there any way to find/refer to a series by something other than it's index?

For example:
     chart.series['lowTemp']

     or

     chart.series[getSeriesIndexByID]

Thanks,
Scott]

1 Reply

BV Bhuvanesh Valarman Syncfusion Team October 2, 2019 01:33 PM UTC

Hi Scott, 

Greetings from Syncfusion. 

We have analyzed your query. From that we suggest you to use the below workaround to achieve your scenario by using series name to get series. 

Code Snippet:  
<input id="seriesname" type="text" placeholder="Enter the series name to change color" style="width: 300px;" onchange="GetSeries()" /> 
<ejs-chart id="container" > 
    <e-chart-primaryxaxis valueType="Category"></e-chart-primaryxaxis> 
    <e-series-collection> 
        <e-series dataSource="ViewBag.DataSource" xName="Day" yName="YValue" type="Column" name="Product A" width="2"></e-series> 
        <e-series dataSource="ViewBag.DataSource" xName="Day" yName="YValue2" type="Column" name="Product B" width="2"></e-series> 
        <e-series dataSource="ViewBag.DataSource" xName="Day" yName="YValue3" type="Column" name="Product C" width="2"></e-series> 
    </e-series-collection> 
</ejs-chart> 
 
<script> 
    function GetSeries() { 
        var chart = document.getElementById("container").ej2_instances[0]; 
        var name = document.getElementById('seriesname').value; 
        var series = GetSeriesByName(chart.series, name); 
        series.fill = "Purple"; 
    } 
    function GetSeriesByName(series, name) { 
        for (var i = 0; i < series.length; i++) { 
            if (series[i].name == name) { 
                return series[i]; 
            } 
        } 
        return null; 
    } 
</script> 
 
Kindly revert us, if you any concerns about this. 

Regards, 
Bhuvanesh V. 


Loader.
Live Chat Icon For mobile
Up arrow icon