我正在寻找一种方法来填充我的图表上所有低于0的内容,作为一个漂亮的红色阴影。在搜索SO和文档之后,我还没有能够找到一种方法来做到这一点。我尝试创建一个用decimal.MinValue填充的数据集,并将所有低于0的内容设置为填充,但是它把整个图移到了最小值,不管数据集是否被设置为隐藏。任何建议都将非常感谢。谢谢。
decimal.MinValue
yqhsw0fo1#
您可以使用自定义内联插件来实现这一点:
var options = { type: 'line', data: { labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"], datasets: [{ label: '# of Votes', data: [100, 19, 3, 5, -10, 3], borderColor: 'red', backgroundColor: 'red' }, { label: '# of Votes2', data: [20, 10, 4, 3, -30, 32], borderColor: 'blue', backgroundColor: 'blue' } ] }, options: { plugins: { backgrounds: { hbars: [{ from: 0, to: 'MIN', color: "rgb(230, 195, 195)" }] } } }, plugins: [{ id: 'backgrounds', beforeDraw: (chart, args, options) => { const { canvas, ctx, chartArea, scales: { y } } = chart; let from = 0; let to = 0; options.hbars.forEach((hBar) => { from = hBar.from; to = hBar.to; from = from === 'MIN' ? y.min : from; from = from === 'MAX' ? y.max : from; to = to === 'MIN' ? y.min : to; to = to === 'MAX' ? y.max : to; ctx.save(canvas.width, canvas.height); ctx.fillStyle = hBar.color; ctx.fillRect(chartArea.left, y.getPixelForValue(from), chartArea.right - chartArea.left, y.getPixelForValue(to) - y.getPixelForValue(hBar.from)); ctx.restore(); }) } }] } var ctx = document.getElementById('chartJSContainer').getContext('2d'); new Chart(ctx, options);
个字符
myss37ts2#
对于v4+,文档中的示例:
fill: { target: 'origin', below: 'rgb(255, 0, 0, 0.7)', // Area will be red above the origin above: 'rgb(0, 0, 255, 0.7)' // And blue below the origin }
字符串https://www.chartjs.org/docs/master/charts/area.html
2条答案
按热度按时间yqhsw0fo1#
您可以使用自定义内联插件来实现这一点:
个字符
myss37ts2#
对于v4+,文档中的示例:
字符串
https://www.chartjs.org/docs/master/charts/area.html