Javascript 使用D3为气泡图创建图例

Javascript 使用D3为气泡图创建图例,javascript,d3.js,bubble-chart,Javascript,D3.js,Bubble Chart,我一直在努力学习D3(特别是气泡图)。我在这里创建了一个非常简单的图表: //This is making the demensions for the circles and canvas //The format and color is for hovering over the circles for the dialog box var diameter = 1500, format = d3.format(",d"), color =

我一直在努力学习D3(特别是气泡图)。我在这里创建了一个非常简单的图表:

//This is making the demensions for the circles and canvas
    //The format and color is for hovering over the circles for the dialog box
    var diameter = 1500,
        format = d3.format(",d"),
        color = d3.scale.category20c();

    //This makes the canvas that the bubble chart will be made on
    var canvas = d3.select("body").append("svg")
                                  .attr("width", diameter)
                                  .attr("height", diameter)
                                  .append("g")
                                    .attr("transform", "translate(50,50)");

    //This is the size of the actual bubbles in the chart
    var pack = d3.layout.pack()
            .size([900, 900])
            .padding(85);

    //This is adding the data into the bubbles and creating their size
    d3.json("CountryData.js", function (data) {
        var nodes = pack.nodes(data);

        var node = canvas.selectAll(".node")
                    .data(nodes)
                    .enter()
                    .append("g")
                        .attr("class", "node")
                        .attr("transform", function (d) { return "translate(" + d.x + "," + d.y + ")"; });

        //This is the actual bubbles
        node.append("circle")
            .attr("r", function (d) {return d.r;})
            .style("fill", function (d, i) { return color(i); });


        //This is the text inside the bubbles
        node.append("text")
            .text(function (d) { return d.children ? "" : d.name; })
            .style("text-anchor", "middle")

        //This is the dialog box
        node.append("title")
            .text(function (d) { return d.name + ": " + format(d.value) + "" + "medals"; });


    });
我一直在尝试添加一个图例,显示“亚洲”、“北美”、“中东”等及其颜色。我得到的最接近的结果是显示带有颜色的框,并显示颜色名称。我试着用这个:作为参考。我可以附上我是如何尝试制作这个传奇的,但我觉得这对于一篇文章来说太多了。以下是我的数据:

{
"name" : "Region Olympic Medals",
"value" : 10000,
"children" : [
    {
        "name" : "Asia",
        "value" : 451,
        "children" : [
            {"name" : "Summer Gold", "value" : 114},
            {"name" : "Summer Silver", "value" : 172},
            {"name" : "Summer Bronze", "value" : 158},
            {"name" : "Winter Gold", "value" : 1},
            {"name" : "Winter Silver", "value" : 1},
            {"name" : "Winter Bronze", "value" : 2}
        ]
    },
    {
        "name" : "Australia & Oceaina",
        "value" : 26,
        "children" : [
            {"name" : "Summer Gold", "value" : 4},
            {"name" : "Summer Silver", "value" : 7},
            {"name" : "Summer Bronze", "value" : 15},
            {"name" : "Winter Gold", "value" : 0},
            {"name" : "Winter Silver", "value" : 0},
            {"name" : "Winter Bronze", "value" : 0}
        ]
    },
    {
        "name" : "Caribbean",
        "value" : 1136,
        "children" : [
            {"name" : "Summer Gold", "value" : 306},
            {"name" : "Summer Silver", "value" : 319},
            {"name" : "Summer Bronze", "value" : 381},
            {"name" : "Winter Gold", "value" : 42},
            {"name" : "Winter Silver", "value" : 41},
            {"name" : "Winter Bronze", "value" : 47}
        ]
    },
    {

            "name" : "Central America",
            "value" : 26,
            "children" : [
                {"name" : "Summer Gold", "value" : 4},
                {"name" : "Summer Silver", "value" : 3},
                {"name" : "Summer Bronze", "value" : 4},
                {"name" : "Winter Gold", "value" : 0},
                {"name" : "Winter Silver", "value" : 0},
                {"name" : "Winter Bronze", "value" : 0}
        ]
    },
    {

        "name" : "Europe",
        "value" : 7264,
        "children" : [
            {"name" : "Summer Gold", "value" : 1924},
            {"name" : "Summer Silver", "value" : 1993},
            {"name" : "Summer Bronze", "value" : 2257},
            {"name" : "Winter Gold", "value" : 362},
            {"name" : "Winter Silver", "value" : 366},
            {"name" : "Winter Bronze", "value" : 362}
        ]
    },
    {

        "name" : "Middle East",
        "value" : 2696,
        "children" : [
            {"name" : "Summer Gold", "value" : 280},
            {"name" : "Summer Silver", "value" : 331},
            {"name" : "Summer Bronze", "value" : 361},
            {"name" : "Winter Gold", "value" : 535},
            {"name" : "Winter Silver", "value" : 582},
            {"name" : "Winter Bronze", "value" : 607}
        ]
    },
    {

        "name" : "North America",
        "value" : 7,
        "children" : [
            {"name" : "Summer Gold", "value" : 4},
            {"name" : "Summer Silver", "value" : 1},
            {"name" : "Summer Bronze", "value" : 2},
            {"name" : "Winter Gold", "value" : 0},
            {"name" : "Winter Silver", "value" : 0},
            {"name" : "Winter Bronze", "value" : 0}
        ]
    },
    {

        "name" : "South America",
        "value" : 6273,
        "children" : [
            {"name" : "Summer Gold", "value" : 1220},
            {"name" : "Summer Silver", "value" : 1014},
            {"name" : "Summer Bronze", "value" : 335},
            {"name" : "Winter Gold", "value" : 1421},
            {"name" : "Winter Silver", "value" : 1193},
            {"name" : "Winter Bronze", "value" : 1090}
        ]
    },
    {

        "name" : "Sub-Saharan Africa",
        "value" : 1847,
        "children" : [
            {"name" : "Summer Gold", "value" : 485},
            {"name" : "Summer Silver", "value" : 549},
            {"name" : "Summer Bronze", "value" : 628},
            {"name" : "Winter Gold", "value" : 50},
            {"name" : "Winter Silver", "value" : 59},
            {"name" : "Winter Bronze", "value" : 76}
        ]
    }
]

}

要创建图例,您可以根据数据是否有d.子项筛选数据

请注意,我将索引保留在原始数据集中
d.original\u index
用于
color
属性

其次,如您引用的[示例]所示,我们可以在新的
元素中
enter()
我们的图例数据,根据需要调整x和y

var legend = canvas.append("g")
    .attr("class", "legend")
    .selectAll("g")
    .data(legend_data)
    .enter()
        .append("g")
        .attr("transform", function(d,i){
            return "translate(" + d.depth*10 + "," + i*20 + ")"; 
          })

// Draw rects, and color them by original_index
legend.append("rect")
    .attr("width", 8)
    .attr("height", 8)
    .style("fill", function(d){return color(d.original_index)});

legend.append("text")
    .attr("x", function(d,i){ return d.depth*10 +10;})
    .attr("dy", "0.50em")
    .text(function(d){return d.name;})
请注意,我使用变量
d.depth
来调整x方向,以便在图例中显示数据层次结构

这是一个包含您的数据的列表。
如果还有什么不清楚的地方,请告诉我。

此外,我在代码中的注释可能不正确。如果没有,请让我知道,以便我可以更改它们。=)我有一个关于这个答案的问题,更具体地说是“d.orginal_索引”。当我将其用于另一个图表时,图例会显示出来,但颜色是错误的。对于新图表,我使用的是类别10,而不是20c。如何更改图例颜色以匹配新的颜色类别?
var legend = canvas.append("g")
    .attr("class", "legend")
    .selectAll("g")
    .data(legend_data)
    .enter()
        .append("g")
        .attr("transform", function(d,i){
            return "translate(" + d.depth*10 + "," + i*20 + ")"; 
          })

// Draw rects, and color them by original_index
legend.append("rect")
    .attr("width", 8)
    .attr("height", 8)
    .style("fill", function(d){return color(d.original_index)});

legend.append("text")
    .attr("x", function(d,i){ return d.depth*10 +10;})
    .attr("dy", "0.50em")
    .text(function(d){return d.name;})