在dimplejs气泡图中显示每个气泡内的标签

时间:2014-12-06 21:09:20

标签: javascript d3.js data-visualization dimple.js

我正在使用dimplejs制作气泡图。那部分完成了。我还想在泡泡中添加所有者名称(气泡标签),而不仅仅是图表外的列表,但这不起作用。我该怎么办呢?

Link to the jsfiddle

all_data = [{"Size": 126, "x": 0.57713748637776141, "y": -0.23994977513487858, "Owner": "a"}, {"Size": 394, "x": -0.23305704511539499, "y": -0.39183313136189662, "Owner": "b"}, {"Size": 127, "x": 0.12093459993205866, "y": 0.56390700220806189, "Owner": "c"}, {"Size": 624, "x": -0.46501504119442505, "y": 0.06787590428871329, "Owner": "d"}] 

var svg = dimple.newSvg("#chartContainer", 600, 200);

var myChart = new dimple.chart(svg, all_data);
myChart.setBounds(25, 25, 550, 150)

var x = myChart.addMeasureAxis("x", "x");
var y = myChart.addMeasureAxis("y", "y");
x.hidden = true;
y.hidden = true;

myChart.addLegend(25, 5, 550, 20, "right");
myChart.addMeasureAxis("z", "Size");

var s = myChart.addSeries("Owner", dimple.plot.bubble);

myChart.draw();

1 个答案:

答案 0 :(得分:2)

看着你的JSFiddle,有一个评论块接近正确的答案,但你错过了几件事。首先,圆圈使用cx和cy属性而不是x和y,因此您需要引用那些位置。其次,您在draw方法之前调用s.shapes.each,并且系列的shapes属性仅在绘制后填充。您可以在绘制方法之后移动块,但我已将其切换为使用series.afterDraw属性:

s.afterDraw = function (shp, d) {
    var shape = d3.select(shp);
    svg.append("text")
        .attr("x", parseFloat(shape.attr("cx")))
        .attr("y", parseFloat(shape.attr("cy")) + 4)
        .style("text-anchor", "middle")
        .style("font-size", "10px")
        .style("font-family", "sans-serif")
        .style("opacity", 0.7)
        .text(d.zValue);
};

您的更新小提琴在这里:http://jsfiddle.net/acjwqpsL/2/