Chart.js - add gradient instead of solid color - implementing solution(Chart.js - 添加渐变而不是纯色 - 实现解决方案)
问题描述
我正在使用 Chart.js,一切正常,但我想用渐变替换当前颜色背景 (fillColor : "rgba(250,174,50,0.5)").我有替换渐变的解决方案,但是用我糟糕的 JS 知识来实现它对我来说太难了.我想对于懂 JS 的人来说很容易.
I am using Chart.js and everything is ok, but I want to replace current color background (fillColor : "rgba(250,174,50,0.5)") with a gradient. I have solution for replacing gradient but it's too dificult for me to implement this with my poor JS knowledge. I guess pretty easy for someone who know JS.
所以我的 Chart.js 代码:
<script>
var data = {
labels : ["02:00","04:00","06:00","08:00","10:00","12:00","14:00","16:00","18:00","20:00","22:00","00:00"],
datasets: [
{
fillColor : "rgba(250,174,50,0.5)",
strokeColor : "#ff6c23",
pointColor : "#fff",
pointStrokeColor : "#ff6c23",
pointHighlightFill: "#fff",
pointHighlightStroke: "#ff6c23",
data : [25.0,32.4,22.2,39.4,34.2,22.0,23.2,24.1,20.0,18.4,19.1,17.4]
}
]
};
var options = {
responsive: true,
datasetStrokeWidth : 3,
pointDotStrokeWidth : 4,
tooltipFillColor: "rgba(0,0,0,0.8)",
tooltipFontStyle: "bold",
tooltipTemplate: "<%if (label){%><%=label + ' hod' %>: <%}%><%= value + '°C' %>",
scaleLabel : "<%= Number(value).toFixed(0).replace('.', ',') + '°C'%>"
};
var ctx = document.getElementById("temp-chart").getContext("2d");
var myLineChart = new Chart(ctx).Line(data, options);
</script>
还有这里是渐变解决方案. 有人可以尝试实现这个渐变背景而不是我当前的纯色背景吗?感谢您的帮助.
And here is solution with gradient. Can someone try implement this gradient background instead of my current solid background? Thanks for help.
我尝试实现它,但其他功能不起作用(如 scaleLabels 等).
I tryed implement it, but then other functions don't work (like scaleLabels etc.).
推荐答案
您提供的链接非常清楚,您必须在数据集中的字段 fillColor
中放入一个线性渐变对象而不是纯色.你可以做复杂的渐变,但这里是一个简单的代码(改变同一个橙色的不透明度):
The link you provided was pretty clear, you have to put in the field fillColor
in datasets a linearGradient object instead of a plain color. You can do complex gradients, but here is the code of a simple one (changing the opacity of the same orange) :
var gradient = ctx.createLinearGradient(0, 0, 0, 400);
gradient.addColorStop(0, 'rgba(250,174,50,1)');
gradient.addColorStop(1, 'rgba(250,174,50,0)');
还有你的完整数据集:
datasets: [
{
fillColor : gradient, // Put the gradient here as a fill color
strokeColor : "#ff6c23",
pointColor : "#fff",
pointStrokeColor : "#ff6c23",
pointHighlightFill: "#fff",
pointHighlightStroke: "#ff6c23",
data : [25.0,32.4,22.2,39.4,34.2,22.0,23.2,24.1,20.0,18.4,19.1,17.4]
}
]
在这个 JSFiddle 中查看它的实际应用
See it in action in this JSFiddle
这篇关于Chart.js - 添加渐变而不是纯色 - 实现解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Chart.js - 添加渐变而不是纯色 - 实现解决方案
基础教程推荐
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01