Skip to content

Commit

Permalink
Merge branch 'master' into log_fix_pr_4198
Browse files Browse the repository at this point in the history
  • Loading branch information
ldemailly committed Nov 13, 2017
2 parents af3cb9d + 683e86e commit f86e669
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 92 deletions.
2 changes: 1 addition & 1 deletion docs/notes/extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ In addition, many charts can be found on the [npm registry](https://www.npmjs.co
- <a href="https://github.com/chartjs/chartjs-plugin-deferred" target="_blank">chartjs-plugin-deferred</a> - Defers initial chart update until chart scrolls into viewport.
- <a href="https://github.com/compwright/chartjs-plugin-draggable" target="_blank">chartjs-plugin-draggable</a> - Makes select chart elements draggable with the mouse.
- <a href="https://github.com/y-takey/chartjs-plugin-stacked100" target="_blank">chartjs-plugin-stacked100</a> - Draws 100% stacked bar chart.
- <a href="https://github.com/everestate/chartjs-plugin-waterfall" target="_blank">chartjs-plugin-waterfall</a> - Enables easy use of waterfall charts.
- <a href="https://github.com/everestate/chartjs-plugin-waterfall" target="_blank">chartjs-plugin-waterfall</a> - Enables easy use of waterfall charts.
- <a href="https://github.com/chartjs/chartjs-plugin-zoom" target="_blank">chartjs-plugin-zoom</a> - Enables zooming and panning on charts.

In addition, many plugins can be found on the [npm registry](https://www.npmjs.com/search?q=chartjs-plugin-).
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"main": "Chart.js"
},
"dependencies": {
"chartjs-color": "~2.2.0",
"moment": "~2.19.1"
"chartjs-color": "^2.1.0",
"moment": "^2.10.2"
}
}
8 changes: 5 additions & 3 deletions src/core/core.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,11 @@ module.exports = function(Chart) {
me.updateLayout();

// Can only reset the new controllers after the scales have been updated
helpers.each(newControllers, function(controller) {
controller.reset();
});
if (me.options.animation && me.options.animation.duration) {
helpers.each(newControllers, function(controller) {
controller.reset();
});
}

me.updateDatasets();

Expand Down
16 changes: 11 additions & 5 deletions src/core/core.tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,10 @@ module.exports = function(Chart) {
}

olf = function(x) {
return x + size.width > chart.width;
return x + size.width + model.caretSize + model.caretPadding > chart.width;
};
orf = function(x) {
return x - size.width < 0;
return x - size.width - model.caretSize - model.caretPadding < 0;
};
yf = function(y) {
return y <= midY ? 'top' : 'bottom';
Expand Down Expand Up @@ -336,7 +336,7 @@ module.exports = function(Chart) {
/**
* @Helper to get the location a tooltip needs to be placed at given the initial position (via the vm) and the size and alignment
*/
function getBackgroundPoint(vm, size, alignment) {
function getBackgroundPoint(vm, size, alignment, chart) {
// Background Position
var x = vm.x;
var y = vm.y;
Expand All @@ -353,6 +353,12 @@ module.exports = function(Chart) {
x -= size.width;
} else if (xAlign === 'center') {
x -= (size.width / 2);
if (x + size.width > chart.width) {
x = chart.width - size.width;
}
if (x < 0) {
x = 0;
}
}

if (yAlign === 'top') {
Expand Down Expand Up @@ -545,7 +551,7 @@ module.exports = function(Chart) {
tooltipSize = getTooltipSize(this, model);
alignment = determineAlignment(this, tooltipSize);
// Final Size and Position
backgroundPoint = getBackgroundPoint(model, tooltipSize, alignment);
backgroundPoint = getBackgroundPoint(model, tooltipSize, alignment, me._chart);
} else {
model.opacity = 0;
}
Expand Down Expand Up @@ -617,7 +623,7 @@ module.exports = function(Chart) {
x1 = x2 - caretSize;
x3 = x2 + caretSize;
} else {
x2 = ptX + (width / 2);
x2 = vm.caretX;
x1 = x2 - caretSize;
x3 = x2 + caretSize;
}
Expand Down
114 changes: 72 additions & 42 deletions src/scales/scale.logarithmic.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,64 +177,94 @@ module.exports = function(Chart) {
getPixelForTick: function(index) {
return this.getPixelForValue(this.tickValues[index]);
},
/**
* Returns the value of the first tick.
* @param {Number} value - The minimum not zero value.
* @return {Number} The first tick value.
* @private
*/
_getFirstTickValue: function(value) {
var exp = Math.floor(helpers.log10(value));
var significand = Math.floor(value / Math.pow(10, exp));

return significand * Math.pow(10, exp);
},
getPixelForValue: function(value) {
var me = this;
var start = me.start;
var newVal = +me.getRightValue(value);
var opts = me.options;
var tickOpts = opts.ticks;
var innerDimension, pixel, range;
var reverse = me.options.ticks.reverse;
var log10 = helpers.log10;
var firstTickValue = me._getFirstTickValue(me.minNotZero);
var offset = 0;
var innerDimension, pixel, start, end, sign;

value = +me.getRightValue(value);
if (reverse) {
start = me.end;
end = me.start;
sign = -1;
} else {
start = me.start;
end = me.end;
sign = 1;
}
if (me.isHorizontal()) {
range = helpers.log10(me.end) - helpers.log10(start); // todo: if start === 0
if (newVal === 0) {
pixel = me.left;
} else {
innerDimension = me.width;
pixel = me.left + (innerDimension / range * (helpers.log10(newVal) - helpers.log10(start)));
}
innerDimension = me.width;
pixel = reverse ? me.right : me.left;
} else {
// Bottom - top since pixels increase downward on a screen
innerDimension = me.height;
if (start === 0 && !tickOpts.reverse) {
range = helpers.log10(me.end) - helpers.log10(me.minNotZero);
if (newVal === start) {
pixel = me.bottom;
} else if (newVal === me.minNotZero) {
pixel = me.bottom - innerDimension * 0.02;
} else {
pixel = me.bottom - innerDimension * 0.02 - (innerDimension * 0.98 / range * (helpers.log10(newVal) - helpers.log10(me.minNotZero)));
}
} else if (me.end === 0 && tickOpts.reverse) {
range = helpers.log10(me.start) - helpers.log10(me.minNotZero);
if (newVal === me.end) {
pixel = me.top;
} else if (newVal === me.minNotZero) {
pixel = me.top + innerDimension * 0.02;
} else {
pixel = me.top + innerDimension * 0.02 + (innerDimension * 0.98 / range * (helpers.log10(newVal) - helpers.log10(me.minNotZero)));
}
} else if (newVal === 0) {
pixel = tickOpts.reverse ? me.top : me.bottom;
} else {
range = helpers.log10(me.end) - helpers.log10(start);
innerDimension = me.height;
pixel = me.bottom - (innerDimension / range * (helpers.log10(newVal) - helpers.log10(start)));
sign *= -1; // invert, since the upper-left corner of the canvas is at pixel (0, 0)
pixel = reverse ? me.top : me.bottom;
}
if (value !== start) {
if (start === 0) { // include zero tick
offset = helpers.getValueOrDefault(
me.options.ticks.fontSize,
Chart.defaults.global.defaultFontSize
);
innerDimension -= offset;
start = firstTickValue;
}
if (value !== 0) {
offset += innerDimension / (log10(end) - log10(start)) * (log10(value) - log10(start));
}
pixel += sign * offset;
}
return pixel;
},
getValueForPixel: function(pixel) {
var me = this;
var range = helpers.log10(me.end) - helpers.log10(me.start);
var value, innerDimension;
var reverse = me.options.ticks.reverse;
var log10 = helpers.log10;
var firstTickValue = me._getFirstTickValue(me.minNotZero);
var innerDimension, start, end, value;

if (reverse) {
start = me.end;
end = me.start;
} else {
start = me.start;
end = me.end;
}
if (me.isHorizontal()) {
innerDimension = me.width;
value = me.start * Math.pow(10, (pixel - me.left) * range / innerDimension);
} else { // todo: if start === 0
value = reverse ? me.right - pixel : pixel - me.left;
} else {
innerDimension = me.height;
value = Math.pow(10, (me.bottom - pixel) * range / innerDimension) / me.start;
value = reverse ? pixel - me.top : me.bottom - pixel;
}
if (value !== start) {
if (start === 0) { // include zero tick
var offset = helpers.getValueOrDefault(
me.options.ticks.fontSize,
Chart.defaults.global.defaultFontSize
);
value -= offset;
innerDimension -= offset;
start = firstTickValue;
}
value *= log10(end) - log10(start);
value /= innerDimension;
value = Math.pow(10, log10(start) + value);
}
return value;
}
Expand Down
67 changes: 67 additions & 0 deletions test/specs/core.tooltip.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -882,4 +882,71 @@ describe('Core.Tooltip', function() {
expect(fn.calls.first().object instanceof Chart.Tooltip).toBe(true);
});
});

it('Should avoid tooltip truncation in x axis if there is enough space to show tooltip without truncation', function() {
var chart = window.acquireChart({
type: 'pie',
data: {
datasets: [{
data: [
50,
50
],
backgroundColor: [
'rgb(255, 0, 0)',
'rgb(0, 255, 0)'
],
label: 'Dataset 1'
}],
labels: [
'Red long tooltip text to avoid unnecessary loop steps',
'Green long tooltip text to avoid unnecessary loop steps'
]
},
options: {
responsive: true,
animation: {
// without this slice center point is calculated wrong
animateRotate: false
}
}
});

// Trigger an event over top of the slice
for (var slice = 0; slice < 2; slice++) {
var meta = chart.getDatasetMeta(0);
var point = meta.data[slice].getCenterPoint();
var tooltipPosition = meta.data[slice].tooltipPosition();
var node = chart.canvas;
var rect = node.getBoundingClientRect();

var mouseMoveEvent = new MouseEvent('mousemove', {
view: window,
bubbles: true,
cancelable: true,
clientX: rect.left + point.x,
clientY: rect.top + point.y
});
var mouseOutEvent = new MouseEvent('mouseout');

// Lets cycle while tooltip is narrower than chart area
var infiniteCycleDefense = 70;
for (var i = 0; i < infiniteCycleDefense; i++) {
chart.config.data.labels[slice] = chart.config.data.labels[slice] + 'l';
chart.update();
node.dispatchEvent(mouseOutEvent);
node.dispatchEvent(mouseMoveEvent);
var model = chart.tooltip._model;
expect(model.x).toBeGreaterThanOrEqual(0);
if (model.width <= chart.width) {
expect(model.x + model.width).toBeLessThanOrEqual(chart.width);
}
expect(model.caretX).toBe(tooltipPosition.x);
// if tooltip is longer than chart area then all tests done
if (model.width > chart.width) {
break;
}
}
}
});
});
Loading

0 comments on commit f86e669

Please sign in to comment.