d3条形图不在局部视图内渲染

时间:2015-03-17 04:32:17

标签: jquery asp.net-mvc d3.js

我正在尝试在asp.net mvc应用程序中的局部视图中渲染条形图。当我将代码放入html并将其放入我的项目(不在任何视图或任何文件夹中,只在根文件夹中)并在浏览器中运行html时,一切正常。当我将html代码放在局部视图中并尝试渲染它时,它不会渲染。以下是我的代码

<script type="text/javascript" src="~/Scripts/jquery-1.11.2.js"></script>
<style>
    .bar {
        fill: steelblue;
    }

        .bar:hover {
            fill: brown;
        }

    .axis {
        font: 10px sans-serif;
    }

        .axis path,
        .axis line {
            fill: none;
            stroke: #000;
            shape-rendering: crispEdges;
        }

    .x.axis path {
        display: none;
    }
</style>
<body>
    <script src="~/Scripts/d3.js"></script>
    <script>

        var margin = { top: 20, right: 20, bottom: 30, left: 40 },
            width = 960 - margin.left - margin.right,
            height = 500 - margin.top - margin.bottom;

        var x = d3.scale.ordinal()
            .rangeRoundBands([0, width], .1);

        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")
            .ticks(10, "%");

        var svg = d3.select("body").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 + ")");

        $.ajax({
            url: 'http://localhost/ABCD.Portal/Dashboard/GetData',
            type: 'GET',
            data: '',
            cache: false,
            dataType: 'text',
            async: true,
            error: function (xhr) {
                //alert('Error: ' + xhr.statusText);
            },
            success: function (result) {
                result = eval(JSON.parse(result));
                alert(result);
                x.domain(result.map(function (d) { return d.letter; }));
                y.domain([0, d3.max(result, function (d) { return d.frequency; })]);

                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", ".71em")
                    .style("text-anchor", "end")
                    .text("Frequency");

                svg.selectAll(".bar")
                    .data(result)
                  .enter().append("rect")
                    .attr("class", "bar")
                    .attr("x", function (d) { return x(d.letter); })
                    .attr("width", x.rangeBand())
                    .attr("y", function (d) { return y(d.frequency); })
                    .attr("height", function (d) { return height - y(d.frequency); });
            }
        });

        function type(d) {
            d.frequency = +d.frequency;
            return d;
        }

    </script>
</body> 

另外,我还会继续收到警告

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/.

请帮忙。

0 个答案:

没有答案