From 74661de188e8bdc1d7d865b863de5d0f08f593ff Mon Sep 17 00:00:00 2001 From: "olau@iola.dk" Date: Sun, 8 Mar 2009 18:24:40 +0000 Subject: [PATCH] Added second parameter to labelFormatter for legend git-svn-id: https://flot.googlecode.com/svn/trunk@140 1e0a6537-2640-0410-bfb7-f154510ff394 --- API.txt | 7 ++++--- NEWS.txt | 4 +++- jquery.flot.js | 15 ++++++++------- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/API.txt b/API.txt index 67b9705..91ecefc 100644 --- a/API.txt +++ b/API.txt @@ -127,7 +127,7 @@ Customizing the legend legend: { show: boolean - labelFormatter: null or (fn: string -> string) + labelFormatter: null or (fn: string, series object -> string) labelBoxBorderColor: color noColumns: number position: "ne" or "nw" or "se" or "sw" @@ -143,8 +143,9 @@ the labels in some way, e.g. make them to links, you can pass in a function for "labelFormatter". Here's an example that makes them clickable: - labelFormatter: function(label) { - return '' + label + ''; + labelFormatter: function(label, series) { + // series is the series object for the label + return '' + label + ''; } "noColumns" is the number of columns to divide the legend table into. diff --git a/NEWS.txt b/NEWS.txt index f2b1478..69a7f1b 100644 --- a/NEWS.txt +++ b/NEWS.txt @@ -52,7 +52,9 @@ Changes: - Thresholding: you can set a threshold and a color, and the data points below that threshold will then get the color. Useful for marking data below 0, for instance. - + +- The legend labelFormatter now passes the series in addition to just + the label (suggestion by Vincent Lemeltier). Bug fixes: diff --git a/jquery.flot.js b/jquery.flot.js index 2a491f3..03fcc56 100644 --- a/jquery.flot.js +++ b/jquery.flot.js @@ -1583,10 +1583,12 @@ if (!options.legend.show) return; - var fragments = []; - var rowStarted = false; + var fragments = [], rowStarted = false, + lf = options.legend.labelFormatter, s, label; for (i = 0; i < series.length; ++i) { - if (!series[i].label) + s = series[i]; + label = s.label; + if (!label) continue; if (i % options.legend.noColumns == 0) { @@ -1596,12 +1598,11 @@ rowStarted = true; } - var label = series[i].label; - if (options.legend.labelFormatter != null) - label = options.legend.labelFormatter(label); + if (lf) + label = lf(label, s); fragments.push( - '
' + + '
' + '' + label + ''); } if (rowStarted)