Skip to content

Enable the line curve in the line annotation #754

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

Merged
merged 40 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
9105c23
Enable the line curve in the line annotation
stockiNail May 30, 2022
4054655
fixes some CC issues
stockiNail May 30, 2022
45de048
changes cp calculation
stockiNail May 30, 2022
77c6872
re-enables auto rotation of label on curve
stockiNail May 30, 2022
f5b7a6f
adds test cases
stockiNail May 31, 2022
f4b1b6e
fixes duplicated code
stockiNail May 31, 2022
0111aca
adds types
stockiNail May 31, 2022
3815858
adds doc
stockiNail May 31, 2022
862afd8
adds sample
stockiNail May 31, 2022
7eaadd9
fixes CC issue of complexity
stockiNail May 31, 2022
7433433
adds import of ts type for doc
stockiNail May 31, 2022
321f83e
removes CC issues
stockiNail May 31, 2022
962ee4b
Add element diagrams to the annotation types guide
stockiNail Jun 6, 2022
05e619c
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Jun 8, 2022
22a3aa3
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Jun 8, 2022
f5186fe
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Jun 8, 2022
3022ba2
Merge remote-tracking branch 'origin/master' into lineCurve
stockiNail Jun 9, 2022
6d87201
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Jul 21, 2022
84366b3
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Jul 22, 2022
ec3a554
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Jul 26, 2022
1324e43
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Aug 4, 2022
693e84d
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Aug 19, 2022
aa9b4e2
Merge remote-tracking branch 'origin/master' into lineCurve
stockiNail Aug 19, 2022
30b5941
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Aug 19, 2022
b30c95e
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Sep 28, 2022
ff2a323
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Sep 28, 2022
58f9161
Update src/helpers/helpers.options.js
stockiNail Sep 28, 2022
62d672c
Update src/types/line.js
stockiNail Sep 28, 2022
b67631c
apply review
stockiNail Sep 28, 2022
2279070
Merge remote-tracking branch 'origin/master' into lineCurve
stockiNail Sep 28, 2022
fcfc9ae
fixes event location with different devicePixelradio
stockiNail Sep 29, 2022
f718cf4
uses chart.currentDevicePixelRatio
stockiNail Sep 30, 2022
b686c7a
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Oct 28, 2022
30446eb
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Nov 15, 2022
63e01f8
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Nov 15, 2022
235712c
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Nov 17, 2022
5114654
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Jan 9, 2023
81c9675
Merge remote-tracking branch 'origin/master' into lineCurve
stockiNail Jan 9, 2023
e43a62e
Merge branch 'master' of https://github.com/chartjs/chartjs-plugin-an…
stockiNail Jan 27, 2023
6b0e946
# WARNING: head commit changed in the meantime
stockiNail Jan 27, 2023
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
1 change: 1 addition & 0 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ module.exports = {
title: 'Line annotations',
children: [
'line/basic',
'line/curve',
'line/lowerUpper',
'line/limited',
'line/average',
Expand Down
4 changes: 4 additions & 0 deletions docs/guide/types/line.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ The following options are available for line annotations.
| ---- | ---- | :----: | ----
| [`arrowHeads`](#arrow-heads) | `{start: object, end:object}` | Yes |
| [`borderWidth`](#styling) | `number` | Yes | `2`
| [`controlPoint`](#general) | `number` \| `string` \| `{x: number | string, y: number | string}` | Yes | `{y:'-50%'}`
| [`curve`](#general) | `boolean` | Yes | `false`
| [`endValue`](#positioning) | `number` | Yes | `undefined`
| [`label`](#label) | `object` | Yes |
| [`scaleID`](#positioning) | `string` | Yes | `undefined`
Expand Down Expand Up @@ -83,6 +85,8 @@ If `scaleID` is unset, then `xScaleID` and `yScaleID` are used to draw a line fr
| Name | Description
| ---- | ----
| `adjustScaleRange` | Should the scale range be adjusted if this annotation is out of range.
| `controlPoint` | if `curve` is enabled, it configures the control point to drawn the curve, calculated in pixels. It can be set by a string in percentage format `'number%'` which are representing the percentage of the distance between the start and end point from the center.
| `curve` | Whether or not a quadratic [Bézier curve](https://developer.mozilla.org/en-US/docs/Glossary/Bezier_curve) is drawn.
| `display` | Whether or not this annotation is visible.
| `drawTime` | See [drawTime](../options#draw-time).
| `endValue` | End two of the line when a single scale is specified.
Expand Down
78 changes: 78 additions & 0 deletions docs/samples/line/curve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Curve

```js chart-editor
// <block:setup:2>
const DATA_COUNT = 8;
const MIN = 10;
const MAX = 100;

Utils.srand(8);

const labels = [];
for (let i = 0; i < DATA_COUNT; ++i) {
labels.push('' + i);
}

const numberCfg = {count: DATA_COUNT, min: MIN, max: MAX};

const data = {
labels: labels,
datasets: [{
data: Utils.numbers(numberCfg)
}]
};
// </block:setup>

// <block:annotation:1>
const annotation = {
type: 'line',
borderColor: 'black',
borderWidth: 3,
xScaleID: 'x',
yScaleID: 'y',
xMin: 1,
xMax: 6,
yMin: ({chart}) => chart.data.datasets[0].data[1] / 2,
yMax: ({chart}) => chart.data.datasets[0].data[6] / 2,
curve: true,
arrowHeads: {
end: {
display: true
}
}
};
// </block:annotation>

/* <block:config:0> */
const config = {
type: 'bar',
data,
options: {
plugins: {
annotation: {
annotations: {
annotation
}
}
}
}
};
/* </block:config> */

const actions = [
{
name: 'Randomize',
handler: function(chart) {
chart.data.datasets.forEach(function(dataset, i) {
dataset.data = dataset.data.map(() => Utils.rand(MIN, MAX));
});
chart.update();
}
}
];

module.exports = {
actions: actions,
config: config
};
```
48 changes: 48 additions & 0 deletions src/helpers/helpers.chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ import {isFinite, toPadding} from 'chart.js/helpers';
import {measureLabelSize} from './helpers.canvas';
import {isBoundToPoint, getRelativePosition, toPosition} from './helpers.options';

const limitedLineScale = {
xScaleID: {min: 'xMin', max: 'xMax', start: 'left', end: 'right', startProp: 'x', endProp: 'x2'},
yScaleID: {min: 'yMin', max: 'yMax', start: 'bottom', end: 'top', startProp: 'y', endProp: 'y2'}
};

/**
* @typedef { import("chart.js").Chart } Chart
* @typedef { import("chart.js").Scale } Scale
* @typedef { import("chart.js").Point } Point
* @typedef { import('../../types/element').AnnotationBoxModel } AnnotationBoxModel
* @typedef { import('../../types/options').CoreAnnotationOptions } CoreAnnotationOptions
* @typedef { import('../../types/options').LineAnnotationOptions } LineAnnotationOptions
* @typedef { import('../../types/options').PointAnnotationOptions } PointAnnotationOptions
* @typedef { import('../../types/options').PolygonAnnotationOptions } PolygonAnnotationOptions
*/
Expand Down Expand Up @@ -144,6 +150,23 @@ export function resolvePointProperties(chart, options) {
}
return getChartCircle(chart, options);
}
/**
* @param {Chart} chart
* @param {LineAnnotationOptions} options
* @returns {AnnotationBoxModel}
*/
export function resolveLineProperties(chart, options) {
const {scales, chartArea} = chart;
const scale = scales[options.scaleID];
const area = {x: chartArea.left, y: chartArea.top, x2: chartArea.right, y2: chartArea.bottom};

if (scale) {
resolveFullLineProperties(scale, area, options);
} else {
resolveLimitedLineProperties(scales, area, options);
}
return area;
}

/**
* @param {Chart} chart
Expand Down Expand Up @@ -185,6 +208,30 @@ function getChartDimensionByScale(scale, options) {
};
}

function resolveFullLineProperties(scale, area, options) {
const min = scaleValue(scale, options.value, NaN);
const max = scaleValue(scale, options.endValue, min);
if (scale.isHorizontal()) {
area.x = min;
area.x2 = max;
} else {
area.y = min;
area.y2 = max;
}
}

function resolveLimitedLineProperties(scales, area, options) {
for (const scaleId of Object.keys(limitedLineScale)) {
const scale = scales[retrieveScaleID(scales, options, scaleId)];
if (scale) {
const {min, max, start, end, startProp, endProp} = limitedLineScale[scaleId];
const dim = getDimensionByScale(scale, {min: options[min], max: options[max], start: scale[start], end: scale[end]});
area[startProp] = dim.start;
area[endProp] = dim.end;
}
}
}

function calculateX({properties, options}, labelSize, position, padding) {
const {x: start, x2: end, width: size} = properties;
return calculatePosition({start, end, size, borderWidth: options.borderWidth}, {
Expand Down Expand Up @@ -234,4 +281,5 @@ function resolveLabelElementProperties(chart, properties, options) {
centerY: y + height / 2,
rotation: label.rotation
};

}
25 changes: 13 additions & 12 deletions src/helpers/helpers.options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import {isObject, valueOrDefault, defined} from 'chart.js/helpers';
import {clamp} from './helpers.core';

const isPercentString = (s) => typeof s === 'string' && s.endsWith('%');
const toPercent = (s) => clamp(parseFloat(s) / 100, 0, 1);
const toPercent = (s) => parseFloat(s) / 100;
const toPositivePercent = (s) => clamp(toPercent(s), 0, 1);

/**
* @typedef { import('../../types/options').AnnotationPointCoordinates } AnnotationPointCoordinates
Expand All @@ -13,7 +14,6 @@ const toPercent = (s) => clamp(parseFloat(s) / 100, 0, 1);
/**
* @param {number} size
* @param {number|string} position
* @param {number} to
* @returns {number}
*/
export function getRelativePosition(size, position) {
Expand All @@ -24,22 +24,22 @@ export function getRelativePosition(size, position) {
return size;
}
if (isPercentString(position)) {
return toPercent(position) * size;
return toPositivePercent(position) * size;
}
return size / 2;
}

/**
* @param {number} size
* @param {number|string} value
* @param {number} to
* @param {boolean} [positivePercent=true]
* @returns {number}
*/
export function getSize(size, value) {
export function getSize(size, value, positivePercent = true) {
if (typeof value === 'number') {
return value;
} else if (isPercentString(value)) {
return toPercent(value) * size;
return (positivePercent ? toPositivePercent(value) : toPercent(value)) * size;
}
return size;
}
Expand All @@ -61,17 +61,18 @@ export function calculateTextAlignment(size, options) {
}

/**
* @param {LabelPositionObject|string} value
* @returns {LabelPositionObject}
* @param {{x: number|string, y: number|string}|string|number} value
* @param {string|number} defaultValue
* @returns {{x: number|string, y: number|string}}
*/
export function toPosition(value) {
export function toPosition(value, defaultValue = 'center') {
if (isObject(value)) {
return {
x: valueOrDefault(value.x, 'center'),
y: valueOrDefault(value.y, 'center'),
x: valueOrDefault(value.x, defaultValue),
y: valueOrDefault(value.y, defaultValue),
};
}
value = valueOrDefault(value, 'center');
value = valueOrDefault(value, defaultValue);
return {
x: value,
y: value
Expand Down
2 changes: 1 addition & 1 deletion src/types/label.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ LabelAnnotation.defaultRoutes = {
function measureRect(point, size, options, padding) {
const width = size.width + padding.width + options.borderWidth;
const height = size.height + padding.height + options.borderWidth;
const position = toPosition(options.position);
const position = toPosition(options.position, 'center');
const x = calculatePosition(point.x, width, options.xAdjust, position.x);
const y = calculatePosition(point.y, height, options.yAdjust, position.y);

Expand Down
Loading