用dotnetCHARTING图表显示数据 dotnet

网上找的一段代码然后自己再次封装.对于饼图没有封装.using System;
using System.Data;
using dotnetCHARTING;
using System.Drawing;
using Report.BLL;
/// <summary>
///ShowData 的摘要说明
/// </summary>
public class ShowData
{ #region Property private string_phaysicalimagepath;//图片存放路径
private string _title; //图片标题
private string _xtitle;//图片x座标名称
private string _ytitle;//图片y座标名称
private string _seriesname;//图例名称
private int _picwidth;//图片宽度
private int _picheight;//图片高度
private SeriesType _seriesType;//图片类型
private DataTable _dt;//图片数据源
///<summary>
/// 图片存放路径
///</summary>
public string PhaysicalImagePath
{
set {_phaysicalimagepath = value; }
get {return _phaysicalimagepath; }
} ///<summary>
/// 图片标题
///</summary>
public string Title
{
set {_title = value; }
get {return _title; }
} ///<summary>
/// 图片标题
///</summary>
public string XTitle
{
set {_xtitle = value; }
get {return _xtitle; }
} ///<summary>
/// 图片标题
///</summary>
public string YTitle
{
set {_ytitle = value; }
get {return _ytitle; }
} ///<summary>
/// 图例名称
///</summary>
public string SeriesName
{
set {_seriesname = value; }
get {return _seriesname; }
} ///<summary>
/// 图片宽度
///</summary>
public int PicWidth
{
set {_picwidth = value; }
get {return _picwidth; }
} ///<summary>
/// 图片高度
///</summary>
public int PicHeight
{
set {_picheight = value; }
get {return _picheight; }
} ///<summary>
/// 图例名称
用dotnetCHARTING图表显示数据 dotnet
///</summary>
public SeriesType SeriesType
{
set {_seriesType = value; }
get {return _seriesType; }
} ///<summary>
/// 图片数据源
///</summary>
public DataTable DataSource
{
set {_dt = value; }
get {return _dt; }
}
#endregion #region 构造函数 public ShowData()
{
//
// TOD 在此处添加构造函数逻辑
//
} public ShowData(string PhaysicalImagePath,string Title, string XTitle, string YTitle, stringSeriesName)
{
_phaysicalimagepath =PhaysicalImagePath;
_title = Title;
_xtitle = XTitle;
_ytitle = YTitle;
_seriesname =SeriesName;
} #endregion #region Create Pie private SeriesCollectiongetArrayData()
{
SeriesCollection SC =new SeriesCollection();
DataTable dt =this.DataSource; for (int i = 0; i< dt.Rows.Count; i++)
{
Series s = new Series();
s.Name = dt.Rows[i][0].ToString();Element e = new Element();// 每元素的名称
e.Name = dt.Rows[i][0].ToString();// 每元素的大小数值
e.YValue = Convert.ToDouble(dt.Rows[i][1].ToString());s.Elements.Add(e);
SC.Add(s);
}
return SC;
}
///<summary>
/// 饼图
///</summary>
///<returns></returns>
public void CreatePie(dotnetCHARTING.Chartchart)
{
chart.Title =this.Title;
chart.TempDirectory =this.PhaysicalImagePath;
chart.Width =this.PicWidth;
chart.Height =this.PicHeight;
chart.Type =ChartType.Pie; chart.Series.Type =SeriesType.Cylinder;
chart.Series.Name =this.SeriesName; chart.ShadingEffect= true;
chart.Use3D =true;
chart.DefaultSeries.DefaultElement.Transparency = 20;
chart.DefaultSeries.DefaultElement.ShowValue = true;
chart.PieLabelMode =PieLabelMode.Outside;
chart.SeriesCollection.Add(getArrayData());
chart.Series.DefaultElement.ShowValue = true;
} #endregion #region Create ///<summary>
/// 曲线图--1根线
/// X轴为DataTable的第一列
/// Y轴为DataTable的第二列
///</summary>
///<returns></returns>
public void Create(dotnetCHARTING.Chartchart)
{
chart.Title =this.Title;
chart.XAxis.Label.Text =this.XTitle;
chart.YAxis.Label.Text =this.YTitle;
chart.TempDirectory =this.PhaysicalImagePath;
chart.Width =this.PicWidth;
chart.Height =this.PicHeight;
chart.Type =ChartType.Combo; chart.Series.Type =this.SeriesType;
chart.Series.Name =this.SeriesName;
chart.Series.Data =this.DataSource;
chart.SeriesCollection.Add();chart.DefaultSeries.DefaultElement.ShowValue = true;
chart.ShadingEffect =true;
chart.Use3D =false;
chart.Series.DefaultElement.ShowValue = true; } ///<summary>
/// 曲线图--1根线
/// xFieldName为X轴字段名
/// yFieldName为Y轴字段名
///</summary>
///<returns></returns>
public void Create(dotnetCHARTING.Chart chart,string xFieldName, string yFieldName)
{
chart.Title =this.Title;
chart.XAxis.Label.Text =this.XTitle;
chart.YAxis.Label.Text =this.YTitle;
chart.TempDirectory =this.PhaysicalImagePath;
chart.Width =this.PicWidth;
chart.Height =this.PicHeight;
chart.Type =ChartType.Combo; chart.Series.Type =this.SeriesType;
chart.Series.Name =this.SeriesName;
chart.Series.Data =GetTableByXY(this.DataSource, xFieldName, yFieldName);
chart.SeriesCollection.Add();chart.DefaultSeries.DefaultElement.ShowValue = true;
chart.ShadingEffect =true;
chart.Use3D =false;
chart.Series.DefaultElement.ShowValue = true; } ///<summary>
/// 曲线图--1根线
/// xFieldIndex为X轴字段索引
/// yFieldIndex为Y轴字段索引
///</summary>
///<returns></returns>
public void Create(dotnetCHARTING.Chart chart,int xFieldIndex, int yFieldIndex)
{
chart.Title =this.Title;
chart.XAxis.Label.Text =this.XTitle;
chart.YAxis.Label.Text =this.YTitle;
chart.TempDirectory =this.PhaysicalImagePath;
chart.Width =this.PicWidth;
chart.Height =this.PicHeight;
chart.Type =ChartType.Combo; chart.Series.Type =this.SeriesType;
chart.Series.Name =this.SeriesName;
chart.Series.Data =GetTableByXY(this.DataSource, xFieldIndex, xFieldIndex);
chart.SeriesCollection.Add();chart.DefaultSeries.DefaultElement.ShowValue = true;
chart.ShadingEffect =true;
chart.Use3D =false;
chart.Series.DefaultElement.ShowValue = true; } ///<summary>
/// 曲线图--多根线
/// X轴为DataTable的第一列
/// Y轴为DataTable的第二列
/// fieldName为多条线的区分列的字段名
///</summary>
///<returns></returns>
public void Create(dotnetCHARTING.Chart chart,string fieldName)
{
chart.Title =this.Title;
chart.XAxis.Label.Text =this.XTitle;
chart.YAxis.Label.Text =this.YTitle;
chart.TempDirectory =this.PhaysicalImagePath;
chart.Width =this.PicWidth;
chart.Height =this.PicHeight;
chart.Type =ChartType.Combo;
if (this.DataSource !=null &&this.DataSource.Columns.Contains(fieldName))
{
if (this.DataSource.Rows.Count > 0)
{
DataRow[] drs = this.DataSource.Select("",string.Format("{0} asc",fieldName));
string splitValue = string.Empty;
foreach (DataRow dr in drs)
{
if (!splitValue.Contains(dr[fieldName].ToString()))
{
splitValue += dr[fieldName].ToString() + ",";
}
}
splitValue = splitValue.TrimEnd(',');
int splitCount = splitValue.Split(',').Length;
for (int i = 0; i < splitCount; i++)
{
Series series = new Series();
series.Type = this.SeriesType;
series.Name = this.SeriesName;
series.Data = GetTableBySplitValue(this.DataSource, fieldName,splitValue.Split(',')[i]);
chart.SeriesCollection.Add(series);
}
}
}
else
{
chart.Series.Type = this.SeriesType;
chart.Series.Name = this.SeriesName;
chart.Series.Data = this.DataSource;
chart.SeriesCollection.Add();
}
chart.DefaultSeries.DefaultElement.ShowValue = true;
chart.ShadingEffect =true;
chart.Use3D =false;
chart.Series.DefaultElement.ShowValue = true; } ///<summary>
/// 曲线图--多根线
/// X轴为DataTable的第一列
/// Y轴为DataTable的第二列
/// fieldName为多条线的区分列的字段名
/// xFieldName为X轴字段名
/// yFieldName为Y轴字段名
///</summary>
public void Create(dotnetCHARTING.Chart chart,string fieldName, string xFieldName, string yFieldName)
{
chart.Title =this.Title;
chart.XAxis.Label.Text =this.XTitle;
chart.YAxis.Label.Text =this.YTitle;
chart.TempDirectory =this.PhaysicalImagePath;
chart.Width =this.PicWidth;
chart.Height =this.PicHeight;
chart.Type =ChartType.Combo;
if (this.DataSource !=null &&ContainFieldName(this.DataSource, fieldName))
{
if (this.DataSource.Rows.Count > 0)
{
DataRow[] drs = this.DataSource.Select("",string.Format("{0} asc",fieldName));
string splitValue = string.Empty;
foreach (DataRow dr in drs)
{
if (!splitValue.Contains(dr[fieldName].ToString()))
{
splitValue += dr[fieldName].ToString() + ",";
}
}
splitValue = splitValue.TrimEnd(',');
int splitCount = splitValue.Split(',').Length;
for (int i = 0; i < splitCount; i++)
{
string val = splitValue.Split(',')[i];
Series series = new Series();
series.Type = this.SeriesType;
series.Name = val;
series.Data = GetTableBySplitValue(this.DataSource, fieldName, val,xFieldName, yFieldName);
chart.SeriesCollection.Add(series);
RandomColor rc = new RandomColor();
series.DefaultElement.Color = rc.GetRandomColor();
}
}
}
else
{
chart.Series.Type = this.SeriesType;
chart.Series.Name = this.SeriesName;
chart.Series.Data = this.DataSource;
chart.SeriesCollection.Add();
}
chart.DefaultSeries.DefaultElement.ShowValue = true;
chart.ShadingEffect =true;
chart.Use3D =false;
chart.Series.DefaultElement.ShowValue = true; } ///<summary>
/// 曲线图--多根线
/// X轴为DataTable的第一列
/// Y轴为DataTable的第二列
/// fieldName为多条线的区分列的字段名
/// xFieldIndex为X轴字段索引
/// yFieldIndex为Y轴字段索引
///</summary>
public void Create(dotnetCHARTING.Chart chart,string fieldName, int xFieldIndex, int yFieldIndex)
{
chart.Title =this.Title;
chart.XAxis.Label.Text =this.XTitle;
chart.YAxis.Label.Text =this.YTitle;
chart.TempDirectory =this.PhaysicalImagePath;
chart.Width =this.PicWidth;
chart.Height =this.PicHeight;
chart.Type =ChartType.Combo;
if (this.DataSource !=null &&this.DataSource.Columns.Contains(fieldName))
{
if (this.DataSource.Rows.Count > 0)
{
DataRow[] drs = this.DataSource.Select("",string.Format("{0} asc",fieldName));
string splitValue = string.Empty;
foreach (DataRow dr in drs)
{
if (!splitValue.Contains(dr[fieldName].ToString()))
{
splitValue += dr[fieldName].ToString() + ",";
}
}
splitValue = splitValue.TrimEnd(',');
int splitCount = splitValue.Split(',').Length;
for (int i = 0; i < splitCount; i++)
{
string val = splitValue.Split(',')[i];
Series series = new Series();
series.Type = this.SeriesType;
series.Name = val;
series.Data = GetTableBySplitValue(this.DataSource, fieldName, val,xFieldIndex, yFieldIndex);
chart.SeriesCollection.Add(series);
}
}
}
else
{
chart.Series.Type = this.SeriesType;
chart.Series.Name = this.SeriesName;
chart.Series.Data = this.DataSource;
chart.SeriesCollection.Add();
}
chart.DefaultSeries.DefaultElement.ShowValue = true;
chart.ShadingEffect =true;
chart.Use3D =false;
chart.Series.DefaultElement.ShowValue = true; } ///<summary>
/// 表是否包含fieldName字段
///</summary>
public bool ContainFieldName(DataTable dt,string fieldName)
{
foreach (DataColumn dcin dt.Columns)
{
if (dc.ColumnName == fieldName.Trim())
{
return true;
break;
}
}
return false;
} #endregion #region Deal DataTable ///<summary>
/// 根据X,Y字段名生成表
/// xFieldName X字段名
/// yFieldName Y字段名
///</summary>
public DataTable GetTableByXY(DataTableoldTable, string xFieldName, string yFieldName)
{
DataTable newTable = newDataTable(oldTable.TableName);
DataColumn x = newDataColumn(xFieldName);
DataColumn y = newDataColumn(yFieldName);
newTable.Columns.Add(x);
newTable. Columns.Add(y);
if (oldTable.Rows.Count> 0 &&oldTable.Columns.Contains(xFieldName)&&oldTable.Columns.Contains(yFieldName))
{
foreach (DataRow dr in oldTable.Rows)
{
DataRow newRow = newTable.NewRow();
newRow[xFieldName] = dr[xFieldName];
newRow[yFieldName] = dr[yFieldName];
newTable.Rows.Add(newRow);
}
}
return newTable;
} ///<summary>
/// 根据X,Y字段索引生成表
/// xFieldName X字段索引
/// yFieldName Y字段索引
///</summary>
public DataTable GetTableByXY(DataTableoldTable, int xFieldIndex, int yFieldIndex)
{
DataTable newTable = newDataTable(oldTable.TableName);
DataColumn x = newDataColumn(xFieldIndex.ToString());
DataColumn y = newDataColumn(yFieldIndex.ToString());
newTable.Columns.Add(x);
newTable.Columns.Add(y);
if (oldTable.Rows.Count> 0 &&oldTable.Columns.Count >= xFieldIndex&& oldTable.Columns.Count>= yFieldIndex)
{
foreach (DataRow dr in oldTable.Rows)
{
DataRow newRow = newTable.NewRow();
newRow[xFieldIndex] = dr[xFieldIndex];
newRow[xFieldIndex] = dr[xFieldIndex];
newTable.Rows.Add(newRow);
}
}
return newTable;
} ///<summary>
/// 根据隔离数据分割表
/// fieldName 隔离字段
/// SplitValue 隔离字段值
///</summary>
public DataTable GetTableBySplitValue(DataTableoldTable, string fieldName, string SplitValue)
{
DataTable newTable =oldTable.Copy();
newTable.TableName =string.Format("{0}_{1}",oldTable.TableName, SplitValue); DataRow[] drs =newTable.Select(string.Format("{0}<>'{1}'",fieldName, SplitValue));
foreach (DataRow dr indrs)
{
dr.Delete();
}
newTable.AcceptChanges();
return newTable;
} ///<summary>
/// 根据隔离数据分割表
/// fieldName 隔离字段
/// SplitValue 隔离字段值
/// xFieldName X字段名
/// yFieldName Y字段名
///</summary>
public DataTable GetTableBySplitValue(DataTableoldTable, string fieldName, string SplitValue, string xFieldName,string yFieldName)
{
returnGetTableByXY(GetTableBySplitValue(oldTable, fieldName, SplitValue),xFieldName, yFieldName);
} ///<summary>
/// 根据隔离数据分割表
/// fieldName 隔离字段
/// SplitValue 隔离字段值
/// xFieldName X字段索引
/// yFieldName Y字段索引
///</summary>
public DataTable GetTableBySplitValue(DataTableoldTable, string fieldName, string SplitValue, int xFieldIndex, intyFieldIndex)
{
returnGetTableByXY(GetTableBySplitValue(oldTable, fieldName, SplitValue),xFieldIndex, yFieldIndex);
} #endregion
}
//使用
//ShowData show = new ShowData();
//show.Title = title;
//show.XTitle = xHeader;
//show.YTitle = yHeader;
//show.SeriesName = "";
//show.DataSource = ds.Tables[0];
//show.PicHight = 500;
//show.PicWidth = 1000;
//show.PhaysicalImagePath = "temp";
//switch (seriesType)
//{
// case "0"://线性图
// show.SeriesType =SeriesType.Line;
// break;
// case "1"://柱状图
// show.SeriesType =SeriesType.Cylinder;
// break;
//}
//show.Create(this.Chart, "区", xHeader, yHeader);

  

爱华网本文地址 » http://www.aihuau.com/a/25101016/316248.html

更多阅读

怎样在任务栏里加上“显示桌面”快捷键 任务栏快捷键

怎样在任务栏里加上“显示桌面”快捷键——简介“显示桌面”是个放在任务栏快速启动模块里的小图标,别看它是个图标,只要一点击它就能回到桌面,不用一个个关闭缩小窗口,非常方便。当我们误操作或者丢失这个图表“显示桌面”,我们该怎么办

数据透视表怎么用 精 2010数据透视表怎么用

数据透视表怎么用 精——简介数据透视表是microsoft excel表格提供的又一便捷的数据分析工具。能够较快的将所需数据呈现在表格或者图形中。来看看具体是如何操作的吧。 数据透视表怎么用 精——工具/原料Microsoft Excel 2003数

怎样制作EXCEL图表 2010excel制作图表

怎样制作EXCEL图表——简介EXCEL是常用的办公软件之一,用它来处理数据方便、快捷。那么,怎样制作使用EXCEL制作图表呢?怎样制作EXCEL图表——方法/步骤怎样制作EXCEL图表 1、首先,新建EXCEL,输入几组数据作为图表引用的数据。怎样制作E

怎样使用EXECL中的数据透视表入门篇 execl数据匹配

用EXECL中的数据透视表做统计非常方便,下面我用一个简单的例子给大家介绍下(本文使用的是MS OFFICE2007):假设我有个表格,要按月将不同列分别作统计步骤如下:点击“插入”-“数据透视表”点击图示中的按钮,选择数据透视表要统计

声明:《用dotnetCHARTING图表显示数据 dotnet》为网友笙清初扇离分享!如侵犯到您的合法权益请联系我们删除