【问题标题】:Append multiple data arrays to data points将多个数据数组附加到数据点
【发布时间】:2018-08-11 13:56:21
【问题描述】:

我有两个相互补充的数据数组。一个数组保存坐标,而另一个数组保存该团队的唯一数据。

我的想法是,当单击数据点时,该数组的索引 0 会打印到屏幕底部的(文本)框。然后,当单击该数据点时,它还会在其团队名称下方显示辅助数组的内容。但是,据我所知,您不能在同一个变量中使用两个数据集。

我不是在寻找给我的答案,而是当想要在同一个元素上使用多个数据集时,D3 的一些知识和方向会起作用。

var dataPoints = [["Arsenal",-0.0032967741593940836, 0.30399753945657115],["Chelsea", 0.2752159801936051, -0.0389675484210763], ["Liverpool",-0.005096951348655329, 0.026678627680541075], ["Manchester City",-0.004715381791104284, -0.12338379196523988], ["Manchester United",0.06877966010653305, -0.0850615090351779], ["Tottenham",-0.3379518099485709, -0.09933664174939877]];

var teamData = [["Arsenal", "a", "b", "c", "d", "e"], ["Chelsea", "f", "g", "h", "i", "j"], ["Liverpool", "l", "m", "m", "o", "p"], ["Manchester City", "q", "r", "s", "t", "u"], ["Manchester United", "v", "w", "x", "y", "z"], ["Tottenahm", “1", "2", "3", "4", "5"]];

var circles = svg.selectAll("circle")
    .data(data)
    .enter()
    .append("circle")
    .attr("r", 7)
    .attr("cx", function(d) { return xScale(d[1]); })
    .attr("cy", function(d) { return yScale(d[2]); })
    .on('click', function(d, i) {
        console.log("click", d[0]);
    })
    .attr("fill", function(d) {
        var result = null;

        if (data.indexOf(d) >= 0) {
            result = colours(d);
        } else {
            result = "white";
        }
        return result;               
    });

var textBox = svg.append("g")
        .attr("transform", "translate(5,385)");

    textBox.append("rect")
        .attr("height", 150)
        .attr("width", 509)
        .style("stroke", bordercolor)
        .style("fill", "none")
        .style("stroke-width", border);

    circles.on("click", function(d) {
        textBox.append("text")
        .attr("x", 10)
        .attr("y", 20)
        .text(d[0])
    })

【问题讨论】:

    标签: javascript arrays d3.js


    【解决方案1】:

    这里适当的解决方案是合并两个数据集,因此每个元素都有一个完整的数据。

    但是,一个简单的解决方法是根据另一个数组过滤一个数组。例如,根据dataPoints 数组过滤teamData 数组并记录结果:

    function filter(d) {
        console.log(teamData.filter(e => e[0]===d[0])[0])
    }
    

    这是一个简单的演示,段落是使用dataPoints 数组作为数据创建的。点击他们获取teamData数组上对应的队伍:

    var dataPoints = [
      ["Arsenal", -0.0032967741593940836, 0.30399753945657115],
      ["Chelsea", 0.2752159801936051, -0.0389675484210763],
      ["Liverpool", -0.005096951348655329, 0.026678627680541075],
      ["Manchester City", -0.004715381791104284, -0.12338379196523988],
      ["Manchester United", 0.06877966010653305, -0.0850615090351779],
      ["Tottenham", -0.3379518099485709, -0.09933664174939877]
    ];
    
    var teamData = [
      ["Arsenal", "a", "b", "c", "d", "e"],
      ["Chelsea", "f", "g", "h", "i", "j"],
      ["Liverpool", "l", "m", "m", "o", "p"],
      ["Manchester City", "q", "r", "s", "t", "u"],
      ["Manchester United", "v", "w", "x", "y", "z"],
      ["Tottenahm", "1", "2", "3 ", "4", "5"]
    ];
    
    d3.select("body").selectAll(null)
      .data(dataPoints)
      .enter()
      .append("p")
      .text(d => d[0])
      .on("click", filter);
    
    function filter(d) {
      console.log(teamData.filter(e => e[0] === d[0])[0])
    }
    <script src="https://d3js.org/d3.v5.min.js"></script>

    【讨论】:

    • 这很有趣...你介意告诉我这个console.log(teamData.filter(e => e[0] === d[0])[0]) 的语法吗?特别是末尾的[0]。那如何返回数组的内容?其次,您是否有任何有效合并这些数据的参考资料?
    • Filter 返回一个数组,所以使用[0] 我们得到数组内的对象。对于合并数组,这里有几个堆栈溢出的 Q/A,只需使用 javascript 搜索,无需 D3。最后,这取决于您想要的结果。
    • 玩过这个解决方案后,它确实有效。但是在自定义显示的内容时,这让我的生活变得更加困难。当它返回对象时,我不能使用 join() 方法来更改每个索引之间的逗号。也不使用 dataPoints 的 index[0] 作为文本框中的标题。不过谢谢
    • 抱歉,join() 方法确实有效(我忘记保存它的状态) - 干杯
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多