用asp.net制作的Chart js线

问题描述 投票:0回答:1

我想在ASP.NET中用图表js做一个线型图,但这个不工作,之前我试过用饼图,这个工作很好,但用线型图不工作,我认为响应是不正确的,因为在控制台显示 "uncaught SyntaxError: unexpected token : "。 这是代码c# 。

 [WebMethod]
    public static string GetChart(string country)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("{");
        sb.Append("labels:[\"January\",\"February\",\"March\",\"April\",\"May\",\"June\"],");
        sb.Append("datasets:[");

            System.Threading.Thread.Sleep(50);
            string color = "rgba(220,220,220,0.2)";
            //
            sb.Append("{");
            sb.Append(string.Format("fillColor:\"{0}\", strokeColor:\"{1}\", pointColor:\"{2}\", pointStrokeColor:\"{3}\", data:{4}", color, "#ACC26D", "#fff", "#9DB86D", "[203,156,99,251,305,247]"));
            //
            sb.Append("}");
            sb.Append("]");


            sb.Append("};");
        return sb.ToString();
    }

这是Javascript。

<script type="text/javascript">
    function LoadChart() {
        var chartType = parseInt($("[id*=rblChartType] input:checked").val());
        $.ajax({
            type: "POST",
            url: "inicioCliente.aspx/GetChart",
            data: "{country: '" + $("[id*=ddlCountries]").val() + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (r) {
                $("#dvChart").html("");
                $("#dvLegend").html("");
                var data = eval((r.d));
                var el = document.createElement('canvas');
                $("#dvChart")[0].appendChild(el);
                var ctx = el.getContext('2d');
                var userStrengthsChart;
                switch (chartType) {
                    case 1:
                        userStrengthsChart = new Chart(ctx).Line(data);
                        break;
                    case 2:
                        userStrengthsChart = new Chart(ctx).Doughnut(data);
                        break;
                }
                for (var i = 0; i < data.length; i++) {
                    var div = $("<div />");
                    div.css("margin-bottom", "10px");
                    div.html("<span style = 'display:inline-block;height:10px;width:10px;background-color:" + data[i].color + "'></span> " + data[i].text);
                    $("#dvLegend").append(div);
                }
            },
            failure: function (response) {
                alert('There was an error.');
            }
        });
    }
    $(function () {
        LoadChart();
        $("[id*=ddlCountries]").bind("change", function () {
            LoadChart();
        });
        $("[id*=rblChartType] input").bind("click", function () {
            LoadChart();
        });
    });


</script>
javascript c# asp.net charts highcharts
1个回答
0
投票

经过一段时间,我找到了解决方案,问题是在eval方法,我需要添加"()"

 var data = (eval("(" + r.d + ")"));

谢谢你

© www.soinside.com 2019 - 2024. All rights reserved.