【问题标题】:How to display values on top of nvd3 multi par graph?如何在 nvd3 多参数图上显示值?
【发布时间】:2013-04-20 13:26:57
【问题描述】:

我希望每个条的实际值以 here 的显示方式显示在顶部

我正在 multi bar chart 上尝试这个。

在任何地方都找不到参考。

【问题讨论】:

    标签: javascript d3.js nvd3.js


    【解决方案1】:

    How to display values in Stacked Multi-bar chart - nvd3 Graphs 的副本

    您可以在https://gist.github.com/topicus/217444acb4204f364e46 自行实施修复

    编辑:如果 github 链接被删除,则复制代码:

    // You need to apply this once all the animations are already finished. Otherwise labels will be placed wrongly.
    
    d3.selectAll('.nv-multibar .nv-group').each(function(group){
      var g = d3.select(this);
    
      // Remove previous labels if there is any
      g.selectAll('text').remove();
      g.selectAll('.nv-bar').each(function(bar){
        var b = d3.select(this);
        var barWidth = b.attr('width');
        var barHeight = b.attr('height');
    
        g.append('text')
          // Transforms shift the origin point then the x and y of the bar
          // is altered by this transform. In order to align the labels
          // we need to apply this transform to those.
          .attr('transform', b.attr('transform'))
          .text(function(){
            // Two decimals format
            return parseFloat(bar.y).toFixed(2);
          })
          .attr('y', function(){
            // Center label vertically
            var height = this.getBBox().height;
            return parseFloat(b.attr('y')) - 10; // 10 is the label's magin from the bar
          })
          .attr('x', function(){
            // Center label horizontally
            var width = this.getBBox().width;
            return parseFloat(b.attr('x')) + (parseFloat(barWidth) / 2) - (width / 2);
          })
          .attr('class', 'bar-values');
      });
    });
    

    【讨论】:

      【解决方案2】:

      显然这还不存在。有一个问题 (https://github.com/novus/nvd3/issues/150) 已关闭,因为这(显然)难以实施。

      【讨论】:

        【解决方案3】:

        我不确定你试过这么胖,但here 中的示例非常简单。

        .showValues(true) 几乎可以解决问题。

        希望对你有帮助。

        【讨论】:

        • 谢谢兄弟.. 但我试图让它在多条形图上工作.. 我现在已经编辑了问题以明确提及这一点。
        • @azi - 您是否也尝试将该线添加到多条形图中?到目前为止你做了什么?
        • 是的,我确实尝试过..没用...我的多条形图正在生产中..只是想在顶部显示值...没有找到参考任何地方......所以没有尝试任何东西(除了你的建议)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-26
        • 1970-01-01
        • 1970-01-01
        • 2014-11-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多