Getting color of a data series from a flot chart

Chris Farmer picture Chris Farmer · Oct 6, 2008 · Viewed 8.3k times · Source

After seeing the cool new "reputation" tab on the stackoverflow user page, I was inspired to play with the Flot charting library a little. I have a line chart that has several hundred series. Only a couple of these series will be visible at any given time. My data series are grouped into several "categories" and I assign the numeric color index based on that category. I'd like to be able to see what actual color was assigned by Flot to a particular color index value, for the ultimate purpose of creating a custom legend that relates the color to my "category" of data. How can I get these color values?

I see that I can provide my own array for colors, but I am reluctant to do this, because I am not sure how many categories I will have until I load the data. I suppose I could just create an array that's just way too big, but that seems wasteful if it's possible to ask Flot what color each series is.

Answer

Tom picture Tom · Oct 6, 2008

There's an example at the bottom of http://flot.googlecode.com/svn/trunk/API.txt that does just that. Something like:

var plot = $.plot(placeholder, data, options)
var series = plot.getData();
for (var i = 0; i < series.length; ++i)
   alert(series[i].color);