javascript - 如何在 C# 中使用 chart.js 和 Ajax 绘制条形图
问题描述
我正在尝试使用以下脚本和代码在我的应用程序中使用带有 Ajax 调用的 chart.js 和 C# 代码显示条形图。图表未显示。代码在 C# 中,我在 aspx 文件中编写 UI 代码。
$(document).ready(function () {
var jsonData = JSON.stringify({
});
$.ajax({
type: "POST",
url: "Home.asmx/GetMonthtlyInspectionData",
data: jsonData,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var data = response.d.Datas;
var label = response.d.Labels;
let ctx = $("#barChart").get(0).getContext("2d");
ctx.height = 130;
new Chart(ctx, {
type: 'bar',
data: {
labels: label,
type: 'bar',
defaultFontFamily: 'Montserrat',
datasets: [{
data: data,
label: "Approved Request",
backgroundColor: 'rgba(54, 162, 235, 0.2)',
borderColor: 'transparent',
borderWidth: 2,
pointStyle: 'circle',
pointRadius: 5,
pointBorderColor: '#847DFA',
pointBackgroundColor: '#fff',
}]
},
options: {
responsive: !0,
maintainAspectRatio: true,
tooltips: {
mode: 'index',
titleFontSize: 12,
titleFontColor: '#fff',
bodyFontColor: '#fff',
backgroundColor: '#000',
titleFontFamily: 'Montserrat',
bodyFontFamily: 'Montserrat',
cornerRadius: 3,
intersect: false,
},
legend: {
display: true,
position: 'top',
labels: {
usePointStyle: true,
fontFamily: 'Montserrat',
},
},
scales: {
xAxes: [{
beginAtZero: true,
display: true,
gridLines: {
display: false,
drawBorder: true
},
scaleLabel: {
display: true,
labelString: 'Month'
}
}],
yAxes: [{
display: true,
beginAtZero: true,
gridLines: {
display: false,
drawBorder: true
},
scaleLabel: {
display: true,
labelString: 'Inspection Request Count'
}
}]
},
title: {
display: true,
}
}
});
}
});
});
下面是我的 aspx.cs 代码 -
public List<HomeModel> GetMonthtlyCaseData()
{
List<HomeModel> t = new List<HomeModel>();
string conn = ConfigurationManager.ConnectionStrings["ConnCase"].ConnectionString;
using (OracleConnection cn = new OracleConnection(conn))
{
string myQuery = "Select count(CaseNo),to_char(to_date(CaseDt, 'DD-MM-YYYY'),'Month') from t_case_detail group by CaseNo,CaseDt ";
OracleCommand cmd = new OracleCommand();
cmd.CommandText = myQuery;
cmd.CommandType = CommandType.Text;
cmd.Connection = cn;
cn.Open();
OracleDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
int counter = 0;
while (dr.Read())
{
HomeModel tsData = new HomeModel();
tsData.month = dr["CaseDt"].ToString();
tsData.requestNo = dr["CaseNo"].ToString();
t.Add(tsData);
counter++;
}
}
}
return t;
}
解决方案
推荐阅读
- r - 访问闪亮 r 中的变量并重用它
- stackexchange-api - 通过 API 访问 Stack Exchange 用户的“about_me”数据?
- php - 如何在pdftotext之后解码PHP中的减号
- c - 如何获取线程的 CPU 指令数?
- c# - 如何在实体框架中使用 AddRange 然后 SaveChanges 使用批量插入来提高插入速度
- sql-server - 尝试了解有关 SQL 的更多信息并需要了解现有代码
- gcc - 在二进制文件中查找计算和 sm 数字
- javascript - 如何防止 Babel 发出两次出口申报单?
- list - 如何简化表示为列表的代数方程
- reactjs - 如何在移动维度上测试 React 应用程序?