簡體   English   中英

如何使用 C# 在 asp.net 中的 ChartJs 中綁定字符串?

[英]How to bind string in ChartJs in asp.net using C#?

我正在嘗試將 object 綁定到 ChartJS 數據集數據,但它沒有出現並且沒有錯誤。 下面是我的 C# 代碼:

 [WebMethod]
    public static List<object> GetChartData()
    {
        List<object> chartData = new List<object>();
        List<string> labels = new List<string>();
        List<int> data = new List<int>();                       
        labels.Add("Completed jobs");
        data.Add(Convert.ToInt32(90));
        labels.Add("Current running jobs");
        data.Add(Convert.ToInt32(10));
        chartData.Add(labels.ToArray());
        chartData.Add(data.ToArray());
        return chartData;
    }

我的客戶端代碼:

  function OnSuccess_(reponse) {
        var aData = reponse.d;
        window.ubdChart = new Chart(t, {
            type: "pie",
            data: {
                datasets: [{
                    hoverBorderColor: "#ffffff",                        
                    data: aData, // Bind int values here
                    backgroundColor: ["rgba(0,123,255,0.9)", "rgba(0,123,255,0.5)"]
                }],
                labels: ["Completed jobs", "Current running jobs"]  //Bind label values here
            },               
            }
        })
    };

更改代碼后,它正在工作,如下所示。

 function OnSuccess_(reponse) {
        var aData = reponse.d;
        console.log(aData);
        window.ubdChart = new Chart(t, {
            type: "pie",
            data: {
                datasets: [{
                    hoverBorderColor: "#ffffff",                        
                    data: aData[1], 
                    backgroundColor: ["rgba(0,123,255,0.9)", "rgba(0,123,255,0.5)"]
                }],
                labels: aData[0] 
            },
            options: {
                legend: {
                    position: "bottom",
                    labels: { padding: 25, boxWidth: 20 }
                }, cutoutPercentage: 0, tooltips: { custom: !1, mode: "index", position: "nearest" }
            }
        })
    };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM