2017-10-15 67 views
-1

我试过在选项中使用showValues:true,但没有运气。 这是图书馆http://krispo.github.io/angular-nvd3/#/ showValues适用于离散条形图,但不适用于多条形图。 This is the expected output如何在nvd3角度多边形图表中的每个条的顶部显示条的值?

+0

这不包括足够的信息,以便能够很好地解决您的问题。请阅读指南,正确格式化您的问题:https://stackoverflow.com/help/how-to-ask –

+0

尝试阅读此问题https://stackoverflow.com/questions/13203404/nvd3-stacked-bar-chart -with-discrete-values –

+0

您需要使用tickFormat格式化轴的值 –

回答

1

此代码适用于我。配置图表选项 后);

1)调用一个函数(setOnTopLabel()。

2)将此函数添加到您的js代码中。

function setOnTopLabel() { 
      $timeout(function() { 
       debugger; 
       d3.selectAll('.nv-multibar .nv-group').each(function (group) { 
        debugger; 
        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); 
           }) 
           .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); 
           }).style("fill", "black").style('stroke-width', "10").style('fill-opacity', "100"); 
        }); 
       }); 
      }, 1000); 
     } 
相关问题