-
Notifications
You must be signed in to change notification settings - Fork 360
Enable centered label drawing for doughnut controllers #825
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
Changes from 20 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
050d45e
Enable centered label drawing for doughnut controllers
stockiNail bffc452
InnerLabel uses the label annotation
stockiNail 00fa4ee
moves code in afterUpdate hook
stockiNail 93e4816
adds listeners to events
stockiNail a2001f1
changes context type for innerlabel
stockiNail 5fb097d
moves to a specific element for doughnut inner label
stockiNail 0fa5b9e
removes specific context type for innerlabel
stockiNail 6cbbf43
fixes returned properties when there is not any doughnut controller
stockiNail b215785
removes inner label plugin and all managed by annotaiton one
stockiNail 33be0fe
adds js doc and fix some CC issues
stockiNail 408de06
improves controller retriever
stockiNail 5181eb7
creates helpers.callout
stockiNail 2699fb6
fixes visibility check on elements
stockiNail a6a76cc
test cases
stockiNail 7e214a7
improves filter on array and more tests
stockiNail 6bc3477
types def
stockiNail dcf09b6
add documentation
stockiNail 102b1c9
fix lint
stockiNail 1e1f0f0
add samples
stockiNail 4c67805
fixes animations and options
stockiNail 21b9e13
no extension from label and background not a box but arc
stockiNail 4eade88
aply some reviews
stockiNail fb32684
lint
stockiNail 7b92d98
fixes test cases
stockiNail 2332455
fixes color animation
stockiNail 9817696
fixes font and colors options as indexable
stockiNail 89e2458
adds new test cases for background
stockiNail fe411e9
fixes types
stockiNail 87f2e07
fixes docs and samples
stockiNail 829ff30
CC and color animations
stockiNail 198d964
change doc about animations
stockiNail a82ede0
Merge remote-tracking branch 'origin/master' into innerLabelProto
stockiNail cec5196
Merge remote-tracking branch 'origin/master' into innerLabelProto
stockiNail b468ef6
apply new features coming from other PRs after merging
stockiNail 4bb7ecd
adds test cases for init animation
stockiNail b2cc907
apply review
stockiNail a359625
Merge remote-tracking branch 'origin/master' into innerLabelProto
stockiNail 91ff42d
Merge remote-tracking branch 'origin/master' into innerLabelProto
stockiNail 4d592cd
fix lineHeight on multiple font when size must be updated by fit ratio
stockiNail 1701b84
Merge branch 'master' into innerLabelProto
stockiNail 26a5ca1
add hitTolerance
stockiNail a51cf39
apply review
stockiNail File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stockiNail marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<mxfile host="Electron" modified="2023-01-04T15:27:51.065Z" agent="5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/20.6.2 Chrome/106.0.5249.199 Electron/21.3.3 Safari/537.36" etag="UwpRt7ZZ2I8QNyQM25Nf" version="20.6.2" type="device"><diagram id="EVLCtf8sz0nmjKRDlPg8" name="Page-1">7ZfZjpswFIafhstUYLMkl4VZoqqVKkXVtFeVJzhg1XCocRqSp68NJonjjKaVMotGw03w7+Pt+31s4uGs6m4FacovkFPuIT/vPHzlIRT401j9aGU7KNNgOgiFYLkJOggLtqNjS6OuWU5bK1ACcMkaW1xCXdOltDQiBGzssBVwe9SGFNQRFkvCXfWO5bI0q0DJQZ9TVpTjyEE8G2oqMgablbQlyWFzJOFrD2cCQA5vVZdRruGNXIZ2Nw/U7icmaC3/pUF6t5vHP3dh1szR76+Lifz86dvE9PKH8LVZsJms3I4EBKzrnOpOfA+nm5JJumjIUtdulOdKK2XFVSlQr+6kxhGokLQ7kswkbylUVIqtCjG1yDfAzI5BoSlvDvxxbLTyiP2+ITGeF/u+D1jUiyHzH5TQ45RUL2pL0scJkbYZ9umKdZpqumKcZ8BB9B3h4bkMSpwkHyILJsZ75Qhn5MKMnggldlB2Hsq2Dk+1QmmDE7RlO3LfB+idSNYS2uG86LlyVtTqfalwUcUy1ZiYSuSPpqJiea4bpw2wWvbrilIvujqxoIZaB7VSwC96ItqpsIJamgMLhbqJ8pzVRQpSQqXE8DImBslJPvhuPkRn0iF8qmwIHQsH5t+Vkfok7gs/3h19+ISbnTg6O5eUwew5TY3cvEQ6Md2T7t3H0ccI2z6GZ26q5DlNjB0Th5m8GgvV/XbTP6/KRxzal+T+k+2lfEwcH82wb9lIAZJIBnoSk5l/obsztjMUn3F2ehlnVfHwLd/XHf0jwtd/AQ==</diagram></mxfile> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
# Doughnut Label Annotations | ||
|
||
Doughnut label annotations are used to add contents (text, image, canvas) in the middle area of the doughnut charts. | ||
|
||
```js chart-editor | ||
/* <block:options:0> */ | ||
const options = { | ||
plugins: { | ||
annotation: { | ||
annotations: { | ||
dLabel: { | ||
type: 'doughnutLabel', | ||
content: ({chart}) => ['Total', | ||
chart.getDatasetMeta(0).total, | ||
'last 7 months' | ||
], | ||
font: [{size: 60}, {size: 50}, {size: 30}], | ||
color: ['black', 'red', 'grey'] | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
/* </block:options> */ | ||
|
||
/* <block:config:1> */ | ||
const config = { | ||
type: 'doughnut', | ||
data: { | ||
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'], | ||
datasets: [{ | ||
label: 'Amount', | ||
data: [65, 59, 80, 81, 56, 55, 40] | ||
}] | ||
}, | ||
options | ||
}; | ||
/* </block:config> */ | ||
|
||
module.exports = { | ||
config | ||
}; | ||
``` | ||
|
||
## Configuration | ||
|
||
### Options | ||
|
||
The following options are available for label annotations. | ||
|
||
| Name | Type | [Scriptable](../options.md#scriptable-options) | Default | ||
| ---- | ---- | :----: | ---- | ||
| [`autoHide`](#general) | `boolean` | Yes | `true` | ||
| [`autoFit`](#general) | `boolean` | Yes | `true` | ||
| [`backgroundColor`](#styling) | [`Color`](../options.md#color) | Yes | `'transparent'` | ||
| [`backgroundShadowColor`](#styling) | [`Color`](../options.md#color) | Yes | `'transparent'` | ||
| [`borderColor`](#styling) | [`Color`](../options.md#color) | Yes | `options.color` | ||
| [`borderDash`](#styling) | `number[]` | Yes | `[]` | ||
| [`borderDashOffset`](#styling) | `number` | Yes | `0` | ||
| [`borderShadowColor`](#styling) | [`Color`](../options.md#color) | Yes | `'transparent'` | ||
| [`display`](#general) | `boolean` | Yes | `true` | ||
| [`drawTime`](#general) | `string` | Yes | `'afterDatasetsDraw'` | ||
| [`borderCapStyle`](#styling) | `string` | Yes | `'butt'` | ||
| [`borderJoinStyle`](#styling) | `string` | Yes | `'miter'` | ||
| [`borderRadius`](#borderradius) | `number` \| `object` | Yes | `0` | ||
| [`borderWidth`](#styling) | `number`| Yes | `0` | ||
| [`color`](#styling) | [`Color`\|`Color[]`](../options.md#color) | Yes | `'black'` | ||
| [`content`](#general) | `string`\|`string[]`\|[`Image`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/Image)\|[`HTMLCanvasElement`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement) | Yes | `null` | ||
| [`font`](#styling) | [`Font`\|`Font[]`](../options.md#font) | Yes | `{}` | ||
| [`height`](#general) | `number`\|`string` | Yes | `undefined` | ||
| [`id`](#general) | `string` | No | `undefined` | ||
| [`padding`](#general) | [`Padding`](../options.md#padding) | Yes | `0` | ||
| [`position`](#position) | `string`\|`{x: string, y: string}` | Yes | `'center'` | ||
| [`rotation`](#general) | `number`| Yes | `0` | ||
| [`shadowBlur`](#styling) | `number` | Yes | `0` | ||
| [`shadowOffsetX`](#styling) | `number` | Yes | `0` | ||
| [`shadowOffsetY`](#styling) | `number` | Yes | `0` | ||
| [`textAlign`](#general) | `string` | Yes | `'center'` | ||
| [`textStrokeColor`](#styling) | [`Color`](../options.md#color) | Yes | `undefined` | ||
| [`textStrokeWidth`](#styling) | `number` | Yes | `0` | ||
| [`width`](#general) | `number`\|`string` | Yes | `undefined` | ||
| [`xAdjust`](#general) | `number` | Yes | `0` | ||
| [`yAdjust`](#general) | `number` | Yes | `0` | ||
|
||
### General | ||
|
||
The content will be rendered in the center of the chart. | ||
|
||
| Name | Description | ||
| ---- | ---- | ||
| `autoFit` | If `true`, the label will be automatically fit inside the chart if its dimension is bigger than the available space. | ||
| `autoHide` | If `true`, the label will be automatically hidden if the dataset or its data items will be all hidden. | ||
| `content` | The content to show in the annotation. | ||
| `display` | Whether or not this annotation is visible. | ||
| `drawTime` | See [drawTime](../options.md#draw-time). | ||
| `height` | Overrides the height of the image or canvas element. Could be set in pixel by a number, or in percentage of current height of image or canvas element by a string. If undefined, uses the height of the image or canvas element. It is used only when the content is an image or canvas element. | ||
| `id` | Identifies a unique id for the annotation and it will be stored in the element context. When the annotations are defined by an object, the id is automatically set using the key used to store the annotations in the object. When the annotations are configured by an array, the id, passed by this option in the annotation, will be used. | ||
| `padding` | The padding to add around the label. | ||
| `rotation` | Rotation of the label in degrees. | ||
| `textAlign` | Text alignment of label content when there's more than one line. Possible options are: `'left'`, `'start'`, `'center'`, `'end'`, `'right'`. | ||
| `width` | Overrides the width of the image or canvas element. Could be set in pixel by a number, or in percentage of current width of image or canvas element by a string. If undefined, uses the width of the image or canvas element. It is used only when the content is an image or canvas element. | ||
| `xAdjust` | Adjustment of label relative to computed position. Negative values move the label left, positive right. | ||
| `yAdjust` | Adjustment along y-axis (top-bottom) of label relative to computed position. Negative values move the label up, positive down. | ||
|
||
### Styling | ||
|
||
| Name | Description | ||
| ---- | ---- | ||
| `backgroundColor` | Fill color. | ||
| `backgroundShadowColor` | The color of shadow. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/shadowColor). | ||
| `borderCapStyle` | Cap style of the border line. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineCap). | ||
| `borderColor` | Stroke color. | ||
| `borderDash` | Length and spacing of dashes. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setLineDash). | ||
| `borderDashOffset` | Offset for border line dashes. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineDashOffset). | ||
| `borderJoinStyle` | Border line join style. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineJoin). | ||
| `borderShadowColor` | The color of the border shadow. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/shadowColor). | ||
| `borderWidth` | Stroke width (in pixels). | ||
| `color` | Text color. When the label to draw has multiple lines, you can use different color for each row of the label. This is enabled configuring an array of colors. When the lines are more than the configured colors, the last configuration of this option is used for all remaining lines. | ||
| `font` | Text font. When the label to draw has multiple lines, you can use different font for each row of the label. This is enabled configuring an array of fonts. When the lines are more than the configured fonts, the last configuration of this option is used for all remaining lines. | ||
| `shadowBlur` | The amount of blur applied to shadow of the box where the label is located. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/shadowBlur). | ||
| `shadowOffsetX` | The distance that shadow, of the box where the label is located, will be offset horizontally. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/shadowOffsetX). | ||
| `shadowOffsetY` | The distance that shadow, of the box where the label is located, will be offset vertically. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/shadowOffsetY). | ||
| `textStrokeColor` | The color of the stroke around the text. | ||
| `textStrokeWidth` | Stroke width around the text. | ||
|
||
### Position | ||
|
||
A position can be set in 2 different values types: | ||
|
||
1. `'start'`, `'center'`, `'end'` which are defining where the label will be located | ||
2. a `string`, in percentage format `'number%'`, is representing the percentage on the size where the label will be located | ||
|
||
If this value is a string (possible options are `'start'`, `'center'`, `'end'` or a string in percentage format), it is applied to vertical and horizontal position in the box. | ||
|
||
If this value is an object, the `x` property defines the horizontal alignment in the label, with respect to the center point of the chart. Similarly, the `y` property defines the vertical alignment in the label, with respect to the center point of the chart. Possible options for both properties are `'start'`, `'center'`, `'end'`, a string in percentage format. Omitted property have value of the default, `'center'`. | ||
|
||
#### borderRadius | ||
|
||
If this value is a number, it is applied to all corners of the rectangle (topLeft, topRight, bottomLeft, bottomRight). If this value is an object, the `topLeft` property defines the top-left corners border radius. Similarly, the `topRight`, `bottomLeft`, and `bottomRight` properties can also be specified. Omitted corners have radius of 0. | ||
|
||
## Element | ||
|
||
The following diagram is showing the element properties about a `'doughnutLabel'` annotation: | ||
|
||
 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Basic | ||
|
||
```js chart-editor | ||
// <block:setup:3> | ||
const DATA_COUNT = 12; | ||
const MIN = 0; | ||
const MAX = 100; | ||
|
||
const numberCfg = {count: DATA_COUNT, min: MIN, max: MAX, decimals: 0}; | ||
|
||
const data = { | ||
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], | ||
datasets: [{ | ||
data: Utils.numbers(numberCfg) | ||
}] | ||
}; | ||
// </block:setup> | ||
|
||
// <block:annotation:1> | ||
const annotation = { | ||
type: 'doughnutLabel', | ||
content: ({chart}) => ['Average', | ||
average(chart).toFixed(2), | ||
'on last ' + chart.data.labels.length + ' months' | ||
], | ||
font: [{size: 60}, {size: 50}, {size: 30}], | ||
color: ['black', 'red', 'grey'] | ||
}; | ||
// </block:annotation> | ||
|
||
/* <block:config:0> */ | ||
const config = { | ||
type: 'doughnut', | ||
data, | ||
options: { | ||
aspectRatio: 2, | ||
plugins: { | ||
annotation: { | ||
annotations: { | ||
annotation | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
/* </block:config> */ | ||
|
||
// <block:utils:2> | ||
function average(chart) { | ||
const values = chart.data.datasets[0].data; | ||
return values.reduce((a, b) => a + b, 0) / values.length; | ||
} | ||
// </block:utils> | ||
|
||
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, | ||
}; | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Using canvas as content | ||
|
||
```js chart-editor | ||
// <block:setup:2> | ||
const DATA_COUNT = 12; | ||
const MIN = 0; | ||
const MAX = 100; | ||
|
||
const numberCfg = {count: DATA_COUNT, min: MIN, max: MAX, decimals: 0}; | ||
|
||
const data = { | ||
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], | ||
datasets: [{ | ||
data: Utils.numbers(numberCfg) | ||
}] | ||
}; | ||
// </block:setup> | ||
|
||
// <block:annotation:1> | ||
const annotation = { | ||
type: 'doughnutLabel', | ||
content: Utils.getHouse(), | ||
}; | ||
// </block:annotation> | ||
|
||
/* <block:config:0> */ | ||
const config = { | ||
type: 'doughnut', | ||
data, | ||
options: { | ||
aspectRatio: 2, | ||
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, | ||
}; | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.