2016-07-15 71 views
0

我有,我想這樣的顯示在列中的數據:如何顯示在分組列highcharts多數據標籤

enter image description here

我試圖this code但它無法正常工作。我怎樣才能做到這一點?

HTML:

<script src="http://code.highcharts.com/highcharts.js"></script> 
<script src="http://code.highcharts.com/modules/exporting.js"></script> 

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div> 

的Javascript:

$(function() { 
    $('#container').highcharts({ 
     chart: { 
      type: 'column' 
     }, 
     title: { 
      text: 'Column chart' 
     }, 
     xAxis: { 
      categories: ['Apples', 'Oranges', 'Pears'] 
     }, 
     yAxis: { 
      min: 0, 
      title: { 
       text: 'Total fruit consumption' 
      }, 
      stackLabels: { 
       enabled: true, 
       style: { 
        fontWeight: 'bold', 
        color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray' 
       } 
      } 
     }, 
     legend: { 
      align: 'right', 
      x: -70, 
      verticalAlign: 'top', 
      y: 20, 
      floating: true, 
      backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || 'white', 
      borderColor: '#CCC', 
      borderWidth: 1, 
      shadow: false 
     }, 
     tooltip: { 
      formatter: function() { 
       var allSeries = this.series.chart.series; 
       var totalAmount = 0; 
       for(var s in allSeries) { totalAmount += allSeries[s].points[this.point.x].Amount; } 

       return '<b>'+ this.x +'</b><br/>'+ 
        this.series.name +': '+ this.y + ' (<b>$ ' + this.point.Amount +') <br/>'+ 
        'Total: '+ this.point.stackTotal + ' (<b>$ ' + totalAmount +')'; 
      } 
     }, 
     plotOptions: { 
      column: { 
       dataLabels: { 
        enabled: true, 
        color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white', 
        style: { 
         textShadow: '0 0 3px black, 0 0 3px black' 
        }, 
        formatter: function() { 
         return this.point.Amount + '%' 
        }, 

       } 
      } 
     }, 
     series: [{ 
      name: 'John', 
      data: [{y: 5, Amount: 100}, {y: 3, Amount: 60}, {y: 4, Amount: 80}] 
     }, { 
      name: 'Joe', 
      data: [{y: 3, Amount: 60}, {y: 4, Amount: 80}, {y: 4, Amount: 80}] 
     }] 
    }); 
}); 
+0

看起來還好吧對我來說,谷歌瀏覽器試試嗎? – Relisora

+0

你應該在這裏包含你的代碼,而不僅僅是鏈接它。 –

+0

謝謝!我包含代碼,可以給我一些建議。 –

回答

1

看看這個Fiddle

dataLabels: { 
     enabled: true, 
     useHTML: true, 
     color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white', 
     style: { 
     textShadow: '0 0 3px black, 0 0 3px black' 
     }, 
     formatter: function() { 
     var pointHeight = this.point.shapeArgs.height + 20; 
     return '<div class="datalabel" style="position: relative; top: 0px">' + this.point.Amount + '%' + '</div><div class="datalabelInside" style="text-shadow:none;color:#000;position: absolute; top:' + pointHeight/2 + 'px">' + this.y + '</div>'; 
     } 

    } 
+0

謝謝Cheralatan!這正是我想要的。但是,如果我編輯數據像https://jsfiddle.net/7bzy06gt/2/,風格的div:class =「datalablelInside」,值this.y不顯示。你可以給我解決方案的CSS這個div!謝謝! –

+0

看看這個小提琴https://jsfiddle.net/7bzy06gt/4/ – cheralathan

+0

它適合我!非常感謝! –