Chart.js折线图设置背景颜色
问题描述:
我正在使用Chart.js并希望将折线图转换为PNG。问题是图像总是以透明背景下载,这不是我需要的。我尝试了很多选项,没有真正有效。
I'm working with Chart.js and want to convert a line chart to a PNG. The problem is that the image always downloads with a transparent background, which is not what I need. I tried many options, nothing really worked.
和建议?
答
以下是获取ChartJS完全不透明版本的一种方法:
等待图表完全动画完成。您可以通过将 onAnimationComplete
属性添加到图表中来实现此目的。
Wait until the chart is fully animated out and complete. You can do this by adding the onAnimationComplete
property to the chart.
在 onAnimationComplete中
function:
In the onAnimationComplete
function:
- 创建一个与图表大小相同的内存临时画布。
- 用白色填充临时画布
-
drawImage
ChartJS画布在白色填充的临时画布上 - 从临时画布创建一个图像。
- Create an in-memory temporary canvas of equal size as your chart.
- Fill the temp canvas with white
-
drawImage
the ChartJS canvas over the white-filled temp canvas - Create an image from the temp canvas.
以下是如何做到的:
var ctx = document.getElementById("canvas").getContext("2d");
window.myLine = new Chart(ctx).Line(lineChartData, {
responsive: true,
onAnimationComplete:function(){
var tcanvas=document.createElement('canvas');
var tctx=tcanvas.getContext('2d');
tcanvas.width=ctx.canvas.width;
tcanvas.height=ctx.canvas.height;
tctx.fillStyle='white';
tctx.fillRect(0,0,tcanvas.width,tcanvas.height);
tctx.drawImage(canvas,0,0);
var img=new Image();
img.onload=function(){
document.body.appendChild(img);
}
img.src=tcanvas.toDataURL();
}
});
以下是示例代码和演示:
var randomScalingFactor = function(){ return Math.round(Math.random()*100)};
var randomColorFactor = function(){ return Math.round(Math.random()*255)};
var lineChartData = {
labels : ["January","February","March","April","May","June","July"],
datasets : [
{
label: "My First dataset",
fillColor : "rgba(220,220,220,0.2)",
strokeColor : "rgba(220,220,220,1)",
pointColor : "rgba(220,220,220,1)",
pointStrokeColor : "#fff",
pointHighlightFill : "#fff",
pointHighlightStroke : "rgba(220,220,220,1)",
data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
},
{
label: "My Second dataset",
fillColor : "rgba(151,187,205,0.2)",
strokeColor : "rgba(151,187,205,1)",
pointColor : "rgba(151,187,205,1)",
pointStrokeColor : "#fff",
pointHighlightFill : "#fff",
pointHighlightStroke : "rgba(151,187,205,1)",
data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
}
]
}
var ctx = document.getElementById("canvas").getContext("2d");
window.myLine = new Chart(ctx).Line(lineChartData, {
responsive: true,
onAnimationComplete:function(){
var tcanvas=document.createElement('canvas');
var tctx=tcanvas.getContext('2d');
tcanvas.width=ctx.canvas.width;
tcanvas.height=ctx.canvas.height;
tctx.fillStyle='white';
tctx.fillRect(0,0,tcanvas.width,tcanvas.height);
tctx.drawImage(canvas,0,0);
var img=new Image();
img.onload=function(){
document.body.appendChild(img);
}
img.src=tcanvas.toDataURL();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js"></script>
<h4>ChartJS line chart</h4>
<div style="width:30%">
<div>
<canvas id="canvas" height="450" width="600"></canvas>
</div>
</div>
<h4>Fully opaque chart as image</h4>