简体   繁体   English

高图-将yAxis.max设置为数据的最大值

[英]Highcharts - set yAxis.max to max value from data

I'm using Highcharts and I have this simple bar chart: 我正在使用Highcharts,并且有一个简单的条形图:

我当前的图表

Max value in data is 27, but on the scale it's 40. How can i set yAxis.max to max value from the provided data? 数据中的最大值为27,但按比例尺为40。如何从提供的数据中将yAxis.max设置为最大值? In this example 27 instead of 40. 在此示例中,使用27代替40。

To be honest I don't even need those values on chart, but I want to force a bar with biggest value to occupy 100% of available height. 老实说,我什至不需要图表上的这些值,但我想强制使用最大值的条形图占据可用高度的100%。

The data will be provided dynamically, so I can't just set max value manually. 数据将动态提供,因此我不能仅手动设置最大值。

Here is my current code: 这是我当前的代码:

 Highcharts.chart('container', { chart: { type: 'column', height: 150, }, yAxis: { title: { text: null, }, labels: { enabled: true, }, }, xAxis: { title: { text: null, }, }, credits: false, legend: false, title: { text: null, }, series: [{ data: [1, 0, 27, 7] }] }); 
 #container { min-width: 310px; max-width: 800px; height: 400px; margin: 0 auto } 
 <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> <div id="container"></div> 

Here is jsfiddle: http://jsfiddle.net/de3qfb3r/ 这是jsfiddle: http : //jsfiddle.net/de3qfb3r/

You say you don't care about the values on the chart, so maybe adding endOnTick: false would be enough for you: 您说您不关心图表上的值,所以也许添加endOnTick:false对您就足够了:

yAxis: {
    title: {
        text: null,
    },
    labels: {
        enabled: true,
    },
    endOnTick: false
}

Highcharts automatically calculates the maximum value and fits it to the available height. Highcharts会自动计算最大值并将其调整为可用高度。 When endOnTick is true though(which is the default), it adds an extra space to finish with a tick 当endOnTick为true时(这是默认设置),它将添加额外的空间以打勾

If you have the data before hand, you can just find the max of the array. 如果您事先有数据,则可以找到数组的最大值。 A gotcha here is that I couldn't get the yAxis max to work without adding a tick interval. 这里的陷阱是,如果不添加刻度间隔,我将无法使yAxis max正常工作。 Here is an example of how you can accomplish this. 这是一个如何完成此操作的示例。

 var data = [1, 0, 27, 7]; var yMax = data.reduce(function(a, b) { return Math.max(a, b); }); Highcharts.chart('container', { chart: { type: 'column', height: 150, }, yAxis: { title: { text: null, }, labels: { enabled: true, }, tickInterval: 2, max: yMax }, xAxis: { title: { text: null, }, }, credits: false, legend: false, title: { text: null, }, series: [{ data: data }] }); 
 #container { min-width: 310px; max-width: 800px; height: 400px; margin: 0 auto } 
 <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> <div id="container"></div> 

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

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