问题
I am using nvd3.js first time in my php code.
I want to show a graph count against date for particular user. I want to show the date horizontally and count vertically. But I did not understand how to do that.
My json data is like:["key":"0","values":[["1374517800000","2"]]},"182398":{"key":"182398","values":[["1375295400000","2"],["1374517800000","2"],["1374604200000","12"],["1374431400000","1"],["1375122600000","4"],["1375209000000","19"]]},"185271":{"key":"185271","values":[["1374604200000","2"],["1374517800000","1"]]]
and
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
.tickFormat(function(d) {
return d3.time.format('%x')(new Date(d))
});
chart.yAxis
.tickFormat(d3.format(',.1%'));
d3.select('#cumulative_line_chart svg')
.datum(stageArr)
//.transition().duration(500)
.call(chart);
//TODO: Figure out a good way to do this automatically
nv.utils.windowResize(chart.update);
//nv.utils.windowResize(function() { d3.select('#chart1 svg').call(chart) });
chart.dispatch.on('stateChange', function(e) { nv.log('New State:', JSON.stringify(e)); });
return chart;
});
Now in this case my first problem is date is not showing properly(I converted the date to strtotime() and then concat 000 with the date e.g 1375295400000 =strtotime("23-07-2013")."000"
and this conversion is happening in php).
Second issue is in y axis I want to show the integer like 2,12,4,19(as per above json data) etc.
So please guide me how to do that.
Thanks in advance.
UPDATE: Sorry to say that it is not d3.js but it is nvd3.js.
回答1:
Looking for something like this ? And here is a working version of the cde
nv.addGraph(function () {
var chart = nv.models.lineChart().margin({
top: 30,
right: 20,
bottom: 50,
left: 45
}).showLegend(true).tooltipContent(function (key, y, e, graph) {
return '<h3>' + key + '</h3>' + '<p>' + e + '% at ' + y + '</p>'
});
chart.xAxis.tickFormat(function (d) {
return d3.time.format('%x')(new Date(d))
});
d3.select('#lineChart svg')
.datum(data)
.transition().duration(500)
.call(chart);
nv.utils.windowResize(chart.update);
return chart;
});
data = [{
"values": [{
"x": 1025409600000 ,
"y": 2
}, {
"x": 1028088000000 ,
"y": 4
}, {
"x": 1030766400000 ,
"y": 1
}, {
"x": 1033358400000 ,
"y": 3
}, {
"x": 1036040400000 ,
"y": 0
}, {
"x": 1038632400000 ,
"y": 3
}],
"key": "Sine Wave",
}]
Hope it helps.
回答2:
You have to define the scale of the axis : it's not computed from you data, but you can use helper function for d3, like d3.max ...
var xScale = d3.scale.linear()
.domain([0, d3.max(dataset, function(d) { return d[0]; })])
.range([0, w]);
c.f. http://alignedleft.com/tutorials/d3/scales
Then, you can add the scale definition to the axis:
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom");
c.f. http://alignedleft.com/tutorials/d3/axes
来源:https://stackoverflow.com/questions/18055791/create-graph-in-nvd3-js-with-proper-values