/*******************************************************************************
* MSChart工具包
* 功能:封装微软图表,实现更简单的加载,直接给定数据源DataSet和TableStyle样式即可,
* TableStyle样式详见TableStyle类
* 作者:蜗牛水里爬
* 版本:1.00
* 适用开发环境:VS2008SP1+MSCHART +net.3.5
*
*
*
* *****************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
using System.Data;
using MYTOOLS = NGGWebSet.Tools;
namespace Tools.MsChart
{
/// <summary>
/// 定义MSChart中的核心部件
/// </summary>
public class ChartStyle
{
#region 定义ChartStyle
/// <summary>
/// 定义ChartStyle
/// </summary>
/// <param name="chart"></param>
/// <returns></returns>
public static Chart SetChart(Chart chart)
{
chart.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
chart.BorderColor = System.Drawing.Color.FromArgb(26, 59, 105);
chart.BorderlineDashStyle = ChartDashStyle.Solid;
chart.BorderWidth = 2;
return chart;
}
#endregion
# region 定义ChartArea
/// <summary>
/// 定义ChartArea
/// </summary>
/// <param name="name">ChartArea名称</param>
/// <returns></returns>
public static ChartArea SetChartAreaStyle(string name)
{
return SetChartAreaStyle(name, false);
}
/// <summary>
/// 定义ChartArea
/// </summary>
/// <param name="name">ChartArea名称</param>
/// <param name="Area3DStyle">是否开启3D</param>
/// <returns></returns>
public static ChartArea SetChartAreaStyle(string name, bool Area3DStyle)
{
ChartArea mycharArea = new ChartArea(name);
mycharArea.AxisX.LineColor = System.Drawing.Color.FromArgb(64, 64, 64, 64);
mycharArea.AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", float.Parse("8.25"), FontStyle.Regular);
mycharArea.AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(64, 64, 64, 64);
mycharArea.AxisY.LineColor = System.Drawing.Color.FromArgb(64, 64, 64, 64);
mycharArea.AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", float.Parse("8.25"), FontStyle.Regular);
mycharArea.AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(64, 64, 64, 64);
if (Area3DStyle)
{
mycharArea.Area3DStyle.Enable3D = true;
mycharArea.Area3DStyle.IsRightAngleAxes = false;//是否倾斜
mycharArea.Area3DStyle.IsClustered = false;
//Chart1.ChartAreas["ChartArea1"].Area3DStyle.WallWidth = 0;
mycharArea.Area3DStyle.Inclination = 15;//X倾斜度
mycharArea.Area3DStyle.Rotation = 10;//Y倾斜度
mycharArea.Area3DStyle.Perspective = 10;
}
mycharArea.BackColor = System.Drawing.Color.FromArgb(64, 165, 191, 228);
// BackSecondaryColor="Transparent" BorderColor="64, 64, 64, 64"
mycharArea.BackSecondaryColor = System.Drawing.Color.Transparent;
mycharArea.BorderColor = System.Drawing.Color.FromArgb(64, 64, 64, 64);
mycharArea.AxisY.ScaleBreakStyle.Enabled = true;//开启对比悬殊的东东
//mycharArea.AxisX.Interval = 1;
//mycharArea.AxisY.Interval = 5000;
//mycharArea.AxisX.MajorGrid.Interval = 1;
//mycharArea.Area3DStyle.Enable3D = true;
//mycharArea.AlignmentOrientation = AreaAlignmentOrientations.Horizontal;
//mycharArea.AxisY.Enabled = AxisEnabled.False;
//mycharArea.AxisY2.Enabled = AxisEnabled.True;
//mycharArea.AxisX.LabelStyle.IsEndLabelVisible = false;
return mycharArea;
}
#endregion
#region 定义Title
/// <summary>
/// 定义Title
/// </summary>
/// <param name="name">Title Name</param>
/// <returns></returns>
public static Title SetTitle(string name)
{
return SetTitle(name, false);
}
/// <summary>
/// 定义Title
/// </summary>
/// <param name="name">Title ID</param>
/// <param name="TitleIsDockedInsideChartArea">是否包含在ChartArea中</param>
/// <returns></returns>
public static Title SetTitle(string name, bool TitleIsDockedInsideChartArea)
{
Title title = new Title(name);
title.Name = name;
title.Alignment = ContentAlignment.MiddleCenter;
title.Font = new System.Drawing.Font("微软雅黑", float.Parse("14"), FontStyle.Bold);
if (TitleIsDockedInsideChartArea)
{
title.IsDockedInsideChartArea = true;
title.DockedToChartArea = name;
}
return title;
}
#endregion
#region 定义Legend
/// <summary>
/// 定义Legend
/// </summary>
/// <param name="name">Legend ID</param>
/// <returns></returns>
public static Legend SetLegend(string name)
{
return SetLegend(name, false);
}
/// <summary>
/// 定义Legend
/// </summary>
/// <param name="name"></param>
/// <param name="legendIsDockedInsideChartArea">是否绘制到图表区</param>
/// <returns></returns>
public static Legend SetLegend(string name, bool legendIsDockedInsideChartArea)
{
Legend legend = new Legend(name);
legend.Title = name;
//26, 59, 105
legend.BackColor = Color.Transparent;//Color.FromArgb(26, 59, 105, 0);
legend.BorderColor = Color.Gray;
//legend.Font = new System.Drawing.Font("Trebuchet MS", float.Parse("8.25"), FontStyle.Bold, GraphicsUnit.World);
if (legendIsDockedInsideChartArea)
{
legend.IsDockedInsideChartArea = true;
legend.DockedToChartArea = name;
}
return legend;
}
#endregion
#region 定义Series 适用大部分图形样式------不适合样式有 、饼图、空心饼图等
/// <summary>
/// 定义Series适用大部分图形样式------不适合样式有、饼图、空心饼图等
/// </summary>
/// <param name="name"></param>
/// <param name="stype"></param>
/// <param name="XValueMember"></param>
/// <param name="YValueMembers"></param>
/// <returns></returns>
public static Series SetSeriesStyle(string name, SeriesChartType stype, string XValueMember, string YValueMembers)
{
Series series = new Series(name);
string PointWidth = "0.8";
series.XValueMember = XValueMember;
series.YValueMembers = name;
series.ToolTip = "#VAL";
series["DrawingStyle"] = "Cylinder";
// series.MarkerStyle = MarkerStyle.Circle;点标记
series["PointWidth"] = PointWidth;
series.ChartType = stype;
//series.ChartArea = name;
//series.Legend = name;
return series;
}
#endregion
#region 定义Series适合样式有 、饼图、空心饼图
/// <summary>
/// 适合样式有 、饼图、空心饼图
/// </summary>
/// <param name="name"></param>
/// <param name="stype"></param>
/// <param name="xValues"></param>
/// <param name="yValues"></param>
/// <returns></returns>
public static Series SetSeriesStyle(string name, SeriesChartType stype, string[] xValues, double[] yValues)
{
Series series = new Series(name);
series.ChartType = stype;
// series["PointWidth"] = PointWidth;
series.Points.DataBindXY(xValues, yValues);
for (int i = 0; i < yValues.Count(); i++)
{
series.Points[i].LegendText = xValues[i] + " #VAL";
//series.Points[i].Label = xValues[i] + " #PERCENT{P1}";
//series.Points[i].Label = "#PERCENT{P1}";
}
series.ToolTip = "#VALX #PERCENT{P1}";
series["PieLabelStyle"] = "Disabled";
series.ChartArea = name;
series.Legend = name;
return series;
}
#endregion
}
/// <summary>
/// 对核心组件添加数据
/// </summary>
public class ChartHelper //ChartAddData
{
#region 饼图添加数据尚需改进
#region 获取PointValue
/// <summary>
/// 饼图SeriesPoint添加数据
/// </summary>
/// <param name="series"></param>
/// <param name="dt"></param>
/// <param name="XColumnName"></param>
/// <param name="YColumnName"></param>
public static void GetSeriesPointValue(Series series, DataTable dt, string XColumnName, int YColumnName)
{
int count = dt.Rows.Count;
double[] yValues = new double[count];
string[] xValues = new string[count];
int i = 0;
foreach (DataRow row in dt.Columns)
{
yValues[i] = string.IsNullOrEmpty(row[YColumnName].ToString()) ? 0.00 : double.Parse(row[YColumnName].ToString());
xValues[i] = row[XColumnName].ToString();
i++;
}
series.Points.DataBindXY(xValues, yValues);
}
/// <summary>
/// 饼图SeriesPoint添加数据
/// </summary>
/// <param name="series"></param>
/// <param name="dt"></param>
/// <param name="XColumnName">x轴显示字段</param>
/// <param name="YColumnName">Y轴显示字段</param>
public static void GetSeriesPointValue(Series series, DataTable dt, string XColumnName, string YColumnName)
{
int count = dt.Rows.Count;
double[] yValues = new double[count];
string[] xValues = new string[count];
int i = 0;
foreach (DataRow row in dt.Columns)
{
yValues[i] = string.IsNullOrEmpty(row[YColumnName].ToString()) ? 0.00 : double.Parse(row[YColumnName].ToString());
xValues[i] = row[XColumnName].ToString();
i++;
}
series.Points.DataBindXY(xValues, yValues);
}
#endregion
/// <summary>
/// 适用饼图 根据【列】统计
/// </summary>
/// <param name="dt"></param>
/// <param name="xValues"></param>
/// <param name="yValues"></param>
/// <param name="YColumnName">Y列名</param>
/// <param name="XColumnName">X列名</param>
public static void GetXYValues(DataTable dt, string[] xValues, double[] yValues, string YColumnName, string XColumnName)
{
int i = 0;
foreach (DataRow row in dt.Rows)
{
yValues[i] = string.IsNullOrEmpty(row[YColumnName].ToString()) ? 0.00 : double.Parse(row[YColumnName].ToString());
xValues[i] = row[XColumnName].ToString();
i++;
}
}
/// <summary>
/// 适用饼图 根据【行】统计
/// </summary>
/// <param name="row"></param>
/// <param name="xValues"></param>
/// <param name="yValues"></param>
/// <param name="YColumnName"></param>
/// <param name="XColumnName"></param>
public static void GetXYValues(DataRow row, string[] xValues, double[] yValues, List<string> YColumnName)
{
int i = 0;
foreach (string _column in YColumnName)
{
yValues[i] = string.IsNullOrEmpty(row[_column].ToString()) ? 0.00 : double.Parse(row[_column].ToString());
xValues[i] = _column;
i++;
}
}
#endregion
#region 获取List<Series>
/// <summary>
/// 行专列后用的 得到【 List<Series>】
/// </summary>
/// <param name="dt"></param>
/// <param name="stype"></param>
/// <returns></returns>
public static List<Series> GetListSeriersChange(PackageStyle.Style TBStyle)
{
string name = null;
string XValueMember;
string YValueMembers;
DataTable dt = TBStyle.TB;
List<Series> GetListSeriers = new List<Series>();
for (int i = 1; i < dt.Columns.Count; i++)
{
name = dt.Columns[i].ColumnName.ToString();
XValueMember = dt.Columns[0].ColumnName.ToString();
YValueMembers = name;
Series series = ChartStyle.SetSeriesStyle(name, TBStyle.SCtype, XValueMember, YValueMembers);
GetListSeriers.Add(series);
}
return GetListSeriers;
}
/// <summary>
/// 得到【 List<Series>】
/// </summary>
/// <param name="dt"></param>
/// <param name="YColumnName">对应列名</param>
/// <param name="stype"></param>
/// <returns></returns>
public static List<Series> GetListSeriers(PackageStyle.Style TBStyle)
{
string Xname = null;
string XValueMember;
string YValueMembers;
List<Series> GetListSeriers = new List<Series>();
int i = 0;
foreach (string _YCol in TBStyle.YColumnName)
{
//for (int i = 0; i < YColumnName.Count; i++)
//{
Xname = _YCol;
XValueMember = TBStyle.XColumnName;
YValueMembers = Xname;
// Series series = ChartStyle.SetSeriesStyle(Xname, stype, XValueMember, YValueMembers);
Series series = ChartStyle.SetSeriesStyle(Xname, TBStyle.SCtype, XValueMember, YValueMembers);
GetListSeriers.Add(series);
// }
i++;
}
return GetListSeriers;
}
#endregion
}
/// <summary>
/// Chart数据绑定
/// </summary>
public class BindData
{
#region 绑定接口
/// <summary>
/// 自动绑定数据到Chart
/// </summary>
/// <param name="ds"></param>
/// <param name="TBStyle"></param>
/// <param name="chart"></param>
public void AutoBindData(DataSet ds, List<PackageStyle.Style> TBStyle, Chart chart)
{
for (int i = 0; i < TBStyle.Count; i++)
{
PackageStyle.Style _style = TBStyle[i];
if (_style.SCtype == SeriesChartType.Pie || _style.SCtype == SeriesChartType.Doughnut)
{
NGGWebSet.Tools.DT.GetChinasesName(_style);
_style.XColumnName = _style.XChinasesName;
_style.YColumnName = _style.YChinasesName;
bindChartToPie(_style, chart);
}
else
{
if (_style.GetReverseTable)
{
ds.Tables.Remove(_style.TB);
ds.Tables.Add(NGGWebSet.Tools.DT.GetReverseTable(_style));
_style.TB = ds.Tables[0];
}
else
{
NGGWebSet.Tools.DT.GetChinasesName(_style);
_style.XColumnName = _style.XChinasesName;
_style.YColumnName = _style.YChinasesName;
}
bindChart(_style, chart);
}
}
string Title = "test";
chart.Titles.Add(ChartStyle.SetTitle(Title));
chart.DataSource = ds;
chart.DataBind();
ChartStyle.SetChart(chart);
}
#endregion
#region 多表添加
/// <summary>
/// 自动绑定数据
/// </summary>
/// <param name="TBStyle"></param>
/// <param name="chart"></param>
public void BindChart(List<PackageStyle.Style> TBStyle, Chart chart)
{
foreach (PackageStyle.Style _style in TBStyle)
{
if (_style.SCtype.ToString() == "17" || _style.SCtype.ToString() == "18")
{
bindChartToPie(_style, chart);
}
else
{
bindChart(_style, chart);
}
}
ChartStyle.SetChart(chart);
}
#endregion
#region 单表添加
/// <summary>
/// 饼图
/// </summary>
/// <param name="TBStyle"></param>
/// <param name="chart"></param>
public void bindChartToPie(PackageStyle.Style TBStyle, Chart chart)
{
if (!TBStyle.GetReverseTable)
{
int count = TBStyle.YColumnName.Count;
string[] xValues = new string[count];
double[] yValues = new double[count];
foreach (DataRow row in TBStyle.TB.Rows)
{
string name = row[TBStyle.XColumnName].ToString();
ChartHelper.GetXYValues(row, xValues, yValues, TBStyle.YColumnName);
if (TBStyle.Area3DStyle)
{
chart.ChartAreas.Add(ChartStyle.SetChartAreaStyle(name, true));//添加图形区
}
else
{
chart.ChartAreas.Add(ChartStyle.SetChartAreaStyle(name));//添加图形区
}
chart.Legends.Add(ChartStyle.SetLegend(name, true));//添加图列区
chart.Series.Add(ChartStyle.SetSeriesStyle(name, TBStyle.SCtype, xValues, yValues));
}
}
else
{
// GetXYValues(DataTable dt, string[] xValues, double[] yValues, string YColumnName, string XColumnName)
int count = TBStyle.YColumnName.Count;
string[] xValues = new string[count];
double[] yValues = new double[count];
foreach (string _YCol in TBStyle.YColumnName)
{
string name = _YCol; //row[TBStyle.XColumnName].ToString();
ChartHelper.GetXYValues(TBStyle.TB, xValues, yValues, _YCol, TBStyle.XColumnName);
if (TBStyle.Area3DStyle)
{
chart.ChartAreas.Add(ChartStyle.SetChartAreaStyle(name, true));//添加图形区
}
else
{
chart.ChartAreas.Add(ChartStyle.SetChartAreaStyle(name));//添加图形区
}
chart.Legends.Add(ChartStyle.SetLegend(name, true));//添加图列区
chart.Series.Add(ChartStyle.SetSeriesStyle(name, TBStyle.SCtype, xValues, yValues));
}
}
}
/// <summary>
/// 非饼图
/// </summary>
/// <param name="TBStyle"></param>
/// <param name="chart"></param>
public void bindChart(PackageStyle.Style TBStyle, Chart chart)
{
//ChartHelper.GetListSeriers(TBStyle);
if (TBStyle.Area3DStyle)
{
chart.ChartAreas.Add(ChartStyle.SetChartAreaStyle(TBStyle.TB.TableName, true));//添加图形区
}
else
{
chart.ChartAreas.Add(ChartStyle.SetChartAreaStyle(TBStyle.TB.TableName));//添加图形区
}
List<Series> series = new List<Series>();
if (!TBStyle.GetReverseTable)
{
series = ChartHelper.GetListSeriers(TBStyle);
}
else
{//进行行列转换
series = ChartHelper.GetListSeriersChange(TBStyle);
}
foreach (Series _seriers in series)
{
chart.Series.Add(_seriers);
}
if (TBStyle.TitleDisplay)
{
chart.Titles.Add(ChartStyle.SetTitle(TBStyle.TB.TableName));
}
Legend legend = ChartStyle.SetLegend(TBStyle.TB.TableName);
legend.Docking = Docking.Bottom;
legend.Alignment = StringAlignment.Center;
chart.Legends.Add(legend);//添加图列区
}
#endregion
}
/// <summary>
/// 定义一个完整的Table样式
/// </summary>
public class PackageStyle
{
#region 定义一个完整的Table样式
/// <summary>
/// 定义一个完整的Table样式
/// </summary>
public struct Style
{
/// <summary>
/// DataTable
/// </summary>
public DataTable TB;
/// <summary>
/// 是否行转列
/// </summary>
public bool GetReverseTable;
/// <summary>
/// Title的内容
/// </summary>
public string Title;
/// <summary>
/// X轴显示列名
/// </summary>
public string XColumnName;
/// <summary>
/// X轴中文对应名称
/// </summary>
public string XChinasesName;
/// <summary>
/// 需显示的在Y轴的列名
/// </summary>
public List<string> YColumnName;
/// <summary>
/// Y轴中文对应名称
/// </summary>
public List<string> YChinasesName;
/// <summary>
/// 显示图标类型
/// </summary>
public SeriesChartType SCtype;
/// <summary>
/// 是否开启3D效果
/// </summary>
public bool Area3DStyle;
/// <summary>
/// Title是否显示在图标中
/// </summary>
public bool TitleIsDockedInsideChartArea;
/// <summary>
/// 是否显示标题
/// </summary>
public bool TitleDisplay;
/// <summary>
/// legend是否显示在图标中
/// </summary>
public bool legendIsDockedInsideChartArea;
}
#endregion
}
}
*****************************************************************************************
using System;
using System.Data;
using MsChart = Tools.MsChart.PackageStyle;
namespace NGGWebSet.Tools
{
/// <summary>
/// DataTable工具类
/// </summary>
public static class DT
{
/// <summary>
/// DataTable添加空行
/// </summary>
/// <param name="table">DataTable</param>
/// <param name="total">总行数</param>
public static void AddRow(DataTable table, int total)
{
int count = table.Columns.Count;
int k = total - table.Rows.Count;
if (total >= k)
{
object[] rowVals = new object[count];
for (int i = 0; i < count; i++)
{
rowVals[i] = null;
}
for (int i = 0; i < k; i++)
{
table.Rows.Add(rowVals);
}
}
}
/// <summary>
/// 行专列
/// </summary>
/// <param name="table">表</param>
/// <param name="TBStyle"></param>
/// <returns></returns>
public static DataTable GetReverseTable(MsChart.Style TBStyle)
{
DataTable _table = new DataTable();
_table.Columns.Add(TBStyle.XChinasesName);
foreach (DataRow row in TBStyle.TB.Rows)
{
_table.Columns.Add(row[TBStyle.XColumnName].ToString ());
}
foreach (string _YCol in TBStyle.YColumnName)
{
object[] _ObjectValue = new object[TBStyle.TB.Rows.Count + 1];
_ObjectValue[0] = _YCol;
int i = 1;
foreach (DataRow row in TBStyle.TB.Rows)
{
_ObjectValue[i] = row[_YCol];
i++;
}
_table.Rows.Add(_ObjectValue);
}
_table.TableName = TBStyle.TB.TableName;
//TBStyle.TB = _table;
return _table;
}
/// <summary>
/// 换中文列名
/// </summary>
/// <param name="TBStyle"></param>
/// <returns></returns>
public static void GetChinasesName(MsChart.Style TBStyle)
{
TBStyle.TB.Columns[TBStyle.XColumnName].ColumnName = TBStyle.XChinasesName;
for (int i = 0; i < TBStyle.YColumnName.Count; i++)
{
TBStyle.TB.Columns[TBStyle.YColumnName[i]].ColumnName = TBStyle.YChinasesName[i];
}
// return TBStyle.TB;
}
}
}