I'm using highcharts and am I trying to develop a chart, which can handle live data.
Here is my first try:
var data = [],
chart_interval = undefined;
Highcharts.setOptions({
global: {
useUTC: false
}
});
function addRandomData(dataSet, frequency, yMax, count) {
for (var i = 1; i <= count; i++) {
var x = new Date((new Date()).getTime() + i * frequency),
y = Math.random() * yMax;
dataSet.push({
x: x,
y: y
});
}
return dataSet;
}
function addDataInterval(dataSet, frequency, yMax) {
return setInterval(function() {
var x = new Date(dataSet[dataSet.length - 1].x.getTime() + frequency),
y = Math.random() * yMax;
dataSet.push({
x: x,
y: y
});
}, frequency);
}
// Parameters: dataSet, frequency, yMax, count
addRandomData(data, 1000, 10000, 60);
// Parameters: dataSet, frequency, yMax
addDataInterval(data, 500, 10000);
// Create the chart
Highcharts.stockChart('container', {
chart: {
zoomType: 'x',
events: {
load: function() {
// set up the updating of the chart each second
var series = this.series[0];
setInterval(function() {
series.update({
data: data
}, true);
}, 1000);
}
}
},
rangeSelector: {
buttons: [{
count: 1,
type: 'minute',
text: '1M'
}, {
count: 5,
type: 'minute',
text: '5M'
}, {
type: 'all',
text: 'All'
}],
inputEnabled: false,
selected: 0
},
title: {
text: 'Live random data'
},
exporting: {
enabled: false
},
series: [{
name: 'Random data',
data: data
}]
});
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/stock/modules/exporting.js"></script>
<div id="container" style="height: 400px; min-width: 310px"></div>
Problem: If already existing data has different time intervals, such as the newly generated data, the freely selectable zoom don't work.
Have I somewhere make mistake or is this a bug of highcharts?
Highcharts wants time values as timestamps in milliseconds instead of Date objects:
What format does the highcharts js library accept for dates?
Highcharts X-Axis time from JS date
Change how you generate your x values, and the zooming magically starts working again:
var x = (new Date()).getTime() + i * frequency,
...
var x = dataSet[dataSet.length - 1].x + frequency,
https://jsfiddle.net/74zdsvbo/
Related
In the combination of series with and without dynamic data,the series with dynamic data can be easily shifted but where as the other series which are not dynamic do not get shifted out completely.
I have reproduced the issue in the following link: https://jsfiddle.net/8uxepk21/
Here as you can see there are two series. The series with static data appears to be shifting but if you increase the size of the rang-selector,then the series with static data do not get shifted out of the chart completely but still stays unlike the other series.
Is there any option to shift data explicitly without using series.addpoint() method.
I have used series.data[0].remove() and it obviously works fine but when a new data has to arrive for the same series after some time,this remove() method would remove the arriving point aswell. Further if I provide any condition for the maximum points to be in series while shifting,even then it will cause performance issue.
EXPECTED RESULT: Both the series data have to be shifted completely irrespective of the data being static or dynamic.
// Create the chart
Highcharts.stockChart('container', {
chart: {
events: {
load: function () {
// set up the updating of the chart each second
var series1 = this.series[0];
setInterval(function () {
var x = (new Date()).getTime(), // current time
y = Math.round(Math.random() * 100);
series1.addPoint([x, y], true, true);
}, 2000);
var series2 = this.series[1];
//setInterval(function () {
//var x = (new Date()).getTime(), // current time
// y = Math.round(Math.random() * 50);
//series2.addPoint([x, y], true, true);
//}, 2000);
}
}
},
time: {
useUTC: false
},
rangeSelector: {
buttons: [{
count: 1,
type: 'minute',
text: '1M'
}, {
count: 5,
type: 'minute',
text: '5M'
}, {
type: 'all',
text: 'All'
}],
inputEnabled: false,
selected: 0
},
title: {
text: 'Live random data'
},
exporting: {
enabled: false
},
legend: {
enabled: true
},
plotOptions: {
series: {
marker: {
states: {
hover: {
enabled: true,
animation: {duration: 100},
enableMouseTracking: true,
stickyTracking: true
}
}
}
}
},
tooltip:{
shared: true,
split: false,
stickyTracking: true,
enableMouseTracking: true,
enabled: true,
followPointer: true,
followTouchMove: true,
formatter: function(){
var tooltip = "";
var phaseNameList = "";
//tooltip += "<b>I-unit "+ "<br/>"+ "x: "+this.x +"</b>";
tooltip += "<b>I-unit "+ "<br/>"+ "x: "+ new Date(this.x)+
"</b>";
tooltip += "<br/>"+ "y: "+this.y +"</b>";
tooltip += "<br/>"+ this + "</b>";
return tooltip;
}
},
series: [{
name: 'Random data1',
data: (function () {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i;
for (i = -90; i <= 0; i += 1) {
data.push([
time + i * 1000,
Math.round(Math.random() * 100)
]);
}
return data;
}())
},
{
name: 'Random data2',
data: (function () {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i;
for (i = -90; i <= 0; i += 1) {
data.push([
time + i * 1000,
Math.round(Math.random() * 50)
]);
}
return data;
}())
}]
});
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/stock/modules/exporting.js"></script>
<script src="https://code.highcharts.com/stock/modules/export-data.js"></script>
<div id="container" style="height: 400px; min-width: 310px"></div>
To achieve it you can add points with null values to the second series. Check the code and demo posted below.
Code:
chart: {
events: {
load: function() {
var chart = this,
series1 = chart.series[0],
series2 = chart.series[1],
x, y;
setInterval(function() {
x = (new Date()).getTime();
y = Math.round(Math.random() * 100);
series1.addPoint([x, y], false, true);
series2.addPoint([x, null], false, true);
chart.redraw();
}, 2000);
}
}
}
Demo:
https://jsfiddle.net/BlackLabel/6vp5cbt8/
Expected behaviour
I hope to let the chart also dynamic update when I drag scrollbar and it is not in left boundary or right boundary.
Thanks for your help.
Actual behaviour
It is a Stationary state when I selected a range via scrollbar and it is not in left boundary or right boundary
Live demo with steps to reproduce highstock dynamic update demo:
Highcharts.setOptions({
global: {
useUTC: false
}
});
// Create the chart
Highcharts.stockChart('container', {
chart: {
events: {
load: function () {
// set up the updating of the chart each second
var series = this.series[0];
setInterval(function () {
var x = (new Date()).getTime(), // current time
y = Math.round(Math.random() * 100);
series.addPoint([x, y], true, true);
}, 1000);
}
}
},
rangeSelector: {
buttons: [{
count: 1,
type: 'minute',
text: '1M'
}, {
count: 5,
type: 'minute',
text: '5M'
}, {
type: 'all',
text: 'All'
}],
inputEnabled: false,
selected: 0
},
title: {
text: 'Live random data'
},
exporting: {
enabled: false
},
series: [{
name: 'Random data',
data: (function () {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i;
for (i = -999; i <= 0; i += 1) {
data.push([
time + i * 1000,
Math.round(Math.random() * 100)
]);
}
return data;
}())
}]
});
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/stock/modules/exporting.js"></script>
<div id="container" style="height: 400px; min-width: 310px"></div>
jsfiddle
Product version
highstock version : 6.0.7
I am using .net core 2.0 web socket to generate some data. I want to send the data to this chart and draw the chart.
In the code below I want to show the live data without default values. I tried a lot but I wasn't successful. If I remove default value from 'series', chart will disappear. IS that possible to draw this chart on the fly?
Highcharts.setOptions({
global: {
useUTC: false
}});
Highcharts.stockChart('container', {
chart: {
events: {
load: function () {
// set up the updating of the chart each second
var series = this.series[0];
setInterval(function () {
var x = (new Date()).getTime(), // current time
y = Math.round(Math.random() * 100);
series.addPoint([x, y], true, true);
}, 1000);
}
}
},
rangeSelector: {
buttons: [{
count: 1,
type: 'minute',
text: '1M'
}, {
count: 5,
type: 'minute',
text: '5M'
}, {
type: 'all',
text: 'All'
}],
inputEnabled: false,
selected: 0
},
title: {
text: 'Live random data'
},
exporting: {
enabled: false
},
series: [{
name: 'Random data',
data: (function () {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i;
for (i = -999; i <= 0; i += 1) {
data.push([
time + i * 1000,
Math.round(Math.random() * 100)
]);
}
return data;
}())
}]});
Finally I could find the solution. I needed to add xAxis property to my chart. By adding the code below, problem solved. Now it starts from current time.
xAxis: {
type: 'datetime',
tickPixelInterval: null,
maxZoom: 10 * 1000,
min: new Date().getTime()
}
QUESTION:
I have created a line chart using C3.js that is updating its values every 2 seconds.
To achieve this goal i use the following code.
UPDATE:
I have noticed that this is happening when i'm swapping from a tab to another of the bowser.Let's say chart is on TAB1 and i swap to TAB2 for 5 minutes, when i get back to TAB1 i have lost some label as you can see in screenshots below and time on the X-axis is 5 minutes later too...
UPDATE2
The problem is related to setInterval almost for sure.
function drawChart1()
{
var chart = c3.generate({
bindto: '#chart11',
//Size of the chart
size: {
height: 250,
width: 952
},
data: {
x: 'x',
columns: []
},
//Color of the lines
color: {
pattern: [ '#ff9896', '#9edae5']
},
axis: {
x: {
//show: false,
label: 'Time',
type: 'timeseries',
tick: {
format: '%H:%M:%S',
//format: '%H:%M',
}
},
y: {
//http://c3js.org/samples/axes_label_position.html
label: 'Kbps'
},
},
legend: {
//position: 'right'
}
});
var chartObj = {
"chart": chart,
"redrawArgs": {},
"truncThreshold": undefined
}
var date = Date.now();
var timeInc = 2000;
//Update values every x seconds
var interval = setInterval(function () {
var dataCols = [];
date = date + timeInc;
var minX = date - 10000;
var maxX = date;
var redrawArgs = chartObj.redrawArgs;
if (!chartObj.truncThreshold) {
chartObj.truncThreshold = maxX;
} else if (minX > chartObj.truncThreshold) {
redrawArgs.length = 1;
} else {
redrawArgs.length = 0;
}
chartObj.chart.axis.range({max: {x: maxX}, min: {x: minX}});
redrawArgs.duration = 0;
var TraficDown1 = 1 + Math.floor(Math.random() * 1000);
var TraficUp1 = 1 + Math.floor(Math.random() * 1000);
dataCols.push(['x', date]);
dataCols.push(['Kbps UP', TraficUp1]);
dataCols.push(['Kbps DOWN', TraficDown1])
redrawArgs.columns = dataCols;
chartObj.chart.flow(redrawArgs);
}, 2000);
}
For some time the code is working as intended:
Problem is that after some minutes I got this strange behaviour, where values are missing on x axis.
Someone can give me some hints? I'm using c3.js and D3.js v3.
found it on here https://c3js.org/reference.html#axis-x-tick-culling
axis: {
x: {
culling: false, // <-- THIS!
//show: false,
label: 'Time',
type: 'timeseries',
tick: {
format: '%H:%M:%S',
//format: '%H:%M',
}
},
I have a highchart chart that displays the current time against euro/dollar currency pair. I am getting live data from a currency layer per second api.
This is the chart http://jsfiddle.net/15vsdy63/25/
This is the javascript code
$(document).ready(function () {
window.setInterval(function(){
$.get( "http://firmbridgecapital.com/live.php", function( data ) {
localStorage.setItem("data", data);
});
}, 5000);
Highcharts.setOptions({
global: {
useUTC: false
}
});
Highcharts.chart('container', {
chart: {
type: 'spline',
animation: Highcharts.svg, // don't animate in old IE
marginRight: 10,
events: {
load: function () {
// set up the updating of the chart each second
var series = this.series[0];
setInterval(function () {
var x = (new Date()).getTime(), // current time
y = Math.random();
series.addPoint([x, y], true, true);
}, 5000);
}
}
},
title: {
text: 'Live random data'
},
xAxis: {
type: 'datetime',
tickPixelInterval: 150
},
yAxis: {
title: {
text: 'Value'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function () {
return '<b>' + this.series.name + '</b><br/>' +
Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
Highcharts.numberFormat(this.y, 2);
}
},
legend: {
enabled: false
},
exporting: {
enabled: false
},
series: [{
name: 'Random data',
data: (function () {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i;
for (i = -150; i <= 0; i += 25) {
data.push({
x: time,
y: parseInt(localStorage.getItem("data"))
});
}
return data;
}())
}]
});
});
I have theorized that in a worst case scenario, Euro/Dollar will never go past 4 usd for 1 euro, so in the Y-axis, i am thinking of having value 0 to 4 and x- axis will have the current time.
In my example above, i cant display 0 to 4 in the y axis. Also i would love the area under the curve to have some color like blue.
How can i make the y -axis show 0 to 4 and
How can i have area under the curve to have a color like blue?
Thanks.
For the y-axis to show values between 0 & 4 use max: your value and for the curve to have a color like blue just change the chart type to area chart:{type:area, .... Here is your example: https://jsfiddle.net/68oe1oLf/