条形图比例

时间:2015-05-21 19:04:18

标签: javascript d3.js

我在D3.js上有这个条形图...它工作正常..

但是我的规模存在问题......当数据系列的值比其他数据更严重时,<rect>不符合规模。 知道如何解决这个问题吗?

以下是代码:

var data = [
{"Anio":"1999","CONTRAVENCIONAL":"78484","PENAL":"0","FALTAS":"0","MULTAS":"0","OTROS":"0","TOTAL":"78484"},
{"Anio":"2000","CONTRAVENCIONAL":"92879","PENAL":"0","FALTAS":"0","MULTAS":"0","OTROS":"0","TOTAL":"92879"},
{"Anio":"2001","CONTRAVENCIONAL":"100018","PENAL":"0","FALTAS":"1818","MULTAS":"0","OTROS":"0","TOTAL":"101836"},
{"Anio":"2002","CONTRAVENCIONAL":"101380","PENAL":"0","FALTAS":"3692","MULTAS":"0","OTROS":"0","TOTAL":"105072 "},
{"Anio":"2003","CONTRAVENCIONAL":"86791","PENAL":"0","FALTAS":"7417","MULTAS":"0","OTROS":"0","TOTAL":"94208"},
{"Anio":"2004","CONTRAVENCIONAL":"47870","PENAL":"255","FALTAS":"1105","MULTAS":"1811","OTROS":"0","TOTAL":"51041"},
{"Anio":"2005","CONTRAVENCIONAL":"33013","PENAL":"348","FALTAS":"1473","MULTAS":"634","OTROS":"0","TOTAL":"35468"},
];

var margin = {top: 20, right: 30, bottom: 30, left: 40},
width = 860 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;

var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);

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

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

var yAxis = d3.svg.axis()
.scale(y)
.orient("left");

var arr_data = [];
for (var i = 0; i < data.length; i++) {
 var o = {'name':data[i].Anio,'value':data[i].TOTAL};  
 arr_data.push(o);
};

var chart = d3.select(".chart")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom )
.append("g")
 .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

x.domain(arr_data.map(function(d) { return d.name; }));
y.domain([0, d3.max(arr_data, function(d) { return d.value; })]);

chart.append("g")
  .attr("class", "x axis")
  .attr("transform", "translate(0," + height + ")")
  .call(xAxis);

chart.append("g")
  .attr("class", "y axis")
  .call(yAxis);

var bar = chart.selectAll(".bar")
  .data(arr_data)
.enter().append("g");

bar.append("rect")
  .attr("class", "rect")
  .attr("x", function(d) { return x(d.name); })
  .attr("y", function(d) { return y(d.value) ; })
  .attr("height", function(d) { return height - y(d.value); })
  .attr("width", x.rangeBand())
  .attr("fill","#632423")
  .on('mouseover',function(d){
      var a = d3.select(this)
      .attr("fill","#733A39");

    var a = d3.select("#tooltip")
    .style("left","100px")
    .style("top","20px")
    .select("#value")
    .text(d.value);

  }).on('mouseout',function(d){
      var a = d3.select(this)
      .attr("fill","#632423"); //old color: #790018        
  });

  bar.append("text")
  .attr("class", "label")
  .text(function(d) { return d.value; })
  .attr("x", function(d) { return x(d.name)+4; })
  .attr("y", function(d) { return y(d.value)+20 ; });

1 个答案:

答案 0 :(得分:2)

您的比例实际上会为您处理具有较大值的系列,因为您将其动态设置为从0到数据中的最大值:

y.domain([0, d3.max(arr_data, function(d) { return d.value; })]);

但是,您没有将数据转换为字符串中的数字,所以现在缩放比例来自[0, 94208]而不是[0, 105072]。这是因为角色"9"大于"1"。您可以在构建arr_data时将值转换为数字来解决此问题,如下所示:

var o = {'name':data[i].Anio,'value':+data[i].TOTAL}; // <---- notice the '+'

这会产生更好看的图表: screenshot