1 回答
TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超9個(gè)贊
最初,您需要一個(gè)支架來(lái)存放您的結(jié)果。例如,您可以創(chuàng)建一個(gè)如下所示的 holder 類
public class MapResult
{
public string[] Products { get; set; }
public int[] Quantity { get; set; }
}
控制器
您可以從控制器設(shè)置 MapResult 類的值,它有 2 個(gè)數(shù)組,一個(gè)用于產(chǎn)品,一個(gè)用于數(shù)量。
public ActionResult DoChart(string data)
{
string[] product = { "Bread", "Milk", "Eggs", "Butter" };
int[] quant = { 10, 20, 30, 40 };
var mapResult = new MapResult()
{
Products = product,
Quantity = quant
};
return Json(mapResult, JsonRequestBehavior.AllowGet);
}
AJAX 成功代碼
AJAX 結(jié)果包含兩個(gè)數(shù)組。您可以將它們添加到地圖中。
success: (result, status) => {
alert(result.Products);
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: result.Products,
datasets: [{
label: '# of Votes',
data: result.Quantity,
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
添加回答
舉報(bào)
