【问题标题】:Show labels inside each bubble in dimplejs bubble chart在dimplejs气泡图中的每个气泡内显示标签
【发布时间】:2014-12-06 21:09:19
【问题描述】:

我正在使用 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();

【问题讨论】:

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


    【解决方案1】:

    查看您的 JSFiddle,有一个接近正确答案的注释块,但是您错过了几件事。首先,圆圈使用 cx 和 cy 属性而不是 x 和 y,因此您需要参考这些属性的位置。其次,您在 draw 方法之前调用了 s.shapes.each,并且该系列的 shapes 属性仅在绘制后填充。您可以在 draw 方法之后移动块,但我已将其切换为使用 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/

    【讨论】:

    • 注意,这个答案需要你升级到dimple.js version 2+
    • 另外,这里有一个quick update,用于在气泡中显示“所有者”而不是“大小”。
    • 感谢约翰和马克!还有一件事,我如何找到气泡的半径,因为我想将文本放在气泡之外,以防气泡小于文本...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-15
    • 2019-08-08
    相关资源
    最近更新 更多