Skip to content

Commit

Permalink
Merge pull request #3261 from desowin/3211
Browse files Browse the repository at this point in the history
Implement moment diff cache in time scale
  • Loading branch information
etimberg authored Sep 12, 2016
2 parents 1c83123 + 0514ed4 commit d8b13ca
Showing 1 changed file with 72 additions and 6 deletions.
78 changes: 72 additions & 6 deletions src/scales/scale.time.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ module.exports = function(Chart) {

return null;
},
getLabelDiff: function(datasetIndex, index) {
var me = this;
if (datasetIndex === null || index === null) {
return null;
}

if (me.labelDiffs === undefined) {
me.buildLabelDiffs();
}

if (typeof me.labelDiffs[datasetIndex] !== 'undefined') {
return me.labelDiffs[datasetIndex][index];
}

return null;
},
getMomentStartOf: function(tick) {
var me = this;
if (me.options.time.unit === 'week' && me.options.time.isoWeekday !== false) {
Expand Down Expand Up @@ -163,6 +179,46 @@ module.exports = function(Chart) {
me.firstTick = (me.firstTick || moment()).clone();
me.lastTick = (me.lastTick || moment()).clone();
},
buildLabelDiffs: function() {
var me = this;
me.labelDiffs = [];
var scaleLabelDiffs = [];
// Parse common labels once
if (me.chart.data.labels && me.chart.data.labels.length > 0) {
helpers.each(me.chart.data.labels, function(label) {
var labelMoment = me.parseTime(label);

if (labelMoment.isValid()) {
if (me.options.time.round) {
labelMoment.startOf(me.options.time.round);
}
scaleLabelDiffs.push(labelMoment.diff(me.firstTick, me.tickUnit, true));
}
}, me);
}

helpers.each(me.chart.data.datasets, function(dataset) {
var diffsForDataset = [];

if (typeof dataset.data[0] === 'object' && dataset.data[0] !== null) {
helpers.each(dataset.data, function(value) {
var labelMoment = me.parseTime(me.getRightValue(value));

if (labelMoment.isValid()) {
if (me.options.time.round) {
labelMoment.startOf(me.options.time.round);
}
diffsForDataset.push(labelMoment.diff(me.firstTick, me.tickUnit, true));
}
}, me);
} else {
// We have no labels. Use common ones
diffsForDataset = scaleLabelDiffs;
}

me.labelDiffs.push(diffsForDataset);
}, me);
},
buildTicks: function() {
var me = this;

Expand Down Expand Up @@ -297,6 +353,9 @@ module.exports = function(Chart) {
}

me.ctx.restore();

// Invalidate label diffs cache
me.labelDiffs = undefined;
},
// Get tooltip label
getLabelForIndex: function(index, datasetIndex) {
Expand Down Expand Up @@ -333,15 +392,22 @@ module.exports = function(Chart) {
},
getPixelForValue: function(value, index, datasetIndex) {
var me = this;
if (!value || !value.isValid) {
// not already a moment object
value = me.parseTime(me.getRightValue(value));
var offset = null;
if (index !== undefined && datasetIndex !== undefined) {
offset = me.getLabelDiff(datasetIndex, index);
}
var labelMoment = value && value.isValid && value.isValid() ? value : me.getLabelMoment(datasetIndex, index);

if (labelMoment) {
var offset = labelMoment.diff(me.firstTick, me.tickUnit, true);
if (offset === null) {
if (!value || !value.isValid) {
// not already a moment object
value = me.parseTime(me.getRightValue(value));
}
if (value && value.isValid && value.isValid()) {
offset = value.diff(me.firstTick, me.tickUnit, true);
}
}

if (offset !== null) {
var decimal = offset !== 0 ? offset / me.scaleSizeInUnits : offset;

if (me.isHorizontal()) {
Expand Down

0 comments on commit d8b13ca

Please sign in to comment.