새로운 asp.net 차트 컨트롤-MVC와 함께 작동합니까 (결국)?
Scott Gu는 방금 .NET 팀에서 배포하는 새로운 차트 컨트롤 집합에 대해 게시했습니다. 그들은 믿을 찾으십시오 http://weblogs.asp.net/scottgu/archive/2008/11/24/new-asp-net-charting-control-lt-asp-chart-runat-quot-server-quot-gt합니다. aspx
백만 달러짜리 질문은 ... MVC와 협력 할 것인가, 그렇다면 언제인가?
다음 두 가지 방법으로 차트 컨트롤을 사용할 수 있습니다.
컨트롤러에서 이미지 생성
차트를 생성하고 액션에서 이미지로 반환함으로써 (Chatuman이 제 생각에 언급 한 것처럼) :
Chart chart = new Chart();
chart.BackColor = Color.Transparent;
chart.Width = Unit.Pixel(250);
chart.Height = Unit.Pixel(100);
Series series1 = new Series("Series1");
series1.ChartArea = "ca1";
series1.ChartType = SeriesChartType.Pie;
series1.Font = new Font("Verdana", 8.25f, FontStyle.Regular);
series1.Points.Add(new DataPoint {
AxisLabel = "Value1", YValues = new double[] { value1 } });
series1.Points.Add(new DataPoint {
AxisLabel = "Value2", YValues = new double[] { value2 } });
chart.Series.Add(series1);
ChartArea ca1 = new ChartArea("ca1");
ca1.BackColor = Color.Transparent;
chart.ChartAreas.Add(ca1);
using (var ms = new MemoryStream())
{
chart.SaveImage(ms, ChartImageFormat.Png);
ms.Seek(0, SeekOrigin.Begin);
return File(ms.ToArray(), "image/png", "mychart.png");
}
WebForms 스타일
이렇게하면 기존 웹 양식과 마찬가지로 .aspx보기에 차트를 포함 할 수 있습니다. 이를 위해 web.config에서 관련 비트를 연결해야합니다.
<controls>
...
<add tagPrefix="asp"
namespace="System.Web.UI.DataVisualization.Charting"
assembly="System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</controls>
<httpHandlers>
...
<add path="ChartImg.axd"
verb="GET,HEAD"
validate="false"
type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</httpHandlers>
<handlers>
...
<add name="ChartImageHandler"
preCondition="integratedMode"
verb="GET,HEAD"
path="ChartImg.axd"
type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</handlers>
차트를 작성할 때 DataPoint 요소 내에서 코드를 실행할 수 없으므로 데이터를 연결하려면 View 클래스에 메서드가 필요합니다. 이것은 나를 위해 잘 작동합니다. 이 방식으로 작업하면 컨트롤이 차트 컨트롤 http 처리기에 의해 생성 된 이미지에 대한 URL을 렌더링합니다. 배포시 이미지를 캐시 할 쓰기 가능한 폴더를 제공해야합니다.
* VS 2010 / .NET 4 지원 *
.NET 4에서이 작업을 수행하려면 적절한 공개 키 토큰을 사용하여 차트 참조를 버전 4.0.0.0으로 변경해야합니다.
Also it seems that the chart control now generates urls to the current request path rather than the request route. For me this meant that all the chart requests resulted in 404 errors because /{Controller}/ChartImg.axd
and equivalents were blocked by routes. To fix this I added extra IgnoreRoute calls that cover my usages - a more general solution would be better:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("ChartImg.axd/{*pathInfo}");
routes.IgnoreRoute("{controller}/ChartImg.axd/{*pathInfo}");
routes.IgnoreRoute("{controller}/{action}/ChartImg.axd/{*pathInfo}");
...
For folks who want to use charting control with MVC 3 using Razor engine see the following link
How to use MS Charts with MVC3 with Razor
You can already use them with MVC all you have to do is render them as images
Make a Usercontrol instead and give it the full Chart object and let it render it self:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.Web.UI.DataVisualization.Charting.Chart>" %>
<%
Model.Page = this.Page;
var writer = new HtmlTextWriter(Page.Response.Output);
Model.RenderControl(writer);
%>
name it Chart.ascx and put it in your Shared view folder.
now you will get all extra html, like image map etc. for free.. as well as caching.
in your Controller:
public ActionResult Chart(){
var c = new Chart();
//...
return View(c);
}
in your View:
<% Html.RenderPartial("Chart", Model); %>
This article worked it out best for me:
http://www.codecapers.com/post/Build-a-Dashboard-With-Microsoft-Chart-Controls.aspx
Doesn't give errors about 'Object not set to an instance of an object' or 'Session id was available but the response stream has been flushed' (not the exact wording of the errors).
I wasn't willing to just render them as an image because if you're doing drilldowns or tooltips or other click actions on the chart, then rendering as an image doesn't preserve any of that.
The key for my needs was to put the chart(s) into a model, pass the model to the view (or partial view) and put an asp:panel in the view and add the chart(s) to the panel in the view markup.
By the way, this was with VS.net 2008 and MVC 2 on Sept. 3, 2010 (dates was something that I found important when searching for answers because of the changes that are continually happening to MVC).
I have been testing with MVC and so far it looks like it is working with MVC.
'Programing' 카테고리의 다른 글
Borg 패턴이 Python의 Singleton 패턴보다 나은 이유 (0) | 2020.10.22 |
---|---|
Chrome 개발자 도구 : html 스크립트가 비어 있습니다 (소스에서) 디버깅 자습서 (0) | 2020.10.22 |
asp.net에서 캐시를 잠그는 가장 좋은 방법은 무엇입니까? (0) | 2020.10.21 |
LaTeX로 타임 라인을 만드는 방법은 무엇입니까? (0) | 2020.10.21 |
RoR-MD5 세대 (0) | 2020.10.21 |