如何在Chart.js中创建带有背景蒙版的线性渐变

ryevplcw  于 2023-02-12  发布在  Chart.js
关注(0)|答案(1)|浏览(149)

我想在Chart.js中重新创建这个图表配色方案。到目前为止,我已经成功地为笔划和背景颜色创建了水平线性渐变,但我找不到一种方法来为背景颜色创建不透明蒙版,以将其“混合”到页面背景中。
这是我目前的图表

  • 注意:* 我可以使用css属性在画布上创建一个不透明遮罩:

-webkit-mask-image: -webkit-gradient(linear, left 50%, left bottom, from(rgba(0,0,0,1)), to(rgba(0,0,0,0)))
但是这种方法掩盖了图表的整个底部,例如图表的笔划

我该如何着手只屏蔽图表的背景色呢?

Chart.js设置

data = {
  labels: labels,
  datasets: [
    {
      label: 'Dataset 1',
      fill: true,
      data: Utils.numbers(NUMBER_CFG),
      borderColor: getGradient,
      pointBorderColor: getGradient,
      pointBackgroundColor: getGradient,
      pointHoverBackgroundColor: getGradient,
      pointHoverBorderColor: getGradient,
      backgroundColor: getGradient
    },
  ]
};

let width, height, gradient;
function gradient(ctx, chartArea) {
  const chartWidth = chartArea.right - chartArea.left;
  const chartHeight = chartArea.bottom - chartArea.top;
  if (!gradient || width !== chartWidth || height !== chartHeight) {
    // Create the gradient because this is either the first render
    // or the size of the chart has changed
    width = chartWidth;
    height = chartHeight;
    var gradientStroke = ctx.createLinearGradient(chartArea.right, chartArea.top, chartArea.left, chartArea.top);
    gradientStroke.addColorStop(0, "#80b6f4");
    gradientStroke.addColorStop(1, "#f49080");
  }

  return gradientStroke;
}

function getGradient(context) {
  const chart = context.chart;
  const {ctx, chartArea} = chart;

  if (!chartArea) {
    // This case happens on initial chart load
    return;
  }
  return gradient(ctx, chartArea);
}
pbgvytdp

pbgvytdp1#

我希望你现在已经解决了这个问题。无论如何,我认为你应该试着改变你传递给createLinearGradient function的值。参见this example(它不是我的,但是帮助我理解了这一点)。

var ctx = document.getElementById("chart").getContext("2d"); 

/*** Gradient ***/
var gradient = ctx.createLinearGradient(0, 0, 0, 200);
    gradient.addColorStop(0, 'rgba(250,174,50,1)');   
    gradient.addColorStop(1, 'rgba(250,174,50,0)');
/***************/

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 : 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]
                }
            ]
        };

        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 myLineChart = new Chart(ctx).Line(data, options);
<canvas id="chart" width="800" height="400"></canvas>

相关问题