代码之家  ›  专栏  ›  技术社区  ›  Simsons

如何在highchart中将序列标签设置为false并更改序列标签文本的颜色

  •  2
  • Simsons  · 技术社区  · 6 年前

    我的网页上有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>
    2 回复  |  直到 6 年前
        1
  •  5
  •   ewolden    6 年前

    基于每个序列隐藏标签的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>
        2
  •  1
  •   Rohit Verma    6 年前

    我想你想要这样:-

    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>