javascript - 如何将 Json 字符串从 mvc Controller 传递到 d3.js 图表?

标签 javascript asp.net ajax asp.net-mvc d3.js

Controller :

    public ActionResult JsonString()
    {
        var p = new Parser("ALL");
        return Content(p.BuildJson());
    }

JSON:

[{"date":"28-Feb-14","count":2},{"date":"27-Feb-14","count":4},{"date":"26-Feb-14","count":4},{"date":"25-Feb-14","count":5},{"date":"24-Feb-14","count":5},{"date":"23-Feb-14","count":2},{"date":"22-Feb-14","count":7},{"date":"21-Feb-14","count":2},{"date":"20-Feb-14","count":3},{"date":"19-Feb-14","count":2},{"date":"18-Feb-14","count":6},{"date":"17-Feb-14","count":2},{"date":"16-Feb-14","count":2},{"date":"15-Feb-14","count":0},{"date":"14-Feb-14","count":2},{"date":"13-Feb-14","count":5},{"date":"12-Feb-14","count":4},{"date":"11-Feb-14","count":3},{"date":"10-Feb-14","count":4},{"date":"9-Feb-14","count":2},{"date":"8-Feb-14","count":5},{"date":"7-Feb-14","count":4},{"date":"6-Feb-14","count":12},{"date":"5-Feb-14","count":2},{"date":"4-Feb-14","count":0},{"date":"3-Feb-14","count":0},{"date":"2-Feb-14","count":0},{"date":"1-Feb-14","count":0},{"date":"31-Jan-14","count":0},{"date":"30-Jan-14","count":0}]

Javascript:

        $.ajax({
        url: '@Url.Action("JsonString", "Dashboard")',
        type: 'GET',
        success: function (dat) {

        var data = jQuery.parseJSON(dat);

        var margin = { top: 20, right: 20, bottom: 30, left: 50 },
        width = @graphWidth - margin.left - margin.right,
        height = @graphHeight - margin.top - margin.bottom;

        var parseDate = d3.time.format("%d-%b-%y").parse;

        var x = d3.time.scale()
            .range([0, width]);

        var y = d3.scale.linear()
            .range([height, 0]);

        var xAxis = d3.svg.axis()
            .scale(x)
            .orient("bottom");

        var yAxis = d3.svg.axis()
            .scale(y)
            .orient("left");

        var line = d3.svg.line()
            .x(function (d) { return x(d.date); })
            .y(function (d) { return y(d.count); });

        var svg = d3.select("#chart").append("svg")
            .attr("width", width + margin.left + margin.right)
            .attr("height", height + margin.top + margin.bottom)
            .append("g")
            .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

        d3.json = data; ??????????????????????????????????????????????????

        x.domain(d3.extent(data, function(d) { return d.date; }));
        y.domain(d3.extent(data, function(d) { return d.count; }));

        svg.append("g")
            .attr("class", "x axis")
            .attr("transform", "translate(0," + height + ")")
            .call(xAxis);

        svg.append("g")
            .attr("class", "y axis")
            .call(yAxis)
            .append("text")
            .attr("transform", "rotate(-90)")
            .attr("y", 6)
            .attr("dy", "1.21em")
            .style("text-anchor", "end")
            .style("font-size", "18px")
            .text("Occurences");

        svg.append("path")
            .datum(data)
            .attr("class", "line")
            .attr("d", line);

        svg.append("text")
            .attr("x", (width / 2))
            .attr("y", 50 - (margin.top))
            .attr("text-anchor", "middle")
            .style("font-size", "26px")
            .style("text-decoration", "underline")
            .text("Incidents/Time");

            alert(data);
            alert(dat);
        }

最佳答案

您需要做的就是以下操作:

public ActionResult JsonString()
{
    var p = new Parser("ALL");
    return Content(p.BuildJson(), "application/json");
}

提供 p.BuildJson() 返回 json 字符串,不指定内容类型,默认为 text/plain

关于javascript - 如何将 Json 字符串从 mvc Controller 传递到 d3.js 图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22096959/

相关文章:

javascript - ReactJS - 重构后无法获取从单独文件加载值的函数

javascript - 使用 JavaScript/JQuery 通过 AJAX 传递数组

ajax - 当另一个 Observable 发出它的第一个值时启动流

javascript - flowjs 0.54.0 中的新 "Missing annotation"错误

javascript - 如何在 Angular js中将选中的单选按钮值发送到服务器

javascript - 如何加粗和更改图表标题的字体大小

c# - Dnn,客户端资源管理 - 模块路径

c# - 使用 Server.Transfer() 将请求传输到静态图像(.jpg、.png 等)是否安全?

javascript - 如何让我的实时 jQuery 搜索在执行搜索之前稍等一下?

javascript - 在函数中调用对象成员