From: etimberg Date: Sun, 18 Oct 2015 22:00:46 +0000 (-0400) Subject: Time scales now support passing in data as points. Added this to the time scale sampl... X-Git-Tag: 2.0.0-beta~4^2~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7603b3cc36f6c5ac172855ff4ec722c4442bf738;p=thirdparty%2FChart.js.git Time scales now support passing in data as points. Added this to the time scale sample file. --- diff --git a/samples/line-time-scale.html b/samples/line-time-scale.html index 327054703..224fb392b 100644 --- a/samples/line-time-scale.html +++ b/samples/line-time-scale.html @@ -63,6 +63,22 @@ }, { label: "My Second dataset", data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()], + }, { + label: "Dataset with point data", + data: [{ + x: "12/31/2014 06:00", + y: randomScalingFactor() + }, { + x: "01/04/2015 13:00", + y: randomScalingFactor() + }, { + x: "01/07/2015 01:15", + y: randomScalingFactor() + }, { + x: "01/15/2015 01:15", + y: randomScalingFactor() + }], + fill: false }] }, options: { diff --git a/src/core/core.scale.js b/src/core/core.scale.js index a2e96eee9..c9f842339 100644 --- a/src/core/core.scale.js +++ b/src/core/core.scale.js @@ -304,14 +304,15 @@ }, afterFit: helpers.noop, - - - - // Shared Methods isHorizontal: function() { return this.options.position == "top" || this.options.position == "bottom"; }, + + // 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" && rawValue !== null) ? (this.isHorizontal() ? rawValue.x : rawValue.y) : rawValue; + }, // Used to get the value to display in the tooltip for the data at the given index // function getLabelForIndex(index, datasetIndex) diff --git a/src/scales/scale.category.js b/src/scales/scale.category.js index 7c7656bcb..e85ba04ea 100644 --- a/src/scales/scale.category.js +++ b/src/scales/scale.category.js @@ -21,7 +21,6 @@ // Used to get data value locations. Value can either be an index or a numerical value getPixelForValue: function(value, index, datasetIndex, includeOffset) { - if (this.isHorizontal()) { var innerWidth = this.width - (this.paddingLeft + this.paddingRight); var valueWidth = innerWidth / Math.max((this.data.labels.length - ((this.options.gridLines.offsetGridLines) ? 0 : 1)), 1); diff --git a/src/scales/scale.linear.js b/src/scales/scale.linear.js index 9b36820d8..194e6da81 100644 --- a/src/scales/scale.linear.js +++ b/src/scales/scale.linear.js @@ -169,12 +169,6 @@ return Math.round(pixel); } }, - - // 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" && rawValue !== null) ? (this.isHorizontal() ? rawValue.x : rawValue.y) : rawValue; - }, - }); Chart.scaleService.registerScaleType("linear", LinearScale, defaultConfig); diff --git a/src/scales/scale.logarithmic.js b/src/scales/scale.logarithmic.js index c8ce9bbba..53ef000ce 100644 --- a/src/scales/scale.logarithmic.js +++ b/src/scales/scale.logarithmic.js @@ -125,10 +125,6 @@ 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; - }, getPixelForTick: function(index, includeOffset) { return this.getPixelForValue(this.tickValues[index], null, null, includeOffset); }, diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index 87b54ca84..2ed211528 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -74,23 +74,59 @@ }; var TimeScale = Chart.Scale.extend({ - buildTicks: function(index) { - - this.ticks = []; - this.labelMoments = []; + getLabelMoment: function(datasetIndex, index) { + return this.labelMoments[datasetIndex][index]; + }, - // Parse each label into a moment - this.data.labels.forEach(function(label, index) { + buildLabelMoments: function() { + // Only parse these once. If the dataset does not have data as x,y pairs, we will use + // these + var scaleLabelMoments = []; + helpers.each(this.data.labels, function(label, index) { var labelMoment = this.parseTime(label); if (this.options.time.round) { labelMoment.startOf(this.options.time.round); } - this.labelMoments.push(labelMoment); + scaleLabelMoments.push(labelMoment); + }, this); + + this.firstTick = moment.min.call(this, scaleLabelMoments); + this.lastTick = moment.max.call(this, scaleLabelMoments); + + helpers.each(this.data.datasets, function(dataset, datasetIndex) { + var momentsForDataset = []; + + if (typeof dataset.data[0] === 'object') { + helpers.each(dataset.data, function(value, index) { + var labelMoment = this.parseTime(this.getRightValue(value)); + if (this.options.time.round) { + labelMoment.startOf(this.options.time.round); + } + momentsForDataset.push(labelMoment); + + // May have gone outside the scale ranges, make sure we keep the first and last ticks updated + this.firstTick = moment.min(this.firstTick, labelMoment); + this.lastTick = moment.max(this.lastTick, labelMoment); + }, this); + } else { + // We have no labels. Use the ones from the scale + momentsForDataset = scaleLabelMoments; + } + + this.labelMoments.push(momentsForDataset); }, this); - // Find the first and last moments, and range - this.firstTick = moment.min.call(this, this.labelMoments).clone(); - this.lastTick = moment.max.call(this, this.labelMoments).clone(); + // We will modify these, so clone for later + this.firstTick = this.firstTick.clone(); + this.lastTick = this.lastTick.clone(); + }, + + buildTicks: function(index) { + + this.ticks = []; + this.labelMoments = []; + + this.buildLabelMoments(); // Set unit override if applicable if (this.options.time.unit) { @@ -124,11 +160,11 @@ this.lastTick.endOf(this.tickUnit); this.smallestLabelSeparation = this.width; - var i = 0; - - for (i = 1; i < this.labelMoments.length; i++) { - this.smallestLabelSeparation = Math.min(this.smallestLabelSeparation, this.labelMoments[i].diff(this.labelMoments[i - 1], this.tickUnit, true)); - } + helpers.each(this.data.datasets, function(dataset, datasetIndex) { + for (var i = 1; i < this.labelMoments[datasetIndex].length; i++) { + this.smallestLabelSeparation = Math.min(this.smallestLabelSeparation, this.labelMoments[datasetIndex][i].diff(this.labelMoments[datasetIndex][i - 1], this.tickUnit, true)); + } + }, this); // Tick displayFormat override if (this.options.time.displayFormat) { @@ -136,13 +172,19 @@ } // For every unit in between the first and last moment, create a moment and add it to the ticks tick - for (i = 0; i <= this.tickRange; ++i) { + for (var i = 0; i <= this.tickRange; ++i) { this.ticks.push(this.firstTick.clone().add(i, this.tickUnit)); } }, // Get tooltip label getLabelForIndex: function(index, datasetIndex) { - return this.data.labels[index]; + var label = this.data.labels[index]; + + if (typeof this.data.datasets[datasetIndex].data[0] === 'object') { + label = this.getRightValue(this.data.datasets[datasetIndex].data[index]); + } + + return label; }, convertTicksToLabels: function() { this.ticks = this.ticks.map(function(tick, index, ticks) { @@ -156,8 +198,8 @@ }, this); }, getPixelForValue: function(value, index, datasetIndex, includeOffset) { - - var offset = this.labelMoments[index].diff(this.firstTick, this.tickUnit, true); + var labelMoment = this.getLabelMoment(datasetIndex, index); + var offset = labelMoment.diff(this.firstTick, this.tickUnit, true); var decimal = offset / this.tickRange;