我正在使用chart.js并有一个这样的折线图:
是否有可能像学校一样进行分离? (见图中的红线)。
答案 0 :(得分:0)
是的!有可能!!
您可以使用以下图表插件实现:
Chart.plugins.register({
afterDatasetsDraw: function(chart) {
var ctx = chart.ctx;
var x_axis = chart.scales['x-axis-0'];
//loop through ticks array
x_axis.ticks.forEach(function(tick, index) {
var x = x_axis.getPixelForTick(index);
var y = chart.scales['y-axis-0'].bottom;
var lineHeight = 8;
var lineColor = 'black';
//draw line
ctx.save();
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x, y + lineHeight);
ctx.strokeStyle = lineColor;
ctx.stroke();
ctx.restore();
});
}
});
*在脚本开头添加
ᴅᴇᴍᴏ⧩
Chart.plugins.register({
afterDatasetsDraw: function(chart) {
var ctx = chart.ctx;
var x_axis = chart.scales['x-axis-0'];
//loop through ticks array
x_axis.ticks.forEach(function(tick, index) {
var x = x_axis.getPixelForTick(index);
var y = chart.scales['y-axis-0'].bottom;
var lineHeight = 8;
var lineColor = 'black';
//draw line
ctx.save();
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x, y + lineHeight);
ctx.strokeStyle = lineColor;
ctx.stroke();
ctx.restore();
});
}
});
var chart = new Chart(ctx, {
type: 'line',
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
datasets: [{
label: 'LINE',
data: [3, 1, 4, 2, 5],
backgroundColor: 'rgba(0, 119, 290, 0.2)',
borderColor: 'rgba(0, 119, 290, 0.6)',
fill: false,
tension: 0
}]
},
options: {
scales: {
xAxes: [{
gridLines: {
display: false
}
}],
yAxes: [{
ticks: {
beginAtZero: true,
stepSize: 1
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="ctx"></canvas>
答案 1 :(得分:0)
现在我看到另一个解决方案:gridLines
还有其他属性用于这些目的。
gridLines: {
display: true,
drawBorder: true,
drawOnChartArea: false
}