前台代码:

                        <asp:Chart ID="Chart1" runat="server" Height="500px" Width="1000px" BorderlineWidth="" >
<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 - ; i < iNowYear + ; 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 > && ds.Tables[].Rows.Count > )
{
//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 = ; i < ds.Tables[].Rows.Count; i++)
{
this.DDLSYS.Items.Add(ds.Tables[].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>)
{
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 = ;
for (int i = ; 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>)
{
DataTable dt = drList.CopyToDataTable();
dt.TableName = DeviceName;
ds.Tables.Add(dt);
_ChartArea = new ChartArea();
_ChartArea.AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(, , );
_ChartArea.AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dash;
_ChartArea.AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(, , );
_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 = ;//起始值
_ChartArea.AxisX.Maximum = ;//结束值
_ChartArea.AxisX.Interval = ;//间隔
_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 = ;
_SeriesJRATE.ShadowOffset = ;
_SeriesJRATE.IsValueShownAsLabel = true;
_SeriesJRATE.MarkerStyle = MarkerStyle.Triangle;
_SeriesJRATE.LegendText = "稼动率";
if (i == ) { _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 = ;
_SeriesDRATE.ShadowOffset = ;
_SeriesDRATE.IsValueShownAsLabel = true;
_SeriesDRATE.MarkerStyle = MarkerStyle.Square;
_SeriesDRATE.LegendText = "直接率";
if (i == ) { _SeriesDRATE.IsVisibleInLegend = true; }
else { _SeriesDRATE.IsVisibleInLegend = false; } Chart1.Series.Add(_SeriesDRATE); //加入Series
}
}
int CRows = Chart1.ChartAreas.Count % == ? (Chart1.ChartAreas.Count / + ) : (Chart1.ChartAreas.Count / );
Chart1.Height = + CRows * ;
for (int i = ; i < Chart1.Legends.Count; i++)
{
Chart1.Legends[i].Docking = Docking.Top;
Chart1.Legends[i].Alignment = StringAlignment.Center;
} Chart1.Legends[].Position.X = (float)(((Chart1.Width.Value / - ) / Chart1.Width.Value) * );
Chart1.Legends[].Position.Y = (float)((/ Chart1.Height.Value) * );
Chart1.Legends[].Position.Height = (float)(( / Chart1.Height.Value) * );
Chart1.Legends[].Position.Width = (float)(( / Chart1.Width.Value) * ); ////Chart1.Legends[0].BackColor = Color.Blue;
//Chart1.Legends[0].BorderColor = Color.Blue;
//Chart1.Legends[0].ForeColor = Color.Blue; for (int i = ; i < Chart1.ChartAreas.Count; i++)
{
int RowNum = i / + ;
if (i % == ) //第一列
{
if (i == ) //第一行
{
Chart1.ChartAreas[i].Position.Y = Chart1.ChartAreas.Count <= ? : ;
}
else //非第一行
{
Chart1.ChartAreas[i].Position.Y = Chart1.ChartAreas[i-].Position.Y+ Chart1.ChartAreas[i-].Position.Height;
}
Chart1.ChartAreas[i].Position.X = 0.0F;
Chart1.ChartAreas[i].Position.Width = ;
Chart1.ChartAreas[i].Position.Height = (float)(( / Chart1.Height.Value) * );
}
else //第二列
{ if (i == ) //第一行
{
Chart1.ChartAreas[i].Position.Y = Chart1.ChartAreas.Count <= ? : ;
}
else //非第一行
{
Chart1.ChartAreas[i].Position.Y = Chart1.ChartAreas[i - ].Position.Y + Chart1.ChartAreas[i - ].Position.Height;
}
Chart1.ChartAreas[i].Position.X =;// (float)(Chart1.Height.Value * 0.5 - 10)
Chart1.ChartAreas[i].Position.Width = ;
Chart1.ChartAreas[i].Position.Height = (float)(( / Chart1.Height.Value) * );
} if (ds.Tables[i] != null && ds.Tables[i].Rows.Count > )
{
for (int j = ; 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 > )
{
DataTable dtChart = ds.Tables[];
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();
}
}

MSChart使用之动态生成多个多行ChartArea的更多相关文章

  1. Aop动态生成代理类时支持带参数构造函数

    一.背景 在某些情况下,我们需要植入AOP代码的类并没有默认构造函数.那么此时动态生成的代理类也需要相同签名的构造函数,并且内部调用原始类的构造函数.自己折腾了1晚上没搞定,现在搞定了发出来供大家一起 ...

  2. dynamic-css 动态 CSS 库,使得你可以借助 MVVM 模式动态生成和更新 css,从 js 事件和 css 选择器的苦海中脱离出来

    dynamic-css 使得你可以借助 MVVM 模式动态生成和更新 css,从而将本插件到来之前,打散.嵌套在 js 中的修改样式的代码剥离出来.比如你要做元素跟随鼠标移动,或者根据滚动条位置的变化 ...

  3. ABP(现代ASP.NET样板开发框架)系列之20、ABP展现层——动态生成WebApi

    点这里进入ABP系列文章总目录 ABP(现代ASP.NET样板开发框架)系列之20.ABP展现层——动态生成WebApi ABP是“ASP.NET Boilerplate Project (ASP.N ...

  4. 【.NET深呼吸】Zip文件操作(2):动态生成Zip文档

    通过前面一篇烂文的介绍,大伙儿知道,ZipArchive类表示一个zip文档实例,除了用上一篇文章中所列的方法来读写zip文件外,还可以直接通过ZipArchive类,动态生成zip文件. 文件流操作 ...

  5. jquery动态生成的元素添加事件的方法

    动态生成的元素如果要添加事件,要写成 $(document).on("click", "#txtName", function() { alert(this.v ...

  6. 利用Java动态生成 PDF 文档

    利用Java动态生成 PDF 文档,则需要开源的API.首先我们先想象需求,在企业应用中,客户会提出一些复杂的需求,比如会针对具体的业务,构建比较典型的具备文档性质的内容,一般会导出PDF进行存档.那 ...

  7. 用C#从数据库动态生成AdminLTE菜单的一种方法

    当前的应用设计风格趋于Flat扁平化,很多基于BootStrap实现了很多UI非常漂亮的管理界面(Bootstrap admin template). 此核心文件开源在Github:https://g ...

  8. 动态生成二维码插件 jquery.qrcode.js

    前段时间做项目,需要动态生成一个二维码,于是就在网上找了一下发现一个jquery插件jquery.qrcode.js,所以今天就简单说一下这个插件的使用: jquery.qrcode.js是依赖jqu ...

  9. Rss 订阅:php动态生成xml格式的rss文件

    Rss 简介: 简易信息聚合(也 叫聚合内容)是一种描述和同步网站内容的格式.使用RSS订阅能更快地获取信息,网站提供RSS输出,有利于让用户获取网站内容的最新更新.网络用户可以在客户端借助于支持RS ...

随机推荐

  1. MyEclipse右键new菜单项的设置 及 Eclipse中各种文件不能保存中文的问题

    有时候,myeclipse右键new的时候经常出现一些ejb等文件你懂的,很是恶心~~ Window --> Customize Perspective --> Submenus --&g ...

  2. 使用 getNextException() 来检索已经过批处理的特定元素的异常。 ERRORCODE=-4228, SQLSTATE=null

    今天查询了一天发现的问题,用ibatis做批量操作时,报错: [非原子批处理出现故障]使用 getNextException() 来检索已经过批处理的特定元素的异常. ERRORCODE=-4228, ...

  3. C#程序设计基础——字符串

    C#字符串使用string关键字声明,且由一个或多个字符构成的一组字符. 串联字符串 串联字符串是将一个字符串追加到另一个字符串末尾的过程.使用“+”或“+=”运算符串链字符符文本或字符串常量.串联字 ...

  4. Pearls DP

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6647   Accepted: 3241 Description In Pe ...

  5. ural-1099-Work Scheduling(裸带花树)

    题意: 有N个人,有限对的人可以在一起工作,问最多能有多少对. 分析: 任意图的最大匹配 // File MAXName: 1099.cpp // Author: Zlbing // Created ...

  6. 【最小生成树】Codeforces 707B Bakery

    题目链接: http://codeforces.com/problemset/problem/707/B 题目大意: 给你N个点M条无向边,其中有K个面粉站,现在一个人要在不是面粉站的点上开店,问到面 ...

  7. bzoj4028: [HEOI2015]公约数数列

    Description 设计一个数据结构. 给定一个正整数数列 a_0, a_1, ..., a_{n - 1},你需要支持以下两种操作: 1. MODIFY id x: 将 a_{id} 修改为 x ...

  8. mysql 添加定时任务

    之前定时任务都是用quartz 或者spring的任务调度来做的,易于管理,但是要写代码加 配置,其实mysql 自带了job ,先创建一个存储过程

  9. Demo_CS(移动,切换枪支,发射子弹)

    using UnityEngine; using System.Collections; public class Gun : MonoBehaviour { private Animator ani ...

  10. hibernate4.3.8与spring mvc结合遇到的问题

    2703 [2015-01-21 16:47:42 ] - [ip=, ref=, ua=, sid=] WARN o.h.e.jdbc.spi.SqlExceptionHelper - SQL Er ...