From 180209e55c3ae3985c01cafffa5f7a51e4fb48d5 Mon Sep 17 00:00:00 2001 From: etimberg Date: Sun, 18 Oct 2015 16:13:57 -0400 Subject: [PATCH] Factor out `getLabelForIndex` into each scale so we can return appropriate data based on the scale type --- src/core/core.scale.js | 9 +++------ src/scales/scale.category.js | 4 ++++ src/scales/scale.linear.js | 4 ++++ src/scales/scale.logarithmic.js | 4 ++++ src/scales/scale.time.js | 4 ++++ 5 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/core/core.scale.js b/src/core/core.scale.js index 854c48892..a2e96eee9 100644 --- a/src/core/core.scale.js +++ b/src/core/core.scale.js @@ -313,12 +313,9 @@ return this.options.position == "top" || this.options.position == "bottom"; }, - getLabelForIndex: function(index, datasetIndex) { - if (this.isHorizontal()) { - return this.data.datasets[datasetIndex].label || this.data.labels[index]; - } - return this.data.datasets[datasetIndex].data[index]; - }, + // Used to get the value to display in the tooltip for the data at the given index + // function getLabelForIndex(index, datasetIndex) + getLabelForIndex: helpers.noop, // Used to get data value locations. Value can either be an index or a numerical value getPixelForValue: helpers.noop, diff --git a/src/scales/scale.category.js b/src/scales/scale.category.js index a2e569d4c..7c7656bcb 100644 --- a/src/scales/scale.category.js +++ b/src/scales/scale.category.js @@ -15,6 +15,10 @@ this.ticks = this.data.labels; }, + getLabelForIndex: function(index, datasetIndex) { + return this.ticks[index]; + }, + // Used to get data value locations. Value can either be an index or a numerical value getPixelForValue: function(value, index, datasetIndex, includeOffset) { diff --git a/src/scales/scale.linear.js b/src/scales/scale.linear.js index d522ae91c..9b36820d8 100644 --- a/src/scales/scale.linear.js +++ b/src/scales/scale.linear.js @@ -147,6 +147,10 @@ this.zeroLineIndex = this.ticks.indexOf(0); }, + getLabelForIndex: function(index, datasetIndex) { + return this.getRightValue(this.data.datasets[datasetIndex].data[index]); + }, + // Utils getPixelForValue: function(value, index, datasetIndex, includeOffset) { // This must be called after fit has been run so that diff --git a/src/scales/scale.logarithmic.js b/src/scales/scale.logarithmic.js index 2db11bf28..c8ce9bbba 100644 --- a/src/scales/scale.logarithmic.js +++ b/src/scales/scale.logarithmic.js @@ -121,6 +121,10 @@ this.ticks = this.tickValues.slice(); }, + // Get the correct tooltip label + getLabelForIndex: function(index, datasetIndex) { + return this.getRightValue(this.data.datasets[datasetIndex].data[index]); + }, // Get the correct value. If the value type is object get the x or y based on whether we are horizontal or not getRightValue: function(rawValue) { return typeof rawValue === "object" ? (this.isHorizontal() ? rawValue.x : rawValue.y) : rawValue; diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index 9ba486ce4..87b54ca84 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -140,6 +140,10 @@ this.ticks.push(this.firstTick.clone().add(i, this.tickUnit)); } }, + // Get tooltip label + getLabelForIndex: function(index, datasetIndex) { + return this.data.labels[index]; + }, convertTicksToLabels: function() { this.ticks = this.ticks.map(function(tick, index, ticks) { var formattedTick = tick.format(this.options.time.displayFormat ? this.options.time.displayFormat : time.unit[this.tickUnit].display); -- 2.47.3