-
Notifications
You must be signed in to change notification settings - Fork 0
S06: Add chart with advanced annotations #15
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,62 @@ | ||
| (async () => { | ||
| const data = await fetch( | ||
| 'https://demo-live-data.highcharts.com/aapl-c.json' | ||
| ).then(response => response.json()); | ||
| const data = await fetch( | ||
| 'https://demo-live-data.highcharts.com/aapl-c.json' | ||
| ).then(response => response.json()); | ||
|
|
||
| const chart = Highcharts.stockChart('container', { | ||
| series: [{ | ||
| data: data | ||
| }], | ||
| const chart = Highcharts.stockChart('container', { | ||
| chart: { | ||
| events: { | ||
| load() { | ||
| const chart = this; | ||
| const addBtn = document.getElementById('add-annotation'); | ||
|
|
||
| annotations: [{ | ||
| shapes: [{ | ||
| x: data[10][0], | ||
| y: data[10][1], | ||
| type: 'rect', | ||
| width: 40, | ||
| height: 40, | ||
| fill: 'red' | ||
| }], | ||
| }] | ||
| }); | ||
| addBtn.addEventListener('click', function() { | ||
| const x = chart.xAxis[0].toValue(chart.plotLeft + 10), | ||
| y = chart.yAxis[0].toValue(chart.plotTop + 10); | ||
|
|
||
| chart.addAnnotation({ | ||
| shapes: [{ | ||
| type: 'rect', | ||
| point: { | ||
| xAxis: 0, | ||
| yAxis: 0, | ||
| x, | ||
| y | ||
| }, | ||
| draggable: true, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This assignment is not needed, since the default option is set to 'xy': https://api.highcharts.com/highcharts/annotations.draggable |
||
| width: 360, | ||
| height: 60, | ||
| fill: 'red' | ||
| }], | ||
| labels: [{ | ||
| point: { | ||
| xAxis: 0, | ||
| yAxis: 0, | ||
| x, | ||
| y | ||
| }, | ||
| x: 180, | ||
| y: 30, | ||
| shape: 'rect', | ||
| text: Math.trunc(y), | ||
| align: 'center', | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| verticalAlign: 'middle' | ||
| }], | ||
| events: { | ||
| drag: function() { | ||
| const label = this.labels[0]; | ||
| const rectY = this.shapes[0].points[0].y; | ||
| label.options.text = Math.trunc(rectY); | ||
| label.redraw(false); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an interesting option to update the props of the label, as it directly saves the prop into options and later redraws it. There is another way worth mentioning, by simply updating the label using label.update({
text: y
}); |
||
| } | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
| } | ||
| }, | ||
| series: [{ | ||
| data: data | ||
| }] | ||
| }); | ||
| })(); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpicking here, but you can safely remove the const assignment, since the variable isn't used anywhere later on.