javascript HighCharts:以百分比显示 y 轴标签,而不是基本柱状图的绝对计数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17547818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:43:23  来源:igfitidea点击:

HighCharts: display the y-axis labels in percentage instead of the absolute count for basic column charts

javascripthighcharts

提问by dehsams123

I just started using highcharts and I am trying to figure out a way to display the y-axis label in % instead of the actual values/counts for a Basic Column chart. Is there a way I can do that?Can someone please suggest me something.

我刚开始使用 highcharts,我试图找出一种以 % 显示 y 轴标签的方法,而不是基本柱形图的实际值/计数。有没有办法做到这一点?有人可以给我建议。

Thanks

谢谢

回答by jlbriggs

If all you want to change is the axis labels, check my answer and fiddle example here:

如果您只想更改轴标签,请在此处查看我的答案和小提琴示例:

Highcharts percentage of total for simple bar chart

简单条形图占总数的 Highcharts 百分比

If you want the tooltip to also show the % value, you can copy the code from the dataLabels formatter in that example to accomplish that.

如果您希望工具提示也显示 % 值,您可以从该示例中的 dataLabels 格式化程序中复制代码来实现这一点。

回答by Sebastian Bochan

Unfortuantely this option is not available, but you can prepare your own function which will count all points and calculate percetn value. Then returns updated values for data series.

不幸的是,此选项不可用,但您可以准备自己的函数,该函数将计算所有点并计算 percetn 值。然后返回数据系列的更新值。

回答by lecorbu

You can change the count to a distribution percentage by taking the sum of each step division and dividing by count.

您可以通过计算每个步骤除法的总和并除以计数来将计数更改为分布百分比。

Here is the Highcharts example histogram() function modified to show percentage, make sure to set yAxis to max 1. If you do not want decimal percentage multiply by 100.

这是修改为显示百分比的 Highcharts 示例 histogram() 函数,请确保将 yAxis 设置为最大值 1。如果您不希望小数百分比乘以 100。

function histogram(data, step) {
    var histo = {},
        x,
        i,
        sum = 0,
        arr = [];

    // Group down
    for (i = 0; i < data.length; i++) {
        x = Math.floor(data[i][0] / step) * step;
        if (!histo[x]) {
        histo[x] = 0;
       }
        histo[x]++;
    }

    // Make the histo group into an array
    for (x in histo) {
        if (histo.hasOwnProperty((x))) {
            arr.push([parseFloat(x), histo[x]]);
        }
    }

    // find sum 
    for (i = 0; i < arr.length; i++) {
            sum += arr[i][1];
       }

    // calculate percent 
    for (i = 0; i < arr.length; i++) {
            arr[i][1] = arr[i][1]/sum ;
        }

    // sort the array
    arr.sort(function (a, b) {
        return a[0] - b[0];
    });

    return arr;
}

fiddle: http://jsfiddle.net/jamie_farrell/xy7uogz4/2/

小提琴:http: //jsfiddle.net/jamie_farrell/xy7uogz4/2/