D3.js或Google Charts或Chart.js中的Grouped Bar Char

时间:2017-02-13 11:16:02

标签: d3.js chart.js

如何在任何图表系统(如D3,Google Charts或Chart.js)中创建如下图所示的分组条形图。

enter image description here

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

您可以使用d3.js创建此类分组条形图。

您可以查看this,了解如何创建分组条形图。希望这会有所帮助。

答案 1 :(得分:0)

我创造了它。不相似但更接近要求。

var datastring="Grade,Your School,Other School\nAcross Grades,10345,8345\nGrade 1,2000,1234\nGrade 2,2345,2600\nGrade 3,4230,1234\nGrade 4,1002,2456\nGrade 5,2034,234\nGrade 6,234,2678\nGrade 7,2434,3000\nGrade 8,1789,2890";
var margin = {top: 20, right: 10, bottom: 60, left: 10},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var x0 = d3.scale.ordinal()
    .rangeRoundBands([0, width], 0.4);

var x1 = d3.scale.ordinal();

var y = d3.scale.linear()
    .range([height, 0]);

var color = d3.scale.ordinal()
    .range(["#FC654C", "#CCCCCC"]);

var xAxis = d3.svg.axis()
    .scale(x0)
    .orient("bottom");

var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left")
    .tickFormat(d3.format(".2s"));

var svg = d3.select("#svg").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 + ")");
	
var data = d3.csv.parse(datastring);
var ageNames = d3.keys(data[0]).filter(function(key) { return key !== "Grade"; });

  data.forEach(function(d) {
    d.ages = ageNames.map(function(name) { return {name: name, value: +d[name]}; });
  });

  x0.domain(data.map(function(d) { return d.Grade; }));
  x1.domain(ageNames).rangeRoundBands([0, x0.rangeBand()]);
  y.domain([0, d3.max(data, function(d) { return d3.max(d.ages, function(d) { return d.value; }); })]);


			
  var grade = svg.selectAll(".grade")
      .data(data)
    .enter().append("g")
      .attr("class", "g")
      .attr("transform", function(d) { return "translate(" + (x0(d.Grade)) + ",0)"; });
	  
	

  grade.selectAll("rect")
      .data(function(d) { return d.ages; })
    	.enter().append("rect")
    	.attr("class","bars")
      .attr("width", x1.rangeBand())
      .attr("x", function(d) { return x1(d.name); })
      .attr("y", function(d) { return y(d.value); })
      .attr("height", function(d) { return height - y(d.value); })
      .style("fill", function(d) { return color(d.name); });
  
  grade.selectAll("text")
  .data(function(d) { return d.ages; })
    	.enter().append("text")
    	.attr("class","barstext")
    	.attr("x",-410)
		.attr("y",function(d,i) {return (i*x1.rangeBand())+(x1.rangeBand()/2)+5; } )
		.attr("transform", "rotate(-90)")
    	.text(function(d){return d.value});
	
	
	grade.append("text")
			.attr("class","barstext") 
			.attr("transform", "translate(0,"+(height+10)+")")
			.attr("dy",1)
			.text(function(d){return d.Grade}).call(wrap, 80);	
	function wrap(text, width) {
    text.each(function() {
        var text = d3.select(this),
        words = text.text().split(/\s+/).reverse(),
        word,
        line = [],
        lineNumber = 0,
        y = text.attr("y"),
        dy = parseFloat(text.attr("dy")),
        lineHeight = 1.1, // ems
        tspan = text.text(null).append("tspan").attr("x", function(d) { return d.children || d._children ? -10 : 10; }).attr("y", y).attr("dy", dy + "em");     
        while (word = words.pop()) {
            line.push(word);
            tspan.text(line.join(" "));
            var textWidth = tspan.node().getComputedTextLength();
            if (tspan.node().getComputedTextLength() > width) {
                line.pop();
                tspan.text(line.join(" "));
                line = [word];
                ++lineNumber;
				tspan.attr("x", function(d) { return d.children || d._children ? -10 : 8; }).attr("y", y).attr("dy", dy + "em");   
                tspan = text.append("tspan").attr("x", function(d) { return d.children || d._children ? -10 : 5; }).attr("y", 0).attr("dy", lineNumber * lineHeight + dy + "em").text(word);
            }
			else{
				tspan.attr("x", function(d) { return d.children || d._children ? -10 : 0; }).attr("y", y).attr("dy", dy + "em");     
			}
        }
    });
}
body {
  font: 10px sans-serif;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

.bar {
  fill: steelblue;
}

.x.axis path {
  display: none;
}
.barstext{
	font-size:18px;
	color:#CCCCCC;
}
.tick text {
    font-size:18px;
	color:#CCCCCC;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<div id="svg" style="position:relative;width:960px;">
	<div style="position: absolute;border-bottom: 2px solid #14B4B4;width: 100%; bottom: 61px;"></div>
</div>