Further, we set the series section. For the datetime axes, you can set your series section by using two different ways. You can use the first way when your data follow a regular time interval and the second way when your data don't necessarily follow a regular time interval. We will use both the ways by setting the two series with two different options. The first series follows a regular interval. For this series, we set the pointInterval parameter where we define the data interval in milliseconds. For our chart, we set an interval of one day. We set the pointStart parameter with the date of the first value. We then set the data section with our values. The tooltip section is set with the valueSuffix element, where we define the suffix to be added after the value inside our tool tip. We set our yAxis element with the axis we want to associate with our series. Because we want to set this series to the first axis, we set the value to 0(zero). For the second series, we will use the second way because our data is not necessarily following the regular intervals. But you can also use this way, even if your data follows a regular interval. We set our data by couple, where the first element represents the date and the second element represents the value. We also override the tooltip section of the second series. We then set the yAxis element with the value 1 because we want to associate this series to the second axis. For your chart, you can also set your date values with a timestamp value instead of using the JavaScript function Date.UTC.
series: [
{
name: 'Temperature',
pointInterval: 24 * 3600 * 1000,
pointStart: Date.UTC(2013, 0, 01),
data: [17.5, 16.2, 16.1, 16.1, 15.9, 15.8, 16.2],
tooltip: {
valueSuffix: ' °C'
},
yAxis: 0
},
{
name: 'Electricity consumption',
data: [
[Date.UTC(2013, 0, 01), 8.1],
[Date.UTC(2013, 0, 02), 6.2],
[Date.UTC(2013, 0, 03), 7.3],
[Date.UTC(2013, 0, 05), 7.1],
[Date.UTC(2013, 0, 06), 12.3],
[Date.UTC(2013, 0, 07), 10.2]
],
tooltip: {
valueSuffix: ' KWh'
},
yAxis: 1
}
]
You should have this as the final code:
$(function () {
var chart = $(‘#myFirstChartContainer’).highcharts({
chart: {
type: ‘line’,
zoomType: ‘x’
},
title: {
text: ‘Energy consumption linked to the temperature’
},
xAxis: {
type: ‘datetime’,
title: {
text: null
}
},
yAxis: [
{
title: {
text: ‘Temperature’
},
min:0
},
{
title: {
text: ‘Electricity consumed’
},
opposite:true,
min:0
}
],
tooltip: {
crosshairs: true,
shared: true
},
series: [
{
name: ‘Temperature’,
pointInterval: 24 * 3600 * 1000,
pointStart: Date.UTC(2013, 0, 01),
data: [17.5, 16.2, 16.1, 16.1, 15.9, 15.8, 16.2],
tooltip: {
valueSuffix: ‘ °C’
},
yAxis: 0
},
{
name: ‘Electricity consumption’,
data: [
[Date.UTC(2013, 0, 01), 8.1],
[Date.UTC(2013, 0, 02), 6.2],
[Date.UTC(2013, 0, 03), 7.3],
[Date.UTC(2013, 0, 05), 7.1],
[Date.UTC(2013, 0, 06), 12.3],
[Date.UTC(2013, 0, 07), 10.2]
],
tooltip: {
valueSuffix: ‘ KWh’
},
yAxis: 1
}
]
});
});