因此,我有一个ChartJS图表,并且尝试更新此图表的标题,到目前为止,我已经使它起作用并显示了新标题!(woohoo)。
但是,当我尝试导出带有新标题的图表时,出现了我的问题。
我正在转换为base64 png。创建一个a-tag,以编程方式单击a-tag,然后删除该a-tag,最后将标题更改回旧标题。
直到我试图将带有新标题的图表保存为base64 png为止,一切正常。然后新标题不在导出的.png中
function downloadImage($id) {
console.log("");
console.log("*///// SAVING /////*");
//get the charts variable name (stored as a data-attribute on the canvas element called chart-var)
var chart_variable_name = $("#" + $id).data('chart-var');
//convert this to a usable variable instead of a string
var chart_variable = eval(chart_variable_name);
console.log("");
console.log("Get Chart Variable");
console.log(chart_variable);
//get canvas element using id passed in
var ctx = $("#" + $id);
//get data-export-title attribute (multiline title with Title, Date, Source)
var exportTitle = $(ctx).data('export-title');
console.log("");
console.log("Get Chart");
console.log(ctx);
console.log("");
console.log("Get Chart Export Title");
console.log(exportTitle);
//get the old title from the ChartJS Object (ready to use for going back to old title)
var old_title = chart_variable.options.title.text;
console.log("");
console.log("Get Chart Old Title");
console.log(old_title);
// get the exportTitle as an array, this will allow for MultiLine titles on export
arr = exportTitle.split(',');
console.log("");
console.log("Get Chart New Title");
console.log(arr);
//push String "Title" + old_title variable so we have the title added at the end of the array
arr.push("Title: " + old_title);
//set the charts title text to the new Multiline export title.
chart_variable.options.title.text = arr;
//run the update on the chart
chart_variable.update();
chart_variable = eval(chart_variable_name);
console.log("");
console.log("Get Chart Current Options");
console.log(chart_variable.options);
//convert the chart to a base64 link PNG
var newBase64 = chart_variable.toBase64Image();
//insert an <a> tag which is hidden, before the save button - irrelevant where it goes as it wont be seen but for consistency
$("<a id='" + $id + "-button-temp' style='display:none;' class='button-style' href='" + newBase64 + "' download='" + $id + ".png' >Download</a>").insertAfter($('#' + $id + '-button'));
//programmatically find and click the <a> tag to initiate the download of the image
($(document).find("#" + $id + "-button-temp")[0]).click();
//programmatically remove the <a> tag so we dont clutter the page with unecessary HTML that are hidden
($(document).find("#" + $id + "-button-temp")[0]).remove();
//set chart title back to previous title
//chart_variable.options.title.text = old_title;
}
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么会这样...?
发生这种情况是因为,当您通过调用方法更新图表(更改标题后)时update(),会使用动画重新渲染整个图表,这需要一些时间(大约1000毫秒)才能完成,从而使图表更新过程延迟了,并且由于此过程是异步进行的,因此即使在图表完全更新之前,您的图像保存代码也会执行,这会使新标题不会显示在导出的图像上。
因此,基本上,您需要的是在更改图表标题后不同步地更新图表,并且可以通过以下两种方式之一来完成:
1.将一个config对象(参数)传递给属性设置为的update方法:duration0
chart_variable.update({
duration: 0
});
Run Code Online (Sandbox Code Playgroud)
2.将0参数作为参数传递给update方法(请参见防止动画):
chart_variable.update(0);
Run Code Online (Sandbox Code Playgroud)
chart_variable.update({
duration: 0
});
Run Code Online (Sandbox Code Playgroud)
chart_variable.update(0);
Run Code Online (Sandbox Code Playgroud)