• MSChart使用之动态生成多个多行ChartArea


    前台代码:

                            <asp:Chart ID="Chart1" runat="server" Height="500px" Width="1000px" BorderlineWidth="1" >
                            <Titles>
                                <asp:Title Name="Title1" runat="server" Text="设备稼动率波动图" Font="宋体,20pt"></asp:Title>
                            </Titles>
                            <legends>
                                <asp:Legend IsTextAutoFit="False" DockedToChartArea="NotSet" Name="Default" BackColor="Transparent" Font="宋体, 10pt, style=Bold">
                                </asp:Legend>
                            </legends>
    
                            <Series>
                            </Series>
    
                            <ChartAreas>
                            </ChartAreas>
                            </asp:Chart>

    后台代码:包括动态生成ChartArea和保存为图片:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Web.UI.DataVisualization.Charting;
    using System.Text;
    using Microsoft.Win32;
    using System.Drawing;
    
    public partial class MSChartTest : System.Web.UI.Page
    {
        int iNowYear = DateTime.Now.Year;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                for (int i = iNowYear - 10; i < iNowYear + 10; i++)
                {
                    this.DDLYear.Items.Add(i.ToString());
                }
                this.DDLYear.SelectedValue = iNowYear.ToString();
                string SQL = "select  lookup_value_code,lookup_value_Name from T_EB_DB_LOOKUP_VALUE where LOOKUP_TYPE_CODE='RES_DEV_LOCATION'";
                DataSet ds = OraHelper.GetDateDS(SQL);
                this.DDLSYS.Items.Add("");
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    //DataTable dtSYS = ds.Tables[0];
                    //DataRow dr = dtSYS.NewRow();
                    //dr[0] = ""; dr[1] = "";
                    //dtSYS.Rows.InsertAt(dr, 0);
                    //this.DDLSYS.DataSource = dtSYS;
                    //this.DDLSYS.DataValueField = "lookup_value_Name";
                    //this.DDLSYS.DataTextField = "lookup_value_Name";
                    //this.DDLSYS.DataBind();
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        this.DDLSYS.Items.Add(ds.Tables[0].Rows[i]["lookup_value_Name"].ToString().Trim());
                    }
                }
            }
        }
    
        protected void btnSearch_Click(object sender, EventArgs e)
        { 
            SearchChart();
        }
    
        protected void SearchChart()
        {
            Chart1.ChartAreas.Clear();
            Chart1.Series.Clear();
    
            DataTable dtChart = this.Getdt();
            //this.GridView1.DataSource = dtChart;
            //this.GridView1.DataBind();
            if (dtChart != null && dtChart.Rows.Count>0)
            {
                DataView dv = dtChart.DefaultView;
                DataTable dtDeviceName = dv.ToTable(true, "DEVICE_NAME");//零件列表,ChartArea数 
                DataSet ds = new DataSet();
                ChartArea _ChartArea = null;
                Series _SeriesJRATE = null;
                Series _SeriesDRATE = null;
                List<string> oCharAreas = new List<string>();
                float firstChartAreaY = 0;
                for (int i = 0; i < dtDeviceName.Rows.Count; i++)
                {
                    string DeviceName = dtDeviceName.Rows[i]["DEVICE_NAME"].ToString();
                    DataRow[] drList = dtChart.Select(" DEVICE_NAME='" + DeviceName + "' ");
                    
                    if (drList != null && drList.Length>0)
                    {
                            DataTable dt = drList.CopyToDataTable();
                            dt.TableName = DeviceName;
                            ds.Tables.Add(dt);
                            _ChartArea = new ChartArea();
                            _ChartArea.AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(224, 224, 224);
                            _ChartArea.AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dash;
                            _ChartArea.AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(224, 224, 224);
                            _ChartArea.AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash;
                            _ChartArea.Position.Auto = true;
                            _ChartArea.Name = DeviceName;//设定Chart Name
    
                            ////_ChartArea.AxisY.Title = "单位";
                            ////_ChartArea.AxisY.TitleAlignment = StringAlignment.Far;
                            ////_ChartArea.AxisX.Title = "月份";
                            ////_ChartArea.AxisX.TitleAlignment = StringAlignment.Far;
    
                            _ChartArea.AxisX.Minimum = 1;//起始值
                            _ChartArea.AxisX.Maximum = 12;//结束值
                            _ChartArea.AxisX.Interval = 1;//间隔
                            _ChartArea.AxisX.IntervalType = DateTimeIntervalType.Number;//间隔类型指定
    
                            //_ChartArea.AxisX.LabelStyle.Interval = 1; //X文本间隔
                            //_ChartArea.AxisX.LabelStyle.Font = new System.Drawing.Font("隶书", 12);
                            //_ChartArea.AxisX.MajorGrid.Interval = 1;  //X主要辅助线间隔
                            //_ChartArea.AxisX.MinorGrid.Interval = 1;//X次要辅助线间隔
                            //_ChartArea.AxisX.MinorTickMark.Interval = 1;//X次要刻度线间隔
                            //_ChartArea.AxisX.MajorTickMark.Interval = 1;//X主要刻度线间隔
                            //_ChartArea.AxisY.MinorGrid.Interval = 1;//Y次要辅助线间隔
                            //_ChartArea.AxisY.MajorGrid.Interval = 1;//Y主要辅助线间隔
                            //_ChartArea.AxisY.LabelStyle.Interval = 0.5;
                            Chart1.ChartAreas.Add(_ChartArea);
    
                            Title title = new Title(_ChartArea.Name, Docking.Top);
                            Chart1.Titles.Add(title);
                            title.DockedToChartArea = _ChartArea.Name;
                            title.IsDockedInsideChartArea = false;
                            title.Alignment = ContentAlignment.TopCenter;
    
                            _SeriesJRATE = new Series();
                            _SeriesJRATE.ChartType = SeriesChartType.Spline;
                            _SeriesJRATE.Name = DeviceName + "_SJ";
                            _SeriesJRATE.ChartArea = _ChartArea.Name;
                            _SeriesJRATE.BorderColor = System.Drawing.Color.Blue;
                            _SeriesJRATE.Color = Color.Blue;
                            _SeriesJRATE.BorderWidth = 1;
                            _SeriesJRATE.ShadowOffset = 1;
                            _SeriesJRATE.IsValueShownAsLabel = true;
                            _SeriesJRATE.MarkerStyle = MarkerStyle.Triangle;
                            _SeriesJRATE.LegendText = "稼动率";
                            if (i == 0) { _SeriesJRATE.IsVisibleInLegend = true; }
                            else{ _SeriesJRATE.IsVisibleInLegend = false; }
                
                            Chart1.Series.Add(_SeriesJRATE); //加入Series
    
                            _SeriesDRATE = new Series();
                            _SeriesDRATE.ChartType = SeriesChartType.Spline;
                            _SeriesDRATE.Name = DeviceName + "_SD";
                            _SeriesDRATE.ChartArea = _ChartArea.Name;
                            _SeriesDRATE.BorderColor = System.Drawing.Color.Green;
                            _SeriesDRATE.Color = Color.Green;
                            _SeriesDRATE.BorderWidth = 1;
                            _SeriesDRATE.ShadowOffset = 1;
                            _SeriesDRATE.IsValueShownAsLabel = true;
                            _SeriesDRATE.MarkerStyle = MarkerStyle.Square;
                            _SeriesDRATE.LegendText = "直接率";
                            if (i == 0) { _SeriesDRATE.IsVisibleInLegend = true; }
                            else { _SeriesDRATE.IsVisibleInLegend = false; }
    
                            Chart1.Series.Add(_SeriesDRATE); //加入Series
                    }
                }
                int CRows = Chart1.ChartAreas.Count % 2 == 1 ? (Chart1.ChartAreas.Count / 2 + 1) : (Chart1.ChartAreas.Count / 2);
                Chart1.Height = 60 + CRows * 420;
                for (int i = 0; i < Chart1.Legends.Count; i++)
                {
                    Chart1.Legends[i].Docking = Docking.Top;
                    Chart1.Legends[i].Alignment = StringAlignment.Center;
                }
    
                Chart1.Legends[0].Position.X = (float)(((Chart1.Width.Value / 2 - 150) / Chart1.Width.Value) * 100);
                Chart1.Legends[0].Position.Y = (float)((65/ Chart1.Height.Value) * 100);
                Chart1.Legends[0].Position.Height = (float)((20 / Chart1.Height.Value) * 100);
                Chart1.Legends[0].Position.Width = (float)((320 / Chart1.Width.Value) * 100);
    
                ////Chart1.Legends[0].BackColor = Color.Blue;
                //Chart1.Legends[0].BorderColor = Color.Blue;
                //Chart1.Legends[0].ForeColor = Color.Blue;        
    
                for (int i = 0; i < Chart1.ChartAreas.Count; i++)
                {
                    int RowNum = i / 2 + 1;
                    if (i % 2 == 0)   //第一列
                    {
                        if (i == 0) //第一行
                        {
                            Chart1.ChartAreas[i].Position.Y = Chart1.ChartAreas.Count <=2 ? 20 : 11;
                        }
                        else  //非第一行
                        {
                            Chart1.ChartAreas[i].Position.Y = Chart1.ChartAreas[i-2].Position.Y+ Chart1.ChartAreas[i-2].Position.Height;
                        }
                        Chart1.ChartAreas[i].Position.X = 0.0F;
                        Chart1.ChartAreas[i].Position.Width = 48;
                        Chart1.ChartAreas[i].Position.Height = (float)((320 / Chart1.Height.Value) * 100);
                    }
                    else  //第二列
                    {
    
                        if (i == 1) //第一行
                        {
                            Chart1.ChartAreas[i].Position.Y = Chart1.ChartAreas.Count <= 2 ? 20 : 11;
                        }
                        else  //非第一行
                        {
                            Chart1.ChartAreas[i].Position.Y = Chart1.ChartAreas[i - 2].Position.Y + Chart1.ChartAreas[i - 2].Position.Height;
                        }
                        Chart1.ChartAreas[i].Position.X =50;// (float)(Chart1.Height.Value * 0.5 - 10)
                        Chart1.ChartAreas[i].Position.Width = 48;
                        Chart1.ChartAreas[i].Position.Height = (float)((320 / Chart1.Height.Value) * 100);
                    }
    
    
                    if (ds.Tables[i] != null && ds.Tables[i].Rows.Count > 0)
                    {
                        for (int j = 0; j < ds.Tables[i].Rows.Count; j++)
                        {
                            int iM = int.Parse(ds.Tables[i].Rows[j]["MONTH"].ToString());
                            double dJ = double.Parse(ds.Tables[i].Rows[j]["JRATE"].ToString());
                            double dD = double.Parse(ds.Tables[i].Rows[j]["DRATE"].ToString());
    
                            Chart1.Series[Chart1.ChartAreas[i].Name + "_SJ"].Points.AddXY(iM, dJ);
                            Chart1.Series[Chart1.ChartAreas[i].Name + "_SD"].Points.AddXY(iM, dD);
                        }
    
                        //for (int m = 2; m <= 11; m++)
                        //{
                        //    string strM = m < 10 ? "0" + m.ToString() : m.ToString();
                        //    Chart1.Series[Chart1.ChartAreas[i].Name + "_SH"].Points.AddXY(m, 0);
                        //}
                    }
    
                }
    
    
            }
        }
    
        protected DataTable Getdt()
        {
            string sYear = this.DDLYear.SelectedValue.Trim();
            string sSYS = this.DDLSYS.SelectedValue.Trim();
            int iYear = iNowYear;
            StringBuilder sb = new StringBuilder();
            sb.Append(@"SELECT NVL(TA.RESOURCE_NAME,'') DEVICE_NAME 
                            ,NVL(SL.LOOKUP_VALUE_NAME,'') INSTALL_LOCATION
                            ,SUBSTR(YEAR_MONTH,6,2) MONTH
                            ,CASE HOURS_STAT_ACT WHEN 0 THEN 0 
                            ELSE ROUND((HOURS_TRAN+HOURS_TRAN_ALLE+HOURS_PREPARE+HOURS_PLAN_CARE)*100/HOURS_STAT_ACT,2)
                            END JRATE
                            ,CASE HOURS_STAT_ACT WHEN 0 THEN 0 
                            ELSE ROUND(HOURS_TRAN*100/HOURS_STAT_ACT,2)
                            END DRATE
                            FROM RES.T_RES_BU_MOVE_REATE TM
                            LEFT JOIN RES.T_RES_BU_MOVE_REATE_D TD ON TM.MOVE_REATE_ID=TD.MOVE_REATE_ID
                            LEFT JOIN T_RES_BU_DEVICE_ACCOUNT TA
                            ON TD.RESOURCE_ID=TA.DEVICE_ACCOUNT_ID
                            LEFT JOIN T_EB_DB_LOOKUP_VALUE SL ON
                            SL.LOOKUP_VALUE_CODE=TA.INSTALL_LOCATION
                            AND SL.LOOKUP_TYPE_CODE='RES_DEV_LOCATION'
                            WHERE 1=1 ");
            if (int.TryParse(sYear, out iYear))
            {
                sb.Append(@" AND TM.YEAR_MONTH LIKE '" + iYear.ToString() + "%' ");
            }
            else { }
            if (!string.IsNullOrEmpty(sSYS))
            {
                sb.Append(@" AND TA.INSTALL_LOCATION='" + sSYS + "' ");
            }
            else { }
            sb.Append(@" ORDER BY DEVICE_NAME,MONTH ");
            //DataTable dtChart = Gateway.Default.FromCustomSql(sb.ToString()).ToDataSet().Tables[0];
    
            DataSet ds = OraHelper.GetDateDS(sb.ToString());
            if (ds != null && ds.Tables.Count > 0)
            {
                DataTable dtChart = ds.Tables[0];
                return dtChart;
            }
            else return null;
        }
    
        protected void btnClear_Click(object sender, EventArgs e)
        {
            this.DDLYear.Text = iNowYear.ToString();
            this.DDLSYS.Text ="";
        }
    
        protected void btnOut_Click(object sender, EventArgs e)
        {
            SearchChart();
            string sPath = Server.HtmlEncode(Request.PhysicalApplicationPath);
            string Path = sPath + "\TempImageFiles\波动图.jpg";
            Chart1.SaveImage(Path);
            string DownloadPath=Server.HtmlEncode(Request.ApplicationPath);
            DownloadFile(DownloadPath+"/TempImageFiles/波动图.jpg", "波动图.jpg");
        }
    
    
    
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="filename">文件物理地址</param>
        protected void DownloadFile(string filePath,string fName)
        {
            System.IO.FileInfo fi = new System.IO.FileInfo(filePath);
            string fileextname = fi.Extension;
            string DEFAULT_CONTENT_TYPE = "application/unknown";
            RegistryKey regkey, fileextkey;
            string filecontenttype;
            try
            {
                regkey = Registry.ClassesRoot;
                fileextkey = regkey.OpenSubKey(fileextname);
                filecontenttype = fileextkey.GetValue("Content Type", DEFAULT_CONTENT_TYPE).ToString();
            }
            catch
            {
                filecontenttype = DEFAULT_CONTENT_TYPE;
            }
            Response.Clear();
            Response.Charset = "utf-8";
            Response.Buffer = true;
            this.EnableViewState = false;
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AppendHeader("Content-Disposition", "attachment;filename=" +
    Context.Server.UrlPathEncode(fName));
            Response.ContentType = filecontenttype;
            Response.WriteFile(filePath);
            Response.Flush();
            Response.Close();
    
            Response.End();
        }
    }
  • 相关阅读:
    hdu1848(sg函数打表)
    hdu1850(nim博弈)
    hdu1847(sg函数&yy)
    hdu2147(yy)
    poj2133(sg函数)
    Educational Codeforces Round 18D(完全二叉树中序遍历&lowbit)
    atcoder057D(组合数模板)
    euler证明
    04_过滤器Filter_04_Filter生命周期
    04_过滤器Filter_03_多个Filter的执行顺序
  • 原文地址:https://www.cnblogs.com/xytmj/p/3893986.html
Copyright © 2020-2023  润新知