为了项目的需要,下面是本人在这段时间学习使用TeeChart控件web曲线绘制
http://php.tech.sina.com.cn/download/d_list.php?f_categoryid=187 下载TeeChart for .NET 3.0控件
或者http://www.steema.com/downloads/download.html 现在TeeChart 控件
如果大家还有其它绘制统计图比较好的控件,可以分享一下
Default.aspx
/// <summary>
/// 获得数据集
/// </summary>
/// <param name="sqlStr">传递查询语句</param>
/// <returns>返回数据集</returns>
public static DataSet GetDataSet(string sqlStr)
{
string connstring = System.Configuration .ConfigurationSettings.AppSettings["conStr"];
//string sqlStr = "select * from chart";
SqlDataAdapter da = new SqlDataAdapter(sqlStr, connstring);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
/// <summary>
/// 绘制统计图
/// </summary>
/// <param name="SelectStr">Sql查询语句</param>
private void DrawBar(string SelectStr)
{
Chart chart = WebChart1.Chart;
DataSet DS = new DataSet();
//显示图片的格式,默认PNG
WebChart1.PictureFormat = Steema.TeeChart.Export.PictureFormats.Bitmap;
//设置保存数据形式,默认File,一般选session。Chart.Aspect(外观属性)
WebChart1.TempChart = Steema.TeeChart.Web.TempChartStyle.Session;
//获取显示图片文件
WebChart1.GetChartFile = "GetChart.aspx";
//整个图表的高度
WebChart1.Height = 300;
//整个图表的宽度
WebChart1.Width = 600;
//显示图片的格式,默认PNG
WebChart1.PictureFormat = Steema.TeeChart.Export.PictureFormats.Bitmap;
//设置保存数据形式,默认File,一般选session。Chart.Aspect(外观属性)
WebChart1.TempChart = Steema.TeeChart.Web.TempChartStyle.Session;
//图表底色开始颜色
//WebChart1.Chart.Panel.Gradient.StartColor = Color.Blue;//图表底色开始颜色
//设置图形标题
WebChart1.Chart.Header.Text = "个人每月开销数目柱形图";
//设置X轴标题
WebChart1.Chart.Axes.Bottom.Title.Text = "月份(月)";
//设置Y1轴标题
WebChart1.Chart.Axes.Left.Title.Text = "金额(元)";
WebChart1.Chart.Axes.Left.Labels.ValueFormat = "#M/N";
//参纵坐标轴是否显示
WebChart1.Chart.Axes.Bottom.Grid.Visible = false ;
//横坐标字体倾斜角度0~360
WebChart1.Chart.Axes.Bottom.Labels.Angle= 30;
//图表左边距
WebChart1.Chart.Panel.MarginLeft = 15;
//图例间是否有间隔线
WebChart1.Chart.Legend.DividingLines.Visible = true;
//图形显示比例(好像无反应)
//WebChart1.Chart .Aspect .Zoom = 10;
WebChart1.Dispose();
//是否立体显示
WebChart1.Chart.Aspect.View3D = false;
foreach (Steema.TeeChart.Styles.Series s in chart.Series)
if (styles == 1)
{
//画柱状图
Steema.TeeChart.Styles.Bar sty1 = new Steema.TeeChart.Styles.Bar(WebChart1.Chart);
Steema.TeeChart.Styles.Bar sty2 = new Steema.TeeChart.Styles.Bar(WebChart1.Chart);
chart.Series.Add(sty1);
chart.Series.Add(sty2);
}
if (styles == 2)
{
//画折线图
Steema.TeeChart.Styles.Line sty1 = new Steema.TeeChart.Styles.Line(WebChart1.Chart);
Steema.TeeChart.Styles.Line sty2 = new Steema.TeeChart.Styles.Line(WebChart1.Chart);
chart.Series.Add(sty1);
chart.Series.Add(sty2);
}
if (styles == 3)
{
Steema.TeeChart.Styles.Kagi sty1 = new Steema.TeeChart.Styles.Kagi(WebChart1.Chart);
//Steema.TeeChart.Styles.Kagi sty2 = new Steema.TeeChart.Styles.Kagi(WebChart1.Chart);
chart.Series.Add(sty1);
//chart.Series.Add(sty2);
}
//添加第二个纵坐标
if (YaxixCount == 2)
{
Axis axisY2 = new Axis(false, false, WebChart1.Chart);
chart.Series[1].CustomVertAxis = axisY2;
//sty2.CustomVertAxis = axisY2;
//bar2.CustomVertAxis = axisY2;
WebChart1.Chart.Axes.Custom.Add(axisY2);
axisY2.AxisPen.Color = Color.DarkGreen;//坐标轴颜色
axisY2.Title.Text = "数量";
axisY2.Title.Font.Color = Color.Blue; //坐标轴标题颜色
axisY2.Title.Font.Bold = false;
axisY2.Title.Angle = 90;//旋转90°
axisY2.RelativePosition = -20;
}
//图形颜色
//sty1.ColorEach = false;
//sty2.ColorEach = false;
//查询语句
//string SelectStr = "select month,Allcount,fee from chart";
DS.Clear();
try
{
DS = GetDataSet(SelectStr);
}
catch (Exception e)
{
Response.Write(e);
}
if (SeriesCount == 1)
{
chart.Series[0].DataSource = DS.Tables[0];
chart.Series[0].LabelMember = DS.Tables [0].Columns[0].ToString();
chart.Series[0].YValues.DataMember = "Allcount";
}
if (SeriesCount == 2)
{
//第一种方法:(画曲线)
//添加一个series系列
//chart.Series.Add(sty1);
chart.Series[0].DataSource = DS.Tables[0];
chart.Series[0].LabelMember = "month";
chart.Series[0].YValues.DataMember = "Allcount";
//chart.Series.Add(sty2);
chart.Series[1].DataSource = DS.Tables[0];
chart.Series[1].LabelMember = "month";
chart.Series[1].YValues.DataMember = "fee";
}
}
protected void Button3_Click(object sender, EventArgs e) //查询
{
styles = 2;
YaxixCount = 2;
SeriesCount = 2;
string sqlStr = "select month,Allcount,fee from chart where month between '" + DropDownList1.SelectedItem.Text.ToString() + "' and '" + DropDownList2.SelectedItem.Text.ToString() + "'";
DrawBar(sqlStr);
}
创建 GetChart.aspx
protected void Page_Load(object sender, EventArgs e)
{
string chartName=Request.QueryString["Chart"];
if (Session[chartName]!=null)
{
System.IO.MemoryStream chartStream = new System.IO.MemoryStream();
chartStream=((System.IO.MemoryStream)Session[chartName]);
Response.OutputStream.Write(chartStream.ToArray(),0,(int)chartStream.Length);
chartStream.Close();
Session.Remove(chartName);
}
}
如果有朋友使用此控件开发统计图的,希望大家能共同交流,探讨问题~