【问题标题】:How to export Chart.js chart using toBase64Image, but with no transparency?如何使用 toBase64Image 导出 Chart.js 图表,但没有透明度?
【发布时间】:2021-07-30 22:26:36
【问题描述】:

我的应用程序中的图表很少,是使用 Chart.js 库创建的。我需要将它们导出并包含在我的应用程序生成的某种报告中。下面附上一个示例图表代码:

export default {
  extends: Line,
  mixins: [reactiveProp],
  data() {
    return {
      options: {
        maintainAspectRatio: false,
        responsive: true,
        legend: {
          display: true,
          position: 'bottom',
        },
        scales: {
          yAxes: [
            {
              ticks: {
                suggestedMin: 0,
                callback(tick) {
                  return `${tick}%`;
                },
              },
            },
          ],
        },
        tooltips: {
          callbacks: {
            label(tooltipItem, data) {
              const dataset = data.datasets[tooltipItem.datasetIndex];
              const currentValue = dataset.data[tooltipItem.index];
              return ` ${dataset.label}: ${currentValue}%`;
            },
          },
        },
        animation: {
          // eslint-disable-next-line no-unused-vars
          onComplete(animation) {
            store.commit('myModule/myMutation', this.toBase64Image());
          },
        },
      },
    };
  },
  mounted() {
    this.renderChart(this.chartData, this.options);
  },
};

正如您所注意到的,我使用 onComplete 将图表保存在我的商店中。我在那里调用函数 toBase64Image() 。几乎可以正确保存图像,但忽略背景,因此结果是透明的。我想要图表,但序列化后有白色背景。有可能实现吗?我该如何解决这个问题(我尝试使用图表样式设置背景颜色,但序列化结果仍然是透明的)?感谢您的帮助

【问题讨论】:

  • 我不太清楚你要扩展的 Line 组件是什么。

标签: javascript vue.js chart.js


【解决方案1】:

出现这种行为是因为图表上没有绘制任何内容作为背景。这可以通过像这样的自定义内联插件来解决(fillStyle 是背景的颜色):

 plugins: [{
        id: 'custom_canvas_background_color',
        beforeDraw: (chart) => {
          const ctx = chart.canvas.getContext('2d');
          ctx.save();
          ctx.globalCompositeOperation = 'destination-over';
          ctx.fillStyle = 'white';
          ctx.fillRect(0, 0, chart.canvas.width, chart.canvas.height);
          ctx.restore();
        }
      }]

示例:

var options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        borderWidth: 1
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        borderWidth: 1
      }
    ]
  },
  options: {
    scales: {
      yAxes: [{
        ticks: {
          reverse: false
        }
      }]
    }
  },
  plugins: [{
    id: 'custom_canvas_background_color',
    beforeDraw: (chart) => {
      const ctx = chart.canvas.getContext('2d');
      ctx.save();
      ctx.globalCompositeOperation = 'destination-over';
      ctx.fillStyle = 'white';
      ctx.fillRect(0, 0, chart.canvas.width, chart.canvas.height);
      ctx.restore();
    }
  }]
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js" integrity="sha512-hZf9Qhp3rlDJBvAKvmiG+goaaKRZA6LKUO35oK6EsM0/kjPK32Yw7URqrq3Q+Nvbbt8Usss+IekL7CRn83dYmw==" crossorigin="anonymous"></script>
</body>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-11
    • 2022-01-20
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多