Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
29 changes: 24 additions & 5 deletions highcharts-api/highcharts-stock/05-annotations/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,35 @@

<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">
<label for="x1">x1:</label>
<input type="date" id="x1" class="x-input">

<label for="y1">y1:</label>
<input type="number" id="y1" class="y-input">

<label for="x2">x2:</label>
<input type="date" id="x2" class="x-input">

<label for="y2">y2:</label>
<input type="number" id="y2" class="y-input">

<button type="button" class="add-line">Add line</button>
</div>
</body>

<script src="main.js"></script>
95 changes: 74 additions & 21 deletions highcharts-api/highcharts-stock/05-annotations/main.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,76 @@
(async () => {
const data = await fetch(
'https://demo-live-data.highcharts.com/aapl-c.json'
).then((response) => response.json());

Highcharts.stockChart('container', {
rangeSelector: {
selected: 1
},

title: {
text: ''
},

series: [
{
name: 'AAPL',
data: data,
type: 'spline'
}
]
});
const data = await fetch(
'https://demo-live-data.highcharts.com/aapl-c.json'
).then((response) => response.json());

Highcharts.stockChart('container', {
chart: {
events: {
load() {
const chart = this;
const extremesX = chart.xAxis[0].getExtremes();
const extremesY = chart.yAxis[0].getExtremes();

const controls = document.getElementById('controls'),
inputsX = controls.querySelectorAll('.x-input'),
inputsY = controls.querySelectorAll('.y-input'),
addLineBtn = controls.querySelector('.add-line');

addLineBtn.addEventListener('click', function() {
const x1 = dateStrToTimestamp(inputsX[0].value),
y1 = Number(inputsY[0].value),
x2 = dateStrToTimestamp(inputsX[1].value),
y2 = Number(inputsY[1].value);

chart.addAnnotation({
type: 'crookedLine',
typeOptions: {
points: [
{ x: x1, y: y1 },
{ x: x2, y: y2 }
]
}
});
});

inputsX.forEach(inputX => {
inputX.min = timestampToDateStr(extremesX.min);
inputX.max = timestampToDateStr(extremesX.max);
});

inputsY.forEach(inputY => {
inputY.min = extremesY.min;
inputY.max = extremesY.max;

inputY.addEventListener('change', function() {
const value = this.value;
if (value < this.min) this.value = this.min;
if (value > this.max) this.value = this.max;
});
});
}
}
},
rangeSelector: {
selected: 1
},
title: {
text: ''
},
series: [
{
name: 'AAPL',
data: data,
type: 'spline'
}
]
});
})();

function timestampToDateStr(timestamp) {
return new Date(timestamp).toISOString().split('T')[0];
}

function dateStrToTimestamp(value) {
return new Date(value).getTime();
}