Skip to content

Commit

Permalink
Add data/dataset indices to line segment context (#9451)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Jul 21, 2021
1 parent 92e39a2 commit 151188e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
3 changes: 3 additions & 0 deletions docs/charts/line.md
Expand Up @@ -168,6 +168,9 @@ Context for the scriptable segment contains the following properties:
* `type`: `'segment'`
* `p0`: first point element
* `p1`: second point element
* `p0DataIndex`: index of first point in the data array
* `p1DataIndex`: index of second point in the data array
* `datasetIndex`: dataset index

[Example usage](../samples/line/segments.md)

Expand Down
1 change: 1 addition & 0 deletions src/controllers/controller.line.js
Expand Up @@ -27,6 +27,7 @@ export default class LineController extends DatasetController {
}

// Update Line
line._datasetIndex = me.index;
line._decimated = !!_dataset._decimated;
line.points = points;

Expand Down
1 change: 1 addition & 0 deletions src/elements/element.line.js
Expand Up @@ -251,6 +251,7 @@ export default class LineElement extends Element {
this._segments = undefined;
this._decimated = false;
this._pointsUpdated = false;
this._datasetIndex = undefined;

if (cfg) {
Object.assign(this, cfg);
Expand Down
22 changes: 15 additions & 7 deletions src/helpers/helpers.segment.js
Expand Up @@ -244,15 +244,14 @@ export function _computeSegments(line, segmentOptions) {

const loop = !!line._loop;
const {start, end} = findStartAndEnd(points, count, loop, spanGaps);
const baseStyle = readStyle(line.options);

if (spanGaps === true) {
return splitByStyles([{start, end, loop}], points, baseStyle, segmentOptions);
return splitByStyles(line, [{start, end, loop}], points, segmentOptions);
}

const max = end < start ? end + count : end;
const completeLoop = !!line._fullLoop && start === 0 && end === count - 1;
return splitByStyles(solidSegments(points, start, max, completeLoop), points, baseStyle, segmentOptions);
return splitByStyles(line, solidSegments(points, start, max, completeLoop), points, segmentOptions);
}

/**
Expand All @@ -261,20 +260,22 @@ export function _computeSegments(line, segmentOptions) {
* @param {object} [segmentOptions]
* @return {Segment[]}
*/
function splitByStyles(segments, points, baseStyle, segmentOptions) {
function splitByStyles(line, segments, points, segmentOptions) {
if (!segmentOptions || !segmentOptions.setContext || !points) {
return segments;
}
return doSplitByStyles(segments, points, baseStyle, segmentOptions);
return doSplitByStyles(line, segments, points, segmentOptions);
}

/**
* @param {LineElement} line
* @param {Segment[]} segments
* @param {PointElement[]} points
* @param {object} [segmentOptions]
* @return {Segment[]}
*/
function doSplitByStyles(segments, points, baseStyle, segmentOptions) {
function doSplitByStyles(line, segments, points, segmentOptions) {
const baseStyle = readStyle(line.options);
const count = points.length;
const result = [];
let start = segments[0].start;
Expand All @@ -285,7 +286,14 @@ function doSplitByStyles(segments, points, baseStyle, segmentOptions) {
let style;
for (i = start + 1; i <= segment.end; i++) {
const pt = points[i % count];
style = readStyle(segmentOptions.setContext({type: 'segment', p0: prev, p1: pt}));
style = readStyle(segmentOptions.setContext({
type: 'segment',
p0: prev,
p1: pt,
p0DataIndex: (i - 1) % count,
p1DataIndex: i % count,
datasetIndex: line._datasetIndex
}));
if (styleChanged(style, prevStyle)) {
result.push({start: start, end: i - 1, loop: segment.loop, style: prevStyle});
prevStyle = style;
Expand Down
5 changes: 4 additions & 1 deletion types/index.esm.d.ts
Expand Up @@ -28,7 +28,10 @@ export interface ScriptableContext<TType extends ChartType> {
export interface ScriptableLineSegmentContext {
type: 'segment',
p0: PointElement,
p1: PointElement
p1: PointElement,
p0DataIndex: number,
p1DataIndex: number,
datasetIndex: number
}

export type Scriptable<T, TContext> = T | ((ctx: TContext, options: AnyObject) => T);
Expand Down

0 comments on commit 151188e

Please sign in to comment.