Skip to content

Commit

Permalink
Fix tooltip when displaying multiple series
Browse files Browse the repository at this point in the history
We replace the "%x" in the tooltip string with the corresponding item tick's
label. We do so by looking at the item's index in the chart's data array
(dataIndex), and getting the tick in the same index in the ticks' array.
That works fine if you have a single series, but doesn't work with multiple
ones.

In that case, you have multiple data arrays, one for each series, but you still
have a single ticks array. For example, if you have the plot's data as:

[
  { data: [ ["Foo", 5] ] },
  { data: [ ["Bar", 10] ] }
]

Both item's dataIndex will be 0, because each one is the first element on its
series' data array. But the ticks array is something like:

["Foo", "Bar"]

If you use only dataIndex, you'll get the tick "Foo" for both items. What we
need is an offset which tells us which series the current item is in: that's
seriesIndex. The ticks' index is then calculated as ```seriesIndex +
dataIndex```.

That solves our problem. In our example, even though the dataIndex for both
items is 0, the seriesIndex for "Foo" is 0, and for "Bar" is 1. Everything
works as expected.

This fixes krzysu#65.
  • Loading branch information
vitorbaptista committed Mar 10, 2014
1 parent 744ea16 commit d21ecc5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
7 changes: 4 additions & 3 deletions js/jquery.flot.tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* author: Krzysztof Urbas @krzysu [myviews.pl]
* website: https://github.com/krzysu/flot.tooltip
*
* build on 2014-02-10
* build on 2014-03-10
* released under MIT License, 2012
*/
(function ($) {
Expand Down Expand Up @@ -224,8 +224,9 @@

// change x from number to given label, if given
if(typeof item.series.xaxis.ticks !== 'undefined') {
if(item.series.xaxis.ticks.length > item.dataIndex && !this.isTimeMode('xaxis', item))
content = content.replace(xPattern, item.series.xaxis.ticks[item.dataIndex].label);
var tickIndex = item.dataIndex + item.seriesIndex;
if(item.series.xaxis.ticks.length > tickIndex && !this.isTimeMode('xaxis', item))
content = content.replace(xPattern, item.series.xaxis.ticks[tickIndex].label);
}

// change y from number to given label, if given
Expand Down
4 changes: 2 additions & 2 deletions js/jquery.flot.tooltip.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions js/jquery.flot.tooltip.source.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,9 @@

// change x from number to given label, if given
if(typeof item.series.xaxis.ticks !== 'undefined') {
if(item.series.xaxis.ticks.length > item.dataIndex && !this.isTimeMode('xaxis', item))
content = content.replace(xPattern, item.series.xaxis.ticks[item.dataIndex].label);
var tickIndex = item.dataIndex + item.seriesIndex;
if(item.series.xaxis.ticks.length > tickIndex && !this.isTimeMode('xaxis', item))
content = content.replace(xPattern, item.series.xaxis.ticks[tickIndex].label);
}

// change y from number to given label, if given
Expand Down

0 comments on commit d21ecc5

Please sign in to comment.