【问题标题】:Angular Highcharts Custom Legend Label In Area Chart区域图中的角度 Highcharts 自定义图例标签
【发布时间】:2019-02-20 14:36:44
【问题描述】:

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

我有这个代码:

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';

【问题讨论】:

    标签: javascript angular charts highcharts angular2-highcharts


    【解决方案1】:

    用 line.prototype.drawLegendSymbol 替换 area.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

    【讨论】:

    • 这里相同:这适用于标签,但对于工具提示是失败的
    • 对于 angular 是不可能的。我没有seriesTypes
    • 即使对于一行或任何其他系列,它也是一个默认的工具提示“标志”。如果你想改变这个,你可以在这个例子中的 tooltip.formatter 函数行中这样做:jsfiddle.net/BlackLabel/kcr1oynf@Segamoto 当然是可以的。我将编辑我的答案。
    【解决方案2】:

    您可以通过手动设置图例符号的宽度和高度来实现。您需要将 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.

    【讨论】:

    • 这适用于标签,但对于工具提示是失败的。
    • 它可以工作,但我怎样才能使符号与文本居中?
    • @Kabulan0lak 现在完美了。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多