簡體   English   中英

圖表區域背景顏色chartjs

[英]Chart area background color chartjs

我的圖表 js 有問題,我想像上圖一樣為圖表區域着色在此處輸入圖像描述

我嘗試從charJs Docs中查找配置,但沒有匹配項。 是否可以更改圖表區域的背景顏色? 如果可能的話,有人可以幫助我嗎?

Html

<canvas id="barChart" width="600" height="300"></canvas>

Javascript

var ctx = document.getElementById("barChart");
var barChart = new Chart(ctx,{
  type: 'bar',
  data: {
    labels:["Label1","Label2","Label3","Label4"],
    borderColor : "#fffff",
    datasets: [
      {
        data: ["2","3","1","4"],
        borderColor : "#fff",
        borderWidth : "3",
        hoverBorderColor : "#000",
        backgroundColor: [
          "#f38b4a",
          "#56d798",
          "#ff8397",
          "#6970d5" 
        ],
        hoverBackgroundColor: [
          "#f38b4a",
          "#56d798",
          "#ff8397",
          "#6970d5"
        ]
      }]
  },
  options: {
    scales: {
      yAxes: [{
        ticks:{
          min : 0,
          stepSize : 1,
          fontColor : "#000",
          fontSize : 14
        },
        gridLines:{
          color: "#000",
          lineWidth:2,
          zeroLineColor :"#000",
          zeroLineWidth : 2
        },
        stacked: true
      }],
      xAxes: [{
        ticks:{
          fontColor : "#000",
          fontSize : 14
        },
        gridLines:{
          color: "#fff",
          lineWidth:2
        }
      }]
    },
    responsive:false
  }
});

這是我當前的代碼jsFiddle

所以每個人都可以嘗試尋找解決方案。 謝謝你的幫助。

沒有內置方法可以更改背景顏色,但您可以使用 CSS。 JSF中。

ctx.style.backgroundColor = 'rgba(255,0,0,255)';

編輯

如果你想填充圖表的確切區域而不是整個 div,你可以編寫自己的 chart.js 插件。 JSFiddle上試試。

        Chart.pluginService.register({
            beforeDraw: function (chart, easing) {
                if (chart.config.options.chartArea && chart.config.options.chartArea.backgroundColor) {
                    var ctx = chart.chart.ctx;
                    var chartArea = chart.chartArea;

                    ctx.save();
                    ctx.fillStyle = chart.config.options.chartArea.backgroundColor;
                    ctx.fillRect(chartArea.left, chartArea.top, chartArea.right - chartArea.left, chartArea.bottom - chartArea.top);
                    ctx.restore();
                }
            }
        });

        var config = {
  type: 'bar',
  data: {
    labels:["Label1","Label2","Label3","Label4"],
    borderColor : "#fffff",
    datasets: [
      {
        data: ["2","3","1","4"],
        borderColor : "#fff",
        borderWidth : "3",
        hoverBorderColor : "#000",
        backgroundColor: [
          "#f38b4a",
          "#56d798",
          "#ff8397",
          "#6970d5" 
        ],
        hoverBackgroundColor: [
          "#f38b4a",
          "#56d798",
          "#ff8397",
          "#6970d5"
        ]
      }]
  },
  options: {
    scales: {
      yAxes: [{
        ticks:{
          min : 0,
          stepSize : 1,
          fontColor : "#000",
          fontSize : 14
        },
        gridLines:{
          color: "#000",
          lineWidth:2,
          zeroLineColor :"#000",
          zeroLineWidth : 2
        },
        stacked: true
      }],
      xAxes: [{
        ticks:{
          fontColor : "#000",
          fontSize : 14
        },
        gridLines:{
          color: "#fff",
          lineWidth:2
        }
      }]
    },
    responsive:false,
    chartArea: {
        backgroundColor: 'rgba(251, 85, 85, 0.4)'
    }
  }
};

        var ctx = document.getElementById("barChart").getContext("2d");
        new Chart(ctx, config);

畫布背景在定義上是透明的,就像任何元素一樣,因此您只需要在畫布中定義背景顏色,例如,在您的情況下,您將需要這個 CSS:

JSFiddle

canvas#barChart {
  background-color: #f00;
}

或 HTML 內聯,以澄清這個想法:

<canvas id="barChart" width="600" height="300" style="background-color: #f00;"></canvas>

這適用於最新的chartjs

    const custom_canvas_background_color = {
      id: 'custom_canvas_background_color',
      beforeDraw: (chart, args, options) => {
        const {
          ctx,
          chartArea: { top, right, bottom, left, width, height },
          scales: { x, y },
        } = chart;
        ctx.save();
        ctx.globalCompositeOperation = 'destination-over';
        ctx.fillStyle = '#E5E5E5';
        ctx.fillRect(left, top, width, height);
        ctx.restore();
      },
    };

然后添加為插件

 plugins: [custom_canvas_background_color]

您可以通過實現這個 Chartjs 插件插件/ 示例來實現這一點。 該插件允許您增強圖表。 例如放大或平移到您的圖表。 而且還要改變背景顏色。 使用setBackgroundColor(color); //updates the background color of the chart setBackgroundColor(color); //updates the background color of the chart

如果要為整個 Canvas(而不僅僅是繪圖區域)設置顏色,可以這樣做:

Chart.pluginService.register({
  beforeDraw: ({ config: { options }, ctx }) => {
    if (options.chartArea?.backgroundColor) {
      ctx.save();
      ctx.fillStyle = options.chartArea.backgroundColor;
      ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
      ctx.restore();
    }
  }
});

const chart = new Chart(document.getElementById('chart'), {
  options: {
    chartArea: {
      backgroundColor: 'rgba(255, 255, 255, 1)'
    }
  }
});

使用chart.toBase64Image()時,這將應用於 PNG

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM