Highcharts数据标签未显示在所有级别的向下钻取中

时间:2013-10-04 15:42:57

标签: highcharts

由于某种原因,Chart的dataLabels仅显示在此深入分析的底层。 dataLabels设置为'column'。但是,它们不会显示每个呈现的列。

我需要更改什么才能让dataLabels在所有级别的钻取中显示?

感谢。

请参阅下面的代码和jsfiddle:

$('document').ready(function () {
var Drilldown = (function () {
    var chart,
    colors = Highcharts.getOptions().colors;

    function setChart(name, categories, data, color, type) {
        chart.xAxis[0].setCategories(categories);
        for (var i = 0; i < chart.series.length; i++) {
           chart.series[0].remove(true);
        }
        chart.addSeries({
            name: name,
            data: data,
            color: color || 'white'
        });
    }

    function buildDrillDown() {
        var stores = ['92nd Gessner', 'Annco'],
        dayparts = ['Breakfast','Lunch','Afternoon','Dinner','Late Night'],
        categories = ['Gross Sales-06/18/2013','Qty Sold-06/18/2013','Gross Sales-06/19/2013','Qty Sold-06/19/2013'],
        name = 'Gross Sales by Store Name',
        moneyLabel = {
                        enabled: true,
                        color: '#89A54E',
                        style: {
                            fontWeight: 'bold'
                        },
                        formatter: function() {
                            return '$'+ Highcharts.numberFormat(this.y, 0);
                        }
                    },
        data = [{
                y: 9297.73,
                color: colors[2],
                drilldown: {
                    name: 'Gross Sales-06/18/2013',
                    dataLabels: moneyLabel,
                    categories: stores,
                    data: [{
                        y: 4567.05,
                        color: colors[0],
                        drilldown: {
                            name: "Gross Sales by Daypart",
                            categories: dayparts,
                            data: [310.71,1279.32,952.65,1059.91,964.46],
                            color: colors[0]
                        },
                    },
                    {
                        y: 4730.68,
                        color: colors[1],
                        drilldown: {
                            name: "Gross Sales by Daypart",
                            categories: dayparts,
                            data: [186.75,1629.05,881.34,1373.96,659.58],
                            color: colors[1]
                        },
                    }]
                }
            }];

chart = new Highcharts.Chart({
        chart: {
            renderTo: 'drilldown_display',
            type: 'column'
        },
        title: {
            text: 'Drillable Column Chart'
        },
        subtitle: {
            text: 'Click the columns to view Stores. Click again to view Dayparts.'
        },
        xAxis: {
            categories: categories
        },
        yAxis: { // Primary yAxis
          labels: {
              format: '${value}',
              style: {
                  color: '#89A54E'
              }
          },
          title: {
              text: "Gross Sales",
              style: {
                  color: '#89A54E'
              }
          }
        },
        plotOptions: {
            column: {
                dataLabels: {
                    enabled: true,
                    color: '#89A54E',
                    style: {
                        fontWeight: 'bold'
                    },
                    formatter: function() {
                        return '$' + this.y
                    }
                },
                cursor: 'pointer',
                point: {
                    events: {
                        click: function() {
                            var drilldown = this.drilldown;
                            if (drilldown) { // drill down
                                setChart(drilldown.name, drilldown.categories, drilldown.data, drilldown.color);
                            } else { // restore
                                setChart(name, categories, data);
                            }
                        }
                    }
                }
            },
        },
        tooltip: {
            formatter: function() {
                var point = this.point,
                    s = this.x +':<b>Gross Sales: ' + this.y + '</b><br/>';
                if (point.drilldown) {
                    s += 'Click to view '+ point.category;
                } else {
                    s += 'Click to return to the beginning';
                }
                return s;
            }
        },
        series: [{
            name: name,
            data: data,
            color: 'white',
            pointWidth: 75,
        }],
        exporting: {
            buttons: {
                customButton: {
                    text: 'Flat Chart',
                    onclick: function () {
                      $( "#chart_display" ).show();
                      $( "#drilldown_display").hide();
                      $("#pie_display").hide();
                    }
                }
            }
        }
    });
    }

    return {
        buildDrillDown: buildDrillDown
    };
})();

Drilldown.buildDrillDown();

});

http://jsfiddle.net/K9fQU/

1 个答案:

答案 0 :(得分:3)

实际上你的代码有点工作。数据标签行为不一致。当我在你的小提琴中玩耍时,数据标签肯定会在enabled: true中注释掉dataLabels并运行后消失。但是,如果我将其添加回来并再次运行,则数据标签会显示,隐藏在绘图区域内。由于数据标签使用与第一级列相同的颜色#89A54E,因此不太明显。

数据标签隐藏在列中的原因是您没有配置为处理数据标签为overflow时的情况。

  

如何处理在绘图区域外流动的数据标签。默认值为justify,它将它们在绘图区域内对齐。对于柱和条,这意味着它将在条内移动。要在绘图区域外显示数据标签,请将crop设置为false并溢出为“none”。默认为合理。

所以你需要做的是,将crop设置为false并溢出为“none”:

plotOptions: {
    column: {
        dataLabels: {
            overflow: 'none',
            crop: false,
            enabled: true,
            color: '#89A54E',
            style: {
                fontWeight: 'bold'
            },
            formatter: function() {
                return '$' + this.y
            }
         },
         // other configuration
}