【发布时间】:2016-07-18 08:19:57
【问题描述】:
我有一个将 morris.js 条形图绑定到数据表的代码,但它不起作用.. 图表不显示。它应该是这样的:我有一个文本框可以通过关键字进行研究。该图应根据日期显示关键字在数据表中出现的次数,因此:横坐标 = 日期,纵坐标 = 关键字出现的次数。
我要显示的内容:
所以这是我的代码不起作用(我使用 ajax 和 webmethod):
js代码:
<script src="morris.js"></script>
<script src="raphael-min.js"></script>
<script>
$(document).ready(function () {
var word = "{'word':'" + $("#tbSearch").val() + "'}";
Morris.Bar
({
element: 'bar-chart',
data: $.parseJSON(Graph()) + word,
xkey: 'value',
ykeys: ['value'],
labels: ["nombre d'occurence"],
fillOpacity: 0.6,
hideHover: 'auto',
behaveLikeLine: true,
resize: true,
pointFillColors: ['#ffffff'],
pointStrokeColors: ['black'],
lineColors: ['yellow', 'pink'],
resize: true
});
});
function Graph() {
var data = "";
$.ajax({
type: 'POST',
url: 'WelcomDigitalHelpDesk.aspx/GetBarchartData',
dataType: 'json',
async: false,
contentType: "application/json; charset=utf-8",
data: {},
success: function (result) {
data = result.d;
},
error: function (xhr, status, error) {
alert(error);
}
});
return data;
}
</script>
c#代码:
公共类 GraphData { 公共字符串标签 { 获取;放; } 公共字符串值 { 获取;放; } }
public class GraphDataList
{
public List<GraphData> ListOfGraphData { get; set; }
}
[WebMethod]
public static string GetBarchartData(string word)
{
string sqlQuery = @"SELECT DateDescription, COUNT(DescriptionDemande) FROM cfao_DigiHelp_index.DigiHelpData WHERE DescriptionDemande like '" + word + "' GROUP BY DateDescription";
DataTable DTGraph = DataBaseCacheDigitalHepDeskConnection.SqlDataTable(sqlQuery, "DIGIHELP_DB");
List<GraphData> dataList = new List<GraphData>();
foreach (DataRow dtrow in DTGraph.Rows)
{
GraphData graphData = new GraphData();
graphData.label = Convert.ToString(dtrow["DateDescription"].ToString());
graphData.label = Convert.ToString(dtrow["DescriptionDemande"].ToString());
dataList.Add(graphData);
}
//oper = null which means its first load.
var jsonSerializer = new JavaScriptSerializer();
string data = jsonSerializer.Serialize(dataList);
return data;
}
谁能告诉我我做错了什么?
【问题讨论】: