You can use d3.dispatch to create the observer pattern:
http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/#observerpatternjavascript
Synapse is a more fleshed-out library based on this pattern:
http://bruth.github.com/synapse/docs/
pax, You could use jQuery events, but that'd add an extra dependency
to your chart. Chart users shouldn't have to use d3.dispatch itself.
Instead, they listen for events from the chart which use d3.dispatch
internally.
var dataSet1 = [{"setName": "Data Set 1", "label": "Label 1", "value": "25"},{"setName": "Data Set 1", "label": "Label 2", "value": "10"},{"setName": "Data Set 1", "label": "Label 3", "value": "2"},{"setName": "Data Set 1", "label": "Label 4", "value": "45"},{"setName": "Data Set 1", "label": "Label 5", "value": "15"},{"setName": "Data Set 1", "label": "Label 6", "value": "85"}];
Also see the problem on Safari v5.1.4 (6534.54.16) on Mac OS X 10.6.8
function generateData() {
var read = [],
write = [];
$.getJSON("data.php<? echo $fullurl ?>", function(json) {
json.forEach(function(values) {
read.push([ new Date(values.timestamp), parseFloat(values.read) ]);
write.push([ new Date(values.timestamp), parseFloat(values.write) ]);
})
console.log(read);
console.log(write);
return [
{
data: read,
label: "<? echo $label1 ?>"
},
{
data: write,
label: "<? echo $label2 ?>"
}
];
})
}
});Example JSON data I'm working with
[
{"timestamp":"2013-02-25 17:29:47","read":"24.37","write":"0.01"},
{"timestamp":"2013-02-25 17:34:47","read":"64.05","write":"0.02"},
{"timestamp":"2013-02-25 17:39:47","read":"37.44","write":"0.01"},
{"timestamp":"2013-02-25 19:09:47","read":"48.37","write":"0.01"},
{"timestamp":"2013-02-25 19:14:47","read":"24.91","write":"0.01"},
{"timestamp":"2013-02-25 19:19:47","read":"45.81","write":"0.03"},
{"timestamp":"2013-02-25 20:49:47","read":"25.44","write":"0.02"}
]What does generateData return in the example, and what does it return in your code?
<script src="nvd3/lib/d3.v2.js"></script>
<script src="nvd3/nv.d3.js"></script>
<script src="nvd3/src/tooltip.js"></script>
<script src="nvd3/src/utils.js"></script>
<script src="nvd3/src/models/legend.js"></script>
<script src="nvd3/src/models/axis.js"></script>
<script src="nvd3/src/models/scatter.js"></script>
<script src="nvd3/src/models/line.js"></script>
<script src="nvd3/src/models/lineChart.js"></script>
<script>
// Wrapping in nv.addGraph allows for '0 timeout render', stores rendered charts in nv.graphs, and may do more in the future... it's NOT required
var chart;
nv.addGraph(function() {
chart = nv.models.cumulativeLineChart()
.x(function(d) { return d[0] })
.y(function(d) { return d[1] })
.color(d3.scale.category10().range())
.clipVoronoi(false);
chart.xAxis
.axisLabel('Timestamp')
.tickFormat(function(d) {
return d3.time.format('%x')(new Date(d))
});
chart.yAxis
.axisLabel('<? echo $ylabel; ?>')
.tickFormat(d3.format('.02f'));
var theData = getData();
console.log(theData);
d3.select('#chart1 svg')
.datum(getData())
.call(chart);
nv.utils.windowResize(chart.update);
chart.dispatch.on('stateChange', function(e) { nv.log('New State:', JSON.stringify(e)); });
return chart;
});
function getData() {
var read = [],
write = [];
d3.json("data.php<? echo $fullurl; ?>", function(json) {
json.forEach(function(values) {
timestamp = new Date(values.timestamp).getTime()/1000;
read.push({ x: timestamp, y: parseFloat(values.read) });
write.push({ x: timestamp, y: parseFloat(values.write) });
})
})
return [
{
key: "<? echo $label1; ?>",
values: read
},
{
key: "<? echo $label2; ?>",
values: write
}
]
}
--