Home › Forums › Chart Support › Using CanvasJS with ASP.NET C# and SQL › Reply To: Using CanvasJS with ASP.NET C# and SQL
Many Thanks, i don’t know if i can reply via this medium below is a section of my code that return datatable and convert to JSON
What I have tried:
unit of my code:
`mData = ConnectAll.GetDataObject(“Select sum(totalg9) as Total from tbl_htc2 group by months”); try { var JSONString = new StringBuilder(); if (mData.Rows.Count > 0) { JSONString.Append(“[“); for (int i = 0; i < mData.Rows.Count; i++) { JSONString.Append(“{“); for (int j = 0; j < mData.Columns.Count; j++) { if (j < mData.Columns.Count – 1) { JSONString.Append(“\”” + mData.Columns[j].ColumnName.ToString() + “\”:” + “\”” + mData.Rows[i][j].ToString() + “\”,”); } else if (j == mData.Columns.Count – 1) { JSONString.Append(“\”” + mData.Columns[j].ColumnName.ToString() + “\”:” + “\”” + mData.Rows[i][j].ToString() + “\””); } } if (i == mData.Rows.Count – 1) { JSONString.Append(“}”); } else { JSONString.Append(“},”); } } JSONString.Append(“]”); }
Response.Redirect(string.Format(“~/data_load/monthly_trend.aspx?mData={0}”, JSONString.ToString()));
The page that render the chart :
window.onload = function () { var mTD = ‘ <%=this.DT%>’ var chart = new CanvasJS.Chart(“chartContainer”, { title: { text: “Trend Analysis” }, axisY: { // minimum: 50, maximum: 300, interval:50 }, axisX: { // minimum: 50, maximum: 200 //interval:-10 }, data: [ { type: “column”, dataPoints: [ mTD ] } ] });
chart.render(); }