高图|分组堆积条形图多个堆栈标签

时间:2019-04-01 10:11:34

标签: highcharts

当前信息只是条形图的底部。但是,需要在堆叠的分组条形图中的条形两端显示信息。

我正在使用堆栈标签来显示已对齐以在条形底部显示信息的信息。

要求:堆叠的条形图的总和需要显示在顶部,而类别名称则需要保留在底部。

我用谷歌搜索了一下,但是没有帮助。

寻找解决方案。预先感谢。

JSFiddle: http://jsfiddle.net/mkdudeja/c5debf6g/6/

$(function () {
    
    var options = {
      chart: {
        type: 'column',
        backgroundColor: '#f6f6f7'
      },
      title: {
        text: ''
      },
      credits: {
        enabled: false
      },
      exporting: {
        enabled: false
      },
      legend: {
        symbolRadius: 2,
        itemStyle: {
          color: '#666',
          fontSize: '1.1rem',
          fontWeight: '400',
          fontFamily: '"Open Sans", Arial, sans-serif'
        }
      },
      xAxis: {
        categories: ['Overall', 'Timing', 'Impact'],
        labels: {
          y: 60,
          style: {
            color: '#333',
            fontSize: '1.3rem',
            fontWeight: '600',
            fontFamily: '"Open Sans", Arial, sans-serif'
          }
        }
      },
      yAxis: {
        min: 0,
        allowDecimals: false,
        lineWidth: 0,
        minorGridLineWidth: 0,
        lineColor: 'transparent',
        minorTickLength: 0,
        tickLength: 0,
        title: {
          text: ''
        },
        labels: {
          enabled: false
        },
        stackLabels: {
          enabled: true,
          verticalAlign: 'bottom',
          crop: false,
          overflow: 'none',
          y: 20,
          formatter: function () {
            const $this = this;
            return '$' + $this.total + 'M' +
              '<br>' + $this.stack;
          },
          style: {
            color: '#666',
            fontSize: '1.1rem',
            fontWeight: '400',
            fontFamily: '"Open Sans", Arial, sans-serif'
          }
        }
      },
      tooltip: {
        formatter: function () {
          const $this = this;
          return '$' + $this.y + 'M' +
            '<br>' + $this.point.params.label;
        }
      },
      plotOptions: {
        column: {
          stacking: 'normal',
          dataLabels: {
            enabled: true,
            color: 'white',
            formatter: function () {
              const $this = this;
              return (($this.y * 100) / $this.total).toFixed(1) + '%';
            }
          }
        }
      },
      series: [{
        type: undefined,
        name: 'Off Track',
        data: [
          {
            y: 12,
            params: {
              label: '12 initiatives'
            }
          },
          {
            y: 5,
            params: {
              label: '5 initiatives'
            }
          },
          {
            y: 4,
            params: {
              label: '4 initiatives'
            }
          }
        ],
        stack: 'Plan',
        color: '#b02c3b'
      }, {
        type: undefined,
        name: 'At Risk',
        data: [
          {
            y: 6,
            params: {
              label: '6 initiatives'
            }
          },
          {
            y: 9,
            params: {
              label: '9 initiatives'
            }
          },
          {
            y: 6,
            params: {
              label: '6 initiatives'
            }
          }
        ],
        stack: 'Plan',
        color: '#fbd155'
      }, {
        type: undefined,
        name: 'On Track',
        data: [
          {
            y: 10,
            params: {
              label: '10 initiatives'
            }
          },
          {
            y: 4,
            params: {
              label: '4 initiatives'
            }
          },
          {
            y: 7,
            params: {
              label: '7 initiatives'
            }
          }
        ],
        stack: 'Plan',
        color: '#35a761'
      }, {
        type: undefined,
        name: 'Off Track',
        data: [
          {
            y: 9,
            params: {
              label: '9 initiatives'
            }
          },
          {
            y: 6,
            params: {
              label: '6 initiatives'
            }
          },
          {
            y: 6,
            params: {
              label: '6 initiatives'
            }
          }
        ],
        stack: 'Forcast',
        color: '#b02c3b',
        showInLegend: false
      }, {
        type: undefined,
        name: 'At Risk',
        data: [
          {
            y: 5,
            params: {
              label: '5 initiatives'
            }
          },
          {
            y: 6,
            params: {
              label: '6 initiatives'
            }
          },
          {
            y: 10,
            params: {
              label: '10 initiatives'
            }
          }
        ],
        stack: 'Forcast',
        color: '#fbd155',
        showInLegend: false
      }, {
        type: undefined,
        name: 'On Track',
        data: [
          {
            y: 7,
            params: {
              label: '7 initiatives'
            }
          },
          {
            y: 6,
            params: {
              label: '6 initiatives'
            }
          },
          {
            y: 8,
            params: {
              label: '8 initiatives'
            }
          }
        ],
        stack: 'Forcast',
        color: '#35a761',
        showInLegend: false
      }]
    };
    

    $('#container').highcharts(options);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<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>

谢谢, 曼尼什(Manish)

1 个答案:

答案 0 :(得分:1)

默认情况下,您可以将堆栈标签放在顶部,在render事件中,将堆栈名称移到底部:

    chart: {
        ...,
        events: {
            render: function() {
                var labels = this.yAxis[0].stackTotalGroup.element.children;

                for (var i = 0; i < labels.length; i++) {
                    labels[i].children[1].setAttribute('y', 275);
                    labels[i].children[3].setAttribute('y', 275);
                }
            }
        }
    }

实时演示:http://jsfiddle.net/BlackLabel/9raLyx85/

API参考:https://api.highcharts.com/highcharts/chart.events.render