【问题标题】:c# bind morris.js with datatable code not workingc#将morris.js与数据表代码绑定不工作
【发布时间】:2016-07-18 08:19:57
【问题描述】:

我有一个将 morris.js 条形图绑定到数据表的代码,但它不起作用.. 图表不显示。它应该是这样的:我有一个文本框可以通过关键字进行研究。该图应根据日期显示关键字在数据表中出现的次数,因此:横坐标 = 日期,纵坐标 = 关键字出现的次数。

我要显示的内容:

bar chart graph

所以这是我的代码不起作用(我使用 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;

    }

谁能告诉我我做错了什么?

【问题讨论】:

    标签: c# jquery ajax morris.js


    【解决方案1】:

    你会在任何地方加载 jQuery 吗?这是一个dependency

    另外,尝试在莫里斯之前加载拉斐尔。您的完整依赖项列表应如下所示:

    <script src="jquery/1.9.0/jquery.min.js"></script>
    <script src="raphael-min.js"></script>
    <script src="morris.js"></script>
    

    【讨论】:

      【解决方案2】:

      我让它工作了,所以这是对我有用的答案:

      代码js:

      function buildChartGraph(data) {
          $('#bar-chart').html('');
      
          Morris.Bar
              ({
                  element: 'bar-chart',
                  data: JSON.parse(data),
                  xkey: 'label',
                  ykeys: ['value'],
                  labels: ['value'],
                  fillOpacity: 0.6,
                  hideHover: 'auto',
                  behaveLikeLine: true,
                  resize: true,
                  pointFillColors: ['blue'],
                  pointStrokeColors: ['darkblue'],
                  lineColors: ['skyblue', 'orange'],
                  resize: true
      
              });
      }
      
      function GetOccurence() {
          var data = "";
      
      
          $.ajax({
              type: "POST",
      
              contentType: "application/json; charset=utf-8",
      
              url: "WelcomDigitalHelpDesk.aspx/GetBarchartData",
      
              data: "{'word':'" + $("#tbSearch").val() + "'}",
      
              dataType: "json",
      
              success: function (result) {
      
                  buildChartGraph(result.d);
      
              },
              error: function (xhr, status, error) {
                  alert(error);
              }
          });
      }
      

      代码c#:

      [WebMethod]
          public static string GetBarchartData(string word)
          {
      
          string sqlQuery = @"SELECT TOP 40 DateDescription, COUNT(DescriptionDemande) FROM TableName WHERE DescriptionDemande like '%" + word + "%' GROUP BY DateDescription";
      
          DataTable DTGraph = DataBaseCacheDigitalHepDeskConnection.SqlDataTable(sqlQuery, "DbName");
      
          List<GraphData> dataList = new List<GraphData>();
      
          foreach (DataRow dtrow in DTGraph.Rows)
          {
              GraphData graphData = new GraphData();
      
              graphData.value = Convert.ToString(dtrow[1].ToString());
              graphData.label = Convert.ToString(DateTime.Parse( dtrow[0].ToString()).ToShortDateString());
      
              dataList.Add(graphData);
          }
      
      
          var jsonSerializer = new JavaScriptSerializer();
          string data = jsonSerializer.Serialize(dataList);
          return data;
      
      }
      
      
      
      }
      
      public class GraphData
      {
          public string label { get; set; }
          public string value { get; set; }
      }
      
      public class GraphDataList
      {
          public List<GraphData> ListOfGraphData { get; set; }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-14
        • 2010-12-01
        • 2015-11-24
        • 2011-04-02
        • 1970-01-01
        • 1970-01-01
        • 2018-10-30
        相关资源
        最近更新 更多