Menu

How to get the automatically assigned color of a Series

Dan Jones
2016-04-27
2016-04-27
  • Dan Jones

    Dan Jones - 2016-04-27

    Is there any way to get the color of a series that was automatically assigned when the series was created? We need to create "mini-graphs" that match the color of larger graphs on a different chart. I know we could explicitly assign the colors to the larger graphs that need to be matched. However, Highcharts does a pretty good job of allocating colors, and I'd rather not add the complexity of assigning and tracking colors if there's a way to get the color of series that had its color automatically assigned. Any help appreciated.

     
  • Shawn Quinn

    Shawn Quinn - 2016-04-27

    If you're ok with waiting until after the chart is rendered, then you can get the series color dynamically through a native method call like the following:

    Window.alert("Series color: " + getSeriesColor(chartSeries.getNativeSeries()));
    
    private static native String getSeriesColor(JavaScriptObject series) /*-{
        return series.color;
    }-*/;
    

    If you need the series color before the chart is rendered though, then I don't know if there is a way to get it through the Highcharts API. It may be easiest to just look at the array of colors that Highcharts provides in the JS, and move those into a variable in your application (I believe Highcharts simply cycles through the array of colors each time you create a new series). E.g. look for a line like the following in either the "highcharts.js" file or the theme "JS" file (if you're using themes):

    colors: ['#7cb5ec', '#434348', '#90ed7d', '#f7a35c', '#8085e9', '#f15c80', '#e4d354', '#8085e8', '#8d4653', '#91e8e1']
    

    Hope that helps!

     
  • Dan Jones

    Dan Jones - 2016-04-27

    We don't need the color-matched mini-charts until after the big charts have been rendered. The native JavaScript method works like a charm! Thanks so much.

     

Log in to post a comment.