Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
17 changes: 12 additions & 5 deletions highcharts-api/highcharts-stock/06-advanced-annotations/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@

<head>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1">

<script src="https://code.highcharts.com/stock/highstock.js"></script>
<link rel="stylesheet" type="text/css" href="https://code.highcharts.com/css/annotations/popup.css">

<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/modules/annotations-advanced.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"></script>

</head>

<body>
<div id="container"></div>
<div id="container"></div>
<div id="controls">
<button type="button" id="add-annotation">Add annotation</button>
</div>
</body>

<script src="main.js"></script>
76 changes: 58 additions & 18 deletions highcharts-api/highcharts-stock/06-advanced-annotations/main.js
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', {

Copy link
Copy Markdown
Collaborator

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.

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,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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',

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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:

label.update({
    text: y
});

}
}
});
});
}
}
},
series: [{
data: data
}]
});
})();