i created a stacked chart in d3.js. reading data from a file. Now I want to read from json. what changes i need to do in order to read from json.
Below is code for the chart.
d3.tsv("./StackedChart/stackeddata_dept1.tsv", type, function(error, data) {
x_stacked.domain(data.map(function(d) { return d.department; }));
y0_stacked.domain([0, d3.max(data, function(d) { return 100; })]);
svg1.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height_stacked + ")")
.call(xAxis_stacked);
svg1.append("g")
.attr("class", "y axis axisLeft")
.attr("transform", "translate(0,0)")
.call(yAxisLeft_stacked)
.append("text")
.attr("y", 6)
.attr("dy", "-2em")
.style("text-anchor", "end")
.style("text-anchor", "end");
bars = svg1.selectAll(".bar").data(data).enter();
bars.append("rect")
.attr("class", "bar1")
.attr("x", function(d) { return x_stacked(d.department); })
.attr("width", x_stacked.rangeBand()/2)
.attr("y", function(d) { return y0_stacked(d.positive); })
.attr("height", function(d,i,j) { return height_stacked - y0_stacked(d.positive); });
});
function type(d) {
d.positive = +d.positive;
return d;
}
Data in file is :
department positive negative
Finance 63.5299997329712 36.9699997901917
HR 43.6424999237061 56.3575010299683
Production 43.1399993896484 57.0625009536743
Sales 65.1949987411499 34.8049998283386
Any help appreciated. all i need to know is what changes in the code needs to be done in order to read from json. as now the data would be in json , not in a file. what are possible ways to read data in d3 instead of using file?