I am trying to plot a graph using D3.js. file.json is my JSON file. Date-Time should be in the X-Axis and Sales should be in Y-Axis.
This is my JSON Structure,
[
{
Date : "2017-12-17 18:30:01",
Sales : "50"
},
{ Date : "2017-12-17 17:30:00",
Sales : "20"
},
{
Date : "2017-12-17 16:30:00",
Sales : "10"
}
]
I want to plot a graph Date vs number of sales. This is my JS :-
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.line {
fill: none;
stroke: steelblue;
stroke-width: 2px;
}
</style>
<body>
<!-- load the d3.js library -->
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
d3.json("file.json", function(data) {
var canvas = d3.select("body").append("svg")
.attr("width", 500)
.attr("height", 500)
.attr("border", "black")
var group = canvas.append("g")
.attr("transform", "translate(100,10)")
var line = d3.line()
.x(function(d, i) {
return d.Date;
})
.y(function(d, i) {
return d.Sales;
});
group.selectAll("path")
.data(data).enter()
.append("path")
.attr("d", function(d){ return line(d) })
.attr("fill", "none")
.attr("stroke", "green")
.attr("stroke-width", 3);
});
</script>
It is showing error :- Uncaught TypeError: Cannot read property 'line' of undefined . I changed from d3.svg.line() to d3.line() , but it is now showing blank page.
var line = d3.line().