如何在d3中的同一数据集中创建多个蜂群图?

时间:2019-03-25 02:00:43

标签: javascript d3.js beeswarm

我正在使用D3.js创建一个交互式图形,我试图使其类似于《纽约时报:https://archive.nytimes.com/www.nytimes.com/interactive/2013/05/25/sunday-review/corporate-taxes.html》中的这个蜂巢图。使用以下代码,当节点沿着单个轴成为单个组时,我可以对节点生成适当的蜂群(如NYT图中的“总体图片”)。

这是我的工作代码:

// index.html
    <!DOCTYPE html>
    <meta charset="utf-8">
    <style>

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

    .axis text {
      font: 10px sans-serif;
    }

    .cells path {
      fill: none;
      pointer-events: all;
    }

    .cells :hover circle {
      fill: red;
    }

    </style>
    <head>
        <title>Beeswarm Plot</title>
    </head>
    <body>
        <svg width="960" height="200"></svg>
    </body>
    <script src="https://d3js.org/d3.v4.min.js"></script>
    <script src="PapaParse-4.6.0/papaparse.js"></script>
    <script>

    var dataset = new Array();
    var sortableData = new Array();
    var authors = [];
    var magazines = [];
    var genres = [];
    var numAuthors, numMagazines, numGenres;

    d3.csv("dataset.csv", function(data){
        dataset = data;

        for(var i=0; i<dataset.length; i++){
            if(!authors.includes(dataset[i].author))
                authors.push(dataset[i].author);
            if(!magazines.includes(dataset[i].magazine))
                magazines.push(dataset[i].magazine);
            if(!genres.includes(dataset[i].genre))
                genres.push(dataset[i].genre);
        }

        sortableData = [authors.sort(), magazines.sort(), genres.sort()];
        numAuthors = sortableData[0].length;
        numMagazines = sortableData[1].length;
        numGenres = sortableData[2].length;

    });

    var svg = d3.select("svg"),
        margin = {top: 40, right: 40, bottom: 40, left: 40},
        width = svg.attr("width") - margin.left - margin.right,
        height = svg.attr("height") - margin.top - margin.bottom;

    var formatValue = d3.format(",d");

    var x = d3.scaleLinear()
        .rangeRound([0, width]);

    // Start for loop here

    var g = svg.append("g")
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

    d3.csv("dataset.csv", type, function(error, data) {
      if (error) throw error;

      x.domain(d3.extent(data, function(d) { return d.year; }));

      var simulation = d3.forceSimulation(data)
          .force("x", d3.forceX(function(d) { return x(d.year); }).strength(1))
          .force("y", d3.forceY(height / 2))
          .force("collide", d3.forceCollide(16))
          .stop();

      for (var i = 0; i < 120; ++i) simulation.tick();

      g.append("g")
          .attr("class", "axis axis--x")
          .attr("transform", "translate(0,0)")
          .call(d3.axisTop(x).ticks(6).tickFormat(d3.format("d")));

      var cell = g.append("g")
          .attr("class", "cells")
        .selectAll("g").data(d3.voronoi()
            .extent([[-margin.left, -margin.top], [width + margin.right, height + margin.top]])
            .x(function(d) { return d.x; })
            .y(function(d) { return d.y; })
          .polygons(data)).enter().append("g");

      cell.append("circle")
          .attr("r", 15)
          .attr("cx", function(d) { return d.data.x; })
          .attr("cy", function(d) { return d.data.y; });

      cell.append("path")
          .attr("d", function(d) { return "M" + d.join("L") + "Z"; });

      cell.append("title")
          .text(function(d) { return d.data.title + "\n" + formatValue(d.data.year); });
    });

    function type(d) {
      if (!d.year) return;
      d.year = +d.year;
      return d;
    }

    </script>

dataset.csv中的每个节点代表来自旧科幻/幻想杂志的故事,其属性包括标题,年份,作者和类型:

//dataset.csv
title,year,author,magazine,genre
    "Nightfall",1941,"Asimov","Astounding","Sci-Fi"
    "Marooned Off Vesta",1939,"Asimov","Amazing","Sci-Fi"
    "Homo Sol",1940,"Asimov","Astounding","Sci-Fi"
    "The Word of Unbinding",1964,"Le Guin","Fantastic","Fantasy"
    "Robbie",1939,"Asimov","Astounding","Sci-Fi"

我的工作代码沿单个水平轴绘制所有节点,并按年份对它们进行排序。但是,我也希望能够按作者对节点进行排序,方法是将整个视图划分为单独的蜂群图,每个图仅绘出某个作者的故事(类似于NYT示例中的“按行业查看”) 。我将如何去做?

0 个答案:

没有答案