目前我已经实现了多条折线绑定数据并显示,但不是动态绑定,而是每一条数据都要进行一次绑定,个人觉得在解决实际问题时,这样的解决方法过于笨重且缺乏扩展性,我希望能够对代码进行优化,实现写一遍代码,无论数据库里增加多少条数据都能自动进行绑定,但是本人是萌新程序猿,水平不够,我把我写的代码贴上来,希望大家能够集思广益,帮忙答疑解惑,LZ将不胜感激!!!
protected void Chart1_Load1(object sender, EventArgs e)
{
Chart1.Titles.Add("折线图报表");
Chart1.ChartAreas[0].AxisX.Title = "日期时间";//设置X轴的标题
Chart1.ChartAreas[0].AxisY.Title = "产品水平";//设置Y轴的标题
Chart1.ChartAreas[0].AxisY.Interval = 10;//设置X轴间距,这样的话,就间距固定为10
string constr = "Data Source=;Initial Catalog=;Persist Security Info=True;User ID=;Password=";
SqlConnection con = new SqlConnection(constr);
//打开连接;
con.Open();
//第一条数据
string sql = "SELECT * FROM APiece WHERE APiece.Piece_PId=1";
DataTable ds = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(sql, con);
sda.Fill(ds);
Series ss = new Series("产品1"); //这里 dt1 ,dt2 任意取名称,但要唯一
ss.Points.DataBind(ds.AsEnumerable(), "Piece_Time", "Piece_Level", "");
ss.XValueType = ChartValueType.DateTime; //设置X轴
ss.ChartType = SeriesChartType.Spline; //设置Y轴为折线
Chart1.Series.Add(ss);
//第二条数据
string sql1 = "SELECT * FROM APiece WHERE APiece.Piece_PId=2";
DataTable ds1 = new DataTable();
SqlDataAdapter sda1 = new SqlDataAdapter(sql1, con);
sda1.Fill(ds1);
Chart1.DataSource = ds1;
Series ss2 = new Series("产品2");
ss2.Points.DataBind(ds1.AsEnumerable(), "Piece_Time", "Piece_Level", "");
ss2.XValueType = ChartValueType.DateTime; //设置X轴
ss2.ChartType = SeriesChartType.Spline; //设置Y轴为折线
Chart1.Series.Add(ss2);
//第三条数据
string sql2 = "SELECT * FROM APiece WHERE APiece.Piece_PId=3";
DataTable ds2 = new DataTable();
SqlDataAdapter sda2 = new SqlDataAdapter(sql2, con);
sda2.Fill(ds2);
Chart1.DataSource = ds2;
Series ss3 = new Series("产品3");
ss3.Points.DataBind(ds2.AsEnumerable(), "Piece_Time", "Piece_Level", "");
ss3.XValueType = ChartValueType.DateTime; //设置X轴
ss3.ChartType = SeriesChartType.Spline; //设置Y轴为折线
Chart1.Series.Add(ss3);
用循环:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web.UI.DataVisualization.Charting;
public partial class Default3 : System.Web.UI.Page
{
SqlConnection con;
protected void Page_Load(object sender, EventArgs e)
{
string constr = "Data Source=.;Initial Catalog=;Persist Security Info=True;User ID=;Password=";
con = new SqlConnection(constr);
//打开连接
con.Open();
}
protected void Chart1_Load1(object sender, EventArgs e)
{
BindChart();
}
private void BindChart()
{
Chart1.Titles.Add("折线图报表");
Chart1.ChartAreas[0].AxisX.Title = "日期时间";//设置X轴的标题
Chart1.ChartAreas[0].AxisY.Title = "产品水平";//设置Y轴的标题
Chart1.ChartAreas[0].AxisY.Interval = 10;//设置X轴间距,这样的话,就间距固定为10
DataTable dt = GetData();
Series ss;
for (int i = 0; i < dt.Rows.Count; i++)
{
ss = new Series("产品" + i);
ss.Points.DataBind(dt.AsEnumerable(), "Piece_Time", "Piece_Level", "");
ss.XValueType = ChartValueType.DateTime; //设置X轴
ss.ChartType = SeriesChartType.Spline; //设置Y轴为折线
Chart1.Series.Add(ss);
}
}
private DataTable GetData()
{
DataTable dt = new DataTable();
//获取所有数据
string sql = "SELECT Piece_Name,Piece_Time,Piece_Level FROM APiece WHERE WITH(NOLOCK)";
SqlDataAdapter sda = new SqlDataAdapter(sql, con);
sda.Fill(dt);
return dt;
}
}
兄弟,我用的是WebForm页面,不是winfrom窗体,我根据你的代码修改了一下,但是效果没有出来,我把代码和效果图贴出来你看一下:
protected void Page_Load(object sender, EventArgs e)
{
BindChart();
string constr = "Data Source=;Initial Catalog=;Persist Security Info=True;User ID=;Password=";
SqlConnection con = new SqlConnection(constr);
//打开连接;
con.Open();
}
private void BindChart()
{
Chart1.Titles.Add("折线图报表");
Chart1.ChartAreas[0].AxisX.Title = "日期时间";//设置X轴的标题
Chart1.ChartAreas[0].AxisY.Title = "产品水平";//设置Y轴的标题
Chart1.ChartAreas[0].AxisY.Interval = 10;//设置X轴间距,这样的话,就间距固定为10
DataTable dt = GetData();
Series ss;
for (int i = 0; i < dt.Rows.Count; i++)
{
ss = new Series(dt.Rows[i]["Piece_Name"].ToString());
ss.Points.DataBind(dt.AsEnumerable(), "Piece_Time", "Piece_Level", "");
ss.XValueType = ChartValueType.DateTime; //设置X轴
ss.ChartType = SeriesChartType.Spline; //设置Y轴为折线
Chart1.Series.Add(ss);
}
}
private DataTable GetData()
{
DataTable dt = new DataTable();
//获取所有数据
//string sql = "SELECT Piece_Name,Piece_Time,Piece_Level FROM APiece WHERE WITH(NOLOCK)";
//SqlDataAdapter sda = new SqlDataAdapter(sql, con);
//sda.Fill(dt);
//新增测试数据
DataRow row;
//建立字段
dt.Columns.Add("Piece_Name", typeof(string));
dt.Columns.Add("Piece_Time", typeof(string));
dt.Columns.Add("Piece_Level", typeof(int));
//新增資料到DataTable
for (int i = 1; i < 10; i++)
{
row = dt.NewRow();
row["Piece_Name"] = "产品" + i.ToString();
row["Piece_Time"] = DateTime.Now.ToString("D");
row["Piece_Level"] = i;
dt.Rows.Add(row);
}
return dt;
}
修改之后的效果图:
原效果图:
@起风了123: 我重新修改了答案,你直接拿去用
@三人乐乐: 問題已經解決了,謝謝了
第一,用循环
第二,把添加曲线的方法和数据库操作分离开吧,看着难受
额,不好意思哈,我是刚入门的菜鸟,如果用循环的话,具体该怎么做呢?