Angular 面积图中的 Highcharts 自定义图例标签

Angular Highcharts Custom Legend Label In Area Chart

我必须在面积图类型中自定义标签符号,就像在折线图类型中一样:

我有这个代码:

Highcharts.chart('container', {
        chart: {
        type: 'area'
    },

    xAxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    },

    plotOptions: {
        series: {
            marker: {
                enabled: false
            }
        }
    },

    series: [{
        data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
    }]
});

我的传说是这样的:

我希望有这个:

我无法编辑 css 样式,这必须是仅此图表的独家定制。

我正在使用 angular 7.2,我已经导入了 Highchart:

import {Chart} from 'angular-highcharts';

您可以通过手动设置图例符号的宽度和高度来实现。您需要将 squareSymbol 设置为 false 以允许具有不同的宽度和高度值:

legend: {
    squareSymbol: false,
    symbolHeight: 3,
    symbolWidth: 20
},

例如

编辑

如果要将符号与文本对齐,请使用 useHTML: true 并设置 lineHeight:

  legend: {
    useHTML: true,
    itemStyle: {
      lineHeight: "23px"
    },
    squareSymbol: false,
    symbolHeight: 3,
    symbolWidth: 20
  },

See updated working jsfiddle here.

area.prototype.drawLegendSymbol 方法替换为 line.prototype.drawLegendSymbol。这在纯 JS 中就足够了:

Highcharts.seriesTypes.area.prototype.drawLegendSymbol =
Highcharts.seriesTypes.line.prototype.drawLegendSymbol;

jsFiddle

在 angular 中,您可以这样包装 Highcharts:

(function(factory) {
  if (typeof module === "object" && module.exports) {
    module.exports = factory;
  } else {
    factory(Highcharts);
  }
})(function(Highcharts) {
  Highcharts.seriesTypes.area.prototype.drawLegendSymbol =
    Highcharts.seriesTypes.line.prototype.drawLegendSymbol;
});

文档:https://github.com/highcharts/highcharts-angular#to-load-a-wrapper