答案 0 :(得分:1)
这是一个小小的hackery。如果您使用的是最新版本的c3
,则可以重新定义d3
步进线生成器以禁止垂直:
<!DOCTYPE html>
<html>
<head>
<script src="https://d3js.org/d3.v5.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/c3/0.6.2/c3.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/c3/0.6.2/c3.js"></script>
</head>
<body>
<div id="chart"></div>
<script>
function Step(context, t) {
this._context = context;
this._t = t;
}
Step.prototype = {
areaStart: function() {
this._line = 0;
},
areaEnd: function() {
this._line = NaN;
},
lineStart: function() {
this._x = this._y = NaN;
this._point = 0;
},
lineEnd: function() {
if (0 < this._t && this._t < 1 && this._point === 2) this._context.lineTo(this._x, this._y);
if (this._line || (this._line !== 0 && this._point === 1)) this._context.closePath();
if (this._line >= 0) this._t = 1 - this._t, this._line = 1 - this._line;
},
point: function(x, y) {
x = +x, y = +y;
switch (this._point) {
case 0:
this._point = 1;
this._line ? this._context.lineTo(x, y) : this._context.moveTo(x, y);
break;
case 1:
this._point = 2; // proceed
default:
{
if (this._t <= 0) {
this._context.lineTo(this._x, y);
this._context.lineTo(x, y);
} else {
var x1 = this._x * (1 - this._t) + x * this._t;
this._context.lineTo(x1, this._y);
this._context.moveTo(x1, y);
}
break;
}
}
this._x = x, this._y = y;
}
};
d3.curveStep = function(context) {
return new Step(context, 0.5);
};
var data1 = ['data1'],
data2 = ['data2'];
d3.range(5).forEach(function(d) {
data1.push(Math.random() * 100);
data2.push(Math.random() * 100);
})
var chart = c3.generate({
data: {
columns: [
data1, data2
],
types: {
data1: 'bar',
data2: 'step'
}
}
});
</script>
</body>
</html>
答案 1 :(得分:0)