在 ASP.NET MVC3 项目中实现 MS Charts
我有一个 MVC 应用程序,它在业务逻辑中创建一个图表,如下所示:
StatisticsModel.Chart.Width = 150
StatisticsModel.Chart.Height = 300
StatisticsModel.Chart.Attributes.Add("align", "left")
StatisticsModel.Chart.Titles.Add("Statistics for: " + StatisticsModel.ProductNumbers)
StatisticsModel.Chart.ChartAreas.Add(New ChartArea)
StatisticsModel.Chart.Series.Add(New Series)
StatisticsModel.Chart.Series(0).ChartType = SeriesChartType.Column
StatisticsModel.Chart.Series(0).Points.DataBindXY(StatisticsModel.FailedTPDescriptionList, "key", StatisticsModel.FailedTPDescriptionList, "value")
现在,我尝试在视图中实现它,但我读过很多文章,他们建议我将图表放在不同的控制器中。但这意味着我必须将图表对象发送到那里,因为我有很多函数需要图表,而且我认为最简单的方法是在模型中实现它,然后从那里渲染它。
我尝试使用: http://code-inside.de/blog-in/2008/11/27/howto-use-the-new-aspnet-chart-controls-with-aspnet-mvc/
但是:
@Code
Dim writer As New HtmlTextWriter(Page.Response.Output)
End Code
对我不起作用。我正在使用 VB.NET
谁能帮助我?非常欢迎提出建议。
I have a MVC application, which creates a Chart in the business logic like this:
StatisticsModel.Chart.Width = 150
StatisticsModel.Chart.Height = 300
StatisticsModel.Chart.Attributes.Add("align", "left")
StatisticsModel.Chart.Titles.Add("Statistics for: " + StatisticsModel.ProductNumbers)
StatisticsModel.Chart.ChartAreas.Add(New ChartArea)
StatisticsModel.Chart.Series.Add(New Series)
StatisticsModel.Chart.Series(0).ChartType = SeriesChartType.Column
StatisticsModel.Chart.Series(0).Points.DataBindXY(StatisticsModel.FailedTPDescriptionList, "key", StatisticsModel.FailedTPDescriptionList, "value")
Now, I am trying to implement it in the View, but I have read many articles, and they suggest me to put the chart in a different controller. But that would mean I have to send the Chart object there, as I have many functions, that require a chart, and I thought the easiest way is to implement it in the Model, and then rendering it from there.
I tried using: http://code-inside.de/blog-in/2008/11/27/howto-use-the-new-aspnet-chart-controls-with-aspnet-mvc/
But the:
@Code
Dim writer As New HtmlTextWriter(Page.Response.Output)
End Code
Didn't work for me. I am using VB.NET
Can anyone help me? Suggestions are very welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 MVC 中创建和显示图表的方法有很多很多,恕我直言,您提到的链接非常好。我正在使用 c#,但我这样做的方式是在视图中使用 img 标签并将 src 属性指向控制器操作:
控制器操作返回 FileContentResult:
ChartHelper 类实现 IDisposable 并具有辅助属性(图像),它将图表作为文件返回,注意这只是示例/片段代码,用于显示我的意思:
对我来说效果很好,希望它能有所帮助,即使它是用 C# 编写的。
编辑如果您想在从“主”控制器操作调用的业务逻辑中创建图像/图表,也许您可以执行类似的操作,生成图像/图表,然后将其保存到磁盘,缓存或数据库并从图像渲染控制器操作中获取它:
视图将是:
There are many, many ways of creating and showing charts in MVC, and the link you referred to is pretty good IMHO. I'm using c#, but the way I'm doing it is to use an img-tag in the view and point the src-attribute to a Controller action:
The controller action returns a FileContentResult:
The ChartHelper class implements IDisposable and has a helper property (Image) which returns the chart as a file, NOTE this is just sample/snippet code to show what I mean:
Works pretty well for me, hope it helps even if it's in C#.
EDIT If you want to create the image/chart in business logic called from your "main" Controller action, maybe you can do something like this where you generate the image/chart and then save it to disk, cache or database and pick it up from the image rendering controller action:
And the view would be: