Question

I believe what I am trying to accomplish should be a fairly common task, yet I'm having difficulty getting it to work. I simply wish to create a multi-series plot from a data set containing (for each record) an ISO8601 timestamp along with multiple data points. The data is in JSON format and I'm using dojox.charting.chart "Lines" type.

I'm already aware that the Dojo charts cannot directly handle time-based axis data, let alone ISO8601. So I've already dealt with converting the x-axis to milliseconds-since-T0 server-side.

Here is a distilled example excerpt of my JSON:

[{"Offset_ms":0,"CP":250.58368,"TP":181.88211},
{"Offset_ms":360000,"CP":233.18443,"TP":119.94824},
{"Offset_ms":540000,"CP":227.15465,"TP":117.99422},
{"Offset_ms":720000,"CP":222.87495,"TP":117.55895},
{"Offset_ms":896000,"CP":218.19876,"TP":117.64221},
{"Offset_ms":900000,"CP":219.77487,"TP":117.93475}]

And the distilled JavaScript (assume the above JSON is in the variable 'sequenceData'):

var chart = new dojox.charting.Chart("sequenceDataGraph");

chart.addPlot("default", {
    type: "Lines",
    tension: "X"
});
chart.addAxis("x", { labelFunc: labelTimeAxis });
chart.addAxis("y", { vertical: true });

var sequenceDataStore = new dojo.store.Observable(new dojo.store.Memory({
    data: {
        label: "Sequence",
        items: sequenceData
    }
}));

addSequenceDataSeries(chart, sequenceDataStore, "TP");
addSequenceDataSeries(chart, sequenceDataStore, "CP");

chart.render();

function addSequenceDataSeries(chart, sequenceDataStore, sColumnName) {
    chart.addSeries(sColumnName, new dojox.charting.StoreSeries(sequenceDataStore, { query: {} },
                    sColumnName));
}

What appears to be happening, is that Dojo Chart is not using the x-axis data at all, but instead plotting each point at a fixed interval based on the number of data points. That is, each data point seems to be assigned an ordinal, such as if Offset_ms was merely 1, 2, 3... Since my data points are not always at fixed intervals, the resulting graph is distorted.

How do I instruct Dojo Chart to use the "Offset_ms" field in the JSON data for the x-axis component?

I've scoured the tutorials, the API docs and performed numerous Google & SO searches to no avail. I've even browsed portions of the Dojo source, particularly StoreSeries.js.uncompressed.js, but I'm not finding any answers. Surely this is possible, and hopefully trivial!

Was it helpful?

Solution

Unfortunately, the official dojo documentation is seriously lacking, and I only figured out how to do something similar by browsing the dojo source. Specifically, line 135 of the StoreSeries test, http://archive.dojotoolkit.org/nightly/dojotoolkit/dojox/charting/tests/test_StoreSeries.html

The StoreSeries constructor's third argument accepts an object that maps the X and Y axis to specific fields in your data store.

Change the following line in your code from this:

chart.addSeries(sColumnName, new dojox.charting.StoreSeries(sequenceDataStore, { query: {} },
                sColumnName));

to this:

chart.addSeries(sColumnName, new dojox.charting.StoreSeries(sequenceDataStore, { query: {} }, 
                { x: "Offset_ms", y: sColumnName }));

sColumnName becomes { x: "Offset_ms", y: sColumnName }

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top