series.addPoint (Object options, [Boolean redraw], [Boolean shift], [Mixed animation])
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, false, true);
}, 1000);
}
}
},
xAxis: {
maxPadding: 1
}
I want to dynamically update data in a fix x-axis range such as 9:00 to 18:00,but when redrawing happened,the max value in x-axis will increase,how can i keep the value not changed?Just like a stock chart dynamically show the stock price.
(http://finance.yahoo.com/echarts?s=FB+Interactive#symbol=fb;range=1d;compare=;indicator=volume;charttype=area;crosshair=on;ohlcvalues=0;logscale=off;source=undefined;)
My code: http://jsfiddle.net/cruelcage/ny43Z/
can someone help me?
You can tell highcharts what the min and max values to plot on the y-axis:
yAxis: {
min:0,
max:1,
See the updated example http://jsfiddle.net/2ghdH/.
Yoo can do the same on the x-axis as well:
var end = (new Date()).getTime()+100000;
xAxis: {
type: 'datetime',
tickPixelInterval: 150,
maxPadding :1.5,
max:end
},
http://jsfiddle.net/nV8cu/
Related
In my chart I have these time stamp that are displayed on the x-axis
How do I make them display in the tooltip when I hover over to certain point in the chart?
Highcharts.chart('container', {
...
xAxis: {
labels: {
formatter: function() {
let seconds = this.value * 5;
let t = new Date(1900, 1, 1, 9, 30, 0);
t.setSeconds(t.getSeconds() + this.value * 5);
return `${t.getHours()}:${t.getMinutes()}:${t.getSeconds()}`
}
},
tickInterval: 2
},
});
Can someone please help as I am not able to figure this out?
You can use the same approach in pointFormatter as in the labels formatter function.
tooltip: {
pointFormatter: function(){
let t = new Date(1900, 1, 1, 9, 30, 0);
t.setSeconds(t.getSeconds() + this.x * 5);
return `
Time: ${t.getHours()}:${t.getMinutes()}:${t.getSeconds()}
Y: ${this.y}
Value: ${this.value}
`
}
}
Live demo: https://jsfiddle.net/BlackLabel/jf9x8y3q/
API Reference: https://api.highcharts.com/highcharts/tooltip.pointFormatter
The Highcharts API has a pointFormat property for tooltips, where you can specify the HTML (e.g. <p>Tooltip data: {variable}</p>). You can find the API reference here: https://api.highcharts.com/highcharts/tooltip.pointFormat
Or you could use pointFormatter to specify a callback function instead (https://api.highcharts.com/highcharts/tooltip.pointFormatter).
I want to limit my y-axis to the range from -43 to 43. I also want my chart to not display space/area/anything under or over those values. Here is what my axis options look like
yAxis: {
tickInterval: 1,
minorTickInterval: null,
endOnTick: false,
min: -41,
max: 43,
title: {
text: 'Y Coordinate'
},
gridLineColor: 'transparent'
}
However, in the following image you can clearly see that highcharts is displaying space for values below my limit and down to -45. I want these hard limits so my background image lines up perfectly with the coordinate grid system.
I read that setting my tickInverval to 1 and endOnTick to false would produce the desired result, however it seems that is not so.
Any ideas?
Tick positions determine start and end axis values. You can use tickPositions in order to pass your positions or create a function which returns tick positions based on your min and max value and pass it as positioner.
const random = () => Math.round((Math.random() * 200) - 100)
const options = {
yAxis: {
min: -99,
max: 99,
// tickPositions: [-99, 99],
tickPositioner () {
const axis = this
return axis.tickPositions.map((pos) => {
if (pos <= axis.max && pos >= axis.min) return pos // If between range
else if (pos > axis.max) return axis.max
else if (pos < axis.min) return axis.min
})
}
},
series: [{
data: [...Array(10)].map(random),
type: 'column'
}]
}
const chart = Highcharts.chart('container', options)
Live example:
https://jsfiddle.net/918zb76r/
I have a area chart which is having a dynamic point that will be added to chart.I got this http://jsfiddle.net/rjpjwve0/
but it looks like the point gets displayed first and then after a delay the chart draws back. Now i want to display the last point which will be a animated point and it should travel with chart without delay in rendering.
Could any one help me to achieve this.
I put together a test, and it seems to work well.
I updated the load event to add a second series, using the same series.data[len -1] values; then in the setInterval portion, we update that new point at each iteration.
That way, by updating the existing marker rather than destroying one marker and creating another, the animation works as desired.
Code:
events: {
load: function () {
var series = this.series[0],
len = series.data.length;
//-------------------------------------
//added this part ->
this.addSeries({
id: 'end point',
type: 'scatter',
marker: {
enabled:true,
symbol:'circle',
radius:5,
fillColor:'white',
lineColor: 'black',
lineWidth:2
},
data: [[
series.data[len - 1].x,
series.data[len - 1].y
]]
});
var series2 = this.get('end point');
//-------------------------------------
setInterval(function () {
var x = (new Date()).getTime(),
y = Math.random();
len = series.data.length;
series.addPoint([x,y], true, true);
//and added this line -->
series2.data[0].update([x,y]);
}, 1000);
}
}
Fiddle:
http://jsfiddle.net/jlbriggs/a6pshutt/
You can try this :
series: [{
name: 'Random data',
marker : {
enabled : false,
lineWidth: 0,
radius: 0
},
data: (function () {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i;
for (i = -19; i <= 0; i += 1) {
data.push({
x: time + i * 1000,
y: Math.random()
});
}
return data;
}())
}]
Its works.
Greg.
I'm wondering if there is a way to apply dynamic settings to individual marker of an highstock chart? I've searched for half a day and I have the feeling that there is a problem with the API. It seems that there is no ways to adjust marker setting on a specific datum. ex:
$('#container').highcharts('StockChart', {
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);
}
}
},
series : [{
data : (function () {
var data = [], time = (new Date()).getTime(), i;
for (i = -999; i <= 0; i += 1) {
data.push([
{ x: time + i * 1000,
y: Math.round(Math.random() * 100),
marker:{
fillColor:'red'
}
}
]);
}
return data;
}())
}]
}
I've fork a basic Highstock demo to illustrate my point. See the jsfiddle that demonstrate the problem: http://jsfiddle.net/9xj0nz72/1/
Maybe I have an error in my fiddle... or may I have to create an issue on Github?
Thanks a lot!!
I had to assign the style in the addPoint method, you can't just push to the data array. And you have to use it on the chart = new Highcharts.StockChart() variable.
I'm pretty sure I got what you were hoping for using the following. And to demonstrate I assigned a random color and radius to each new point.
$(function () {
var chart = new Highcharts.StockChart({
chart: {
renderTo: 'container'
},
plotOptions: {
series: {
marker: {
enabled: true
}
}
},
series: [{
name: 'Random data',
data: [],
time: (new Date()).getTime()
}]
});
/* add new random point every 1 second */
var i = 0;
setInterval(function () {
i++;
chart.series[0].addPoint({
marker: {
/* assign a random hex color and radius */
fillColor: '#' + (Math.random() * 0xFFFFFF << 0).toString(16),
radius: Math.floor(Math.random() * 10) + 1
},
y: Math.random() * 100,
x: i * 1000,
}, true, false);
}, 1000);
});
Your updated JSFiddle
The problem I am encountering is that the black line is doing some funky stuff compared to the blue line. If you scroll in the middle or somewhere else (use bottom scroll tool), you can clearly see that the black line is changing its shape, while the blue line holds its shape, strange. This only happens when you use the scroll tool.
How can I prevent the black line changing its shape? Copy this code and replace it with the JSfiddle to see the problem:
$(function () {
Highcharts.setOptions({
global : {
useUTC : false
}
});
// Create the chart
$('#container').highcharts('StockChart', {
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);
var series1 = this.series[1];
setInterval(function () {
var x = (new Date()).getTime(), // current time
y = Math.round(Math.random() * 100);
series1.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 : 'diagram1',
data : (function () {
// generate an array of random data
var data1 = [], time = (new Date()).getTime(), i;
for (i = -999; i <= 0; i += 1) {
data1.push([
time + i * 1000,
Math.round(Math.random() * 100)
]);
}
return data1;
}())
},
{
name : 'diagram2',
data : (function () {
// generate an array of random data
var data2 = [], time = (new Date()).getTime(), i;
for (i = -999; i <= 0; i += 1) {
data2.push([
time + i * 1000,
Math.round(Math.random() * 100)
]);
}
return data2;
}())
}]
});
});
The only thing I have done is to add an extra dynamic line (black one) to the diagram. Here is the original code without the black line. Review original code
The reason of the movement in the black line is the redraw and the animation function of the chart. For some reason the first series(blue line) doesn't show animation after calling addPoint so you don't see the movement. The second parameter in the addPoint function is redraw. Setting it to false for the second series(black line) will stop the movement when updating the points:
series1.addPoint([x, y], false, true);
Here's the DEMO.