Javascript 空白PNG图像使用图表JS。toBase64Image()函数
我正在尝试保存使用chart.JS图形库创建的图表的副本。当我使用.toBase64Image()函数显示或保存.png图像时,得到的是一张空白图像。我尝试过在img标签中显示并用PHP保存图像。save64Img是我显示/保存图像的功能。有人使用过这个功能吗Javascript 空白PNG图像使用图表JS。toBase64Image()函数,javascript,php,html,charts,chart.js,Javascript,Php,Html,Charts,Chart.js,我正在尝试保存使用chart.JS图形库创建的图表的副本。当我使用.toBase64Image()函数显示或保存.png图像时,得到的是一张空白图像。我尝试过在img标签中显示并用PHP保存图像。save64Img是我显示/保存图像的功能。有人使用过这个功能吗 lineOptions = { scaleShowGridLines : true, scaleGridLineColor : "rgba(0,0,0,.05)", scaleGridLin
lineOptions = {
scaleShowGridLines : true,
scaleGridLineColor : "rgba(0,0,0,.05)",
scaleGridLineWidth : 1,
bezierCurve : false,
pointDot : true,
pointDotRadius : 4,
pointDotStrokeWidth : 1,
pointHitDetectionRadius : 20,
datasetStroke : true,
datasetStrokeWidth : 1,
datasetFill : true,
responsive: true,
scaleOverride : true,
scaleSteps : 5,
scaleStepWidth : 20,
scaleStartValue : 0
}
lineChart = {
labels : label,
datasets : [
{
label: "stat1",
fillColor : "rgba(128,0,0,0.2)",
strokeColor : "rgba(128,0,0,1)",
pointColor : "rgba(128,0,0,1)",
pointStrokeColor : "#fff",
pointHighlightFill : "#fff",
pointHighlightStroke : "rgba(128,0,0,1)",
data : stat1
},
{
label: "stat2",
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 : stat2
}
]
};
var myChart = new Chart(ctx).Line(lineChart, lineOptions);
save64Img(myChart.toBase64Image());
----------
<?php
$data = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $img));
file_put_contents('/tmp/image.png', $data);
?>
lineOptions={
scaleShowGridLines:对,
scaleGridLineColor:“rgba(0,0,0,05)”,
scaleGridLineWidth:1,
贝塞尔曲线:错,
是的,
点半径:4,
pointDotStrokeWidth:1,
PointHit检测半径:20,
datasetStroke:对,
datasetStrokeWidth:1,
datasetFill:true,
回答:是的,
scaleOverride:对,
比例:5,
刻度宽度:20,
scaleStartValue:0
}
线形图={
标签:标签,
数据集:[
{
标签:“stat1”,
fillColor:“rgba(128,0,0,0.2)”,
strokeColor:“rgba(128,0,0,1)”,
点颜色:“rgba(128,0,0,1)”,
pointStrokeColor:“fff”,
pointHighlightFill:“fff”,
pointHighlightStroke:“rgba(128,0,0,1)”,
数据:stat1
},
{
标签:“stat2”,
填充颜色:“rgba(151187205,0.2)”,
strokeColor:“rgba(151187205,1)”,
点颜色:“rgba(151187205,1)”,
pointStrokeColor:“fff”,
pointHighlightFill:“fff”,
pointHighlightStroke:“rgba(151187205,1)”,
数据:stat2
}
]
};
var myChart=新图表(ctx).Line(折线图,折线选项);
save64Img(myChart.toBase64Image());
----------
您必须通过额外的回调调用save64Img(myChart.toBase64Image())
。
您可以将此回调添加到选项中
lineOptions = {
onAnimationComplete: function () {
save64Img(myChart.toBase64Image());
}
}
我知道你问这个问题已经有一段时间了,但是在Chart.js v2中,你必须在动画选项中使用
onComplete
方法
animation: {
duration: 1500,
onComplete: function (animation) {
this.toBase64Image();
}
非常感谢。花了好几个小时才找到这个对我有用的答案。