最近一个项目,因项目需要,需要在mvc3中使用webform。
需要用到图表来显示时时温度,而我选择使用微软的mschart。
mschart控件在webform里面显示正常,如下图:
但是放在mvc的webform里图片却显示不出来,放在mvc里面却又可以显示出来。经过几个小时的查找,发现自己忽略了细节,解决方法是在Global.asax中忽略webform文件夹路径的路由,图片就显示出来了,代码如下:
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); // 忽略对 ManagerWebForm 路径的路由 routes.IgnoreRoute("ManagerWebForm/{weform}");
图标页面StationTemperature.aspx的代码如下:
<%@ Page Title="" Language="C#" MasterPageFile="~/ManagerWebForm/Manager.Master" AutoEventWireup="true" CodeBehind="StationTemperature.aspx.cs" Inherits="ChellonaMobileStationElectricity.ManagerWebForm.StationTemperature" %> <%@ Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> <asp:Chart ID="Chart1" runat="server" ImageLocation="~/TempImages/ChartPic_#SEQ(300,3)" Palette="BrightPastel" ImageType="Png" BorderlineDashStyle="Solid" BackSecondaryColor="White" BackGradientStyle="TopBottom" BackColor="#D3DFF0" BorderColor="26, 59, 105"> <Legends> <asp:Legend IsTextAutoFit="False" Name="Default" BackColor="Transparent" Font="Trebuchet MS, 8.25pt, style=Bold"> </asp:Legend> </Legends> <BorderSkin SkinStyle="Emboss"></BorderSkin> <Series> </Series> <ChartAreas> <asp:ChartArea Name="ChartArea1" BorderColor="64, 64, 64, 64" BorderDashStyle="Solid" BackSecondaryColor="White" BackColor="64, 165, 191, 228" ShadowColor="Transparent" BackGradientStyle="TopBottom"> <Area3DStyle Rotation="15" Perspective="15" Inclination="15" IsRightAngleAxes="False" WallWidth="0" IsClustered="False"></Area3DStyle> <AxisY LineColor="64, 64, 64, 64"> <LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" /> <MajorGrid LineColor="64, 64, 64, 64" /> </AxisY> <AxisX LineColor="64, 64, 64, 64"> <LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" /> <MajorGrid LineColor="64, 64, 64, 64" /> </AxisX> </asp:ChartArea> </ChartAreas> </asp:Chart> </asp:Content>
StationTemperature.aspx.cs的代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Drawing; using System.Data; using System.Web.UI.DataVisualization.Charting; namespace ChellonaMobileStationElectricity.ManagerWebForm { public partial class StationTemperature : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindChart(); } } private void BindChart() { Chart1.Width = 800; Chart1.Height = 500; Chart1.BackColor = Color.Honeydew;//图表背景色 Chart1.Titles.Add(DateTime.Now.ToLongDateString() + " XX 基站温度时时曲线图");//图表标题 string sql = "select Temperature,CONVERT(varchar(5),CreateTime,108)as minutes from StationTemperature"; DataSet ds = DB.getdataset(sql); Chart1.DataBindTable(ds.Tables[0].DefaultView, "minutes"); //注意数据绑定 Chart1.Series[0].ChartType = SeriesChartType.Spline;//指明是曲线图 Chart1.Series[0].BorderWidth = 3; Chart1.Series[0].ShadowOffset = 3; Chart1.Series[0].IsValueShownAsLabel = true;//是否显示数据 Chart1.Series[0].IsVisibleInLegend = false;//是否显示数据说明 Chart1.Series[0].MarkerStyle = MarkerStyle.Circle;//标志大小 Chart1.Series[0].MarkerSize = 8;//标志大小 //Chart1.ChartAreas[0].AxisX.LineColor = Color.Blue;//X轴颜色 //Chart1.ChartAreas[0].AxisY.LineColor = Color.Blue;//Y轴颜色 Chart1.ChartAreas[0].AxisX.LineWidth = 2; Chart1.ChartAreas[0].AxisY.LineWidth = 2; Chart1.ChartAreas[0].AxisY.Title = "温度(度数)";//Y轴标题 Chart1.ChartAreas[0].AxisX.Title = "时间(分钟)"; #region 解决mschartX坐标显示不全的问题 Chart1.ChartAreas[0].AxisX.Interval = 1;//设置X轴坐标的间隔为1 Chart1.ChartAreas[0].AxisX.IntervalOffset = 1;//设置X轴坐标偏移为1 Chart1.ChartAreas[0].AxisX.LabelStyle.IsStaggered = false;//设置是否交错显示,比如数据多的时候分成两行来显示 #endregion } } }
效果图如上。