Highcharts - 动态设置yAxis标题

时间:2016-09-09 12:27:58

标签: javascript highcharts

我想根据yAxis是显示Gbps还是Mbps来动态设置yAxis的标题。

虽然找不到标题的格式化程序功能,但我尝试过的所有其他方法都失败了。

有关如何操作的任何建议吗?

这是yAxis选项的代码:

yAxis: [{
              labels: {
                formatter: function() {
                  maxDataValue = ((this.chart.yAxis["0"].dataMax * 8) / 300) / 1024;
                  if (maxDataValue < 1000) {
                    return Math.floor(((this.value * 8) / 300) / 1024) + " Mbps";
                  } else {
                    return Math.floor(((this.value * 8) / 300) / 1048576) + " Gbps";
                  }
                }
              },
              title: {
                enabled: true,
                text: unit,
                style:{
                  fontWeight:'bold'
                }
              },
              tickmarkPlacement: 'on',
              plotLines: [{
                value: 0,
                width: 2,
                color: '#333333'
              }]
            }],

2 个答案:

答案 0 :(得分:0)

yAxis的标题。

var title = '';
.
.
.
.
yAxis: [{
    labels: {
        formatter: function() {
            maxDataValue = ((this.chart.yAxis["0"].dataMax * 8) / 300) / 1024;
            if (maxDataValue < 1000) {
                title = 'Mbps';
                return Math.floor(((this.value * 8) / 300) / 1024) + " Mbps";
            } else {
                title = 'Gbps';
                return Math.floor(((this.value * 8) / 300) / 1048576) + " Gbps";
            }
        }
    },
    title: {
        enabled: true,
        text: title,
        style:{
            fontWeight:'bold'
        }
    },
    tickmarkPlacement: 'on',
    plotLines: [{
        value: 0,
        width: 2,
        color: '#333333'
    }]
}],

答案 1 :(得分:0)

您可以在yAxis内部标签格式化程序中添加新参数,并使用此参数使用Axis.setTitle()方法更改回调函数中的yAxis标题:http://api.highcharts.com/highcharts/Axis.setTitle

#include <stdlib.h>
#include <bits/time.h>
#include <time.h>
#include <stdio.h>
int main() {
    int i;
    char *test[10];
    clock_t begin = clock();
    for (i = 0; i < 10; i++) {
        test[i] = malloc(50 * (sizeof(char)));
    }
    clock_t end = clock();
    double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
    printf("time malloc %f\n", time_spent);
    begin = clock();
    for (i = 0; i < 10; i++) {
        free(test[i]);
    }
    end = clock();
    time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
    printf("time free %f\n", time_spent);
    return 0;
}

在这里,您可以找到实时示例:http://jsfiddle.net/orgtn6xq/1/

相关问题