Javascript ajax调用完成后,如何呈现我的顶点图表?
我用的是apex动态图表 我被困在ajax调用和图表渲染部分。有国家条形图。当我点击一个国家栏时,所选的countryID被传递给下面的updateStacked100Chart函数(该函数使用countryID调用ajax webapi),以在stacked100 apex图表中获取该国家的地区数据。在ajax调用完成之前,首先呈现空的stacked100 apex图表。我该如何解决这个问题Javascript ajax调用完成后,如何呈现我的顶点图表?,javascript,jquery,ajax,apexcharts,webapi,Javascript,Jquery,Ajax,Apexcharts,Webapi,我用的是apex动态图表 我被困在ajax调用和图表渲染部分。有国家条形图。当我点击一个国家栏时,所选的countryID被传递给下面的updateStacked100Chart函数(该函数使用countryID调用ajax webapi),以在stacked100 apex图表中获取该国家的地区数据。在ajax调用完成之前,首先呈现空的stacked100 apex图表。我该如何解决这个问题 //sourceChart=CountryBarChart,destChartIDToUpdate=
//sourceChart=CountryBarChart,destChartIDToUpdate=Stacked100RegionYearChart(Chart to be updated based on country bar selected)
function updateStacked100Chart(sourceChart, destChartIDToUpdate) {
var series = [];
var seriesIndex = 0;
var categorySource = [];
var color = ['#008FFB', '#00E396', '#FEB019', '#FF4560', '#775DD0', '#00D9E9', '#FF66C3'];
if (sourceChart.w.globals.selectedDataPoints[0]) {
//Get selected country bar in CountryBarChart
var selectedPoints = sourceChart.w.globals.selectedDataPoints;
for (var i = 0; i < selectedPoints[seriesIndex].length; i++) {
var selectedIndex = selectedPoints[seriesIndex][i];
var selectedCountry = sourceChart.w.globals.labels[selectedIndex];
$.ajax({
url: "http://localhost:51604/api/Sales/GetSalesByRegion",
type: "Get",
dataType: "json",
cache: false,
data: {
"CountryId": selectedCountry,
"page": 0,
"limit": 0
},
success: function (data) {
// Prepare series & xaxis category data for stacked100 region chart
$.each(data.salesByRegQuery, function (index, item) {
series.push(
{
name: item.EnglishProductCategoryName,
data: item.Total_Margin
});
categorySource.push(item.CalendarYear);
});
},
complete: function (data) {
},
error: function (data) {
}
});
}
//These lines of codes below get executed first before ajax call is completed.
if (series.length === 0) series = [{
data: []
}];
//So, region stacked100 apex chart source data for series and xaxis aren't updated
return ApexCharts.exec(destChartIDToUpdate, 'updateOptions', {
series: series,
xaxis: categorySource,
colors: color,
fill: {
colors: color
}
});
}
}
//sourceChart=CountryBarChart,destChartIDToUpdate=Stacked100RegionYearChart(根据所选的国家栏更新图表)
函数更新打包100Chart(sourceChart、destChartIDToUpdate){
var系列=[];
var系列指数=0;
var categorySource=[];
变量颜色=['#008FFB'、'#00E396'、'#FEB019'、'#FF4560'、'#775DD0'、'#00D9E9'、'#FF66C3'];
if(sourceChart.w.globals.selectedDataPoints[0]){
//在CountryBarChart中获取选定的国家/地区栏
var selectedPoints=sourceChart.w.globals.selectedDataPoints;
对于(变量i=0;i
之所以发生这种情况,是因为默认情况下Ajax是异步的。您应该在“Success”部分调用render。所以你可以肯定,“系列”数组有数据。这是我几个小时以来的问题!!!!这是一条很棒的信息。我在用一个函数做一个ajax调用。通过设置async:false
,我能够运行由ajax设置的return
全局变量。好东西!