Skip to content

Commit

Permalink
fastline controller
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Feb 23, 2020
1 parent 0d6c05c commit 090d1ef
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 12 deletions.
74 changes: 74 additions & 0 deletions src/controllers/controller.fastline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use strict';

const Line = require('../elements/element.line');
const LineController = require('./controller.line');
const defaults = require('../core/core.defaults');

defaults.fastline = defaults.line;

module.exports = LineController.extend({

addElements: function() {},

insertElements: function(start, count) {
this._parse(start, count);
},

/**
* @private
*/
_getMinMax: function(scale) {
const meta = this._cachedMeta;
const {_parsed} = meta;
const ilen = _parsed.length;
const isIndexScale = scale === meta.iScale;
let min = Number.POSITIVE_INFINITY;
let max = Number.NEGATIVE_INFINITY;
let i, value;


if (isIndexScale) {
if (ilen > 0) {
min = _parsed[0][scale.axis];
max = _parsed[ilen - 1][scale.axis];
}
} else {
for (i = 0; i < ilen; ++i) {
value = _parsed[i][scale.id];
min = Math.min(min, value);
max = Math.max(max, value);
}
}

return {min, max};
},

update: function() {
const me = this;
me._cachedMeta._options = me._resolveDatasetElementOptions();
},

draw: function() {
const me = this;
const ctx = me._ctx;
const meta = me._cachedMeta;
const {xScale, yScale, _stacked} = meta;
const options = meta._options;
let i = 0;

ctx.save();
Line._setStyle(ctx, options);
ctx.beginPath();

for (i = 0; i < meta._parsed.length; ++i) {
const parsed = me._getParsed(i);
const x = xScale.getPixelForValue(parsed[xScale.id]);
const y = yScale.getPixelForValue(_stacked ? me._applyStack(yScale, parsed) : parsed[yScale.id]);

ctx.lineTo(x, y);
}

ctx.stroke();
ctx.restore();
}
});
2 changes: 2 additions & 0 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import bar from './controller.bar';
import bubble from './controller.bubble';
import doughnut from './controller.doughnut';
import horizontalBar from './controller.horizontalBar';
import fastline from './controller.fastline';
import line from './controller.line';
import polarArea from './controller.polarArea';
import pie from './controller.pie';
Expand All @@ -16,6 +17,7 @@ export default {
bar,
bubble,
doughnut,
fastline,
horizontalBar,
line,
polarArea,
Expand Down
14 changes: 12 additions & 2 deletions src/core/core.interaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ import {_lookupByKey, _rlookupByKey} from '../helpers/helpers.collection';
* @typedef {{axis?:'x'|'y'|'xy', intersect:boolean}} IInteractionOptions
*/

function getOrCreateElement(metaset, index) {
let element = metaset.data[index];
if (!element) {
const controller = metaset.controller;
element = new controller.dataElementType();
controller.updateElements([element], index);
}
return element;
}

/**
* Helper function to get relative position for an event
* @param {Event|IEvent} e - The event to get the position for
Expand Down Expand Up @@ -92,7 +102,7 @@ function optimizedEvaluateItems(chart, axis, position, handler, intersect) {
const {index, data} = metasets[i];
const {lo, hi} = binarySearch(metasets[i], axis, value, intersect);
for (let j = lo; j <= hi; ++j) {
const element = data[j];
const element = getOrCreateElement(metasets[i], j);
if (!element.skip) {
handler(element, index, j);
}
Expand Down Expand Up @@ -207,7 +217,7 @@ export default {

chart.getSortedVisibleDatasetMetas().forEach((meta) => {
const index = items[0].index;
const element = meta.data[index];
const element = getOrCreateElement(meta, index);

// don't count items that are skipped (null data)
if (element && !element.skip) {
Expand Down
20 changes: 10 additions & 10 deletions src/elements/element.line.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@ defaults.set('elements', {
}
});

function setStyle(ctx, vm) {
ctx.lineCap = vm.borderCapStyle;
ctx.setLineDash(vm.borderDash);
ctx.lineDashOffset = vm.borderDashOffset;
ctx.lineJoin = vm.borderJoinStyle;
ctx.lineWidth = vm.borderWidth;
ctx.strokeStyle = vm.borderColor;
}

function lineTo(ctx, previous, target) {
ctx.lineTo(target.x, target.y);
}
Expand Down Expand Up @@ -345,7 +336,7 @@ export default class Line extends Element {

ctx.save();

setStyle(ctx, me.options);
Line._setStyle(ctx, me.options);

ctx.beginPath();

Expand All @@ -356,4 +347,13 @@ export default class Line extends Element {
ctx.stroke();
ctx.restore();
}

static _setStyle(ctx, options) {
ctx.lineCap = options.borderCapStyle;
ctx.setLineDash(options.borderDash);
ctx.lineDashOffset = options.borderDashOffset;
ctx.lineJoin = options.borderJoinStyle;
ctx.lineWidth = options.borderWidth;
ctx.strokeStyle = options.borderColor;
}
}

0 comments on commit 090d1ef

Please sign in to comment.