简体   繁体   English

highcharts如何为系列数据(类别)而非系列(传奇)设置y轴索引

[英]highcharts how to set y-axis index for series data(categories),not series(legend)

how to set y-axis index for series data(categories),not series(legend) http://jsfiddle.net/n7zxvc4q/ 如何为系列数据(类别)而不是系列(传奇)设置y轴索引http://jsfiddle.net/n7zxvc4q/

Highcharts.chart('container', {
    chart: {
        marginRight: 80
    },
    xAxis: {
        categories: ['time', 'bytes']
    },
    yAxis: [{
        title: {
            text: 'seconds'
        }
    }, {
        title: {
            text: 'Mb'
        },
        opposite: true
    }],

    series: [{
        type: 'column',
        data: [29.9, 71.5],
        name: '192.168.0.1'
    }, {
        type: 'column',
        data: [14.1, 95.6],
        name: '192.168.0.2'
    }, {
        type: 'column',
        data: [34.1, 75.6],
        name: '192.168.0.3'
    }]
});

I hope "time" use the left y-axis:seconds(yAxis:0) and "bytes" use the right y-axis:Mb(yAxis:1) 我希望“时间”使用左侧的y轴:seconds(yAxis:0),“字节”使用右侧的y轴:Mb(yAxis:1)

I found other one is not the way I want it,like this 我发现另一个不是我想要的方式,像这样
http://jsfiddle.net/141nw7vw/ http://jsfiddle.net/141nw7vw/

Highcharts.chart('container', {
    chart: {
        marginRight: 80
    },
    xAxis: {
        categories: ['192.168.0.1', '192.168.0.2','192.168.0.3']
    },
    yAxis: [{
        title: {
            text: 'seconds'
        }
    }, {
        title: {
            text: 'Mb'
        },
        opposite: true
    }],

    series: [{
        type: 'column',
        data: [29.9, 71.5],
        name: 'time',
        yAxis:0
    }, {
        type: 'column',
        data: [14.1, 95.6],
        name: 'seconds',
        yAxis:1
    }]
});

In Highcharts x/y/zAxis properties can be assigned only to a series. 在Highcharts中, x/y/zAxis属性只能分配给一个序列。 Categories are only the information how to format axis' labels (their "real" values are their indexes from categories array). 类别只是有关如何格式化轴标签的信息(它们的“实际”值是它们在categories数组中的索引)。 One series should contain only one type of the data (time or bytes). 一个系列应仅包含一种类型的数据(时间或字节)。 You should rearrange your data like this (notice that all points have x coordinate defined): 您应该像这样重新排列数据(注意所有点都定义了x坐标):

  series: [
    // time
    {
      data: [
        [0, 29.9]
      ],
      name: '192.168.0.1',
      pointPlacement: -0.3,
      color: 'orange'
    }, {
      data: [
        [0, 14.1]
      ],
      name: '192.168.0.2',
      color: 'pink'
    }, {
      data: [
        [0, 34.1]
      ],
      name: '192.168.0.3',
      pointPlacement: 0.3,
      color: 'blue'

    },
    // bytes
    {
      data: [
        [1, 71.5]
      ],
      name: '192.168.0.1',
      yAxis: 1,
      pointPlacement: -0.3,
      color: 'orange',
      showInLegend: false
    }, {
      data: [
        [1, 95.6]
      ],
      name: '192.168.0.2',
      yAxis: 1,
      color: 'pink',
      showInLegend: false
    }, {
      data: [
        [1, 75.6]
      ],
      name: '192.168.0.3',
      yAxis: 1,
      pointPlacement: 0.3,
      color: 'blue',
      showInLegend: false
    }
  ]

Live working example: http://jsfiddle.net/kkulig/rxrys3nx/ 实际工作示例: http : //jsfiddle.net/kkulig/rxrys3nx/

I disabled grouping to position columns using pointPlacement and pointPadding properties. 我禁用了使用pointPlacementpointPadding属性对列进行grouping

I assigned the same color to the mutually corresponding series and used showInLegend = false to prevent duplicates in legend. 我为相互对应的系列分配了相同的颜色,并使用showInLegend = false防止图例重复。

Then I programmed a legend so that it performs the same action (show/hide) for all series with the common name: 然后,我对图例进行了编程,以使其对所有具有通用名称的系列执行相同的操作(显示/隐藏):

    legendItemClick: function(event) {
      var series = this,
        chart = this.chart;

      var isVisible = series.visible;
      chart.series.forEach(function(s) {
        if (s.name === series.name) {
          if (isVisible) {
            s.hide();
          } else {
            s.show();
          }
        }
      });
      event.preventDefault();
    }

API references: API参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM