1

I'm trying to create a Highstock chart from Xively data.

I'm able to create regular line chart from a historic query of a Xively datastream and with some basic data format adaptation I am able to pass it to Highcharts.Chart().

I have tried making Highstock chart without any success.

I'm new to using Highcharts and I'm not familiar with JavaScript.

errordeveloper
  • 6,716
  • 6
  • 41
  • 54

1 Answers1

1

In the basic Highcharts example you have a bit of code which adapts the data format. That code takes the array of datapoint objects with value/at keys and making a two-dimensional array.

It maps this:

[
  { value: "59", at: "2013-05-01T00:59:45.645022Z" },
  { value: "59", at: "2013-05-01T01:59:48.550144Z" },
  { value: "59", at: "2013-05-01T02:59:51.313604Z" }
]

to this:

[
  ["2013-05-01T00:59:45.645022Z", 59],
  ["2013-05-01T01:59:48.550144Z", 59],
  ["2013-05-01T02:59:51.313604Z", 59]
]

I can do this using a simple for-loop and I also used Date.parse() as well as parseFloat() to ensure Highcharts understand my data correctly:

    var xively_datapoints = data.datapoints;
    var chartdata = [];

    for (i = 0; i < xively_datapoints.length; i++) {
        chartdata.push([
            Date.parse(xively_datapoints[i].at),
            parseFloat(xively_datapoints[i].value)
        ]);
    }

I then pass chartdata array to the Highcharts like so:

    $('#container').highcharts('StockChart', {
        rangeSelector : {
            selected : 1
        },
        series : [{
            name : 'Highstock+Xively',
            data : chartdata, // reformatted data
            tooltip: {
                valueDecimals: 2
            }
        }]
    });
errordeveloper
  • 6,716
  • 6
  • 41
  • 54
  • @ScottGoldthwaite hope you pick up from now on! A few suggestions: a) do not share an API key that has read access to your data b) try using [Xively JavaScript library](http://xively.github.io/xively-js/) for this and you should be able to make real-time graphs using `xively.datastream.subscribe` c) mark this as answered and upvote please! – errordeveloper May 20 '13 at 00:26
  • That wasn't my API key, it was one I reused from an example someone else posted. I don't know how to mark this as answered. I don't see anything that let's me do that. – Scott Goldthwaite May 20 '13 at 04:44
  • on the right of my answer there is a tick sign that you can click and it goes green... – errordeveloper May 20 '13 at 07:46