1 回答

TA貢獻1831條經驗 獲得超9個贊
最初,您需要一個支架來存放您的結果。例如,您可以創建一個如下所示的 holder 類
public class MapResult
{
public string[] Products { get; set; }
public int[] Quantity { get; set; }
}
控制器
您可以從控制器設置 MapResult 類的值,它有 2 個數組,一個用于產品,一個用于數量。
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 結果包含兩個數組。您可以將它們添加到地圖中。
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
}
}]
}
}
});
添加回答
舉報