Skip to content

Commit 357d856

Browse files
committed
fastline controller
1 parent 2dd0c2f commit 357d856

File tree

4 files changed

+98
-12
lines changed

4 files changed

+98
-12
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
'use strict';
2+
3+
const Line = require('../elements/element.line');
4+
const LineController = require('./controller.line');
5+
const defaults = require('../core/core.defaults');
6+
7+
defaults.fastline = defaults.line;
8+
9+
module.exports = LineController.extend({
10+
11+
addElements: function() {},
12+
13+
insertElements: function(start, count) {
14+
this._parse(start, count);
15+
},
16+
17+
/**
18+
* @private
19+
*/
20+
_getMinMax: function(scale) {
21+
const meta = this._cachedMeta;
22+
const {_parsed} = meta;
23+
const ilen = _parsed.length;
24+
const isIndexScale = scale === meta.iScale;
25+
let min = Number.POSITIVE_INFINITY;
26+
let max = Number.NEGATIVE_INFINITY;
27+
let i, value;
28+
29+
30+
if (isIndexScale) {
31+
if (ilen > 0) {
32+
min = _parsed[0][scale.axis];
33+
max = _parsed[ilen - 1][scale.axis];
34+
}
35+
} else {
36+
for (i = 0; i < ilen; ++i) {
37+
value = _parsed[i][scale.id];
38+
min = Math.min(min, value);
39+
max = Math.max(max, value);
40+
}
41+
}
42+
43+
return {min, max};
44+
},
45+
46+
update: function() {
47+
const me = this;
48+
me._cachedMeta._options = me._resolveDatasetElementOptions();
49+
},
50+
51+
draw: function() {
52+
const me = this;
53+
const ctx = me._ctx;
54+
const meta = me._cachedMeta;
55+
const {xScale, yScale, _stacked} = meta;
56+
const options = meta._options;
57+
let i = 0;
58+
59+
ctx.save();
60+
Line._setStyle(ctx, options);
61+
ctx.beginPath();
62+
63+
for (i = 0; i < meta._parsed.length; ++i) {
64+
const parsed = me._getParsed(i);
65+
const x = xScale.getPixelForValue(parsed[xScale.id]);
66+
const y = yScale.getPixelForValue(_stacked ? me._applyStack(yScale, parsed) : parsed[yScale.id]);
67+
68+
ctx.lineTo(x, y);
69+
}
70+
71+
ctx.stroke();
72+
ctx.restore();
73+
}
74+
});

src/controllers/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var bubble = require('./controller.bubble');
55
var doughnut = require('./controller.doughnut');
66
var horizontalBar = require('./controller.horizontalBar');
77
var line = require('./controller.line');
8+
var fastline = require('./controller.fastline');
89
var polarArea = require('./controller.polarArea');
910
var pie = require('./controller.pie');
1011
var radar = require('./controller.radar');
@@ -19,6 +20,7 @@ module.exports = {
1920
bubble: bubble,
2021
doughnut: doughnut,
2122
horizontalBar: horizontalBar,
23+
fastline: fastline,
2224
line: line,
2325
polarArea: polarArea,
2426
pie: pie,

src/core/core.interaction.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
import helpers from '../helpers/index';
44
import {isNumber} from '../helpers/helpers.math';
55

6+
function getOrCreateElement(metaset, index) {
7+
let element = metaset.data[index];
8+
if (!element) {
9+
const controller = metaset.controller;
10+
element = new controller.dataElementType();
11+
controller.updateElements([element], index);
12+
}
13+
return element;
14+
}
15+
616
/**
717
* Helper function to get relative position for an event
818
* @param {Event|IEvent} event - The event to get the position for
@@ -65,7 +75,7 @@ function evaluateItemsAtIndex(chart, axis, position, handler) {
6575
for (let i = 0, ilen = metasets.length; i < ilen; ++i) {
6676
const metaset = metasets[i];
6777
const index = indices[i];
68-
const element = metaset.data[index];
78+
let element = getOrCreateElement(metaset, index);
6979
if (!element.skip) {
7080
handler(element, metaset.index, index);
7181
}
@@ -190,7 +200,7 @@ export default {
190200

191201
chart._getSortedVisibleDatasetMetas().forEach(function(meta) {
192202
const index = items[0].index;
193-
const element = meta.data[index];
203+
const element = getOrCreateElement(meta, index);
194204

195205
// don't count items that are skipped (null data)
196206
if (element && !element.skip) {

src/elements/element.line.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,6 @@ defaults._set('elements', {
2424
}
2525
});
2626

27-
function setStyle(ctx, vm) {
28-
ctx.lineCap = vm.borderCapStyle;
29-
ctx.setLineDash(vm.borderDash);
30-
ctx.lineDashOffset = vm.borderDashOffset;
31-
ctx.lineJoin = vm.borderJoinStyle;
32-
ctx.lineWidth = vm.borderWidth;
33-
ctx.strokeStyle = vm.borderColor;
34-
}
35-
3627
function lineTo(ctx, previous, target) {
3728
ctx.lineTo(target.x, target.y);
3829
}
@@ -324,7 +315,7 @@ class Line extends Element {
324315

325316
ctx.save();
326317

327-
setStyle(ctx, me.options);
318+
Line._setStyle(ctx, me.options);
328319

329320
ctx.beginPath();
330321

@@ -335,6 +326,15 @@ class Line extends Element {
335326
ctx.stroke();
336327
ctx.restore();
337328
}
329+
330+
static _setStyle(ctx, options) {
331+
ctx.lineCap = options.borderCapStyle;
332+
ctx.setLineDash(options.borderDash);
333+
ctx.lineDashOffset = options.borderDashOffset;
334+
ctx.lineJoin = options.borderJoinStyle;
335+
ctx.lineWidth = options.borderWidth;
336+
ctx.strokeStyle = options.borderColor;
337+
}
338338
}
339339

340340
Line.prototype._type = 'line';

0 commit comments

Comments
 (0)