d3.js - Map data for lineWithFocusChart time series -
i trying create timeseries using nvd3 linewithfocuschart
model. data array of objects this:
[ { "key": "red", "values": [ { "date": "2015-06-17t11:00:00.000z", "value": 17 }, ...] }, { "key": "green", "values": [ { "date": "2015-06-17t11:00:00.000z", "value": 20 }, ...] }, ]
i want map date
x
, value
y
, looking @ other examples typically done this:
nv.addgraph(function() { var chart = nv.models.linewithfocuschart() .x(function(d) { return new date(d.daterun)}) .y(function(d) { return d.value}); chart.brushextent([50,70]); chart.xaxis.tickformat(d3.format(function(d) { return d3.time.format('%x')(new date(d)); })); chart.x2axis.tickformat(d3.format(',f')); chart.yaxis.tickformat(d3.format(',.2f')); chart.y2axis.tickformat(d3.format(',.2f')); chart.useinteractiveguideline(true); d3.select('#chart svg') .datum(data) .call(chart); nv.utils.windowresize(chart.update); return chart; });
but on .x(function(d) { return new date(d.date)})
getting typeerror: d undefined
.
how can map data correctly chart model?
i have created following code parts provided not receive error refer to, lines not draw because have 2 points of data. see below:
<link href="libs/nv.d3.css" rel="stylesheet" type="text/css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.2/d3.min.js" charset="utf-8"></script> <script src="libs/nv.d3.js"></script> <script src="libs/stream_layers.js"></script> <body> <div id="chart" class='with-3d-shadow with-transitions'> <svg></svg> </div> <script> nv.addgraph(function() { var chart = nv.models.linewithfocuschart() .x(function(d) { return new date(d.date)}) .y(function(d) { return d.value}); chart.brushextent([50,70]); chart.xaxis.tickformat(d3.format(function(d) { return d3.time.format('%x')(new date(d.date)); })); chart.x2axis.tickformat(d3.format(',f')); chart.yaxis.tickformat(d3.format(',.2f')); chart.y2axis.tickformat(d3.format(',.2f')); chart.useinteractiveguideline(true); d3.select('#chart svg') .datum(data()) .call(chart); nv.utils.windowresize(chart.update); return chart; }); function data() { return [ { "key": "red", "values": [ { "date": "2015-06-17t11:00:00.000z", "value": 17 }, { "date": "2015-06-17t11:00:00.000z", "value": 18 }, ] }, { "key": "green", "values": [ { "date": "2015-06-17t11:00:00.000z", "value": 20 }, { "date": "2015-06-17t11:00:00.000z", "value": 17 }, ] } ] } </script> </body>
the thing found wrong you're referring date data point "d.daterun" , in data "date" should "d.date" in code.
hope helps.
Comments
Post a Comment