【问题标题】:How to set series-label to false by default and change the color of series label text in highchart如何将 series-label 默认设置为 false 并更改 highchart 中系列标签文本的颜色
【发布时间】:2018-08-07 06:01:24
【问题描述】:

我的页面上有 5 个高位图表,我只需要其中一个图表中的系列标签。

但当我包含 series-label.js 时,它会为所有图表添加系列标签。

还有如何更改系列标签的颜色。例如,在以下示例中,我需要所有系列标签为黑色。

Highcharts.chart('container', {
    chart: {
        type: 'line'
    },   
    series: [
    {
        name: 'Unites States',
        data: [7.5, 15.2, 18.7, 21.5, 25.9, 30.2, 29.0, 28.6, 27.2, 20.3, 18.6, 14.8]
    },
    {
        name: 'Tokyo',
        data: [7.0, 6.9, 9.5, 14.5, 18.4, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
    }, {
        name: 'London',
        data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]
    }]
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/series-label.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

【问题讨论】:

    标签: javascript jquery highcharts timeserieschart


    【解决方案1】:

    Highcharts 按系列隐藏标签的方法是使用series.line.label.enabled 切换。要隐藏图表中所有系列的标签,可以切换以下选项 (plotOptions.series.label.enabled):

    plotOptions: {
      series: {
        label: {enabled: false},
        ...
      }
    }
    

    同样,要更改系列标签的颜色,可以使用series.line.label.style,或更改图表中所有系列的颜色 (plotOptions.series.label.style):

    plotOptions: {
      series: {
        label: {style: {color: 'black'}},
        ...
      }
    }
    

    这导致了这个例子:

    Highcharts.chart('container', {
        chart: {
            type: 'line'
        },
        title: { text: 'No labels' },
        plotOptions: {
          series: {
            label: {
              enabled: false
            }
          }
        },
        series: [
        {
            name: 'Unites States',
            data: [7.5, 15.2, 18.7, 21.5, 25.9, 30.2, 29.0, 28.6, 27.2, 20.3, 18.6, 14.8]
        },
        {
            name: 'Tokyo',
            data: [7.0, 6.9, 9.5, 14.5, 18.4, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
        }, {
            name: 'London',
            data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]
        }]
    });
    
    Highcharts.chart('container2', {
        chart: {
            type: 'line',
        },
        title: { text: 'Black labels' },
        plotOptions: {
          series: {
            label: {
              style: {
                color: 'black'
              }
            }
          }
        },
        series: [
        {
            name: 'Unites States',
            data: [7.5, 15.2, 18.7, 21.5, 25.9, 30.2, 29.0, 28.6, 27.2, 20.3, 18.6, 14.8]
        },
        {
            name: 'Tokyo',
            data: [7.0, 6.9, 9.5, 14.5, 18.4, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
        }, {
            name: 'London',
            data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]
        }]
    });
    <script src="https://code.highcharts.com/highcharts.js"></script>
    <script src="https://code.highcharts.com/modules/series-label.js"></script>
    <script src="https://code.highcharts.com/modules/exporting.js"></script>
    <script src="https://code.highcharts.com/modules/export-data.js"></script>
    
    <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
    <div id="container2" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

    【讨论】:

    • 当问题要求“仅在一个图表中的系列标签”时,我认为对于每个图表设置使用 plotOptions.series.label 可能是合适的,以避免在几个系列中的每个系列上执行 enabled: false图表?
    • 看起来不错并解决了实际问题。我将放弃一个小提琴,将全局默认设置为false,如果有很多图表需要单独禁用,这可能是值得的:JSFiddle
    • @HalvorStrand,将此标记为答案
    【解决方案2】:

    我想你想要这样:-

    Highcharts.chart('container', {
        chart: {
            type: 'line'
        },   
        series: [
        {
            name: 'Unites States',
            data: [7.5, 15.2, 18.7, 21.5, 25.9, 30.2, 29.0, 28.6, 27.2, 20.3, 18.6, 14.8],
    	
        },
        {
            name: 'Tokyo',
            data: [7.0, 6.9, 9.5, 14.5, 18.4, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6],
        }, {
            name: 'London',
            data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8],
        }]
    });
    .highcharts-label text {fill: rgb(0, 0, 0) !important;}
    <script src="https://code.highcharts.com/highcharts.js"></script>
    <script src="https://code.highcharts.com/modules/series-label.js"></script>
    <script src="https://code.highcharts.com/modules/exporting.js"></script>
    <script src="https://code.highcharts.com/modules/export-data.js"></script>
    
    <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

    【讨论】:

    • 它也改变了系列的颜色。我需要系列颜色保持原样,只需要更新标签颜色即可。
    • 我很困惑。与问题中的代码相比,此地址仅在 5 个图表中的 1 个中如何处理?
    • @HalvorStrand,将此标记为答案,因为它部分回答了问题,尽管 5 个图表部分中的 1 个未回答,我正在浏览大腿图表 API
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-25
    • 1970-01-01
    • 2012-01-15
    • 2017-07-28
    • 2017-02-17
    • 1970-01-01
    相关资源
    最近更新 更多