简体   繁体   English

HighCharts - 如何设置最大值为 100%

[英]HighCharts - How to set a max value of 100%

I have a bar graph with values converted to a percentage with a loop.我有一个条形图,其值通过循环转换为百分比。 When my values are returned it seems to be displaying the data exactly how I expect it to but the graph extends past 100% with empty values.当我的值被返回时,它似乎完全按照我的预期显示数据,但图表扩展超过 100%,值为空。 I tried to set a max: 100 on my yaxis but I am unable to set it 100%.我试图在我的yaxis上设置一个max: 100 ,但我无法将其设置为 100%。

When adding max:100 it displays one bar in my graph which is why I believe it is focusing on the value 100 and not 100%.添加max:100时,它会在我的图表中显示一个条形图,这就是为什么我认为它专注于值 100 而不是 100%。

Is there a way to handle this in Highcharts?有没有办法在 Highcharts 中处理这个问题?

Here is a jsfiddle这是一个jsfiddle

Highcharts 图表

As seen in the photo it seems to only calculate 100% but the graph ends at 104%.如图所示,它似乎只计算了 100%,但图表以 104% 结束。

My expected outcome is to have the percentage at the end of the graph to be at 100%.我的预期结果是图表末尾的百分比为 100%。 I'm not sure what is causing this.我不确定是什么原因造成的。

Here is my code:这是我的代码:

let data = [10, 31, 13, 19, 21, 50, 10]

let dataSum = 0

for (let i = 0; i < data.length; i++) {
dataSum += data[i]
}

console.log(dataSum, "dataSum")

Highcharts.chart('container', {
  chart: {
    type: 'bar'
  },
  title: {
    text: "Bar Graph"
  },
  xAxis: {

  },
  yAxis: {
    min: 0,
               labels: {
            formatter: function () {
                var pcnt = (this.value / dataSum) * 100;
                return Highcharts.numberFormat(pcnt, 0, ',') + '%';
            }
        },

    title: {
      text: '% of Total'
    }
  },
  legend: {
    reversed: false
  },
  plotOptions: {
    series: {
      stacking: 'normal'
    }
  },
  series: [{
    name: 'Low',
    index: 4,
    color: '#0D6302',
    data: [data[0]],
    showInLegend: true,
  }, {
    name: 'Medium-Low',
    index: 3,
    color: '#0B7070',
    data: [data[2]]
  }, {
    name: 'Medium',
    index: 2,
    color: '#DC9603',
    data: [data[3]]
  },{
    name: 'Low',
    index: 5,
    color: '#0D6302',
    data: [data[1]],
    showInLegend: false
  }, 
  {
    name: 'Medium-High',
    index: 1,
    color: '#DD5F0C',
    data: [data[4]]
  }, {
    name: 'High',
    index: 0,
    color: '#C50710',
    data: [data[5]]
  }]
});

In this, as the ticks are made by default by the highcharts, that is why you get extra values even without percentage.在此,由于刻度是由 highcharts 默认生成的,这就是为什么即使没有百分比也可以获得额外值的原因。 see.看。 fiddle with the issue.摆弄这个问题。

so what you can do is you can use tickPositioner function to render the click according to your data.所以你可以做的是你可以使用 tickPositioner function 根据你的数据呈现点击。 and then convert the Value for the label in the formatter to the percentage.然后将格式化程序中 label 的值转换为百分比。 Apart from this, I can not find any other way to do this as of now.除此之外,到目前为止,我找不到任何其他方法来做到这一点。 see.看。 fiddle with the proposed solution.摆弄建议的解决方案。

align your data so that the percentage looks inline with data.对齐您的数据,使百分比看起来与数据一致。

some other suggestions/corrections are most welcome其他一些建议/更正是最受欢迎的

You have converted only axis labels, not an axis scale.您只转换了轴标签,而不是轴刻度。 (Max - dataSum is 154, not 100). (Max - dataSum是 154,而不是 100)。

As a solution you can convert your data to percentage values before creating a chart:作为一种解决方案,您可以在创建图表之前将数据转换为百分比值:

let data = [10, 31, 13, 19, 21, 50, 10]
let dataSum = 0

for (let i = 0; i < data.length; i++) {
    dataSum += data[i]
}

let percentageData = data.map((el) => (el / dataSum * 100));

Highcharts.chart('container', {
    ...,
    yAxis: {
        min: 0,
        max: 100,
        labels: {
            format: '{value}%'
        },
        ...
    },
    series: [{
            ...,
            data: [percentageData[0]],
        }, {
            ...,
            data: [percentageData[2]]
        }, {
            ...,
            data: [percentageData[3]]
        }, {
            ...,
            data: [percentageData[1]]
        }, {
            ...,
            data: [percentageData[4]]
        }, {
            ...,
            data: [percentageData[5]]
        }
    ]
});

Live demo: http://jsfiddle.net/BlackLabel/6m4e8x0y/5002/现场演示: http://jsfiddle.net/BlackLabel/6m4e8x0y/5002/

API Reference: https://api.highcharts.com/highcharts/yAxis.labels.format API 参考: https://api.highcharts.com/highcharts/yAxis.labels.format

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

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