2
A
回答
3
它在某種程度上是可行的。下面介紹的一種簡單方法僅假設一個數據集(儘管如此,應該很容易擴展處理更多數據集的方法)。這個想法如下。我們將創建一個插件來覆蓋beforeUpdate
方法(在每次更新開始時調用該方法)。在每次更新開始時,計算數據集的最小值和最大值的確切Y像素。然後使用createLinearGradient
從畫布的上下文創建一個垂直線性漸變,其中Y像素的一種紅色對應於數據集的最小值,對於Y像素的對應於最大值的爵士類型的藍色的數據集。查看評論的代碼以獲取更多信息。關於懸停點和傳奇着色可能會有一些小問題,我並不十分熱衷於研究。 A working fiddle is here以下代碼也可用。
var gradientLinePlugin = {
// Called at start of update.
beforeUpdate: function(chartInstance) {
if (chartInstance.options.linearGradientLine) {
// The context, needed for the creation of the linear gradient.
var ctx = chartInstance.chart.ctx;
// The first (and, assuming, only) dataset.
var dataset = chartInstance.data.datasets[0];
// Calculate min and max values of the dataset.
var minValue = Number.MAX_VALUE;
var maxValue = Number.MIN_VALUE;
for (var i = 0; i < dataset.data.length; ++i) {
if (minValue > dataset.data[i])
minValue = dataset.data[i];
if (maxValue < dataset.data[i])
maxValue = dataset.data[i];
}
// Calculate Y pixels for min and max values.
var yAxis = chartInstance.scales['y-axis-0'];
var minValueYPixel = yAxis.getPixelForValue(minValue);
var maxValueYPixel = yAxis.getPixelForValue(maxValue);
// Create the gradient.
var gradient = ctx.createLinearGradient(0, minValueYPixel, 0, maxValueYPixel);
// A kind of red for min.
gradient.addColorStop(0, 'rgba(231, 18, 143, 1.0)');
// A kind of blue for max.
gradient.addColorStop(1, 'rgba(0, 173, 238, 1.0)');
// Assign the gradient to the dataset's border color.
dataset.borderColor = gradient;
// Uncomment this for some effects, especially together with commenting the `fill: false` option below.
// dataset.backgroundColor = gradient;
}
}
};
Chart.pluginService.register(gradientLinePlugin);
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: ["First", "Second", "Third", "Fourth", "Fifth"],
datasets: [{
label: 'My Sample Dataset',
data: [20, 30, 50, 10, 40],
// No curves.
tension: 0,
// No fill under the line.
fill: false
}],
},
options: {
// Option for coloring the line with a gradient.
linearGradientLine: true,
scales: {
yAxes: [{
ticks: {
min: 0,
max: 100,
stepSize: 20
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>
<canvas id="myChart" width="400" height="200"></canvas>
還有一個pluginless方法,提到here,但該方法是缺乏的。根據該方法,人們必須將borderColor
設置爲應在創建圖表之前創建的漸變。梯度是靜態計算的,並且不會適合任意範圍或按原樣響應調整大小。
相關問題
- 1. Chartjs甜甜圈圖與漸變色
- 2. 餅圖與線性漸變 - 迅速
- 3. 與線性漸變(逗號)
- 4. chartjs折線圖Javascript
- 5. 漸變顏色條形圖每個酒吧使用ChartJS
- 6. IE10中的漸變線(漸變)
- 7. 線性漸變百分比與像素
- 8. 線性漸變css3
- 9. 線性漸變庫
- 10. 核心繪圖漸變線條
- 11. Google地圖漸變折線爲Android?
- 12. 背景圖像和線性漸變
- 13. 淡出線性漸變。 CSS或圖像?
- 14. 背景圖像上的線性漸變
- 15. 漸變填充react-highcharts的折線圖
- 16. AchartEngine中折線圖的漸變顏色
- 17. 邊界圖像的線性漸變
- 18. CSS背景漸變圖案 - 虛線
- 19. 帶線性漸變的邊界陰影與線性漸變的背景圖像組合?
- 20. 鉻帆布線性漸變= Firefox的帆布線性漸變
- 21. 跨瀏覽器的CSS背景圖像與線性漸變
- 22. imagick |漸變圖
- 23. jQuery將圖像漸變漸變爲0
- 24. Chartjs - pointColor按照漸變筆劃的當前顏色
- 25. 與背景漸變
- 26. 在GDI中用線性漸變繪製線條漸變的折線+
- 27. 漸變漸變?
- 28. CALayer:線性漸變問題
- 29. CSS線性漸變邊框
- 30. 反方向線性漸變
太棒了!非常感謝,我整個早上都試了一下,最後放棄了。 –
我也有這些掙扎,所以我編寫了教程來幫助別人,如果他們需要的話,你可以在這裏查看https://blog.vanila.io/chart-js-tutorial-how-to-make-gradient-line-chart -af145e5c92f9 – Plavookac