Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fastline controller #6881

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/controllers/controller.fastline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import Line from '../elements/element.line';
import LineController from './controller.line';
import defaults from '../core/core.defaults';

defaults.fastline = defaults['line'];

export default class FastLineController extends LineController {

addElements() {}

insertElements(start, count) {
this.parse(start, count);
}

getMinMax(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() {
const me = this;
me._cachedMeta._options = me.resolveDatasetElementOptions();
}

draw() {
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();
}
}
1 change: 1 addition & 0 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export {default as bar} from './controller.bar';
export {default as bubble} from './controller.bubble';
export {default as doughnut} from './controller.doughnut';
export {default as fastline} from './controller.fastline';
export {default as line} from './controller.line';
export {default as polarArea} from './controller.polarArea';
export {default as pie} from './controller.pie';
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 @@ -9,6 +9,16 @@ import {getRelativePosition as helpersGetRelativePosition} from '../helpers/help
* @typedef {{datasetIndex: number, index: number, element: import("./core.element").default}} InteractionItem
*/

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 @@ -93,7 +103,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 @@ -213,7 +223,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 @@ -23,15 +23,6 @@ defaults.set(scope, {

defaults.route(scope, ['backgroundColor', 'borderColor'], '', 'color');

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 @@ -346,7 +337,7 @@ class Line extends Element {

ctx.save();

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

ctx.beginPath();

Expand All @@ -359,6 +350,15 @@ class Line extends Element {
}
}

Line._setStyle = function(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;
};

Line._type = 'line';

export default Line;