MSChart使用之动态生成多个多行ChartArea
前台代码:
<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的更多相关文章
- Aop动态生成代理类时支持带参数构造函数
一.背景 在某些情况下,我们需要植入AOP代码的类并没有默认构造函数.那么此时动态生成的代理类也需要相同签名的构造函数,并且内部调用原始类的构造函数.自己折腾了1晚上没搞定,现在搞定了发出来供大家一起 ...
- dynamic-css 动态 CSS 库,使得你可以借助 MVVM 模式动态生成和更新 css,从 js 事件和 css 选择器的苦海中脱离出来
dynamic-css 使得你可以借助 MVVM 模式动态生成和更新 css,从而将本插件到来之前,打散.嵌套在 js 中的修改样式的代码剥离出来.比如你要做元素跟随鼠标移动,或者根据滚动条位置的变化 ...
- ABP(现代ASP.NET样板开发框架)系列之20、ABP展现层——动态生成WebApi
点这里进入ABP系列文章总目录 ABP(现代ASP.NET样板开发框架)系列之20.ABP展现层——动态生成WebApi ABP是“ASP.NET Boilerplate Project (ASP.N ...
- 【.NET深呼吸】Zip文件操作(2):动态生成Zip文档
通过前面一篇烂文的介绍,大伙儿知道,ZipArchive类表示一个zip文档实例,除了用上一篇文章中所列的方法来读写zip文件外,还可以直接通过ZipArchive类,动态生成zip文件. 文件流操作 ...
- jquery动态生成的元素添加事件的方法
动态生成的元素如果要添加事件,要写成 $(document).on("click", "#txtName", function() { alert(this.v ...
- 利用Java动态生成 PDF 文档
利用Java动态生成 PDF 文档,则需要开源的API.首先我们先想象需求,在企业应用中,客户会提出一些复杂的需求,比如会针对具体的业务,构建比较典型的具备文档性质的内容,一般会导出PDF进行存档.那 ...
- 用C#从数据库动态生成AdminLTE菜单的一种方法
当前的应用设计风格趋于Flat扁平化,很多基于BootStrap实现了很多UI非常漂亮的管理界面(Bootstrap admin template). 此核心文件开源在Github:https://g ...
- 动态生成二维码插件 jquery.qrcode.js
前段时间做项目,需要动态生成一个二维码,于是就在网上找了一下发现一个jquery插件jquery.qrcode.js,所以今天就简单说一下这个插件的使用: jquery.qrcode.js是依赖jqu ...
- Rss 订阅:php动态生成xml格式的rss文件
Rss 简介: 简易信息聚合(也 叫聚合内容)是一种描述和同步网站内容的格式.使用RSS订阅能更快地获取信息,网站提供RSS输出,有利于让用户获取网站内容的最新更新.网络用户可以在客户端借助于支持RS ...
随机推荐
- [BZOJ 3236] [Ahoi2013] 作业 && [BZOJ 3809] 【莫队(+分块)】
题目链接: BZOJ - 3236 BZOJ - 3809 算法一:莫队 首先,单纯的莫队算法是很好想的,就是用普通的第一关键字为 l 所在块,第二关键字为 r 的莫队. 这样每次端点移动添加或删 ...
- [BZOJ 1084] [SCOI2005] 最大子矩阵 【DP】
题目链接:BZOJ - 1084 题目分析 我看的是神犇BLADEVIL的题解. 1)对于 m = 1 的情况, 首先可能不取 Map[i][1],先 f[i][k] = f[i - 1][k]; ...
- 看奢侈品Prada如何使用物联网
这是峰哥在一家国际顶级商学院听课的笔记.这是个巨变的时代,有趣的时代. 一 PRADA在纽约的旗舰店.每件衣服上都有RFID码.每当一个顾客拿起一件PRADA进试衣间,RFID会被自动识别,试衣间里的 ...
- 【HDOJ】3325 Arithmetically Challenged
简单DFS. /* 3325 */ #include <iostream> #include <set> #include <cstdio> #include &l ...
- BZOJ1621: [Usaco2008 Open]Roads Around The Farm分岔路口
1621: [Usaco2008 Open]Roads Around The Farm分岔路口 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 521 S ...
- hihoCoder 1392 War Chess 【模拟】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)
#1392 : War Chess 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Rainbow loves to play kinds of War Chess gam ...
- 【最短路】BAPC2014 B Button Bashing (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 动态规划——E (LIS())最长上升子序列
E - LIS Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Stat ...
- C# 基础小知识之yield 关键字 语法糖
原文地址:http://www.cnblogs.com/santian/p/4389675.html 对于yield关键字我们首先看一下msdn的解释: 如果你在语句中使用 yield 关键字,则意味 ...
- [Locked] Count Univalue Subtrees
Count Univalue Subtrees Given a binary tree, count the number of uni-value subtrees. A Uni-value sub ...