【问题标题】:How to show multi data Labels on group column highcharts如何在组列高图中显示多数据标签
【发布时间】:2016-07-15 07:14:49
【问题描述】:

我有想要在列上显示的数据,如下所示:

我尝试了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}]
        }]
    });
});

【问题讨论】:

  • 我觉得没问题,用谷歌浏览器试试?
  • 您应该在此处包含您的代码,而不仅仅是链接它。
  • 谢谢!我包含代码,可以给我一些建议。

标签: css


【解决方案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>';
      }

    }

【讨论】:

  • 感谢 Cheralatan!这正是我想要的。但是,如果我像jsfiddle.net/7bzy06gt/2 那样编辑数据,div 的样式:class="datalablelInside",值 this.y 不会显示。你能给我那个div的css解决方案吗!谢谢!
  • 嗨 Cheralathan,有一个小问题,我看到了,datalabel 的值在工具提示上被覆盖,这对我很不利。我试图修复但它失败了。如何解决?谢谢!
  • 你好 sinh,我在工具提示对象上添加了定位器属性。请看小提琴。
  • 非常好,非常感谢 Cheralathan!
  • 嗨 Cheralathan,我也有同样的问题,我在 jsfiddle.net/SpzBa/23 中尝试了这段代码,但列顶部的百分比值错误,(百分比的这个值是数据中 y 的值)如何我可以解决这个问题吗?
猜你喜欢
  • 1970-01-01
  • 2016-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多