在MVC3视图中显示动态图表

时间:2012-10-10 11:57:16

标签: asp.net-mvc asp.net-mvc-3 jquery charts

我有一种情况,我需要在MVC3视图中显示图表。 直到现在我已经完成了以下工作 在我的控制器中

public ActionResult MyChart()
        {
            var bytes = new Chart(width: 400, height: 200)
                .AddSeries(
                    chartType: "bar",
                    xValue: new[] {"Math", "English", "Computer", "Urdu"},
                    yValues: new[] {"60", "70", "68", "88"})
                .GetBytes("png");
            return File(bytes, "image/png");
        }

在我的Jquery文件中

function getChart() {
    $.ajax({
        url: ('/Home/MyChart'),
        type: 'GET',
        cache: false,
        success: function (result) {
            alert(result.length);
            $('#BottomGrid').html(result);
        },
        error: function () { alert("error"); }
    });
    }

在我看来,我只是调用getChart()方法。 我的视图是一个.aspx视图,而bottomgrid是我需要显示图表的视图上的div。

在IE中运行上面的代码给了我一个png符号,在firefox中它显示了我特殊的字符。

任何想法我哪里出错?

1 个答案:

答案 0 :(得分:1)

编辑“getChart()”函数:

 success: function (result) {
        alert(result.length);
        $('#BottomGrid').append('<img id="myChart" />');
        $('#myChart').attr("src", "/Home/MyChart")
    }

但我不认为这是一个理想的解决方案,因为它请求图像两次,第一次是在执行ajax时(它需要一种方法将返回的文本呈现为图像),第二种是在浏览器解析时“ img“tag。

相关问题