简体   繁体   English

如何通过变量分配动态x轴值和y轴?

[英]How to assign dynamic x axis value and y axis from variable?

Each time i am sending different data to d3js chart. 每次我向d3js图表发送不同的数据时。 so each time user will select x axis and y axis in chart. 因此,每次用户将在图表中选择x轴和y轴。

I have created a sample example in which my chart is not working how to us variable in it. 我创建了一个示例示例,其中我的图表无法正常运行,如何在其中进行变量设置。

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');

}

I have found the solution some how it could be possibly this way 我已经找到解决方案的某种方式

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');

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM