multi bar chart d3

Solutions on MaxInterview for multi bar chart d3 by the best coders in the world

showing results for - "multi bar chart d3"
Roland
03 Aug 2018
1<!DOCTYPE html>
2<meta charset="utf-8">
3<style>
4
5body {
6  font: 10px sans-serif;
7}
8
9.axis path,
10.axis line {
11  fill: none;
12  stroke: #000;
13  shape-rendering: crispEdges;
14}
15
16.x.axis path {
17  display: none;
18}
19
20</style>
21<body>
22<script src="https://d3js.org/d3.v3.min.js"></script>
23<script>
24
25var margin = {top: 20, right: 20, bottom: 30, left: 40},
26    width = 960 - margin.left - margin.right,
27    height = 500 - margin.top - margin.bottom;
28
29var x0 = d3.scale.ordinal()
30    .rangeRoundBands([0, width], .1);
31
32var x1 = d3.scale.ordinal();
33
34var y = d3.scale.linear()
35    .range([height, 0]);
36
37var xAxis = d3.svg.axis()
38    .scale(x0)
39    .tickSize(0)
40    .orient("bottom");
41
42var yAxis = d3.svg.axis()
43    .scale(y)
44    .orient("left");
45
46var color = d3.scale.ordinal()
47    .range(["#ca0020","#f4a582","#d5d5d5","#92c5de","#0571b0"]);
48
49var svg = d3.select('body').append("svg")
50    .attr("width", width + margin.left + margin.right)
51    .attr("height", height + margin.top + margin.bottom)
52  .append("g")
53    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
54
55d3.json("data.json", function(error, data) {
56
57  var categoriesNames = data.map(function(d) { return d.categorie; });
58  var rateNames = data[0].values.map(function(d) { return d.rate; });
59
60  x0.domain(categoriesNames);
61  x1.domain(rateNames).rangeRoundBands([0, x0.rangeBand()]);
62  y.domain([0, d3.max(data, function(categorie) { return d3.max(categorie.values, function(d) { return d.value; }); })]);
63
64  svg.append("g")
65      .attr("class", "x axis")
66      .attr("transform", "translate(0," + height + ")")
67      .call(xAxis);
68
69  svg.append("g")
70      .attr("class", "y axis")
71      .style('opacity','0')
72      .call(yAxis)
73  .append("text")
74      .attr("transform", "rotate(-90)")
75      .attr("y", 6)
76      .attr("dy", ".71em")
77      .style("text-anchor", "end")
78      .style('font-weight','bold')
79      .text("Value");
80
81  svg.select('.y').transition().duration(500).delay(1300).style('opacity','1');
82
83  var slice = svg.selectAll(".slice")
84      .data(data)
85      .enter().append("g")
86      .attr("class", "g")
87      .attr("transform",function(d) { return "translate(" + x0(d.categorie) + ",0)"; });
88
89  slice.selectAll("rect")
90      .data(function(d) { return d.values; })
91  .enter().append("rect")
92      .attr("width", x1.rangeBand())
93      .attr("x", function(d) { return x1(d.rate); })
94      .style("fill", function(d) { return color(d.rate) })
95      .attr("y", function(d) { return y(0); })
96      .attr("height", function(d) { return height - y(0); })
97      .on("mouseover", function(d) {
98          d3.select(this).style("fill", d3.rgb(color(d.rate)).darker(2));
99      })
100      .on("mouseout", function(d) {
101          d3.select(this).style("fill", color(d.rate));
102      });
103
104  slice.selectAll("rect")
105      .transition()
106      .delay(function (d) {return Math.random()*1000;})
107      .duration(1000)
108      .attr("y", function(d) { return y(d.value); })
109      .attr("height", function(d) { return height - y(d.value); });
110
111  //Legend
112  var legend = svg.selectAll(".legend")
113      .data(data[0].values.map(function(d) { return d.rate; }).reverse())
114  .enter().append("g")
115      .attr("class", "legend")
116      .attr("transform", function(d,i) { return "translate(0," + i * 20 + ")"; })
117      .style("opacity","0");
118
119  legend.append("rect")
120      .attr("x", width - 18)
121      .attr("width", 18)
122      .attr("height", 18)
123      .style("fill", function(d) { return color(d); });
124
125  legend.append("text")
126      .attr("x", width - 24)
127      .attr("y", 9)
128      .attr("dy", ".35em")
129      .style("text-anchor", "end")
130      .text(function(d) {return d; });
131
132  legend.transition().duration(500).delay(function(d,i){ return 1300 + 100 * i; }).style("opacity","1");
133
134});
135
136</script>
137