【问题标题】:Chart area background color chartjs图表区域背景颜色chartjs
【发布时间】:2016-11-24 09:59:44
【问题描述】:

我的图表 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

所以每个人都可以尝试寻找解决方案。 感谢您的帮助。

【问题讨论】:

    标签: javascript jquery canvas html5-canvas chart.js


    【解决方案1】:

    没有改变背景颜色的内置方法,但您可以使用 CSS。 JSFiddle.

    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);
    

    【讨论】:

    • 感谢您的回答,但我只需要在我的问题中像图像一样的图表区域内着色,而不是标签区域
    • 但是为什么yAxis不是从0开始?
    • @PajarFathurrahman。对不起,这是一个错字。这是最终的JSFiddle
    • 雷达图可以播种吗?不给整个矩形上色,而只给雷达上色?
    • 我有一个相关的问题:stackoverflow.com/questions/51358701/…。请帮我提个建议。谢谢。
    【解决方案2】:

    根据定义,画布背景是透明的,就像任何元素一样,因此您只需要在画布中定义背景颜色,例如,在您的情况下,您将需要这个 CSS:

    JSFiddle

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

    或HTML内联,以澄清这个想法:

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

    【讨论】:

    • 这在屏幕上正确显示,但是当右键单击画布并将其复制或保存为图像时,图像背景仍然未设置(透明)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    • 2017-07-11
    • 2018-09-01
    相关资源
    最近更新 更多