您的位置:首页 > 编程语言 > ASP

asp.net 开源制图组件(1) ZedGraph

2011-07-11 11:22 411 查看
asp.net 开源制图组件(1) ZedGraph

asp.net 开源制图组件(1) ZedGraph.resource

View Code

using System;
using System.Data;
using System.Drawing;
using System.Text;
using ZedGraph;
using Zhx.Co2.DAL.Utils;
using ZhxC02Sys.Model;

namespace ZhxC02Sys.Utils
{
public enum EnumDateType
{
年, 月
}

public enum AnalyticsType
{
Line, //折线图
Line2,//带阴影区域的折线图
Curve,//带星的折线图
Curve2,//带阴影区域的星行折线图
Bar, //柱状图
Graph,
Pie //饼图
};

public class ZhxUtilChart
{
const double offset = 1.0;

#region OWC10

/*/// <summary>
/// 线状
/// </summary>
public static Image ShowChar(EnumDateType enumDT, string topTitle, string leftTitle,
string bottomTitle, ChartChartTypeEnum charTypeEnum, DataTable dataSource)
{
#region 设置画布

//创建ChartSpace对象来放置图表
ChartSpace mySpace = new ChartSpaceClass();
//在ChartSpace对象中添加图表,Add方法返回chart对象
ChChart myChart = mySpace.Charts.Add(0);
//设置边框
mySpace.Border.Color = "white";
//图例
myChart.HasLegend = true;
//给定标题
myChart.HasTitle = true;
myChart.Title.Font.Name = "宋体";
myChart.Title.Font.Size = 9;
myChart.Title.Position = ChartTitlePositionEnum.chTitlePositionTop;
myChart.Title.Caption = topTitle;

//设定图例文字
myChart.Legend.Position = ChartLegendPositionEnum.chLegendPositionBottom;
myChart.Legend.Font.Name = "宋体";
myChart.Legend.Font.Size = 9;

//设置背景色
myChart.PlotArea.Interior.Color = "white";
myChart.PlotArea.Border.Color = "#b5cae1";

//设置图表的X坐标
myChart.Axes[0].Font.Name = "宋体";
myChart.Axes[0].Font.Size = 8;
myChart.Axes[0].HasTitle = true;
myChart.Axes[0].Title.Font.Name = "宋体";
myChart.Axes[0].Title.Font.Size = 9;
myChart.Axes[0].Title.Caption = bottomTitle;
myChart.Axes[0].Font.Color = "#282727";     //lxj X轴字体颜色
myChart.Axes[0].Line.Color = "#b6cbe8";     //lxj X轴颜色
myChart.Axes[0].HasMajorGridlines = true;   //lxj 显示X轴网格线
myChart.Axes[0].MajorGridlines.Line.Color = "#b5cae1";//lxj X轴网格线颜色

//设置图表的Y坐标
myChart.Axes[1].Font.Name = "Arial";
myChart.Axes[1].Font.Size = 7;
myChart.Axes[1].HasTitle = true;
myChart.Axes[1].Title.Font.Name = "宋体";
myChart.Axes[1].Title.Font.Size = 9;
myChart.Axes[1].Title.Caption = leftTitle;
myChart.Axes[1].Font.Color = "#282727";     //lxj Y轴字体颜色
myChart.Axes[1].Line.Color = "#b6cbe8";     //lxj Y轴颜色
myChart.Axes[1].MajorGridlines.Line.Color = "#b5cae1";//Y轴网格线颜色

#endregion 设置画布

myChart.Type = charTypeEnum;//ChartChartTypeEnum.chChartTypeLineMarkers;
int itmp = 0;
string strValueX = ValueStrX(dataSource, enumDT);
//给系列命名
string Temp = "";

#region 跟据产品的价格生成相应的线

for (int i = 0; i < itmp; i++)
{
string strValueY = ValueStrY(dataSource);
Temp = "co2";
//添加图表系列
myChart.SeriesCollection.Add(i);
myChart.SeriesCollection[i].SetData(ChartDimensionsEnum.chDimSeriesNames, (int)(ChartSpecialDataSourcesEnum.chDataLiteral), Temp);
//给系列分组
myChart.SeriesCollection[i].SetData(ChartDimensionsEnum.chDimCategories, (int)(ChartSpecialDataSourcesEnum.chDataLiteral), strValueX);
//给系列各分组赋值
myChart.SeriesCollection[i].SetData(ChartDimensionsEnum.chDimValues, (int)(ChartSpecialDataSourcesEnum.chDataLiteral), strValueY);
//指定各系列的属性
string colortmp = "#000066";
myChart.SeriesCollection[i].Line.Color = colortmp;
myChart.SeriesCollection[i].Interior.Color = colortmp;
myChart.SeriesCollection[i].Marker.Size = 5;
myChart.SeriesCollection[i].Marker.Style = ChartMarkerStyleEnum.chMarkerStyleCircle;
}

#endregion 跟据产品的价格生成相应的线

int width = 550, height = 450;
//byte[] pic = (byte[])mySpace.GetPicture("GIF", width, height);
Image pic = (Image)mySpace.GetPicture("GIF", width, height);
//mySpace.ExportPicture(
mySpace.Clear();
mySpace.ClearUndo();
mySpace = null;
return pic;
}
/// <summary>
/// ex:
/// select * from Zhx_Global_Times_N2O;
///
/// </summary>
/// <param name="dttmp"></param>
/// <returns></returns>
protected static string ValueStrY(DataTable dt)
{
StringBuilder sb = new StringBuilder();
try
{
foreach (DataRow dr in dt.Rows)
{
string sv = dr["sumvalue"].ToString();
if (string.IsNullOrEmpty(sv) || sv == "0")
{
sb.Append(",0");
}
else
{
sb.AppendFormat(",{0}", sv);
}
}
}
catch (Exception ex)
{
throw ex;
}
return sb.ToString().TrimStart(new char[] { ',' });
}
/// <summary>
/// 生成以逗号分隔的日期字符串
/// </summary>
/// <returns>逗号分隔的字符串</returns>
protected static string ValueStrX(DataTable dt, EnumDateType edt)
{
StringBuilder sb = new StringBuilder();
try
{
foreach (DataRow dr in dt.Rows)
{
string writeTime = dr["writeTime"].ToString();
switch (edt)
{
case EnumDateType.月:
string yy = DateTime.Parse(writeTime).ToString("YY");
string mm = DateTime.Parse(writeTime).ToString("MM");
sb.AppendFormat("{0}.{1}", yy, mm);
break;
case EnumDateType.年:
yy = DateTime.Parse(writeTime).ToString("YYYY");
sb.AppendFormat("{0},", yy);
break;
}
}
}
catch (Exception ex)
{
throw ex;
}
return sb.ToString().TrimEnd(new char[] { ',' });
}*/

#endregion OWC10

#region ZedGraph

/// <summary>
/// ex:
/// select * from Zhx_Global_Times_N2O;
///
/// </summary>
/// <param name="dttmp"></param>
/// <returns></returns>
protected static string ValueStrY(DataTable dt)
{
StringBuilder sb = new StringBuilder();
try
{
foreach (DataRow dr in dt.Rows)
{
string sv = dr["sumvalue"].ToString();
if (string.IsNullOrEmpty(sv) || sv == "0")
{
sb.Append(",0");
}
else
{
sb.AppendFormat(",{0}", sv);
}
}
}
catch (Exception ex)
{
ZhxLog.writeLog(ex.ToString());
}
return sb.ToString().TrimStart(new char[] { ',' });
}

/// <summary>
/// 生成以逗号分隔的日期字符串
/// </summary>
/// <returns>逗号分隔的字符串</returns>
protected static string ValueStrX(DataTable dt, EnumDateType edt)
{
StringBuilder sb = new StringBuilder();
try
{
foreach (DataRow dr in dt.Rows)
{
string writeTime = string.Empty;
switch (edt)
{
case EnumDateType.月:
writeTime = dr["writeTime"].ToString();
string yy = DateTime.Parse(writeTime).ToString("yyyy");
string mm = DateTime.Parse(writeTime).ToString("MM");
sb.AppendFormat("{0}.{1},", yy, mm);
break;
case EnumDateType.年:
writeTime = dr["wt"].ToString();
yy = writeTime;
sb.AppendFormat("{0},", yy);
break;
}
}
}
catch (Exception ex)
{
ZhxLog.writeLog(ex.ToString());
}
return sb.ToString().TrimEnd(new char[] { ',' });
}

// Call this method from the Form_Load method, passing your ZedGraphControl
public void CreateChart(ZedGraphControl zgc)
{
GraphPane myPane = zgc.GraphPane;
// Set the title and axis labels
myPane.Title.Text = "Horizontal Bars with Value Labels Above Each Bar";
myPane.XAxis.Title.Text = "Some Random Thing";
myPane.YAxis.Title.Text = "Position Number";

// Create data points for three BarItems using Random data
PointPairList list = new PointPairList();
PointPairList list2 = new PointPairList();
PointPairList list3 = new PointPairList();
Random rand = new Random();
for (int i = 0; i < 4; i++)
{
double y = (double)i + 5;
double x = rand.NextDouble() * 1000;
double x2 = rand.NextDouble() * 1000;
double x3 = rand.NextDouble() * 1000;
list.Add(x, y);
list2.Add(x2, y);
list3.Add(x3, y);
}
// Create the three BarItems, change the fill properties so the angle is at 90
// degrees for horizontal bars
BarItem myCurve = myPane.AddBar("curve 1", list, Color.Blue);
myCurve.Bar.Fill = new Fill(Color.Blue, Color.White, Color.Blue, 90);
BarItem myCurve2 = myPane.AddBar("curve 2", list2, Color.Red);
myCurve2.Bar.Fill = new Fill(Color.Red, Color.White, Color.Red, 90);
BarItem myCurve3 = myPane.AddBar("curve 3", list3, Color.Green);
myCurve3.Bar.Fill = new Fill(Color.Green, Color.White, Color.Green, 90);
// Set BarBase to the YAxis for horizontal bars
myPane.BarSettings.Base = BarBase.Y;
// Fill the chart background with a color gradient
myPane.Chart.Fill = new Fill(Color.White,
Color.FromArgb(255, 255, 166), 45.0F);
zgc.AxisChange();
// Create TextObj's to provide labels for each bar
BarItem.CreateBarLabels(myPane, false, "f0");
}

public static void ShowCharNO2(User user, EnumDateType enumDateType,
string topTitle, string xTitle, string yTitle, DataTable dt_Zhx_N20_Sum, ZedGraph.ZedGraphControl zg1
, string charType)
{
zg1.GraphPane.CurveList.Clear();
zg1.GraphPane.GraphObjList.Clear();

GraphPane myPane = zg1.GraphPane;
// Set the titles and axis labels
myPane.Title.Text = topTitle;
myPane.XAxis.Title.Text = xTitle;
myPane.YAxis.Title.Text = yTitle;
// Make up some data points from the Sine function
PointPairList list = new PointPairList();
string[] strXs = ValueStrX(dt_Zhx_N20_Sum, enumDateType).Split(',');
for (int i = 0; i < dt_Zhx_N20_Sum.Rows.Count; i++)
{
string y = string.Empty;
if (enumDateType == EnumDateType.年)
{
y = dt_Zhx_N20_Sum.Rows[i]["sv"].ToString();
}
else
{
y = dt_Zhx_N20_Sum.Rows[i]["sumvalue"].ToString();
}
if (string.IsNullOrEmpty(y))
{
y = "0";
}
list.Add(i, double.Parse(y));
}
myPane.XAxis.Scale.TextLabels = strXs;
myPane.XAxis.Type = AxisType.Text;
switch (charType)
{
case "曲线图":
// Generate a blue curve with circle symbols, and "My Curve 2" in the legend
LineItem myCurve = myPane.AddCurve("值", list, Color.Blue, SymbolType.Default);
// Fill the area under the curve with a white-red gradient at 45 degrees
myCurve.Line.Fill = new Fill(Color.White, Color.White, 1F);
// Make the symbols opaque by filling them with white
myCurve.Symbol.Fill = new Fill(Color.White);
// Fill the axis background with a color gradient
//myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45F);
// Fill the pane background with a color gradient
myPane.Fill = new Fill(Color.White, Color.FromArgb(220, 220, 255), 45F);
// Calculate the Axis Scale Ranges
for (int i = 0; i < list.Count; i++)
{
// Get the pointpair
PointPair pt = myCurve.Points[i];
// Create a text label from the Y data value
TextObj text = new TextObj(pt.Y.ToString("0.000"), pt.X + offset, pt.Y, CoordType.AxisXYScale, AlignH.Right, AlignV.Top);
text.ZOrder = ZOrder.A_InFront;
// Hide the border and the fill
text.FontSpec.FontColor = Color.Red;
text.FontSpec.Border.IsVisible = false;
text.FontSpec.Fill.IsVisible = false;
//text.FontSpec.Fill = new Fill( Color.FromArgb( 100, Color.White ) );
// Rotate the text to 90 degrees
text.FontSpec.Angle = 0;
myPane.GraphObjList.Add(text);
}
zg1.AxisChange();
zg1.Refresh();
break;
case "柱状图":
// Generate a blue curve with circle symbols, and "My Curve 2" in the legend
BarItem bItem = myPane.AddBar("值", list, Color.Blue);
// Fill the area under the curve with a white-red gradient at 45 degrees
bItem.Bar.Fill = new Fill(Color.White, Color.Red, 90F);
// Make the symbols opaque by filling them with white
// Fill the axis background with a color gradient
//myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45F);
// Fill the pane background with a color gradient
// Calculate the Axis Scale Ranges
zg1.AxisChange();
BarItem.CreateBarLabels(myPane, false, "0.000", "", 10, Color.Black, false, false, false);
zg1.Refresh();
break;
}
}

#endregion ZedGraph

public static void ShowCharCO2(ZhxC02Sys.Model.User user, EnumDateType ed, string topTitle,
string xValue, string yValue, DataTable dt_Zhx_N20_Sum,
ZedGraphControl zg1, string charType)
{
zg1.GraphPane.CurveList.Clear();
zg1.GraphPane.GraphObjList.Clear();
GraphPane myPane = zg1.GraphPane;
// Set the titles and axis labels
myPane.Title.Text = topTitle;
myPane.XAxis.Title.Text = xValue;
myPane.YAxis.Title.Text = yValue;
// Make up some data points from the Sine function
PointPairList list = new PointPairList();
string[] strXs = ValueStrX(dt_Zhx_N20_Sum, ed).Split(',');
for (int i = 0; i < dt_Zhx_N20_Sum.Rows.Count; i++)
{
string y = string.Empty;
if (user.rllx == "1")
{
if (ed == EnumDateType.年)
{
y = dt_Zhx_N20_Sum.Rows[i]["ms"].ToString();
}
else
{
y = dt_Zhx_N20_Sum.Rows[i]["m_S总"].ToString();
}
}
else if (user.rllx == "2")
{
if (ed == EnumDateType.年)
{
y = dt_Zhx_N20_Sum.Rows[i]["ts"].ToString();
}
else
{
y = dt_Zhx_N20_Sum.Rows[i]["t_S总"].ToString();
}
}
if (string.IsNullOrEmpty(y))
{
y = "0";
}
list.Add(i, double.Parse(y));
}
myPane.XAxis.Scale.TextLabels = strXs;
myPane.XAxis.Type = AxisType.Text;
//BarItem.CreateBarLabels(myPane, true, "0.00");//保留2位小数
switch (charType)
{
case "曲线图":
// Generate a blue curve with circle symbols, and "My Curve 2" in the legend
LineItem myCurve = myPane.AddCurve("值", list, Color.Blue, SymbolType.Default);
// Fill the area under the curve with a white-red gradient at 45 degrees
myCurve.Line.Fill = new Fill(Color.White, Color.White, 1F);
// Make the symbols opaque by filling them with white
myCurve.Symbol.Fill = new Fill(Color.White);
// Fill the axis background with a color gradient
//myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45F);
// Fill the pane background with a color gradient
//myPane.Fill = new Fill(Color.White, Color.FromArgb(220, 220, 255), 45F);
// Loop to add text labels to the points
for (int i = 0; i < list.Count; i++)
{
// Get the pointpair
PointPair pt = myCurve.Points[i];
// Create a text label from the Y data value
TextObj text = new TextObj(pt.Y.ToString("0.000"), pt.X + offset, pt.Y, CoordType.AxisXYScale, AlignH.Right, AlignV.Top);
text.ZOrder = ZOrder.A_InFront;
// Hide the border and the fill
text.FontSpec.FontColor = Color.Red;
text.FontSpec.Border.IsVisible = false;
text.FontSpec.Fill.IsVisible = false;
//text.FontSpec.Fill = new Fill( Color.FromArgb( 100, Color.White ) );
// Rotate the text to 90 degrees
text.FontSpec.Angle = 0;
myPane.GraphObjList.Add(text);
}
// Leave some extra space on top for the labels to fit within the chart rect
//myPane.YAxis.Scale.MaxGrace = 0.2;
// Calculate the Axis Scale Ranges
zg1.AxisChange();
zg1.Refresh();
break;
case "柱状图":
// Generate a blue curve with circle symbols, and "My Curve 2" in the legend
BarItem bItem = myPane.AddBar("值", list, Color.Blue);
// Fill the area under the curve with a white-red gradient at 45 degrees
bItem.Bar.Fill = new Fill(Color.White, Color.Red, 0F);
//BarItem.CreateBarLabels(myPane, true, "0.00");//保留2位小数
// Make the symbols opaque by filling them with white
// Fill the axis background with a color gradient
//myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45F);
// Fill the pane background with a color gradient
// Calculate the Axis Scale Ranges
//myPane.Chart.Fill = new Fill(Color.White, Color.Black, 45.0F);
//myPane.BarSettings.Base = BarBase.Y;
zg1.AxisChange();
BarItem.CreateBarLabels(myPane, false, "0.000", "", 10, Color.Black, false, false, false);
zg1.Refresh();
break;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: