问题
How to align the Y 0 axis value to Y2 0 axis value?
See jsFiddle to understand.
I tried the option center:0
on the axe's but i don't want 0 on center of graph..
var chart = c3.generate({
data: {
columns: [
['data1', 2000, 350, 300, -200, -50, 0],
['data2', 130, 100, 140, 200, 150, 50]
],
types: {
data1: 'line',
data2: 'bar'
},
axes:{
data1:'y',
data2:'y2'
}
},
axis: {
y: {
padding: {bottom: 0}
},
y2: {
show:true,
padding: {bottom: 0}
},
x: {
min: 0,
show: true
}
},
grid: {
x: {
show: false
},
y: {
show: false,
lines: [
{value: 0.0, text: 'i want 0 here ->', class:'red'}
]
}
}
});
Official documentation.
Thanks.
回答1:
In general you want the same proportion of the axes ranges above and below zero, which is why min: -20 would work for this particular example
This would work in general after you've generated your chart:
var d1 = d3.extent(chart.data.values("data1"));
var d2 = d3.extent(chart.data.values("data2"));
if (d2[0] < 0 || d1[0] < 0) {
var prop1 = d1[0] < 0 ? -(d1[1] / d1[0]) : Math.Infinity;
var prop2 = d2[0] < 0 ? -(d2[1] / d2[0]) : Math.Infinity;
if (prop1 > prop2) {
d1[0] = -(d1[1] / prop2);
} else {
d2[0] = -(d2[1] / prop1);
}
chart.axis.range({
min: {y: d1[0], y2: d2[0]},
max: {y: d1[1], y2: d2[1]}
});
}
Basically if either (or both) series has a negative value then it scales both series to have the same proportion of positive to negative range using the min/max axis settings. This means zero sits at the same vertical point for both
https://jsfiddle.net/ynz6quum/4/
来源:https://stackoverflow.com/questions/38851558/c3js-value-0-aligned-between-y-and-y2