<html> <head> <p>Sales of different Regions and Quarters</p> </head> <body> <meta charset="utf-8"> <script src = "https://d3js.org/d3.v4.js"></script> <div id = "sales" ></div> <script> var p = d3.select("body") .selectAll("p") .style("color","#1979a9") .style("font-size","24px");
var margin = { top: 10, right: 30, bottom: 20, left: 50 }, width = 460 - margin.left - margin.right, height = 400 - margin.top - margin.bottom;
var svg = d3.select("#sales") .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/Co10/d3_files/master/CSV_files/01/quarter_region_data.csv", function(data) {
var subgroups = data.columns.slice(1)
var groups = d3.map(data, function(d) { return (d.group) }).keys()
var x = d3.scaleBand() .domain(groups) .range([0, width]) .padding([0.4]) svg.append("g") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x).tickSize(0));
var y = d3.scaleLinear() .domain([0, 90]) .range([height, 0]); svg.append("g") .call(d3.axisLeft(y));
var xSubgroup = d3.scaleBand() .domain(subgroups) .range([0, x.bandwidth()]) .padding([0.2])
var color = d3.scaleOrdinal() .domain(subgroups) .range(['#248760','#e33617','#ecb814','#66abdd'])
var gbars = svg.append("g") .selectAll("g") .data(data) .enter() .append("g") .attr("transform", function(d) { return "translate(" + x(d.group) + ",0)"; }); gbars.selectAll("rect") .data(function(d) { return subgroups.map(function(key) { return {key: key, value: d[key]}; }); }) .enter().append("rect") .attr("x", function(d) { return xSubgroup(d.key); }) .attr("y", function(d) { return y(d.value); }) .attr("width", xSubgroup.bandwidth()) .attr("height", function(d) { return height - y(d.value); }) .attr("fill", function(d) { return color(d.key); });
svg.append("text") .attr("x",5) .attr("y",0) .text("Sales") .style("font-size", "12px") .attr("alignment-baseline","middle")
svg.append("circle") .attr("cx",280) .attr("cy",10) .attr("r", 4) .style("fill", color(1)) svg.append("circle") .attr("cx",280) .attr("cy",30) .attr("r", 4) .style("fill", color(2)) svg.append("circle") .attr("cx",280) .attr("cy",50) .attr("r", 4) .style("fill", color(3)) svg.append("circle") .attr("cx",280) .attr("cy",70) .attr("r", 4) .style("fill", color(4)) svg.append("text") .attr("x", 290) .attr("y", 10) .text("Quarter 1") .style("font-size", "12px") .attr("alignment-baseline","middle") svg.append("text") .attr("x", 290) .attr("y", 30) .text("Quarter 2") .style("font-size", "12px") .attr("alignment-baseline","middle") svg.append("text") .attr("x", 290) .attr("y", 50) .text("Quarter 3") .style("font-size", "12px") .attr("alignment-baseline","middle") svg.append("text") .attr("x", 290) .attr("y", 70) .text("Quarter 4") .style("font-size", "12px") .attr("alignment-baseline","middle") }) </script> </body> </html>
|