D3.js计算组平均值

时间:2016-05-09 23:10:14

标签: javascript d3.js

来自包含" name"的对象"游"和"月"我想弄清楚如何将所有"旅行"与具有相同名称的对象相关联,然后与每个名称的总游览总数相加的总和的百分比相关联。

有5个可能的名字" marriott"," springhill"等

我已经弄清楚如何找到所有旅游的总数,但现在我需要找出这5个名字中有多少旅游相关以及每个旅游的旅行占用的金额的百分比。

这里是代码和笔的链接...... http://codepen.io/mcKrack/pen/Vaggxq?editors=1111



 var outerWidth = 500;
      var outerHeight = 250;
      var margin = { left: 55, top: 5, right: 100, bottom: 60 };

      var xColumn = "month";
      var yColumn = "tours";
      var colorColumn = "name";
      var areaColumn = colorColumn;

      var xAxisLabelText = "Month";
      var xAxisLabelOffset = 48;

      var innerWidth  = outerWidth  - margin.left - margin.right;
      var innerHeight = outerHeight - margin.top  - margin.bottom;

      var svg = d3.select("body").append("svg")
        .attr("width", outerWidth)
        .attr("height", outerHeight);
      var g = svg.append("g")
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

      var xAxisG = g.append("g")
        .attr("class", "x axis")
        .attr("transform", "translate(0," + innerHeight + ")")
      var xAxisLabel = xAxisG.append("text")
        .style("text-anchor", "middle")
        .attr("transform", "translate(" + (innerWidth / 2) + "," + xAxisLabelOffset + ")")
        .attr("class", "label")
        .text(xAxisLabelText);

      var colorLegendG = svg.append("g")
        .attr("class", "color-legend")
        .attr("transform", "translate(403, 5)");

      var xScale = d3.time.scale().range([0, innerWidth]);
      var yScale = d3.scale.linear().range([innerHeight, 0]);
      var colorScale = d3.scale.category10();

      var xAxis = d3.svg.axis().scale(xScale).orient("bottom")
        .ticks(2)
        .outerTickSize(0);

      var stack = d3.layout.stack()
        .y(function (d){ return d[yColumn]; })
        .values(function (d){ return d.values; });

      var area = d3.svg.area()
        .x(function(d) { return xScale(d[xColumn]); })
        .y0(function(d) { return yScale(d.y0); })
        .y1(function(d) { return yScale(d.y0 + d.y); });

      function render(data){

        var nested = d3.nest()
          .key(function (d){ return d[areaColumn]; })
          .entries(data);

        colorScale.domain(nested.map(function (d){ return d.key; }));

        var layers = stack(nested.reverse());
        console.log(layers);

        xScale.domain(d3.extent(data, function (d){ return d[xColumn]; }));
        yScale.domain([
          0,
          d3.max(layers, function (layer){
            return d3.max(layer.values, function (d){
              return d.y0 + d.y;
            });
          })
        ]);

        var total = d3.sum(layers, function(layer) {
         return d3.sum(layer.values, function(d) {
            return d.tours;
          });
        });
        console.log(total);

        var paths = g.selectAll(".chart-area").data(layers);
        paths.enter().append("path").attr("class", "chart-line");
        paths.exit().remove();
        paths
          .attr("d", function (d){ return area(d.values); })
          .attr("fill", function (d){ return colorScale(d.key); });

        xAxisG.call(xAxis);

        svg.append("g")
        .attr("class", "legend")
          .selectAll("text")
          .data(layers)
            .enter().append("text")
              .text(function(d) { return (d.key) + ' (' + d.key + ')'; })
              .attr('fill', function(d) { return colorScale(d.key); })
              .attr('y', function(d, i) { return 20 * (i + 1); })
              .attr('x', "0");

      }

      function type(d){
        d.month = new Date(d.month);
        d.tours = +d.tours;
        return d;
      }

var toursByHotel = [
        {
          "name": "Marriott",
          "month": 1,
          "tours": 10
        },
        {
          "name": "Marriott",
          "month": 2,
          "tours": 15
        },
        {
          "name": "Marriott",
          "month": 3,
          "tours": 8
        },
        {
          "name": "Marriott",
          "month": 4,
          "tours": 12
        },
        {
          "name": "Marriott",
          "month": 5,
          "tours": 18
        },
        {
          "name": "Marriott",
          "month": 6,
          "tours": 25
        },
        {
          "name": "Marriott",
          "month": 7,
          "tours": 40
        },
        {
          "name": "Marriott",
          "month": 8,
          "tours": 33
        },
        {
          "name": "Marriott",
          "month": 9,
          "tours": 25
        },
        {
         "name": "Marriott",
          "month": 10,
          "tours": 21
        },
        {
         "name": "Marriott",
          "month": 11,
          "tours": 18
        },
        {
         "name": "Marriott",
          "month": 12,
          "tours": 14
        },
        {
          "name": "Springhill",
          "month": 1,
          "tours": 10
        },
        {
          "name": "Springhill",
          "month": 2,
          "tours": 15
        },
        {
          "name": "Springhill",
          "month": 3,
          "tours": 8
        },
        {
          "name": "Springhill",
          "month": 4,
          "tours": 12
        },
        {
          "name": "Springhill",
          "month": 5,
          "tours": 18
        },
        {
          "name": "Springhill",
          "month": 6,
          "tours": 25
        },
        {
          "name": "Springhill",
          "month": 7,
          "tours": 40
        },
        {
          "name": "Springhill",
          "month": 8,
          "tours": 33
        },
        {
          "name": "Springhill",
          "month": 9,
          "tours": 25
        },
        {
         "name": "Springhill",
          "month": 10,
          "tours": 21
        },
        {
         "name": "Springhill",
          "month": 11,
          "tours": 18
        },
        {
         "name": "Springhill",
          "month": 12,
          "tours": 14
        },
        {
          "name": "Residence",
          "month": 1,
          "tours": 10
        },
        {
          "name": "Residence",
          "month": 2,
          "tours": 15
        },
        {
          "name": "Residence",
          "month": 3,
          "tours": 8
        },
        {
          "name": "Residence",
          "month": 4,
          "tours": 12
        },
        {
          "name": "Residence",
          "month": 5,
          "tours": 18
        },
        {
          "name": "Residence",
          "month": 6,
          "tours": 25
        },
        {
          "name": "Residence",
          "month": 7,
          "tours": 40
        },
        {
          "name": "Residence",
          "month": 8,
          "tours": 33
        },
        {
          "name": "Residence",
          "month": 9,
          "tours": 25
        },
        {
         "name": "Residence",
          "month": 10,
          "tours": 21
        },
        {
         "name": "Residence",
          "month": 11,
          "tours": 18
        },
        {
         "name": "Residence",
          "month": 12,
          "tours": 14
        },
        {
          "name": "Courtyard",
          "month": 1,
          "tours": 10
        },
        {
          "name": "Courtyard",
          "month": 2,
          "tours": 15
        },
        {
          "name": "Courtyard",
          "month": 3,
          "tours": 8
        },
        {
          "name": "Courtyard",
          "month": 4,
          "tours": 12
        },
        {
          "name": "Courtyard",
          "month": 5,
          "tours": 18
        },
        {
          "name": "Courtyard",
          "month": 6,
          "tours": 25
        },
        {
          "name": "Courtyard",
          "month": 7,
          "tours": 40
        },
        {
          "name": "Courtyard",
          "month": 8,
          "tours": 33
        },
        {
          "name": "Courtyard",
          "month": 9,
          "tours": 25
        },
        {
         "name": "Courtyard",
          "month": 10,
          "tours": 21
        },
        {
         "name": "Courtyard",
          "month": 11,
          "tours": 18
        },
        {
         "name": "Courtyard",
          "month": 12,
          "tours": 14
        },
        {
          "name": "Renaissance",
          "month": 1,
          "tours": 10
        },
        {
          "name": "Renaissance",
          "month": 2,
          "tours": 15
        },
        {
          "name": "Renaissance",
          "month": 3,
          "tours": 8
        },
        {
          "name": "Renaissance",
          "month": 4,
          "tours": 12
        },
        {
          "name": "Renaissance",
          "month": 5,
          "tours": 18
        },
        {
          "name": "Renaissance",
          "month": 6,
          "tours": 25
        },
        {
          "name": "Renaissance",
          "month": 7,
          "tours": 40
        },
        {
          "name": "Renaissance",
          "month": 8,
          "tours": 33
        },
        {
          "name": "Renaissance",
          "month": 9,
          "tours": 25
        },
        {
         "name": "Renaissance",
          "month": 10,
          "tours": 21
        },
        {
         "name": "Renaissance",
          "month": 11,
          "tours": 18
        },
        {
         "name": "Renaissance",
          "month": 12,
          "tours": 14
        }
      ];

      render(toursByHotel);

.axis text {
        font-family: 'Open Sans', sans-serif;
        font-size: 12pt;
      }
      .axis .label {
        font-size: 18pt;
      }

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

<div class="chart"></div>
<script src="http://d3js.org/d3.v3.min.js"  charset="utf-8"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/d3-legend/1.1.0/d3-legend.js"></script>
&#13;
&#13;
&#13;

0 个答案:

没有答案