在下面的代码中,我从response
参数获取数据,然后呈现一个图形。然后,通过从imgData
参数获取图像的base64字符串进行导出,然后使用AJAX调用在会话中设置base64字符串。
之后,我要进行另一个AJAX调用以创建报告。在此AJAX调用中,我将会话中存储的图像用作base64字符串。在打印报告时,我得到了部分图像。这是由于在某些情况下setTimeout
函数中的大部分数据无法正常工作。
当我删除setTimeout
函数时,由于它在完成任务之前进行了另一个AJAX调用,因此图像未在报告中生成。
有人可以给我解决方案吗?预先感谢。
function LoadChart(response, callback) {
if (response.length > 1) {
//var aData = JSON.parse(response);
document.getElementById('loading').style.display = "block";
var aData = $.parseJSON(response);
var chartData = [];
$.each(aData, function(inx, val) {
var obj = {};
obj.AVARating = val.AVARating;
obj.Per = val.Per;
//obj.color = "#696969";
chartData.push(obj);
});
var assessmentId = $('#hdnAssessmentId').val();
var siteName = $('#hdnSiteName').val();
var companyName = $('#hdnCompanyDesc').val();
var countryName = $('#hdnCountryDesc').val();
var latitude = $('#hdnLatitude').val();
var longitude = $('#hdnLongitude').val();
var division = $('#hdnDivisionDesc').val();
var reportedDate = $('#hdnReportedDate').val();
var username = $('#hdnWindowsID').val();
var countryCode = $('#hdnCountryCode').val();
var CompanyCode = $('#hdnCompanyCode').val();
var SiteCode = $('#hdnSiteCode').val();
var DivisionCode = $('#hdnDivisionCode').val();
//am4core.disposeAllCharts();
var chart = am4core.create("donutchart", am4charts.RadarChart);
//chart.dataSource.url = 'VATReportGeneration.aspx?key=1&windowsId=' + username + '&divisionCode=' + DivisionCode + '&countryCode=' + countryCode + '&companyCode=' + CompanyCode + '&siteCode=' + SiteCode + '&assessmentId=' + assessmentId;
chart.data = chartData;
//chart.data = response;
/* Add data */
//chart.data = [{
// "AVARating": "Perimeter",
// "Per": 10
//}, {
// "AVARating": "CCTV",
// "Per": 12
//}, {
// "AVARating": "Intrusion Detection",
// "Per": 30
//}, {
// "AVARating": "Information Protection",
// "Per": 40
//}, {
// "AVARating": "Guarded forces",
// "Per": 50
//}, {
// "AVARating": "Safe Haven",
// "Per": 60
//}, {
// "AVARating": "Control Room",
// "Per": 70
//}, {
// "AVARating": "Security Plan",
// "Per": 80
//}, {
// "AVARating": "Procedures",
// "Per": 90
//}];
/* Create axes */
var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "AVARating";
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.renderer.axisFills.template.fill = chart.colors.getIndex(2);
valueAxis.renderer.axisFills.template.fillOpacity = 0.05;
valueAxis.renderer.gridType = "polygons";
valueAxis.min = 0;
valueAxis.max = 100;
valueAxis.renderer.minGridDistance = 10;
/* Create and configure series */
var series = chart.series.push(new am4charts.RadarSeries());
series.dataFields.valueY = "Per";
series.dataFields.categoryX = "AVARating";
series.strokeWidth = 3;
series.fillOpacity = 0.3;
var circleBullet = series.bullets.push(new am4charts.CircleBullet());
circleBullet.circle.stroke = am4core.color("#fff");
circleBullet.circle.strokeWidth = 2;
setTimeout(function() {
chart.exporting.getImage("png").then(function(imgData) {
//console.log(imgData); // contains exported image data
console.log(imgData);
var image = imgData.replace('data:image/png;base64,', '');
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "VATReportGeneration.aspx/SetImageInSession",
data: JSON.stringify({
"image": image
}),
dataType: "json",
success: function(data) {
if (data != '') {
// document.getElementById('loading').style.display = "none";
var url = "../../ReportHandler/VATConfigurationFileHandler.aspx";
var file = url + '?VAT_Report=VAT_Report&assessmentId=' + assessmentId + '&siteName=' + siteName + '&companyName=' + companyName + '&countryName=' + countryName + '&latitude=' + latitude + '&longitude=' + longitude + '&division=' + division + '&reportedDate=' + reportedDate; //+'&image='+txt;
$get("exporthandler").src = file;
chart.dispose();
document.getElementById('loading').style.display = "none";
}
},
error: function(data) { }
});
});
}, 5000);
}
}
答案 0 :(得分:0)
您可以使用setInterval
setInterval(function() {
// Code Here
}, 3000);
3000ms是间隔的超时时间