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

Allow styling of line segments #8844

Merged
merged 9 commits into from Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/.vuepress/config.js
Expand Up @@ -111,6 +111,7 @@ module.exports = {
'line/interpolation',
'line/styling',
// 'line/point-styling',
'line/segments',
]
},
{
Expand Down
17 changes: 15 additions & 2 deletions docs/charts/line.md
Expand Up @@ -52,8 +52,8 @@ The line chart allows a number of properties to be specified for each dataset. T
| [`borderJoinStyle`](#line-styling) | `string` | Yes | - | `'miter'`
| [`borderWidth`](#line-styling) | `number` | Yes | - | `3`
| [`clip`](#general) | `number`\|`object` | - | - | `undefined`
| [`data`](#data-structure) | `object`\|`object[]`\| `number[]`\|`string[]` | - | - | **required**
| [`cubicInterpolationMode`](#cubicinterpolationmode) | `string` | Yes | - | `'default'`
| [`data`](#data-structure) | `object`\|`object[]`\| `number[]`\|`string[]` | - | - | **required**
| [`fill`](#line-styling) | `boolean`\|`string` | Yes | - | `false`
| [`hoverBackgroundColor`](#line-styling) | [`Color`](../general/colors.md) | Yes | - | `undefined`
| [`hoverBorderCapStyle`](#line-styling) | `string` | Yes | - | `undefined`
Expand All @@ -64,7 +64,6 @@ The line chart allows a number of properties to be specified for each dataset. T
| [`hoverBorderWidth`](#line-styling) | `number` | Yes | - | `undefined`
| [`indexAxis`](#general) | `string` | - | - | `'x'`
| [`label`](#general) | `string` | - | - | `''`
| [`tension`](#line-styling) | `number` | - | - | `0`
| [`order`](#general) | `number` | - | - | `0`
| [`pointBackgroundColor`](#point-styling) | `Color` | Yes | Yes | `'rgba(0, 0, 0, 0.1)'`
| [`pointBorderColor`](#point-styling) | `Color` | Yes | Yes | `'rgba(0, 0, 0, 0.1)'`
Expand All @@ -77,10 +76,12 @@ The line chart allows a number of properties to be specified for each dataset. T
| [`pointRadius`](#point-styling) | `number` | Yes | Yes | `3`
| [`pointRotation`](#point-styling) | `number` | Yes | Yes | `0`
| [`pointStyle`](#point-styling) | `string`\|`Image` | Yes | Yes | `'circle'`
| [`segments`](#segments) | `object` | - | - | `undefined`
| [`showLine`](#line-styling) | `boolean` | - | - | `true`
| [`spanGaps`](#line-styling) | `boolean`\|`number` | - | - | `undefined`
| [`stack`](#general) | `string` | - | - | `'line'` |
| [`stepped`](#stepped) | `boolean`\|`string` | - | - | `false`
| [`tension`](#line-styling) | `number` | - | - | `0`
| [`xAxisID`](#general) | `string` | - | - | first x axis
| [`yAxisID`](#general) | `string` | - | - | first y axis

Expand Down Expand Up @@ -158,6 +159,18 @@ The `'monotone'` algorithm is more suited to `y = f(x)` datasets: it preserves m

If left untouched (`undefined`), the global `options.elements.line.cubicInterpolationMode` property is used.

### Segments

Line segment styles can be overridde by scriptable options in the `segments` object. Currently all of the `border*` options are supported. The segment styles are resolved for each section of the line between each point. `undefined` fallbacks to main line styles.
kurkle marked this conversation as resolved.
Show resolved Hide resolved

Context for the scriptable segment contains the following properties:

* `type`: `'segment'`
* `p1`: first point element
kurkle marked this conversation as resolved.
Show resolved Hide resolved
* `p2`: second point element

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

### Stepped

The following values are supported for `stepped`.
Expand Down
43 changes: 43 additions & 0 deletions docs/samples/line/segments.md
@@ -0,0 +1,43 @@
# Line Segment Styling

```js chart-editor

// <block:segmentUtils:1>
const skipped = (ctx, value) => ctx.p1.skip || ctx.p2.skip ? value : undefined;
const down = (ctx, value) => ctx.p1.y < ctx.p2.y ? value : undefined;
// </block:segmentUtils>

// <block:genericOptions:2>
const genericOptions = {
fill: false,
interaction: {
intersect: false
},
radius: 0,
};
// </block:genericOptions>

// <block:config:0>
const config = {
type: 'line',
data: {
labels: Utils.months({count: 7}),
datasets: [{
label: 'My First Dataset',
data: [65, 59, NaN, 48, 56, 57, 40],
borderColor: 'rgb(75, 192, 192)',
segments: {
borderColor: ctx => skipped(ctx, 'rgb(0,0,0,0.2)') || down(ctx, 'rgb(192,75,75)'),
borderDash: ctx => skipped(ctx, [6, 6]),
}
}]
},
options: genericOptions
};
// </block:config>

module.exports = {
actions: [],
config: config,
};
```