如何从变量中分配动态x轴值和y轴?

时间:2014-09-15 12:24:54

标签: javascript jquery d3.js

每次我向d3js图表发送不同的数据。所以每次用户都会在图表中选择x轴和y轴。

我创建了一个示例示例,其中我的图表无法正常工作,如何使用变量。

var barData=  [ { "Name":"Liu, Laurin", "Age":22, "Gender":"Female"},
{ "Name":"Mourani, Maria", "Age":43, "Gender":"Female"},
{ "Name":"Sellah, Djaouida", "Gender":"Female"},
{ "Name":"St-Denis, Lise", "Age":72, "Gender":"Female"},
{ "Name":"Fry, Hedy", "Age":71, "Gender":"Female"}]
    var xs = "Name";
    var ys = "Age";
     var vis = d3.select('#visualisation'),
        WIDTH = 600,
        HEIGHT = 400,
        MARGINS = {
          top: 20,
          right: 20,
          bottom: 20,
          left: 30
        },
        xRange = d3.scale.ordinal().rangeRoundBands([MARGINS.left, WIDTH - MARGINS.right], 0.5).domain(barData.map(function (d) {
          return d.xs;
        })),


        yRange = d3.scale.linear().range([HEIGHT - MARGINS.top, MARGINS.bottom]).domain([0,
          d3.max(barData, function (d) {
            return d.ys;
          })
        ]),

        xAxis = d3.svg.axis()
          .scale(xRange)
          .tickSize(5)
          .tickSubdivide(true),

        yAxis = d3.svg.axis()
          .scale(yRange)
          .tickSize(5)
          .orient("left")
          .tickSubdivide(true);


    var xd =  vis.append('svg:g')
        .attr('class', 'x axis')
        .attr('transform', 'translate(0,' + (HEIGHT - MARGINS.bottom) + ')')
        .call(xAxis);
    xd.selectAll("text")
    .attr("dx", "-1em")
         .attr("dy", "2em")
    .attr({
        transform: function (d) {
            return "rotate(-30, 0, 0)";
        }
    });
      vis.append('svg:g')
        .attr('class', 'y axis')
        .attr('transform', 'translate(' + (MARGINS.left) + ',0)')
        .call(yAxis);

      vis.selectAll('rect')
        .data(barData)
        .enter()
        .append('rect')
        .attr('x', function (d) {
          return xRange(d.xs);
        })
        .attr('y', function (d) {
          return yRange(d.ys);
        })
        .attr('width', xRange.rangeBand())
        .attr('height', function (d) {
          return ((HEIGHT - MARGINS.bottom) - yRange(d.ys));
        })
        .attr('fill', 'steelblue');

}

1 个答案:

答案 0 :(得分:0)

我已经找到了一些解决方案,可能就是这样

var barData=  [ { "Name":"Liu, Laurin", "Age":22, "Gender":"Female"},
{ "Name":"Mourani, Maria", "Age":43, "Gender":"Female"},
{ "Name":"Sellah, Djaouida", "Gender":"Female"},
{ "Name":"St-Denis, Lise", "Age":72, "Gender":"Female"},
{ "Name":"Fry, Hedy", "Age":71, "Gender":"Female"}]
var xs = "Name";
var ys = "Age";
 var vis = d3.select('#visualisation'),
    WIDTH = 600,
    HEIGHT = 400,
    MARGINS = {
      top: 20,
      right: 20,
      bottom: 20,
      left: 30
    },
    xRange = d3.scale.ordinal().rangeRoundBands([MARGINS.left, WIDTH - MARGINS.right], 0.5).domain(barData.map(function (d) {
      return d[xs];
    })),


    yRange = d3.scale.linear().range([HEIGHT - MARGINS.top, MARGINS.bottom]).domain([0,
      d3.max(barData, function (d) {
        return d[ys];
      })
    ]),

    xAxis = d3.svg.axis()
      .scale(xRange)
      .tickSize(5)
      .tickSubdivide(true),

    yAxis = d3.svg.axis()
      .scale(yRange)
      .tickSize(5)
      .orient("left")
      .tickSubdivide(true);


var xd =  vis.append('svg:g')
    .attr('class', 'x axis')
    .attr('transform', 'translate(0,' + (HEIGHT - MARGINS.bottom) + ')')
    .call(xAxis);
xd.selectAll("text")
.attr("dx", "-1em")
     .attr("dy", "2em")
.attr({
    transform: function (d) {
        return "rotate(-30, 0, 0)";
    }
});
  vis.append('svg:g')
    .attr('class', 'y axis')
    .attr('transform', 'translate(' + (MARGINS.left) + ',0)')
    .call(yAxis);

  vis.selectAll('rect')
    .data(barData)
    .enter()
    .append('rect')
    .attr('x', function (d) {
      return xRange(d[xs]);
    })
    .attr('y', function (d) {
      return yRange(d[ys]);
    })
    .attr('width', xRange.rangeBand())
    .attr('height', function (d) {
      return ((HEIGHT - MARGINS.bottom) - yRange(d[ys]));
    })
    .attr('fill', 'steelblue');

}