tags:

views:

1165

answers:

3
+6  Q: 

Flot Data Labels

I'm trying to produce a line chart using Flot, but I want the data labels to show up on the chart - meaning, I want the value of each point to appear next to that point. I feel like this should be an option, but can't find it in the API. Am I just missing something, or does someone know a workaround?

Thanks in advance.

+5  A: 

The feature you want is requested here in the Flot Google group. It doesn't look like it was ever implemented (there's nothing in the API about putting any labels inside the chart itself). I think that the answer to your question is that No, it's not possible at this time to show values next to certain points on lines inside the graph.

Ole Larson, head developer on Flot, mentioned that showing labels inside the chart is different than anything else on FLot and that they would have to think about how to extend the API / plot parameters to do it.

That said, you might want to go post a question on the Flot forum or make a suggestion on the bug-tracker for the new feature. Ole Larson is actually really good at getting back to all the questions, bugs, and suggestions himself.

the Will Cole
That said, Flot is open source, so you can write the feature yourself if you're an experienced JS developer.
the Will Cole
Zeth
Zeth, fixed position divs sounds like a good idea. You should also look at the Flot examples (on code.google.com/p/flot) to look at the tool tip code and the on-hover event code. The tool tip code might help to write the labels and the on-hover event code might show you how to get the positions of the bars.
the Will Cole
Another idea is to set the size of the graph manually by using the min and max properties in the options configuration, like so: { yaxis: { min: 0, max: 10 }, xaxis: { min: 0, max: 10 } } (you can actually just retrieve the min and max in the tick formatter method, see API). If you knew the size of the graph area, you could plot the labels on the bars using the bars height and position as x,y coordinates, adjusting them according to the dimensions of the graph. --IF this was helpful and you're feeling generous, rate up (or select) my answer and/or comments. I'm new and trying to get to 250.--
the Will Cole
+1  A: 

If anyone else is looking for a quick solution, see this plugin:

http://sites.google.com/site/petrsstuff/projects/flotvallab

Ross Martin
+1  A: 

Here is how I added the feature, including a pleasant animation effect:

var p = $.plot(...);

$.each(p.getData()[0].data, function(i, el){
  var o = p.pointOffset({x: el[0], y: el[1]});
  $('<div class="data-point-label">' + el[1] + '</div>').css( {
    position: 'absolute',
    left: o.left + 4,
    top: o.top - 43,
    display: 'none'
  }).appendTo(p.getPlaceholder()).fadeIn('slow');
});

You can move the position and display css to a stylesheet.

tom