xxxxxxxxxx
<html lang="en">
<head>
<meta charset="utf-8">
<title>Line Chart with Two Lines</title>
<script type="text/javascript" src="https://d3js.org/d3.v3.min.js"></script>
<style type="text/css">
body {
background-color: white;
font-family: Futura, Arial, sans-serif;
}
h1 {
font-size: 24px;
font-family: Futura, Arial, sans-serif;
margin: 0;
}
p {
font-size: 14px;
margin: 10px 0 0 0;
}
svg {
background-color: white;
}
circle:hover {
fill: orange;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: Helvetica, Arial, sans-serif;
font-size: 11px;
}
</style>
</head>
<body>
<h1>Sales of Alcohol and Tobacco vs. pharmaceutical, medical and toiletry products (1989 - 2014)</h1>
<p>The sales of <strong style="color: #A4A4A4;">Alcohol and Tobacco’</strong> and <strong style="color: #01DFD7;">Pharmaceutical, medical and toiletry products’s</strong> products (all Business) in percent, with seasonal_adjustment, and their base_period for 2011, (CONS). Source: <a href="https://www.ons.gov.uk/ons/rel/rsi/retail-sales/march-2015/tsd-retail-sales--march-2015.html">Office for National Statistics (ONS.gov.uk)</a>, March 2015</p>
<script type="text/javascript">
var w = 900;
var h = 400;
var padding = [ 20, 10, 50, 100 ]; //Top, right, bottom, left
var dateFormat = d3.time.format("%Y");
var xScale = d3.time.scale()
.range([ padding[3], w - padding[1] - padding[3] ]);
var yScale = d3.scale.linear()
.range([ padding[0], h - padding[2] ]);
//Configure axis generators
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(15)
.tickFormat(function(d) {
return dateFormat(d);
});
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.ticks(15)
.tickFormat(function(d) { return d + "%" })
var area = d3.svg.area()
.x(function(d) {
return xScale(dateFormat.parse(d.year));
})
.y0(h - padding[2])
.y1(function(d) {
return yScale(d.sales);
});
var line = d3.svg.line()
.x(function(d) {
return xScale(dateFormat.parse(d.year));
})
.y(function(d) {
return yScale(d.sales);
});
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
d3.csv("dataAT.csv", function(dataAT) {
d3.csv("dataPharma.csv", function(dataPharma) {
d3.csv("database.csv", function(database) {
var mergedData = dataAT.concat(dataPharma, database);
console.log(mergedData);
xScale.domain([
d3.min(mergedData, function(d) {
return dateFormat.parse(d.year);
}),
d3.max(mergedData, function(d) {
return dateFormat.parse(d.year);
})
]);
yScale.domain([
d3.max(mergedData, function(d) {
return +d.sales;
}),
-25
]);
svg.data([ database ])
.append("path")
.attr("class", "database")
.attr("d", area)
.attr("fill", "#FE2E2E")
.attr("stroke", "#FE2E2E")
.attr("opacity", .3)
.attr("stroke-width", 5);
svg.data([ database ])
.append("path")
.attr("class", "database")
.attr("d", line)
.attr("fill", "none")
.attr("stroke", "black")
.attr("opacity", 1)
.attr("stroke-width", 10);
svg.data([ dataPharma ])
.append("path")
.attr("class", "line Pharma")
.attr("d", area)
.attr("fill", "#01DFD7")
.attr("stroke", "#01DFD7")
.attr("opacity", 0.9)
.attr("stroke-width", 2);
svg.data([ dataPharma ])
.append("path")
.attr("class", "line AT")
.attr("d", line)
.attr("fill", "none")
.attr("stroke", "#04B4AE")
.attr("opacity", 0.5)
.attr("stroke-width", 5);
svg.data([ dataAT ])
.append("path")
.attr("class", "line AT")
.attr("d", area)
.attr("fill", "#BDBDBD")
.attr("stroke", "##A4A4A4")
.attr("opacity", 1)
.attr("stroke-width", 2);
svg.data([ dataAT ])
.append("path")
.attr("class", "line AT")
.attr("d", line)
.attr("fill", "none")
.attr("stroke", "#A4A4A4")
.attr("opacity", 1)
.attr("stroke-width", 4);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (h - padding[2]) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + (padding[3]) + ",0)")
.call(yAxis);
});
});
});
</script>
</body>
</html>
Modified http://d3js.org/d3.v3.min.js to a secure url
https://d3js.org/d3.v3.min.js