};
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) {
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) {
}
// 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) {
}, 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;