use Highcharts Api to show multiples values - highcharts

Hi everyone I am new using highcharts, I have my data structure and when I try to show, I don't see anything
function nueva (current_data){
var seriesOptions = [],
seriesCounter = 0,
type = ['jobs_running', 'jobs_pending'];
function createChart() {
$('#containerChart').highcharts('StockChart', {
rangeSelector: {
selected: 4
},
tooltip: {
pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.change}%)<br/>',
valueDecimals: 2
},
series: seriesOptions
});
}
for (var j = 0; j < current_data['names'].length; j++){
var all_element = []
name_project = current_data['names'][j];
for (var i = 0; i < type.length; i++){
seriesCounter = 0;
for (var i = 0; i < type.length; i++){
seriesOptions[j] = {
name: type[i],
data: current_data[name_project][type[i]],
};
}
}
createChart();
}
}
I pass current_data to my function that is like this
I want to show 'jobs_running' and 'jobs_pendding' I set the value to seriesOptions
and my array of data has this
Any idea why I don't see anything in the chart! I am missing something.
Thanks in advance

Hopefully you can find your answer in this:
https://jsfiddle.net/ekekp8rh/1/
Not sure what you were hoping to get but at least this displays a chart.
var data = {
projects: [{
name: 'Project X',
jobs_running: [
[1459814400000, 121],
[1459900800000, 205],
[1459987200000, 155],
[1460073600000, 458]
],
jobs_pending: [
[1459814400000, 146],
[1459900800000, 149],
[1459987200000, 158],
[1460073600000, 184]
]
}, {
name: 'Project Y',
jobs_running: [
[1459814400000, 221],
[1459900800000, 295],
[1459987200000, 255],
[1460073600000, 258]
],
jobs_pending: [
[1459814400000, 246],
[1459900800000, 249],
[1459987200000, 258],
[1460073600000, 284]
]
}]
};
nueva(data);
function nueva(current_data) {
var seriesOptions = [],
type = ['jobs_running', 'jobs_pending'];
for (var j = 0; j < current_data['projects'].length; j++) {
var project = current_data['projects'][j];
for (var i = 0; i < type.length; i++) {
seriesOptions.push({
name: project.name + ' ' + type[i],
data: project[type[i]]
});
}
}
$('#containerChart').highcharts('StockChart', {
series: seriesOptions
});
}

Related

HighChart is not working with multiple axis

I want to construct multiple y-axis spline Highchart, but I am getting Null in series.
When I use data manually so it works fine, but When I fetch data from URL it does not work. Also, I am going to prepare a function for creating multiple y-axes.
Can anyone please help me where I am wrong?
Here is my code
<script type="text/javascript">
var axisdata = [{url:"",text:"", yAxis: 1, scrollbar: true,type: "spline"},
{url:"", text:"", yAxix: 2, scrollbar:true, type: "spline"}];
var HighChartObj = {
title: {
text: 'Exchange Rates'
},
scrollbar: {
enabled: true,
showFull: false
},
yAxis: [],
series: [],
rangeSelector: {
selected: 1,
buttonTheme: {
visibility: 'hidden'
},
labelStyle: {
visibility: 'hidden'
},
inputDateFormat: '%H:%M:%S.%L'
},
};
function a(){
var defer = $.Deferred();
z = -1;
var axisLength = axisdata.length - 1;
jQuery.each(axisdata, function(key, value){
$.getJSON( value.url, function (data) {
var dataLength = data.length,
i = 0;
minValue = data[0].open;
maxValue = data[0].open;
var finalData = [];
var new_from_date = 0;
var new_to_date = 0;
var minValue;
var maxValue;
for (i; i < dataLength; i += 1) {
if(i == (dataLength + 99 - dataLength)){
new_to_date = (data[i].start*1000);
}
finalData.push([
(data[i].start*1000),
data[i].open,
]);
if (data[i].open < minValue) {
minValue = data[i].open;
}
else{
if(data[i].open > maxValue){
maxValue = data[i].open;
}
}
}
HighChartObj.xAxis = {
type: 'datetime',
min: new_from_date,
max: new_to_date,
range: new_to_date - new_from_date,
ordinal: false,
endOnTick: false,
startOnTick: false
};
z++;
HighChartObj.yAxis[z] = {
title: {
text: value.text
},
opposite: true,
min: minValue,
max: maxValue,
height: "100%",
scrollbar: {
enabled: value.scrollbar
}
};
var series = {
"type": value.type,
"name": value.text,
"yAxis": value.yAxis,
"id": value.text.toLowerCase().replace(' ', ''),
"zIndex": 1,
"data": finalData
};
if(HighChartObj.series == null){
HighChartObj.series = new Array();
}
HighChartObj.series[z] = series;
if(key == axisLength){
defer.resolve();
}
});
});
return defer.promise();
}
function b(){
// console.log(HighChartObj.series = HighChartObj.test);
console.log(HighChartObj);
Highcharts.stockChart('container', HighChartObj);
}
$.when(a()).then(b());
</script>
Try fetching data like this. Please data in different variables and then pass it to HighChart Object.
function a(){
z = -1;
var axisLength = axisdata.length - 1;
var dataSeries = new Array()
var dataYaxis = new Array();
jQuery.each(axisdata, function(key, value){
var axisValue = value;
$.ajax({
dataType: "json",
url: axisValue.url,
success: function (data) {
var dataLength = data.length,
i = 0;
minValue = data[0].open;
maxValue = data[0].open;
var finalData = [];
var new_from_date = 0;
var new_to_date = 0;
var minValue;
var maxValue;
for (i; i < dataLength; i += 1) {
if(i == (dataLength + 99 - dataLength)){
new_to_date = (data[i].start*1000);
}
var date = (data[i].start*1000);
finalData.push([
date,
data[i].open,
]);
if (data[i].open < minValue) {
minValue = data[i].open;
}
else{
if(data[i].open > maxValue){
maxValue = data[i].open;
}
}
}
z++;
HighChartObj.xAxis = {
type: 'datetime',
min: new_from_date,
max: new_to_date,
range: new_to_date - new_from_date,
ordinal: false,
endOnTick: false,
startOnTick: false
};
var yAxis = {
title: {
text: axisValue.text
},
opposite: false,
min: minValue,
max: maxValue,
height: "100%",
scrollbar: {
enabled: axisValue.scrollbar
}
};
dataYaxis.push(yAxis);
var series = {
type: axisValue.type,
name: axisValue.text,
id: axisValue.text.toLowerCase().replace(' ', ''),
zIndex: 2,
data: finalData
};
dataSeries.push(series);
if(key == axisLength){
HighChartObj.yAxis = dataYaxis;
HighChartObj.series = dataSeries;
b();
}
}
});
});
}

Highcharts more than one series

I have a simple json document :
[ {
"x" : "a",
"y" : 2
}, {
"x" : "b",
"y" : 8
}, {
"x" : "c",
"y" : 4
}, {
"x" : "d",
"y" : 15
} ]
I want to visualize it using Highcharts having 4 series. I could success, however, the data appeared only as one series (see the next Figure).
Here is part of the code:
var options = {
.
.
.
series: [{ }]
};
.
.
.
var data = JSON.parse(json);
var seriesData = [];
for (var i = 0; i < data.length; i++) {
seriesData.push([data[i].x, data[i].y]);
options.xAxis.categories.push( data[i].x );
}
options.series[0].data = seriesData;
var chart = new Highcharts.Chart(options);
also updating the series
chart.series[0].update({
type: type,
});
works fine.
using
options.series.push({name: data[i].x, data: [data[i].x, data[i].y]});
creates 4 series but not appropriately visualized and also updating the series
chart.series[0].update({
type: type,
});
doesn't work, therefore, I want to focus in the first mentioned method.
any hints?
EDit: code which partially works for me:
var options = {
chart: {
renderTo: 'container',
type: 'column' //default
},
title: {
text: ''
},
yAxis: {
title: {
enabled: true,
text: 'Count',
style: {
fontWeight: 'normal'
}
}
},
xAxis: {
title: {
enabled: true,
text: '',
style: {
fontWeight: 'normal'
}
},
categories: [],
crosshair: true
} ,
plotOptions: {
pie: {
innerSize: 125,
depth: 80
},
column: {
pointPadding: 0.2,
borderWidth: 0,
grouping: false
}
},
series: [{ }]
};
// Set type
$.each(['column', 'pie'], function (i, type) {
$('#' + type).click(function () {
chart.series[0].update({
type: type
});
});
var data = get the data fom json file**
var seriesData = [];
for (var i = 0; i < data.length; i++) {
seriesData.push([data[i].x, data[i].y]);
options.xAxis.categories.push( data[i].x );
}
options.series[0].data = seriesData;
var chart = new Highcharts.Chart(options);
});
});
You have to decide whether you want to have 4 different series and update all 4 series at once or you want to have one series an then build e.g. legend on your own.
If you want to have 4 series, set grouping to false, set xAxis categories and each point should be mapped to one series with one point - the point.x should have the index of the series.
const series = data.map((point, x) => ({ name: point.x, data: [{ x, y: point.y }]}))
const chart = Highcharts.chart('container', {
chart: {
type: 'column'
},
plotOptions: {
column: {
grouping: false
}
},
xAxis: {
categories: data.map(point => point.x)
},
series: series
});
Then you can update your all 4 series:
chart.series.forEach(series => series.update({
type: series.type === 'column' ? 'scatter' : 'column'
}, false))
chart.redraw()
example: http://jsfiddle.net/pdjqrj5y/

Memory leak problems for column chart

I am not good at speaking English. Please understand.
I'm having a memory leak problem when using the stack column chart.
We update the data using setCategories, setData on a stack column chart every 2 seconds.
But, the memory usage continues to increase and the browser will be forcibly terminated.
The only thing that program does is call setCategories, setData. Is there anything I need to do to clean up the memory?
The test environment is os: windows7, windows10, browser: lastest version chrome 57.0.2987.133
you can see that you keep increasing after 1, 3, 5, 7, 10, 15, and 20 minutes.
Memory usage increases with time, so you can check it immediately.
You can check memory usage in the task window by running [shift + esc] shortcut in Chrome.
Send the sample code as an attachment. Thank you.
JQuery and Highcharts(5.0.10) use the latest version.
<script>
var dy = {
instanceAlias : {}
};
var CATEGORIES = ["a1","a2","a3","a4","a5","a6","a7","a8","a9","a10","a11","a12","a13","a14","a15","a16","a17","a18","a19","a20"];
var THEME = "";
var HEIGHT = "290";
var highchartConfig = {
column : {
chart: {
renderTo: null,
type: 'column'
},
xAxis: {
gridLineWidth: 1,
tickLength: 4,
categories : null,
labels : {
rotation: 0,
formatter : function(d){
return dy.instanceAlias[this.value] || this.value;
}
}
},
yAxis: {
min : 0,
max : 5,
lineWidth: 1,
tickLength: 4, // Same value as offset
tickPosition: "outside",
tickWidth: 1,
tickAmount: 3,
tickInterval : 25,
title: {
style : {
display: 'none'
}
},
stackLabels: {
style : {
"fontWeight": "normal",
color : (THEME == "dark") ? "#b0b0b3" : "#000"
}
}
},
plotOptions: {
series: {
borderRadius: 6,
borderWidth: 0,
marker :{
enabled :true,
symbol : "circle",
radius : 4
}
}
},
series: null
}
};
var stack1Config = $.extend(true,{},highchartConfig.column,{
colors : ["#009651","#ff9800","#ff1100","#009651"], //stack colors
chart: {
animation: false,
renderTo: "stack1",
height : HEIGHT
},
xAxis: {
categories : CATEGORIES
},
title: {
style: {
display: 'none'
}
},
subtitle: {
style: {
display: 'none'
}
},
legend : false,
tooltip : false,
yAxis: {
max : null,
tickInterval : null,
tickLength : null,
stackLabels: {
enabled: true,
useHTML : false,
style : {
"fontWeight": "normal",
color : (THEME == "dark") ? "#b0b0b3" : "#000"
}
}
},
plotOptions: {
column: {
stacking: 'normal',
},
series: {
animation: false,
stickyTracking: false,
enableMouseTracking: false,
lineWidth: 0,
marker: {
enabled : true,
symbol : "circle",
radius : 4
},
}
},
series : (function(){
var series = new Array();
var stack = [
{id : "normal", alias : "normal"},
{id : "warning", alias : "warning"},
{id : "danger", alias : "danger"}
];
for (var i = 0; i < stack.length; i++) {
//stack column 브러시 추가.
series.push({
name : stack[i].alias || stack[i].id,
id : stack[i].id,
lineWidth: 1,
data : (function(categories){
var data = [];
for (var i = 0; i < categories.length; i++) {
data.push(0);
}
return data;
})(CATEGORIES)
});
}
//circle
series.push({
type : "scatter",
name : "scatter",
id : "scatter",
marker: {
fillColor: '#FFFFFF',
lineWidth: 1,
lineColor: null
},
data : (function(b){
var data = [];
for (var i = 0; i < b.length; i++) {
data.push(0);
}
return data;
})(CATEGORIES)
});
console.log(series)
return series;
})()
});
var stack2Config = $.extend(true,{},highchartConfig.column,{
colors :["#009651"],
chart: {
animation: false,
renderTo: "stack2",
height : HEIGHT
},
xAxis: {
categories : CATEGORIES
},
title: {
style: {
display: 'none'
}
},
subtitle: {
style: {
display: 'none'
}
},
legend : false,
tooltip : false,
yAxis: {
max : null,
tickInterval : null,
tickLength : null,
stackLabels: {
enabled: true,
useHTML : false,
style : {
"fontWeight": "normal",
color : (THEME == "dark") ? "#b0b0b3" : "#000"
}
}
},
plotOptions: {
animation: false,
stickyTracking: false,
enableMouseTracking: false,
lineWidth: 0,
marker : {
enabled : false
},
column : {
dataLabels: {
enabled: true,
useHTML: true,
x : 0,
y : 0,
style : {
"fontWeight": "normal"
}
}
}
},
series : (function(){
var series = new Array();
var stack = [
{id : "cpu", alias : "CPU"}
];
for (var i = 0; i < stack.length; i++) {
//stack column 브러시 추가.
series.push({
name : stack[i].alias || stack[i].id,
id : stack[i].id,
lineWidth: 1,
data : (function(categories){
var data = [];
for (var i = 0; i < categories.length; i++) {
data.push(0);
}
return data;
})(CATEGORIES)
});
}
//circle
series.push({
type : "scatter",
name : "scatter",
id : "scatter",
marker: {
fillColor: '#FFFFFF',
lineWidth: 1,
lineColor: null
},
data : (function(b){
var data = [];
for (var i = 0; i < b.length; i++) {
data.push(0);
}
return data;
})(CATEGORIES)
});
console.log(series)
return series;
})()
});
var stack1 = Highcharts.chart(stack1Config);
var stack2 = Highcharts.chart(stack2Config);
var alarmTimeoutId = null;
var alarmTimeout = function(){
return setTimeout(function(){
var ACT_COLUMN = {
normal : [],
warning : [],
danger : [],
scatter : [] // success,warning,danger total value
};
var length = 20;
for (var i = 0; i < length; i++) {
var normalCnt = Math.round(Math.random()*100);
var warningCnt = Math.round(Math.random()*100);
var dangerCnt = Math.round(Math.random()*100);
ACT_COLUMN.scatter.push({ y: normalCnt+warningCnt+dangerCnt });
ACT_COLUMN.normal.push(normalCnt);
ACT_COLUMN.warning.push(warningCnt);
ACT_COLUMN.danger.push(dangerCnt);
}
for (var key in ACT_COLUMN) {
stack1.get(key).setData(ACT_COLUMN[key],false,false);
}
stack1.xAxis[0].setCategories(CATEGORIES);
ACT_COLUMN.scatter = null;
ACT_COLUMN.normal = null;
ACT_COLUMN.warning = null;
ACT_COLUMN.danger = null;
ACT_COLUMN = null;
var cpuData = [];
for (var i = 0; i < length; i++) {
var cpu = Math.round(Math.random()*100);
var color = "#009651";
if(cpu <= 50){
color = "#009651";
}else if(cpu <= 80){
color = "#ff9800";
}else{
color = "#ff1100";
}
cpuData.push({y : cpu, color : color });
}
var series = stack2.series;
var seriesLength = series.length;
for (var i = 0; i < seriesLength; i++) {
series[i].setData(cpuData,false,false);
}
stack2.xAxis[0].setCategories(CATEGORIES);
clearTimeout(alarmTimeoutId);
alarmTimeoutId = alarmTimeout();
}, 2000);
}
alarmTimeout();
</script>

highcharts gauge multineedles datalabels are overlapping

I have Highcharts Gauge chart with multiple needles and the datalabels of the needles are overlapped each other like http://jsfiddle.net/edLHB/3/
plotOptions I have used as following
plotOptions: {
guage: {
dataLabels: {
enabled: true,
crop: false,
overflow: 'none'
}
}
},
Please help me to get datalabels without overlapping.
Thanks
Give them individual x or y values in the series
series: [{
name: 'Speed',
data: [80],
dataLabels: {
y:50
}
}, {
name: 'Speed',
data: [100],
dataLabels: {
y:30
}
}, {
name: 'Speed',
data: [150],
dataLabels: {
y:10
}
}]
Demo: http://jsfiddle.net/robschmuecker/edLHB/7/
In general this solution is not supported (I mean collision detection), but you adapt this solution http://jsfiddle.net/menXU/6/.
function StaggerDataLabels(series) {
sc = series.length;
if (sc < 2) return;
for (s = 1; s < sc; s++) {
var s1 = series[s - 1].points,
s2 = series[s].points,
l = s1.length,
diff, h;
for (i = 0; i < l; i++) {
if (s1[i].dataLabel && s2[i].dataLabel) {
diff = s1[i].dataLabel.y - s2[i].dataLabel.y;
h = s1[i].dataLabel.height + 2;
if (isLabelOnLabel(s1[i].dataLabel, s2[i].dataLabel)) {
if (diff < 0) s1[i].dataLabel.translate(s1[i].dataLabel.translateX, s1[i].dataLabel.translateY - (h + diff));
else s2[i].dataLabel.translate(s2[i].dataLabel.translateX, s2[i].dataLabel.translateY - (h - diff));
}
}
}
}
}

How to retrieve a variable in jquery ajax .done()

Stackoverflow(ers),
I have created a javascript function in which to process data from an array of objects, and in this function I am iterating through the array calling an ajax request for each object in the array.
However, inside the ajax.done() function, I need to pass in the index of the iteration, j. Inside the iteration, however, j is stuck on 4, whereas outside the iteration, j counts successfully with the iteration. Note that the i iteration in the code below loops through inside each ajax request to pull out certain values to form an array, so can be ignored.
Can anyone help me in working out what I need to do to make j iterate inside the .done() block?
Thanks,
Jamie
Object Passed To Code:
var dataConfig = [
{ targetDiv: "#chart", chartTitle: "Title", tooltipVisible: true, xAxisLabel: "Label", leftYAxisLabel: "Unit" },
{ apiUrl: "URL", type: "column", yAxis: "right", visibleInLegend: false },
{ apiUrl: "URL", type: "line", yAxis: "left", visibleInLegend: false },
{ apiUrl: "URL", type: "line", yAxis: "left", visibleInLegend: false },
];
The Code:
for ( var j = 2; j < dataConfig.length; j++ ) {
console.log(j);
chartConfig[j] = {
yAxisValues: [],
type: dataConfig[j].type,
yAxis: dataConfig[j].yAxis,
visibleInLegend: dataConfig[j].visibleInLegend
}
$.ajax({
url: baseURL + dataConfig[j].apiUrl,
beforeSend: function ( xhr ) {
xhr.setRequestHeader('Authorization', 'yes');
}
}).done(function (data) {
//get Y Axis Values
var yAxisData = data.DataSeries.Data;
yAxisValues = [];
for ( var i = 0; i < yAxisData.length; i++ ) {
var yAxisValue = yAxisData[i].Y[0];
yAxisValues.push(parseInt(yAxisValue, 10));
};
console.log(yAxisValues);
console.log("j:", j);
// chartConfig[j].yAxisValues = yAxisValues;
});
};
AJAX is asynchronous. The .done() method will run at a point that is most likely out of sync with the parent for loop, thus you can't rely on the variable 'j'.
You can use the following syntax to achieve what you want to by capturing the value of j in a self executing function:
for ( var j = 2; j < dataConfig.length; j++ ) {
(function(index) {
$.ajax({
url: baseURL + dataConfig[j].apiUrl,
beforeSend: ...
}).done(function(data) {
console.log(index);
});
})(j);
}

Resources