Example 1: d3 area chart with yaxis starting from 0
<script>
var margin = {top: 10, right: 30, bottom: 30, left: 50},
width = 460 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
var svg = d3.select("#my_dataviz")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
d3.csv("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/3_TwoNumOrdered_comma.csv",
function(d){
return { date : d3.timeParse("%Y-%m-%d")(d.date), value : d.value }
},
function(data) {
data = data.filter(function(d,i){ return i<90})
var x = d3.scaleTime()
.domain(d3.extent(data, function(d) { return d.date; }))
.range([ 0, width ]);
svg.append("g")
.attr("transform", "translate(0," + (height+5) + ")")
.call(d3.axisBottom(x).ticks(5).tickSizeOuter(0));
var y = d3.scaleLinear()
.domain([0, d3.extent(data, function(d) { return +d.value; })] )
.range([ height, 0 ]);
svg.append("g")
.attr("transform", "translate(-5,0)")
.call(d3.axisLeft(y).tickSizeOuter(0));
svg.append("path")
.datum(data)
.attr("fill", "#69b3a2")
.attr("fill-opacity", .3)
.attr("stroke", "none")
.attr("d", d3.area()
.x(function(d) { return x(d.date) })
.y0( height )
.y1(function(d) { return y(d.value) })
)
svg.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "#69b3a2")
.attr("stroke-width", 4)
.attr("d", d3.line()
.x(function(d) { return x(d.date) })
.y(function(d) { return y(d.value) })
)
svg.selectAll("myCircles")
.data(data)
.enter()
.append("circle")
.attr("fill", "red")
.attr("stroke", "none")
.attr("cx", function(d) { return x(d.date) })
.attr("cy", function(d) { return y(d.value) })
.attr("r", 3)
})
</script>
Example 2: d3 area chart with yaxis starting from 0
<script>
var margin = {top: 10, right: 30, bottom: 30, left: 50},
width = 460 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
var svg = d3.select("#my_dataviz")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
d3.csv("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/3_TwoNumOrdered_comma.csv",
function(d){
return { date : d3.timeParse("%Y-%m-%d")(d.date), value : d.value }
},
function(data) {
data = data.filter(function(d,i){ return i<90})
var x = d3.scaleTime()
.domain([0, d3.extent(data, function(d) { return d.date; }]))
.range([ 0, width ]);
svg.append("g")
.attr("transform", "translate(0," + (height+5) + ")")
.call(d3.axisBottom(x).ticks(5).tickSizeOuter(0));
var y = d3.scaleLinear()
.domain( d3.extent(data, function(d) { return +d.value; }) )
.range([ height, 0 ]);
svg.append("g")
.attr("transform", "translate(-5,0)")
.call(d3.axisLeft(y).tickSizeOuter(0));
svg.append("path")
.datum(data)
.attr("fill", "#69b3a2")
.attr("fill-opacity", .3)
.attr("stroke", "none")
.attr("d", d3.area()
.x(function(d) { return x(d.date) })
.y0( height )
.y1(function(d) { return y(d.value) })
)
svg.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "#69b3a2")
.attr("stroke-width", 4)
.attr("d", d3.line()
.x(function(d) { return x(d.date) })
.y(function(d) { return y(d.value) })
)
svg.selectAll("myCircles")
.data(data)
.enter()
.append("circle")
.attr("fill", "red")
.attr("stroke", "none")
.attr("cx", function(d) { return x(d.date) })
.attr("cy", function(d) { return y(d.value) })
.attr("r", 3)
})
</script>