Update graph from json data - javascript

I got a go program that outputs json data:
{ "cpu" : {
"Idle" : 9875425,
"Iowait" : 28338,
"Irq" : 5,
"Nice" : 9707,
"Softirq" : 4051,
"System" : 153933,
"Time" : 1329211407,
"User" : 392229
},
"cpu0" : {
"Idle" : 2417441,
"Iowait" : 3212,
"Irq" : 5,
"Nice" : 1419,
"Softirq" : 3935,
"System" : 62177,
"Time" : 1329211407,
"User" : 109227
},
}
I'm looking for a good efficient way to present and update a graph using javascript (say for every 1s).

There is no shortage of javascript libraries to graph data. I've worked with Highcharts, which is free for personal projects. To make a graph using your data in highcharts, you could do something like this:
var data = [] //your data from above; you'll need to convert it to an array of y-values or one of the other available formats
var chart;
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
defaultSeriesType: 'line',
},
series: [{
name: 'Series Title',
data: data
}]
});
});
...However, as mentioned, there are lots of JS graphing libraries. To name a few:
JQPlot
D3
Processing.js
Sencha Charts
If you're looking for a more specific answer, I'm not sure folks can offer that much in response to a vague question.

I'm a big fan of dygraphs. Very powerful. Very flexible.

I like to work with the d3js library for this kind of work.
http://mbostock.github.com/d3/
It has very nice functions to update graphs with new data.
Maybe you can base your work on the "bullet charts" example.

Google has a BUNCH of apis. You should check some of them out. One of them is the chart api here. It let's you make QR codes too. Google even has some examples in the js playground: http://code.google.com/apis/ajax/playground/?type=visualization#annotated_time_line

Related

load json into d3.js for use in Radial Dendrogram

so javascript newbie here, been banging my head on how to get JSON source json
loaded into d3.js Radial Dendrogram graph.
located here: d3.js Radial Dendrogram graph example
the current plan is to import JSON into python
or maybe convert the json into a 2 column (id,value) csv using python.
but i would love to understand the how this can be accomplished as i've been banging my head on javascript and d3.js for almost 2 days now.
Basically i'm trying to load the data into d3.js
to allow the visualization of the following
with the root being system
EDIT: to show the hopeful tendril flow
system --> model_type --> system_revision --> firmware_version --> firmware_build, system_name
to follow the example csv given in the link above.
id,value
systems,
systems_PowerEdge R720xd,
systems_PowerEdge R720xd_I,
systems_PowerEdge R720xd_I_1.50.50,
systems_PowerEdge R720xd_I_1.50.50_34,ldc0000
is the kind of output i would love to get.
Just coming back to this question from yesterday. As I said in my comment, you need to convert your JSON structure to the parent/child hierarchy required by d3.hierarchy. Unfortunately, your source data is not really hierarchical at all, so the only way I see to do this is in a very manual manner:
var root = {
id: "system",
children: []
};
data.systems.forEach(function(d,i){
var key = Object.keys(d)[0],
sysInfo = d[key].sysInfo;
root.children.push({
id: sysInfo.system_model,
children: [{
id: sysInfo.system_revision,
children: [{
id: sysInfo.firmware_version,
children: [{
id: sysInfo.firmware_build
}, {
id: key
}]
}]
}]
});
});
root = d3.hierarchy(root);
After that the dendrogram code remains unchanged except for fixing the text labels:
.text(function(d) { console.log(d); return d.data.id });
Full running code.

Can't get Dashing to display bar and line on same Rickshaw graph

I'm trying to use this widget to display 3 sets of data. the first set should be shown as a bar chart, and the rest can be displayed as lines on the chart.
From what I've read, Rickshaw Graphs should be able to do this by using the 'multi' renderer but I can't get that render to work at all.
Here is the ruby Array data object I'm sending, formatted to make it more readable:
iterationData:
[
{
:name=>"Delivered",
:renderer=>"bar",
:data=> [
{:x=>1, :y=>0},
{:x=>2, :y=>4},
{:x=>3, :y=>4},
{:x=>4, :y=>11}
]
},
{
:name=>"Estimated",
:renderer=>"line",
:data=> [
{:x=>1, :y=>2.7},
{:x=>2, :y=>5.4},
{:x=>3, :y=>8.10},
{:x=>4, :y=>10.8},
{:x=>5, :y=>13.5},
{:x=>6, :y=>16.2},
{:x=>7, :y=>18.9},
{:x=>8, :y=>21.59},
{:x=>9, :y=>24.29},
{:x=>10, :y=>26.99}
]
},
{
:name=>"Outlook",
:renderer=>"line",
:data=> [
{:x=>1, :y=>2.75},
{:x=>2, :y=>5.5},
{:x=>3, :y=>8.25},
{:x=>4, :y=>11.0},
{:x=>5, :y=>13.75},
{:x=>6, :y=>16.5},
{:x=>7, :y=>19.25},
{:x=>8, :y=>22.0},
{:x=>9, :y=>24.75},
{:x=>10, :y=>27.5}
]
}
]
and here is my calls to show the graph and to send data to the graph:
send_event in the ruby job:
send_event("#{projectID}-burnup-chart", {:series => iterationData})
dashboard.erb code:
<div data-id="23405488441-burnup-chart" data-view="Rickshawgraph" style="background-color:#ff9618" data-legend=true data-unstack=true data-renderer="bar" data-color-scheme="compliment" data-max="40"></div>
This just displays a blank widget with an orange background. The graph hasn't rendered at all. Can anyone suggest how I might achieve this? Or has anyone used a different widget to create a working burn up chart like this that they might be able to suggest?
I believe what you are trying to do is impossible because of the way that this widget marshals parameters from Dashing to Rickshaw.
Specifically, these complicated lines of code:
https://gist.github.com/jwalton/6614023#file-rickshawgraph-coffee-L193-L253
I'm not sure if Rickshaw gets mad if you send it extra data, but the author of the widget seems very concerned about sending it incorrectly. Essentially, you are falling into this case:
https://gist.github.com/jwalton/6614023#file-rickshawgraph-coffee-L237-L245
You might be able to get it to work if you added a line to that else if, like this:
else if series?.data?
# Rickshaw data. Need to clone, otherwise we could end up with multiple graphs sharing
# the same data, and Rickshaw really doesn't like that.
answer = {
renderer: series.renderer # add your renderer
name: series.name
data: series.data
color: series.color
stroke: series.stroke
}
Or better yet, just use jQuery Extend to make a new copy:
answer = $.extend(true, {}, series);
Let me know how it goes and I'll update my answer to assist if further problems arise.

Nested/multi-level/drill-down Pie Chart in Javascript?

I'm trying to create a pie chart with javascript that will allow users to click on a slice and "view what makes up that slice." If you've ever used mint.com you'll know what I mean - say you're viewing an expenses pie chart and you click the "Automobile" slice, then you see the slice expand into a new chart of Gas, Maintenance, etc.
To take this a step further, I'm dealing with a large amount of data, so being able to fetch (ajax) the new data when the slice is clicked would be a useful option as well (though I can probably get away without it).
Perhaps "nested", "multi-level" and "drill-down" are not the right terms because I've been searching all day and cannot seem to find a solution.
Does anyone know of a library for this? Thanks in advance!
I've implemented similar drilldown systems using the HighCharts point click event. Here's the rough syntax:
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'pie'
},
series: [{
data: myInitialDataArray, // make sure each data point has an id
point: {
events: {
click: function () {
$.post('/get/data/by/id/' + this.id, function(data) {
// you may need to format your data here
chart.series[0].setData(data);
});
}
}
}
}]
});
In this example, you define a click event that uses the point's id value (this.id) to perform an Ajax post to a URL. You then use the data from your post to re-bind the chart series.
Please note that each time you use the setData function to update the chart, each data point needs to have an id value in order for the drilldown to continue.
Hope this helps!
Try psd3 pie chart library
Demo - https://pshivale.github.io/psd3
Source - https://github.com/pshivale/psd3
It supports multi-level pie charts, donut charts and sunburst charts. It also supports drilling down a pie slice by double clicking it.

Dojo Line chart from JSON with multiple series and common x-axis

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!
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 }

implementing a javascript graph application to the existing admin panel's listing view

I want to implement a javascript graph plot application (ie http://people.iola.dk/olau/flot/examples/turning-series.html) to the existing admin view, where the instances of a model at the listing view also showing charts of these items and can be filtered through by using the already implemented list_filter option which I added at the admin.py of my application.
I would be greatful for any direction, example or already existing tutorial
Cheers.
If you follow the FLOT tutorials they are pretty complete.
include jQuery
include flot
Then just follow the examples / API
Essentially you "attach" the chart to a container...
<div id="myChartGoesHere" style="width:500px;height:250px;"></div>
Then populate it with the data you want:
<script>
//data can be points [ [x1, y1], [x2, y2], ... ]
var data = [ [3,4], [5,7], [2,9] ]
//options is an object containing all your desired features
var options = {
lines: { show: true },
points: { show: true }
};
var plot = $.plot($('myChartGoesHere'), data, options)
</script>
I would look into django admin templates.
http://www.djangobook.com/en/1.0/chapter17/ is a good place to start. At the top of the page, do a for loop over all your data, and print it out as a variable to your javascript. I hope this can help you in the right direction.

Categories

Resources