Reputation: 3303
Before posting, I looked at this SO question that had the same issue, and I'm sure the solution is similar to his, but I'm new to d3 so I have no idea how to fix mine.
I'm trying to modify a d3 chart so that it reads from a json
stream instead of the csv
file. The graph is based on this bl.ocks.org link.
So I essentially created a WCF service
that returns exactly the same data as the data in the CSV
. The stream returned looks like this:
[
{"date":"6/1/2017","sales":1394.46},
{"date":"6/2/2017","sales":1366.42},
{"date":"6/3/2017","sales":1498.58}
]
My d3
chart that looks like this:
//d3.csv("sp500.csv", type, function(error, data) {
d3.json("http://MyServer:86/Erase.svc/GetDataErase", function(error, data) {
console.log(data);
if (error) throw error;
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.sales; })]);
x2.domain(x.domain());
y2.domain(y.domain());
focus.append("path")
.datum(data)
.attr("class", "area")
.attr("d", area);
focus.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
focus.append("g")
.attr("class", "axis axis--y")
.call(yAxis);
context.append("path")
.datum(data)
.attr("class", "area")
.attr("d", area2);
context.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height2 + ")")
.call(xAxis2);
context.append("g")
.attr("class", "brush")
.call(brush)
.call(brush.move, x.range());
svg.append("rect")
.attr("class", "zoom")
.attr("width", width)
.attr("height", height)
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.call(zoom);
});
This csv
file looks like this:
date,sales
6/1/2017,1394.46
6/2/2017,1366.42
console.log(data);
returns the array correctly, so the wcf service is not the problem.
Any help is appreciated.
Thanks.
Upvotes: 0
Views: 1406
Reputation: 331
In the example, a row conversion function is passed to d3.csv to parse the dates. Since d3.json doesn't take a row conversion function, you need to parse the dates yourself after the data is loaded:
d3.json("mydata.json", function(error, data) {
if (error) throw error;
data.forEach(function(d) {
d.date = parseDate(d.date);
});
...
}
Upvotes: 2