Set y-axis of d3 chart to fit widest label?

后端 未结 1 429
感动是毒
感动是毒 2021-02-08 03:23

I\'m drawing line charts with d3 and it all works fine. However, I have to leave enough margin on the left of the chart area to fit whatever I think might be the widest y-axis t

相关标签:
1条回答
  • 2021-02-08 03:47

    You can put everything inside a g element and set transform based on the max width. Something along the lines of

    var maxw = 0;
    yAxisContainer.selectAll("text").each(function() {
        if(this.getBBox().width > maxw) maxw = this.getBBox().width;
    });
    graphContainer.attr("transform", "translate(" + maxw + ",0)");
    
    0 讨论(0)
提交回复
热议问题