【问题标题】:D3 Stacked Bar Chart with 3 columns of data具有 3 列数据的 D3 堆积条形图
【发布时间】:2021-12-26 23:39:37
【问题描述】:

我正在尝试绘制这组数据(显示更大集合的 sn-p)link to photo of chart shown below

month_started Member_casual count_of_rides

7 休闲 435,289

8 休闲 407,009

9 成员 385,843

8 名成员 385,305

7 成员 373,710

如您所见,我有一个映射到成员或临时属性的 month_started。我想将 month_started 作为自变量 (x) 和 count_of_rides 作为因变量 (y) 制作一个质押条形图,其中每月堆叠在成员之上。

我目前有一个 d3 条形图,将 month_started = 7 叠加在一起,而不是堆叠在一起。我不确定如何让 d3 识别它需要读取 member_casual 变量以分隔两个值。

我有额外的代码,但这里是我相信的相关部分

另外,我认为 .data(bike_trips) 应该是 .data(stackedData) 但由于某种原因没有显示任何条形,这让我认为我的错误可能与stakedData 变量有关。

谁能指点我正确的方向

bike_trip_chart = {
  const svg = d3.create("svg")
  .attr("viewBox", [0, 0, width, height]);

 svg.append("g")
      .attr("fill-opacity", .8)
    .selectAll("rect")
    .data(bike_trips)
    .join("rect")
      .attr("fill", (d) => colorScale(d.key))
      .attr("x", d => x(d.month_started))
      .attr("width", x.bandwidth())
      .attr("y", d => y1(d.count_of_rides))
      .attr("height", d => y1(0) - y1(d.count_of_rides));

  svg.append("path")
      .attr("fill", "none")
      .attr("stroke", "#274e13")
      .attr("stroke-miterlimit", 4)
      .attr("stroke-width", 4)
      .attr("d", line(chicago_weather));

  svg.append("g")
      .attr("fill", "none")
      .attr("pointer-events", "all")
    .selectAll("rect")
    .data(bike_trips)
    .join("rect")
      .attr("x", d => x(d.month_started))
      .attr("width", x.bandwidth())
      .attr("y", 0)
      .attr("height", height);

  svg.append("g")
      .call(xAxis);

  svg.append("g")
      .call(y1Axis);

  svg.append("g")
      .call(y2Axis);

  return svg.node();
}

stackedData = d3.stack()
    .keys(["member","casual"])
    (bike_trips)

colorScale = d3.scaleOrdinal()
  .domain(["member","casual"])
  .range(["#E4BA14","#45818e"]);

【问题讨论】:

    标签: d3.js bar-chart stacked-chart stackedbarseries


    【解决方案1】:

    在将数据传递给 d3.stack() 之前,您希望它看起来像这样:

    [
      {month: 7, casual: 435289, member: 373710},
      {month: 8, casual: 407009, member: 385305},
      {month: 9, member: 385843}
    ]
    

    这是一个完整的例子:

    <!DOCTYPE html>
    <html>
    
    <head>
      <meta charset="UTF-8">
      <script src="https://d3js.org/d3.v7.js"></script>
    </head>
    
    <body>
      <div id="chart"></div>
    
      <script>
        // set up
    
        const margin = { top: 25, right: 25, bottom: 50, left: 50 };
    
        const width = 500 - margin.left - margin.right;
        const height = 500 - margin.top - margin.bottom;
    
        const svg = d3.select('#chart')
          .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})`);
    
        // data
    
        const data = [
          { month_started: 7, member_casual: "casual", count_of_rides: 435289 },
          { month_started: 8, member_casual: "casual", count_of_rides: 407009 },
          { month_started: 9, member_casual: "member", count_of_rides: 385843 },
          { month_started: 8, member_casual: "member", count_of_rides: 385305 },
          { month_started: 7, member_casual: "member", count_of_rides: 373710 },
        ];
    
        const keys = ["member", "casual"];
        const months = Array.from(new Set(data.map(d => d.month_started))).sort(d3.ascending);
    
    
        // get a map from the month_started to the member_casual to the count_of_rides
        const monthToTypeToCount = d3.rollup(
          data,
          // g is an array that contains a single element
          // get the count for this element
          g => g[0].count_of_rides,
          // group by month first
          d => d.month_started,
          // then group by member of casual
          d => d.member_casual
        );
    
        // put the data in the format mentioned above
        const countsByMonth = Array.from(monthToTypeToCount, ([month, counts]) => {
          // counts is a map from member_casual to count_of_rides
          counts.set("month", month);
          counts.set("total", d3.sum(counts.values()));
          // turn the map into an object
          return Object.fromEntries(counts);
        });
    
        const stackedData = d3.stack()
            .keys(keys)
            // return 0 if a month doesn't have a count for member/casual
            .value((d, key) => d[key] ?? 0)
            (countsByMonth);
    
        // scales
    
        const x = d3.scaleBand()
            .domain(months)
            .range([0, width])
            .padding(0.25);
    
        const y = d3.scaleLinear()
            .domain([0, d3.max(countsByMonth, d => d.total)])
            .range([height, 0]);
    
        const color = d3.scaleOrdinal()
            .domain(keys)
            .range(["#E4BA14","#45818e"]);
    
        // axes
    
        const xAxis = d3.axisBottom(x);
    
        svg.append('g')
            .attr('transform', `translate(0,${height})`)
            .call(xAxis)
    
        const yAxis = d3.axisLeft(y);
    
        svg.append('g')
            .call(yAxis);
    
        // draw bars
    
        const groups = svg.append('g')
          .selectAll('g')
          .data(stackedData)
          .join('g')
            .attr('fill', d => color(d.key));
    
        groups.selectAll('rect')
          .data(d => d)
          .join('rect')
            .attr('x', d => x(d.data.month))
            .attr('y', d => y(d[1]))
            .attr('width', x.bandwidth())
            .attr('height', d => y(d[0]) - y(d[1]));
    
        // title
    
        svg.append('g')
            .attr('transform', `translate(${width / 2},${-10})`)
            .attr('font-family', 'sans-serif')
          .append('text')
            .attr('text-anchor', 'middle')
            .call(
              text => text.append('tspan')
                .attr('fill', color('member'))
                .text('member')
            )
            .call(
              text => text.append('tspan')
                .attr('fill', 'black')
                .text(' vs. ')
            )
            .call(
              text => text.append('tspan')
                .attr('fill', color('casual'))
                .text('casual')
            )
      </script>
    </body>
    
    </html>

    【讨论】:

    • 谢谢我最终只是像这样偏移 y 轴:.attr("y", d => y1(d.count_of_rides + (bike_trips_casual.find(item => item.month_started == d .month_started).count_of_rides))) .attr("身高", d => y1(0) - y1(d.count_of_rides));
    猜你喜欢
    • 1970-01-01
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    • 2019-06-19
    相关资源
    最近更新 更多