简体   繁体   English

如何动态设置c3图表的y轴比例?

[英]How do I dynamically set the scale of y-axis for c3 chart?

I'm making a c3 chart that has grouped data, and I'd like to have a y-axis grid line that shows a maximum value, that the grouped data combined should not exceed. 我正在制作一个具有分组数据的c3图表,并且我希望有一个显示最大值的y轴网格线,该分组数据的总和不应超过。

So, the y-axis grid line could be called "Maximum" 因此,y轴网格线可以称为“最大”

I know how to make the grid, but it doesn't always show because the grouped data might be way below the grid value. 我知道如何制作网格,但是它并不总是显示,因为分组的数据可能比网格值低很多。 But I want the grid to always show. 但我希望网格始终显示。

I looked at padding for the y-axis, but it's uses pixels and is hard to get accurate. 我查看了y轴的填充,但是它使用像素,很难准确。

Is there a away to just set the y-axis to show like 5 values greater than whatever the y-axis grid line is? 是否有办法仅将y轴设置为比y轴网格线大5个值?

Unfortunately, C3 does not have a setting for "set Y axis to maximum of yvalue or largest data". 不幸的是,C3没有“将Y轴设置为yvalue的最大值或最大数据”的设置。 However, let's assume you have added a grid line using a method like below. 但是,假设您已使用以下方法添加了网格线。 You can then set the y-axis padding and maximum value using the settings shown here: 然后,您可以使用此处显示的设置来设置y轴填充和最大值:

grid: {
    y: {
        lines: [{ value: 20 }], 
    }
},
axis: {
    y: {
        padding: {top:5, bottom,0},
        max: 20
    }
}

In this case I have set the max value to be the same as the gridline value and used padding to create a small space above it. 在这种情况下,我将最大值设置为与网格线值相同,并使用填充在其上方创建一个小空间。 This will handle the case where your data is below the gridline. 这将处理您的数据低于网格线的情况。 However, if your data exceeds the (gridline+padding) total then the chart won't grow to accommodate that data because the max setting is fixed. 但是,如果您的数据超过了(网格+填充)总数,则图表将无法容纳该数据,因为最大设置是固定的。 So, you will need to dynamically change this max value using javascript if your data is greater than the max value by finding the maximum value in your data and setting max above that if it exceeds your gridline value, something like this... 因此,如果您的数据大于最大值,则需要通过JavaScript动态更改此最大值,方法是查找数据中的最大值,然后将max设置为大于最大值(如果它超过网格线值),类似这样...

if(highValue > gridlineValue)
  chart.axis.max(highValue);
else
  chart.axis.max(gridlineValue);

I just noticed that your data never exceeds the gridline, so you can go with the first part of this answer. 我只是注意到您的数据永远不会超出网格线,因此您可以使用此答案的第一部分。 I've left the rest in here in case someone else wants a more dynamic solution. 如果有人希望获得更动态的解决方案,我将其余的内容留在这里。 Note that you could also just use the max value without padding if you wanted to set it explicitly to a value higher than the gridline. 请注意,如果您想将其显式设置为高于网格线的值,也可以不使用填充而使用最大值。

As of this writing, there actually is a way to reset the y-max to be dynamic, after setting it to specific max. 在撰写本文时,实际上有一种方法可以将y-max设置为特定的max,然后将其重置为动态。 You do so by setting max to undefined , like so: chart.axis.max(undefined); 您可以通过将max设置为undefined ,例如: chart.axis.max(undefined); .

Here, have a working jsfiddle . 在这里,有一个工作的jsfiddle

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

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