mirror of
https://github.com/chartjs/Chart.js.git
synced 2025-12-08 20:36:08 +00:00
Default options can now be accessed by importing `core/core.defaults`. The returned object acts as a singleton and is populated when importing classes that expose their own default values (meaning that importing only `code.defaults` results in an empty object). Also make `Chart.Ticks` and `Chart.Interaction` importable since existing defaults rely on these values. Add the `defaults._set` method that make easier declaring new defaults by merging given values with existing ones for a specific scope (`global`, `scale`, `bar`, etc).
94 lines
2.5 KiB
JavaScript
94 lines
2.5 KiB
JavaScript
'use strict';
|
|
|
|
var defaults = require('../core/core.defaults');
|
|
var helpers = require('../helpers/index');
|
|
|
|
var globalDefaults = defaults.global;
|
|
|
|
defaults._set('global', {
|
|
elements: {
|
|
line: {
|
|
tension: 0.4,
|
|
backgroundColor: globalDefaults.defaultColor,
|
|
borderWidth: 3,
|
|
borderColor: globalDefaults.defaultColor,
|
|
borderCapStyle: 'butt',
|
|
borderDash: [],
|
|
borderDashOffset: 0.0,
|
|
borderJoinStyle: 'miter',
|
|
capBezierPoints: true,
|
|
fill: true, // do we fill in the area between the line and its base axis
|
|
}
|
|
}
|
|
});
|
|
|
|
module.exports = function(Chart) {
|
|
|
|
Chart.elements.Line = Chart.Element.extend({
|
|
draw: function() {
|
|
var me = this;
|
|
var vm = me._view;
|
|
var ctx = me._chart.ctx;
|
|
var spanGaps = vm.spanGaps;
|
|
var points = me._children.slice(); // clone array
|
|
var globalOptionLineElements = globalDefaults.elements.line;
|
|
var lastDrawnIndex = -1;
|
|
var index, current, previous, currentVM;
|
|
|
|
// If we are looping, adding the first point again
|
|
if (me._loop && points.length) {
|
|
points.push(points[0]);
|
|
}
|
|
|
|
ctx.save();
|
|
|
|
// Stroke Line Options
|
|
ctx.lineCap = vm.borderCapStyle || globalOptionLineElements.borderCapStyle;
|
|
|
|
// IE 9 and 10 do not support line dash
|
|
if (ctx.setLineDash) {
|
|
ctx.setLineDash(vm.borderDash || globalOptionLineElements.borderDash);
|
|
}
|
|
|
|
ctx.lineDashOffset = vm.borderDashOffset || globalOptionLineElements.borderDashOffset;
|
|
ctx.lineJoin = vm.borderJoinStyle || globalOptionLineElements.borderJoinStyle;
|
|
ctx.lineWidth = vm.borderWidth || globalOptionLineElements.borderWidth;
|
|
ctx.strokeStyle = vm.borderColor || globalDefaults.defaultColor;
|
|
|
|
// Stroke Line
|
|
ctx.beginPath();
|
|
lastDrawnIndex = -1;
|
|
|
|
for (index = 0; index < points.length; ++index) {
|
|
current = points[index];
|
|
previous = helpers.previousItem(points, index);
|
|
currentVM = current._view;
|
|
|
|
// First point moves to it's starting position no matter what
|
|
if (index === 0) {
|
|
if (!currentVM.skip) {
|
|
ctx.moveTo(currentVM.x, currentVM.y);
|
|
lastDrawnIndex = index;
|
|
}
|
|
} else {
|
|
previous = lastDrawnIndex === -1 ? previous : points[lastDrawnIndex];
|
|
|
|
if (!currentVM.skip) {
|
|
if ((lastDrawnIndex !== (index - 1) && !spanGaps) || lastDrawnIndex === -1) {
|
|
// There was a gap and this is the first point after the gap
|
|
ctx.moveTo(currentVM.x, currentVM.y);
|
|
} else {
|
|
// Line to next point
|
|
helpers.canvas.lineTo(ctx, previous._view, current._view);
|
|
}
|
|
lastDrawnIndex = index;
|
|
}
|
|
}
|
|
}
|
|
|
|
ctx.stroke();
|
|
ctx.restore();
|
|
}
|
|
});
|
|
};
|