From: Jack Valuntine Date: Thu, 9 Jun 2016 04:34:15 +0000 (+0900) Subject: time type xAxis height crah in line chart X-Git-Tag: v2.2.0-rc.1~28^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F2741%2Fhead;p=thirdparty%2FChart.js.git time type xAxis height crah in line chart if input only one data in dataset and xAxis type is date make offset crash. check offset value and don't devide 0 value. Add xAxis check test case. --- diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js old mode 100644 new mode 100755 index 1c2ef44b9..fbb4fbfa1 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -332,7 +332,7 @@ module.exports = function(Chart) { if (labelMoment) { var offset = labelMoment.diff(me.firstTick, me.tickUnit, true); - var decimal = offset / me.scaleSizeInUnits; + var decimal = offset !== 0 ? offset / me.scaleSizeInUnits : offset; if (me.isHorizontal()) { var innerWidth = me.width - (me.paddingLeft + me.paddingRight); diff --git a/test/scale.time.tests.js b/test/scale.time.tests.js old mode 100644 new mode 100755 index 9966f9cc0..0d1b8b8c0 --- a/test/scale.time.tests.js +++ b/test/scale.time.tests.js @@ -435,4 +435,47 @@ describe('Time scale tests', function() { expect(xScale.getLabelForIndex(6, 0)).toBe('2015-01-10T12:00'); }); + it('should get the correct pixel for only one data in the dataset', function() { + var chart = window.acquireChart({ + type: 'line', + data: { + labels: ["2016-05-27"], + datasets: [{ + type: "line", + data: [5] + }] + }, + options: { + scales: { + xAxes: [{ + display: true, + type: "time", + time: { + displayFormats: { + "day": "YYYY-MM-DD" + } + } + }], + yAxes: [{ + type: "linear", + ticks: { + reverse: true, + min: 0, + max: 10 + } + }] + } + } + }); + + var xScale = chartInstance.scales.xScale0; + + expect(xScale.getPixelForValue('', 0, 0)).toBeCloseToPixel(78); + + expect(xScale.getValueForPixel(78)).toBeCloseToTime({ + value: moment(chartInstance.data.labels[0]), + unit: 'day', + threshold: 0.75 + }); + }); });