【问题标题】:jqplot and rendering it with asp.net mvc modeljqplot 并用 asp.net mvc 模型渲染它
【发布时间】:2013-06-18 19:28:23
【问题描述】:

在我的 asp.net mvc 3.0 应用程序中,我将绘制一个图表,其中 X 轴包含日期值,y 轴包含每天的视图。对于图表绘制,我使用 jqplot date-axes ,并将其传递给 json 结果。这是模型:

 public class MyDateModel
    {
        public string Date { get; set; }
        public int Views { get; set; }
    }

这是行动:

public ActionResult Index()
        {
            var res = new List<MyDateModel>();

            res.Add(new MyDateModel { Date=DateTime.Now.AddDays(-9).ToString("yyyy-MM-dd hh:mm"),Views=10});
            res.Add(new MyDateModel { Date = DateTime.Now.AddDays(-3).ToString("yyyy-MM-dd hh:mm"), Views = 3 });
            res.Add(new MyDateModel { Date = DateTime.Now.ToString("yyyy-MM-dd hh:mm"), Views = 3 });

            return View(res);
        }

最后是视图:

@model IEnumerable<Chartjquery_jqplot.Models.MyDateModel>
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>Index</title>
    <link href="../../Content/jquery.jqplot.min.css" rel="stylesheet" type="text/css" />
    <script src="../../Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery.jqplot.min.js" type="text/javascript"></script>
    <script src="../../Scripts/jqplot.dateAxisRenderer.min.js" type="text/javascript"></script>
    <script src="../../Scripts/jqplot.json2.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {

            var jsonurl=@Html.Raw(Json.Encode(Model));
            var plot2 = $.jqplot('chart2', jsonurl, {
                title: "AJAX JSON Data Renderer",
                axes:{xaxis:{renderer:$.jqplot.DateAxisRenderer}}

            });




        });
    </script>
</head>
<body>
    <div>
        @Html.Raw(Json.Encode(Model))
        <div id="chart2" style="height: 300px; width: 500px;">
        </div>
    </div>
</body>
</html>

但它不会在页面上绘制任何东西。

你能帮帮我吗?

除了json结果是:

[{"Date":"2012-07-06 12:10","Views":10},{"Date":"2012-07-12 12:10","Views":3}, {"日期":"2012-07-15 12:10","浏览次数":3}]

【问题讨论】:

    标签: jquery json asp.net-mvc-3 jqplot


    【解决方案1】:

    我找到了一种将正确的 json 数据传递给图表的方法。这是我写的 html hepler:

    public static MvcHtmlString GetStrippedJson(this HtmlHelper helper,string json)
            {
                json=json.Replace("\"Date\":", string.Empty);
    
                json=json.Replace("\"Views\":", string.Empty);
    
                json=json.Replace('}', ']');
    
                json=json.Replace('{', '[');
    
                return MvcHtmlString.Create(json);
            }
    

    但我不知道如何根据每日滴答声设置滴答声间隔

    【讨论】:

      【解决方案2】:

      不确定这是否有帮助,但我传递数据的代码如下。 @Model.data 是一个列表。

      $(document).ready(function () {
              var data = @Html.Raw(Json.Encode(@Model.data));
              //debugger;
              //alert(data);
              $.jqplot('chartdiv', data);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-26
        • 1970-01-01
        • 2012-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-10
        相关资源
        最近更新 更多