建立RadControls图表方法
浏览量:4208
#region 图表方法
/// <summary>
/// 创建图表数据集
/// </summary>
public static DataSet CreateChartDataSet()
{
DataSet dsChart = new DataSet();
// 添加基本表
DataTable dtChartBase = new DataTable("ChartBase");
dtChartBase.Columns.Add("Title", typeof(string));
dtChartBase.Columns.Add("Show3D", typeof(bool)).DefaultValue = false;
dtChartBase.Columns.Add("ShowLegend", typeof(bool)).DefaultValue = false;
dtChartBase.Columns.Add("ChartType", typeof(string));
dtChartBase.Columns.Add("Width", typeof(string));
dtChartBase.Columns.Add("Height", typeof(string));
dsChart.Tables.Add(dtChartBase);
// 添加轴表
DataTable dtChartAxis = new DataTable("ChartAxis");
dtChartAxis.Columns.Add("XTitle", typeof(string));
dtChartAxis.Columns.Add("YTitle", typeof(string));
dtChartAxis.Columns.Add("XStartFromZero", typeof(bool)).DefaultValue = false;
dtChartAxis.Columns.Add("YStartFromZero", typeof(bool)).DefaultValue = false;
dtChartAxis.Columns.Add("XValueType", typeof(ChartValueTypes)).DefaultValue = ChartValueTypes.Double;
dtChartAxis.Columns.Add("YValueType", typeof(ChartValueTypes)).DefaultValue = ChartValueTypes.Double;
dtChartAxis.Columns.Add("XFormat", typeof(string)).DefaultValue = "N2";
dtChartAxis.Columns.Add("YFormat", typeof(string)).DefaultValue = "N2";
dtChartAxis.Columns.Add("XAngle", typeof(int)).DefaultValue = 30;
dtChartAxis.Columns.Add("YAngle", typeof(int)).DefaultValue = 30;
dsChart.Tables.Add(dtChartAxis);
// 添加数据表
DataTable dtSeriesData = new DataTable("SeriesData");
dtSeriesData.Columns.Add("Series", typeof(string));
dtSeriesData.Columns.Add("XValue", typeof(string));
dtSeriesData.Columns.Add("YValue", typeof(string));
dsChart.Tables.Add(dtSeriesData);
// 添加明细表
DataTable dtSeriesDetail = new DataTable("SeriesDetail");
dtSeriesDetail.Columns.Add("Series", typeof(string));
dtSeriesDetail.Columns.Add("ChartType", typeof(string));
dtSeriesDetail.Columns.Add("ShowLabelAsValue", typeof(bool)).DefaultValue = false;
dsChart.Tables.Add(dtSeriesDetail);
dsChart.RemotingFormat = System.Data.SerializationFormat.Binary;
return dsChart;
}
/// <summary>
/// 显示图表
/// </summary>
/// <param name="chart">图表</param>
/// <param name="ds">数据集</param>
public static void ShowChart(Dundas.Charting.WebControl.Chart chart, DataSet ds)
{
if (ds == null)
{
chart.ImageUrl = string.Empty;
return;
}
DataTable dtChartBase = ds.Tables["ChartBase"];
DataTable dtChartAxis = ds.Tables["ChartAxis"];
DataTable dtSeriesData = ds.Tables["SeriesData"];
DataTable dtSeriesDetail = ds.Tables["SeriesDetail"];
Font fontTitle = new Font("Trebuchet MS", 10.5f, FontStyle.Bold, GraphicsUnit.Pixel);
Font fontLegend = new Font("Trebuchet MS", 10.5f, FontStyle.Regular, GraphicsUnit.Pixel);
// 添加图表标题
chart.Titles.Clear();
Dundas.Charting.WebControl.Title title = new Title("Title1");
title.Text = dtChartBase.Rows[0]["Title"].ToString();
title.Font = fontTitle;
chart.Titles.Add(title);
// 是否显示三维图表
bool bShow3D = Convert.ToBoolean(dtChartBase.Rows[0]["Show3D"]);
// 设置三维风格
chart.ChartAreas["Default"].Area3DStyle.Enable3D = bShow3D;
// 是否显示图例
bool bShowLegend = Convert.ToBoolean(dtChartBase.Rows[0]["ShowLegend"]);
if (bShowLegend)
{
chart.Legends.Clear();
Dundas.Charting.WebControl.Legend legend = new Legend("Default");
legend.AutoFitText = false;
legend.BackColor = System.Drawing.Color.Transparent;
legend.Enabled = true;
legend.Font = fontLegend;
chart.Legends.Add(legend);
}
else
{
chart.Legends.Clear();
}
string strDefaultChartType = dtChartBase.Rows[0]["ChartType"].ToString();
if (dtChartBase.Rows[0]["Width"] != DBNull.Value)
{
chart.Width = new Unit(dtChartBase.Rows[0]["Width"].ToString());
}
if (dtChartBase.Rows[0]["Height"] != DBNull.Value)
{
chart.Height = new Unit(dtChartBase.Rows[0]["Height"].ToString());
}
// 添加横轴、纵轴标题
chart.ChartAreas["Default"].AxisX.Title = dtChartAxis.Rows[0]["XTitle"].ToString();
chart.ChartAreas["Default"].AxisY.Title = dtChartAxis.Rows[0]["YTitle"].ToString();
chart.ChartAreas["Default"].AxisX.TitleFont = fontTitle;
chart.ChartAreas["Default"].AxisY.TitleFont = fontTitle;
// 设置横轴、纵轴是否从0开始
chart.ChartAreas["Default"].AxisX.StartFromZero = Convert.ToBoolean(dtChartAxis.Rows[0]["XStartFromZero"]);
chart.ChartAreas["Default"].AxisY.StartFromZero = Convert.ToBoolean(dtChartAxis.Rows[0]["YStartFromZero"]);
// 获取横轴、纵轴数据类型
ChartValueTypes xValueType = (ChartValueTypes)Convert.ToInt32(dtChartAxis.Rows[0]["XValueType"]);
ChartValueTypes yValueType = (ChartValueTypes)Convert.ToInt32(dtChartAxis.Rows[0]["YValueType"]);
string xFormat = dtChartAxis.Rows[0]["XFormat"].ToString();
string yFormat = dtChartAxis.Rows[0]["YFormat"].ToString();
chart.ChartAreas["Default"].Area3DStyle.XAngle = Convert.ToInt32(dtChartAxis.Rows[0]["XAngle"]);
chart.ChartAreas["Default"].Area3DStyle.YAngle = Convert.ToInt32(dtChartAxis.Rows[0]["YAngle"]);
// DISTINCT字段数组
string[] myColumnNames = { "Series" };
// 准备填充的序列
DataTable dtSeriesList = dtSeriesData.DefaultView.ToTable(true, myColumnNames);
// 填充数据
chart.Series.Clear();
foreach (DataRow dr1 in dtSeriesList.Rows)
{
string strSeries = dr1["Series"].ToString();
Series series = new Series(strSeries);
// 组织序列数据
DataTable dtSeries = dtSeriesData.Clone();
dtSeries.Columns.Remove("Series");
foreach (DataRow dr2 in dtSeriesData.Select("Series='" + strSeries + "'"))
{
dtSeries.ImportRow(dr2);
}
// 设置序列类型
DataRow[] drRows = dtSeriesDetail.Select("Series='" + strSeries + "'");
if (drRows.Length > 0)
{
string strChartType = drRows[0]["ChartType"].ToString();
if (!string.IsNullOrEmpty(strChartType))
{
series.Type = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), strChartType, true);
}
else if (!string.IsNullOrEmpty(strDefaultChartType))
{
series.Type = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), strDefaultChartType, true);
}
// 设置序列标签
series.ShowLabelAsValue = Convert.ToBoolean(drRows[0]["ShowLabelAsValue"]);
}
else if (!string.IsNullOrEmpty(strDefaultChartType))
{
series.Type = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), strDefaultChartType, true);
}
switch (series.Type)
{
case SeriesChartType.Area://面积图
break;
case SeriesChartType.Bar://条形图
break;
case SeriesChartType.BoxPlot://盒形图
break;
case SeriesChartType.Bubble://泡泡图
break;
case SeriesChartType.CandleStick:
break;
case SeriesChartType.Column://柱状图
series["DrawingStyle"] = "Default";
series["PointWidth"] = "1.0";
break;
case SeriesChartType.Doughnut://圆环图
series["PieLabelStyle"] = "Inside";
series["PieDrawingStyle"] = "SoftEdge";
series["DoughnutRadius"] = "60";
break;
case SeriesChartType.ErrorBar:
break;
case SeriesChartType.FastLine:
break;
case SeriesChartType.FastPoint:
break;
case SeriesChartType.Funnel:
break;
case SeriesChartType.Gantt:
break;
case SeriesChartType.Kagi:
break;
case SeriesChartType.Line://折线图
break;
case SeriesChartType.Pie://饼形图
series["PieLabelStyle"] = "Inside";
series["PieDrawingStyle"] = "SoftEdge";
break;
case SeriesChartType.Point://散点图
break;
case SeriesChartType.PointAndFigure:
break;
case SeriesChartType.Polar:
break;
case SeriesChartType.Pyramid:
break;
case SeriesChartType.Radar://雷达图
break;
case SeriesChartType.Range:
break;
case SeriesChartType.RangeColumn:
break;
case SeriesChartType.Renko:
break;
case SeriesChartType.Rose:
break;
case SeriesChartType.Spline://曲线图
break;
case SeriesChartType.SplineArea:
break;
case SeriesChartType.SplineRange:
break;
case SeriesChartType.StackedArea:
break;
case SeriesChartType.StackedArea100:
break;
case SeriesChartType.StackedBar:
break;
case SeriesChartType.StackedBar100:
break;
case SeriesChartType.StackedColumn:
break;
case SeriesChartType.StackedColumn100:
break;
case SeriesChartType.StackedRose:
break;
case SeriesChartType.StepLine:
break;
case SeriesChartType.Stock:
break;
case SeriesChartType.ThreeLineBreak:
break;
default:
break;
}
series.Points.DataBind(dtSeries.DefaultView, "XValue", "YValue", string.Empty);
series.XValueType = xValueType;
series.YValueType = yValueType;
chart.ChartAreas["Default"].AxisX.LabelStyle.Format = xFormat;
chart.ChartAreas["Default"].AxisX.LabelStyle.Format = yFormat;
if (yValueType == ChartValueTypes.Double || yValueType == ChartValueTypes.Single)
{
if (yFormat.Length >= 2)
{
series.LabelFormat = "{0:F" + yFormat.Substring(1, yFormat.Length - 1) + "}";
}
}
// 设置三维风格
if (bShow3D)
{
series.BorderWidth = 1;
chart.ChartAreas["Default"].Area3DStyle.RightAngleAxes = true;
chart.ChartAreas["Default"].Area3DStyle.Clustered = true;
}
else
{
series.BorderWidth = 3;
}
// 添加序列
chart.Series.Add(series);
}
}
/// <summary>
/// 另存为 Excel
/// </summary>
/// <param name="title">标题</param>
/// <param name="dt">数据表</param>
/// <param name="imageDir">图片绝对路径</param>
/// <param name="imageWidth">图片宽度</param>
/// <param name="imageHeight">图片高度</param>
public static void SaveAsExcel(string title, DataTable dt, string imageDir, int imageWidth, int imageHeight)
{
string down = Path.GetDirectoryName(imageDir) + @"\" + title + ".xls";
string dir = Path.GetDirectoryName(down);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
ExportToExcel dte = new ExportToExcel(down);
dte.DataTableToExcelSheet(dt, "A1");
int count = dt.Rows.Count + 2;
dte.InsertPicture("A" + count, imageDir, imageWidth, imageHeight);
dte.SaveFile();
dte.DownloadFile();
}
/// <summary>
/// 获取导出路径
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static string AppSettingItemValue(string name)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
KeyValueConfigurationElement item = config.AppSettings.Settings[name];
return item.Value;
}