I'm using websockets to stream valid JSON to highcharts.js. My goal is to chart a few lines simultaneously on the same graph. The JSON, which I control, contains data 4-16 series (called parsers) that I'd like overlay with highcharts. Example of JSON:
[
{
"y": 91,
"x": 1403640998,
"name": "parser1"
},
{
"y": 184,
"x": 1403640998,
"name": "parser2"
},
{
"y": 26,
"x": 1403640998,
"name": "parser3"
}
]
I can get a single line to graph, but they get combined into a single series. I'd like to dynamically adjust the series based on the number of parsers I'm monitoring. If my JSON contains information for 3 parsers, like I posted above, I'd like to see 3 lines automatically update every second.
As you can see, I can only get 1 to show.
My HTML
<script type="text/javascript">
$(function () {
$('#container').highcharts({
chart: {
type: 'spline',
events: {
load: function () {
var $message = $('#message');
var connection = new WebSocket('ws://x.x.x.x:8888/ws');
var self = this;
connection.onmessage = function(event) {
var data = JSON.parse(event.data);
var series = self.series[0];
var redrawVal = true;
var shiftVal = false;
if (series.data && series.data.length > 25) {shiftVal = true;}
var newseries = {
name: '',
x: 0,
y: 0
};
$.each(data, function(i,item){
newseries.name = item.name;
newseries.x = item.x;
newseries.y = item.y;
console.log(newseries)
series.addPoint(newseries, redrawVal, shiftVal);
});
};
}
}
},
title: {
text: 'Using WebSockets for realtime updates'
},
xAxis: {
type: 'date'
},
series: [{
name: 'series',
data: []
}]
});
});
Can someone help me get multiple series to dynamically display in highcharts.js?
The general idea should be that for each series you set it's id. Then you cna get that series this way: chart.get(id). So if you have series, then add point to that series, if not, then create new one, just like this: http://jsfiddle.net/9FkJc/8/
var self = this;
data = [{
"y": 91,
"x": 1403640998,
"name": "parser1"
}, {
"y": 184,
"x": 1403640998,
"name": "parser2"
}, {
"y": 26,
"x": 1403640998,
"name": "parser3"
}];
var series = self.series[0];
var redrawVal = true;
$.each(data, function (i, item) {
var series = self.get(item.name);
if (series) { // series already exists
series.addPoint(item, redrawVal, series.data.length > 25);
} else { // new series
self.addSeries({
data: [item],
id: item.name
});
}
});
Related
How can I fetch data from a JSON API using the following code:
var options = {
chart: {
type: "bar",
},
series: [
{
name: "Bitcoin",
data: [],
},
],
xaxis: {
categories: [5, 30, 60],
},
};
var chart = new ApexCharts(document.querySelector("#chart"), options);
chart.render();
I want data() to return the following amountList array, which contains numbers inside:
function data() {
return {
apiEndPoint: "https://api.coinbase.com/v2/prices/spot?currency=",
apiData: [],
amountList: [],
};
};
I am working an angular + node dashboard application and having trouble displaying JSON data in highcharts
JSON response:
[{"output":"FAIL","count":"4"},{"output":"PASS","count":"17"}]
public uat_deployment_chart_options: Highcharts.Options;
//this is the method I created to fetch the data
public get_uat_deployment_data() {
this.uat_deployment_subscription = this.curl_connection
.get_uat_deployment_data()
.subscribe(data => {
this.uat_deployment_data = data;
this.uat_deployment_data.forEach(val => {
val.count = parseInt(val.count)
});
this.uat_deployment_chart_options = {
title: {
text: "UAT Deployments"
},
series: [
{
data: this.uat_deployment_data,
type: "pie",
colors: ["#F44336", "#CDDC39"]
}
],
credits: {
enabled: false
},
};
});
}
If I manually paste the data and change the object keys to, "name" and "y" respectively the data is displayed, otherwise I get nothing
[{ "name": "FAIL", "y": 3 }, { "name": "PASS", "y": 16 }]
So I've got the code to express my spreadsheet data in a pivot table, but I need the values to show as a percentage of column total. It seems that there's a property to achieve that result, but I don't know how to integrate that into the script.
https://developers.google.com/apps-script/reference/spreadsheet/pivot-value-display-type
function addPivotTable3(spreadsheetId3, pivotSourceDataSheetId3, destinationSheetId3)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetName = "Sheet3";
var pivotTableParams = {};
// The source indicates the range of data you want to put in the table.
// optional arguments: startRowIndex, startColumnIndex, endRowIndex, endColumnIndex
pivotTableParams.source = {
sheetId: ss.getSheetByName(sheetName).getSheetId()
};
// Group rows, the 'sourceColumnOffset' corresponds to the column number in the source range
// eg: 0 to group by the first column
pivotTableParams.rows = [{
sourceColumnOffset: 2,
sortOrder: "ASCENDING"
}];
// Defines how a value in a pivot table should be calculated.
pivotTableParams.values = [{
summarizeFunction: "SUM",
displayType: "PERCENT_OF_COLUMN_TOTAL",
sourceColumnOffset: 3
}];
var requests = [{
'updateCells': {
'rows': {
'values': [
{
'pivotTable': {
'source': {
'sheetId': pivotSourceDataSheetId3,
'startRowIndex': 0,
'startColumnIndex': 0,
'endRowIndex': 94,
'endColumnIndex': 4,
},
'rows': [
{
'sourceColumnOffset': 2,
'showTotals': true,
'sortOrder': 'ASCENDING',
'valueBucket': {
'buckets': [
{
'stringValue': 'BAE Stages',
},
],
},
},
{
'sourceColumnOffset': 94,
'showTotals': true,
'sortOrder': 'ASCENDING',
'valueBucket': {},
},
],
'columns': [
{
'sourceColumnOffset': 0,
'sortOrder': 'ASCENDING',
'showTotals': true,
'valueBucket': {},
},
],
'values': [
{
'summarizeFunction': "SUM",
'sourceColumnOffset': 3,
//'displayType': "PERCENT_OF_COLUMN_TOTAL",
//This line triggers a JSON erorr
},
],
'valueLayout': 'HORIZONTAL',
},
},
],
},
'start': {
'sheetId': destinationSheetId3,
'rowIndex': 0,
'columnIndex': 0,
},
'fields': 'pivotTable',
},
}];
var response =
Sheets.Spreadsheets.batchUpdate({'requests': requests}, spreadsheetId3);
}
Well, I didn't know either and the documentation is soooooo mysterious.
I was about to give up then I came across this: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets#PivotValueCalculatedDisplayType
Eventually I worked out your line should be:
'calculatedDisplayType' : "PERCENT_OF_COLUMN_TOTAL",
(On the above webpage is PivotValueCalculatedDisplayType and within the table is the ENUM you mentioned, "PERCENT_OF_COLUMN_TOTAL"
I had a hunch that since we are in pivot table/value part of the object, we don't need the PivotValue ... so we're left with calculatedDisplayType.
Phew.
I'm trying to implement a stacked bar chart with data coming from a CSV.
I need to update series: with the data from the CSV file which contains, for example "John,10,5,3,4,1".
Help please!
$(function () {
$('#container').highcharts({
chart: {
type: 'bar'
},
title: {
text: 'Stacked bar chart'
},
xAxis: {
categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas']
},
yAxis: {
min: 0,
title: {
text: 'Total fruit consumption'
}
},
legend: {
reversed: true
},
plotOptions: {
series: {
stacking: 'normal'
}
},
series: [{
name: 'John',
data: [5, 3, 4, 7, 2]
}, {
name: 'Jane',
data: [2, 2, 3, 2, 1]
}, {
name: 'Joe',
data: [3, 4, 4, 2, 5]
}]
});
});
UPDT
I finally got it working, but still there's a problem. The bars are inverted and I need them to be exactly in the same order as in the CSV file.
Here's my parser:
$.get('chart.csv', function(data) {
var lines=data.split('\n');
$.each(lines, function(lineNo, line) {
var items = line.split(',');
var series = {
data: []
};
$.each(items, function(itemNo, item) {
if (itemNo == 0) {
series.name = item;
} else {
series.data.push(parseFloat(item));
}
});
options.series.push(series);
});
var chart = new Highcharts.Chart(options);
The contents of the CSV file:
Disconnection,30,30
Site Care,12,12
Documentation,35,35
Lining,22,22
Connection,70,52
I need the stacked bars in the same order as in the legend:
http://i.stack.imgur.com/6EkHg.png
You could try to custom setting the series indices before you render the chart to fix the inversion of the bars.
Something like this might do the trick:
for (var i = 0; i < options.series.length; i++) {
options.series[i].index = options.series.length - 1 - i;
options.series[i].legendIndex = i;
}
I have searched and searched but i'm unable to find the solution yet.
I'm trying to load data into Highcharts with json from my server.
This is the json returned from my server:
[{
"x": "\/Date(1352674800000)\/",
"y": 621
}, {
"x": "\/Date(1352761200000)\/",
"y": 646
}, {
"x": "\/Date(1352847600000)\/",
"y": 690
}, {
"x": "\/Date(1352934000000)\/",
"y": 688
}, {
"x": "\/Date(1353020400000)\/",
"y": 499
}]
this is my highchart: (from my jsfiddle)
var seriesData = [];
for (var i = 0; i < data.length; i++) {
var dateString = data[i].x;
var x = dateString.replace(/\/Date\((\d+)\)\//, '$1');
var y = data[i].y;
seriesData.push([x, y]);
}
alert(seriesData);
var options = {
chart: {
renderTo: 'container'
},
xAxis: {
type: 'datetime',
labels: {
formatter: function() {
var monthStr = Highcharts.dateFormat('%a', this.value);
var firstLetter = monthStr.substring(0, 1);
return firstLetter;
}
}
},
series: []
};
function myfunk() {
options.series.push(seriesData);
chart = new Highcharts.Chart(options);
}
myfunk();
but my data is not showing.
i made a jsfiddel: http://jsfiddle.net/grVFk/12105/
Edit:
Now it's working but is the data point showing the wrong dates? http://jsfiddle.net/dxCHB/18/
If someone could help me i would be very grateful ! :)
You were very close. The series array needs to contain an object with a data member:
series: [
{
data:[]
}
]
You can then set it using:
function myfunk() {
options.series[0].data = seriesData;
I modified your jsfiddle: http://jsfiddle.net/dxCHB/
Format your date numerically
seriesData.push([x/1, y]);
Your data should be number, so you can use parseFloat() function to transform string to number.