C# chart变量曲线问题 期待高手正解 在线等待中······ 本帖最后由 newboyjia 于 2010-07-22 14:41:59 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 要看你用的是什么Chart控件了,一般的Chart控件都可绑定DataTable把你的数据写到DataTable中,绑定下 /// <summary> /// Listbox中的信息向左或者右移动 /// </summary> /// <param name="listSource"></param> /// <param name="listTarget"></param> private void moveTo(System.Windows.Forms.ListBox listSource, System.Windows.Forms.ListBox listTarget) { int temp = listSource.SelectedIndex; ArrayList listTemp = new ArrayList(); for (int i = 0; i < listSource.SelectedItems.Count; i++) { listTemp.Add(listSource.SelectedItems[i].ToString()); } for (int i = 0; i < listTemp.Count; i++) { listSource.Items.Remove(listTemp[i].ToString()); listTarget.Items.Add(listTemp[i].ToString()); } if (listTarget.Items.Count >= 1) listTarget.SetSelected(listTarget.Items.Count - 1, true); listSource.Refresh(); listTarget.Refresh(); listTarget.ClearSelected(); for (int i = 0; i < listTarget.Items.Count; i++) { if (listTemp.Contains(listTarget.Items[i].ToString())) { listTarget.SetSelected(i, true); } } try { listSource.SelectedIndex = temp;//选择转移过后自动选择到下一项 } catch (ArgumentOutOfRangeException)//如果选择到了最后一项则返回到第一项 { try { listSource.SelectedIndex = 0; } catch (ArgumentOutOfRangeException)//如果没有任何可以选择的项则不选择 { } } }以前写的 就是这个功能 至于你说的线图用WellChart吧 很好用 listbox 中左右移动的功能早就实现了 主要就是chart 的变量曲线问题··········· protected void Button3_Click(object sender, EventArgs e) { DataTable dtTemp = new DataTable(); for (int j = 0; j < lstWcq.Items.Count; j++) { SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["table_1"]); SqlCommand myCommand = new SqlCommand("dian", myConnection); myCommand.CommandType = CommandType.StoredProcedure; myConnection.Open(); myCommand.Parameters.Add(new SqlParameter("@sj1", SqlDbType.DateTime)); myCommand.Parameters["@sj1"].Value = TextBox1.Text.Trim(); myCommand.Parameters.Add(new SqlParameter("@sj2", SqlDbType.DateTime)); myCommand.Parameters["@sj2"].Value = TextBox2.Text.Trim(); myCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.NChar)); myCommand.Parameters["@id"].Value = lstWcq.Items[j].ToString(); SqlDataAdapter da = new SqlDataAdapter(myCommand); DataSet ds = new DataSet(); da.Fill(ds); dtTemp.Merge(ds.Tables[0]); // GridView1.DataSource = ds; // GridView1.DataBind(); myConnection.Close(); } GridView1.DataSource = dtTemp; GridView1.DataBind(); //图表 Chart1.Series.Clear(); Series series1 = Chart1.Series.Add("电量值"); string[] l = new string[2000]; series1.ChartType = "line"; series1.BorderWidth = 2; //坐标值 Chart1.ChartAreas["default"].AxisX.Title = "日期/时间"; Chart1.ChartAreas["default"].AxisY.Title = "电量"; Chart1.ChartAreas["Default"].AxisX.LabelStyle.Interval = 1; //赋值给线 for (int i = 0; i <= dtTemp.Rows.Count - 1; i++) { Chart1.Series["电量值"].Points.AddXY( dtTemp.Rows[i][2],dtTemp.Rows[i][1]); } }这就是我的 代码 现在只能出来一条线 如果出来两条线就是连在一起的 不知道怎么做循环了··· private void Bind_InitIndexTrend(object args, string IndexNameDisplay) { DataTable tmpDt = args as DataTable; if (tmpDt != null) { List<string> theIndexList = new List<string>(); string sIndex = IndexNameDisplay; theIndexList.Add(sIndex);//所有指标都取别名为:IndexName了,原来是取sIndex JUDE 2010-04-09"indexName" IndexDataChart.ClearChart(); IndexDataChart.ShowChart(tmpDt, " 小区 " + ObjectMsgDataGrid["小区", ObjectMsgDataGrid.SelectedCells[0].RowIndex].Value.ToString() + IndexNameDisplay + "走势", "时间", IndexNameDisplay); Series A = (IndexDataChart.TChart.Series[0]); Series.ChangeType(ref A, typeof(Line)); IndexDataChart.TChart.Axes.Left.Title.Text = sIndex; this.Text = IndexNameDisplay + "走势"; } else { ShowTishiMsg("处理出错"); } }这个是TreeChart的 就是WellChart 我非大侠啊。WellChart是一个绘图画表的很好的工具我只会画线而已 protected void Button3_Click(object sender, EventArgs e) { DataTable dtTemp = new DataTable(); for (int j = 0; j < lstWcq.Items.Count; j++) { SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["table_1"]); SqlCommand myCommand = new SqlCommand("dian", myConnection); myCommand.CommandType = CommandType.StoredProcedure; myConnection.Open(); myCommand.Parameters.Add(new SqlParameter("@sj1", SqlDbType.DateTime)); myCommand.Parameters["@sj1"].Value = TextBox1.Text.Trim(); myCommand.Parameters.Add(new SqlParameter("@sj2", SqlDbType.DateTime)); myCommand.Parameters["@sj2"].Value = TextBox2.Text.Trim(); myCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.NChar)); myCommand.Parameters["@id"].Value = lstWcq.Items[j].ToString(); SqlDataAdapter da = new SqlDataAdapter(myCommand); DataSet ds = new DataSet(); da.Fill(ds); dtTemp.Merge(ds.Tables[0]); // GridView1.DataSource = ds; // GridView1.DataBind(); myConnection.Close(); } GridView1.DataSource = dtTemp; GridView1.DataBind(); //图表 Chart1.Series.Clear(); Series series1 = Chart1.Series.Add("电量值");//此处的线应该和上面的lstWcq.Items[j].ToString();想对应,每增加一个ID随之增加一条线。线应该是分开表示的。 string[] l = new string[2000]; series1.ChartType = "line"; series1.BorderWidth = 2; //坐标值 Chart1.ChartAreas["default"].AxisX.Title = "日期/时间"; Chart1.ChartAreas["default"].AxisY.Title = "电量"; Chart1.ChartAreas["Default"].AxisX.LabelStyle.Interval = 1; //赋值给线 for (int i = 0; i <= dtTemp.Rows.Count - 1; i++) { Chart1.Series["电量值"].Points.AddXY( dtTemp.Rows[i][2],dtTemp.Rows[i][1]); } }这就是我的 代码 现在只能出来一条线 如果出来两条线就是连在一起的 不知道怎么做循环了··· 是不是 假如有5个不同的ID ,就出来5条线, 每个ID(线)的数据记录数是一致的? 假如有5个不同的ID ,我选出那个ID就出来那个ID线,每条ID线的值是不同的。 DataTable dt = new DataTable("dtTest"); dt.Columns.Add("ID", typeof(int)); dt.Columns.Add("Val", typeof(int)); dt.Columns.Add("Date", typeof(string)); //ID=1 dt.Rows.Add(1, 123, "1"); dt.Rows.Add(1, 135, "2"); dt.Rows.Add(1, 132, "3"); dt.Rows.Add(1, 163, "4"); dt.Rows.Add(1, 184, "5"); //ID=2 dt.Rows.Add(2, 143, "1"); dt.Rows.Add(2, 115, "2"); dt.Rows.Add(2, 152, "3"); dt.Rows.Add(2, 133, "4"); dt.Rows.Add(2, 164, "5"); //ID=3 dt.Rows.Add(3, 173, "1"); dt.Rows.Add(3, 135, "2"); dt.Rows.Add(3, 172, "3"); dt.Rows.Add(3, 123, "4"); dt.Rows.Add(3, 114, "5"); chart1.ChartAreas["Default"].AxisX.Title = "日期/时间"; chart1.ChartAreas["Default"].AxisY.Title = "电量"; chart1.ChartAreas["Default"].AxisX.LabelStyle.Interval = 1; List<int> ids = new List<int>(3) { 1, 2, 3 }; foreach (int id in ids) { DataRow[] data = dt.Select(string.Format("ID={0}", id.ToString())); Series series = chart1.Series.Add("电量值" + id); series.ChartType = SeriesChartType.Line; series.Points.DataBindXY(data, "Date", data, "Val"); }这个是例子代码;你参考修改下;---------------------------------------你把你获取数据合并DataTable (dtTemp.Merge(ds.Tables[0]);)这个代码去掉;然后把创建图表曲线的代码,放到循环内,让每获取一个ID,就生成一条曲线;主要是生成序列和赋值的代码; 好多错误 去掉DataTable (dtTemp.Merge(ds.Tables[0]);)的话我的GridView 也不来数了 List<int> ids = new List<int>(3) { 1, 2, 3 }; 这里出现错误 你要加入命名空间; 直接给你修改一下的,你再试试--------------------------------------protected void Button3_Click(object sender, EventArgs e) { DataTable dtTemp = new DataTable(); for (int j = 0; j < lstWcq.Items.Count; j++) { SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["table_1"]); SqlCommand myCommand = new SqlCommand("dian", myConnection); myCommand.CommandType = CommandType.StoredProcedure; myConnection.Open(); myCommand.Parameters.Add(new SqlParameter("@sj1", SqlDbType.DateTime)); myCommand.Parameters["@sj1"].Value = TextBox1.Text.Trim(); myCommand.Parameters.Add(new SqlParameter("@sj2", SqlDbType.DateTime)); myCommand.Parameters["@sj2"].Value = TextBox2.Text.Trim(); myCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.NChar)); myCommand.Parameters["@id"].Value = lstWcq.Items[j].ToString(); SqlDataAdapter da = new SqlDataAdapter(myCommand); DataSet ds = new DataSet(); da.Fill(ds); //----------------------------------- Series series = chart1.Series.Add("电量值" + lstWcq.Items[j].ToString()); series.ChartType = SeriesChartType.Line; series.BorderWidth = 2; DataTable dtSeries = ds.Tables[0]; for (int i = 0; i < dtSeries.Rows.Count; i++) { Chart1.Series["电量值"].Points.AddXY(dtSeries.Rows[i][2], dtSeries.Rows[i][1]); } //----------------------------------- dtTemp.Merge(ds.Tables[0]); // GridView1.DataSource = ds; // GridView1.DataBind(); myConnection.Close(); } GridView1.DataSource = dtTemp; GridView1.DataBind(); //图表 //Chart1.Series.Clear(); //Series series1 = Chart1.Series.Add("电量值");//此处的线应该和上面的lstWcq.Items[j].ToString();想对应,每增加一个ID随之增加一条线。线应该是分开表示的。 string[] l = new string[2000]; // series1.ChartType = "line"; // series1.BorderWidth = 2; //坐标值 Chart1.ChartAreas["default"].AxisX.Title = "日期/时间"; Chart1.ChartAreas["default"].AxisY.Title = "电量"; Chart1.ChartAreas["Default"].AxisX.LabelStyle.Interval = 1; //赋值给线 //for (int i = 0; i <= dtTemp.Rows.Count - 1; i++) //{ // Chart1.Series["电量值"].Points.AddXY(dtTemp.Rows[i][2], dtTemp.Rows[i][1]); //} } Chart1.Series["电量值"].Points.AddXY(dtSeries.Rows[i][2], dtSeries.Rows[i][1]);错了,要改成这样;------------------ Chart1.Series["电量值" + lstWcq.Items[j].ToString()].Points.AddXY(dtSeries.Rows[i][2], dtSeries.Rows[i][1]); www.codeproject.com 上面有相关的控件源码,你可以到上面看看。很不错的。 C#串口通讯 接收数据问题。急!!!!!!! c#+oracle 汉字输入oracle后是乱码 byte[] 索引越界 如何在gridView中动态绑定 DropDownList数据(在线等) 如何编写自已的ORM工具? C#分割字符串的问题 重写与隐藏的区别,马上结分! 毕业了,哈哈,散分喽~~~~~~ 求一个CheckBoxList解决思路 谁能把这段VB程序转化为C#? 一个显示数据库图片的问题 Ibatisnet的有关问题
把你的数据写到DataTable中,绑定下
/// <summary>
/// Listbox中的信息向左或者右移动
/// </summary>
/// <param name="listSource"></param>
/// <param name="listTarget"></param>
private void moveTo(System.Windows.Forms.ListBox listSource, System.Windows.Forms.ListBox listTarget)
{
int temp = listSource.SelectedIndex;
ArrayList listTemp = new ArrayList();
for (int i = 0; i < listSource.SelectedItems.Count; i++)
{
listTemp.Add(listSource.SelectedItems[i].ToString());
}
for (int i = 0; i < listTemp.Count; i++)
{
listSource.Items.Remove(listTemp[i].ToString());
listTarget.Items.Add(listTemp[i].ToString());
} if (listTarget.Items.Count >= 1)
listTarget.SetSelected(listTarget.Items.Count - 1, true);
listSource.Refresh();
listTarget.Refresh();
listTarget.ClearSelected();
for (int i = 0; i < listTarget.Items.Count; i++)
{
if (listTemp.Contains(listTarget.Items[i].ToString()))
{
listTarget.SetSelected(i, true);
}
}
try
{
listSource.SelectedIndex = temp;//选择转移过后自动选择到下一项
}
catch (ArgumentOutOfRangeException)//如果选择到了最后一项则返回到第一项
{
try
{
listSource.SelectedIndex = 0;
}
catch (ArgumentOutOfRangeException)//如果没有任何可以选择的项则不选择
{ }
}
}以前写的 就是这个功能
用WellChart吧 很好用
{
DataTable dtTemp = new DataTable(); for (int j = 0; j < lstWcq.Items.Count; j++)
{
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["table_1"]); SqlCommand myCommand = new SqlCommand("dian", myConnection);
myCommand.CommandType = CommandType.StoredProcedure; myConnection.Open(); myCommand.Parameters.Add(new SqlParameter("@sj1", SqlDbType.DateTime));
myCommand.Parameters["@sj1"].Value = TextBox1.Text.Trim(); myCommand.Parameters.Add(new SqlParameter("@sj2", SqlDbType.DateTime));
myCommand.Parameters["@sj2"].Value = TextBox2.Text.Trim(); myCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.NChar));
myCommand.Parameters["@id"].Value = lstWcq.Items[j].ToString(); SqlDataAdapter da = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
da.Fill(ds);
dtTemp.Merge(ds.Tables[0]); // GridView1.DataSource = ds;
// GridView1.DataBind(); myConnection.Close(); } GridView1.DataSource = dtTemp;
GridView1.DataBind(); //图表
Chart1.Series.Clear();
Series series1 = Chart1.Series.Add("电量值");
string[] l = new string[2000]; series1.ChartType = "line";
series1.BorderWidth = 2;
//坐标值
Chart1.ChartAreas["default"].AxisX.Title = "日期/时间";
Chart1.ChartAreas["default"].AxisY.Title = "电量";
Chart1.ChartAreas["Default"].AxisX.LabelStyle.Interval = 1;
//赋值给线
for (int i = 0; i <= dtTemp.Rows.Count - 1; i++)
{ Chart1.Series["电量值"].Points.AddXY( dtTemp.Rows[i][2],dtTemp.Rows[i][1]); }
}这就是我的 代码 现在只能出来一条线 如果出来两条线就是连在一起的 不知道怎么做循环了···
private void Bind_InitIndexTrend(object args, string IndexNameDisplay)
{
DataTable tmpDt = args as DataTable; if (tmpDt != null)
{
List<string> theIndexList = new List<string>(); string sIndex = IndexNameDisplay;
theIndexList.Add(sIndex);//所有指标都取别名为:IndexName了,原来是取sIndex JUDE 2010-04-09"indexName"
IndexDataChart.ClearChart();
IndexDataChart.ShowChart(tmpDt, " 小区 " + ObjectMsgDataGrid["小区", ObjectMsgDataGrid.SelectedCells[0].RowIndex].Value.ToString() + IndexNameDisplay + "走势", "时间", IndexNameDisplay);
Series A = (IndexDataChart.TChart.Series[0]);
Series.ChangeType(ref A, typeof(Line));
IndexDataChart.TChart.Axes.Left.Title.Text = sIndex;
this.Text = IndexNameDisplay + "走势";
}
else
{
ShowTishiMsg("处理出错");
}
}这个是TreeChart的 就是WellChart
WellChart是一个绘图画表的很好的工具
我只会画线而已
{
DataTable dtTemp = new DataTable(); for (int j = 0; j < lstWcq.Items.Count; j++)
{
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["table_1"]); SqlCommand myCommand = new SqlCommand("dian", myConnection);
myCommand.CommandType = CommandType.StoredProcedure; myConnection.Open(); myCommand.Parameters.Add(new SqlParameter("@sj1", SqlDbType.DateTime));
myCommand.Parameters["@sj1"].Value = TextBox1.Text.Trim(); myCommand.Parameters.Add(new SqlParameter("@sj2", SqlDbType.DateTime));
myCommand.Parameters["@sj2"].Value = TextBox2.Text.Trim(); myCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.NChar));
myCommand.Parameters["@id"].Value = lstWcq.Items[j].ToString(); SqlDataAdapter da = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
da.Fill(ds);
dtTemp.Merge(ds.Tables[0]); // GridView1.DataSource = ds;
// GridView1.DataBind(); myConnection.Close(); } GridView1.DataSource = dtTemp;
GridView1.DataBind(); //图表
Chart1.Series.Clear();
Series series1 = Chart1.Series.Add("电量值");//此处的线应该和上面的lstWcq.Items[j].ToString();想对应,每增加一个ID随之增加一条线。线应该是分开表示的。 string[] l = new string[2000]; series1.ChartType = "line";
series1.BorderWidth = 2;
//坐标值
Chart1.ChartAreas["default"].AxisX.Title = "日期/时间";
Chart1.ChartAreas["default"].AxisY.Title = "电量";
Chart1.ChartAreas["Default"].AxisX.LabelStyle.Interval = 1;
//赋值给线
for (int i = 0; i <= dtTemp.Rows.Count - 1; i++)
{ Chart1.Series["电量值"].Points.AddXY( dtTemp.Rows[i][2],dtTemp.Rows[i][1]); }
}这就是我的 代码 现在只能出来一条线 如果出来两条线就是连在一起的 不知道怎么做循环了···
DataTable dt = new DataTable("dtTest");
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Val", typeof(int));
dt.Columns.Add("Date", typeof(string)); //ID=1
dt.Rows.Add(1, 123, "1");
dt.Rows.Add(1, 135, "2");
dt.Rows.Add(1, 132, "3");
dt.Rows.Add(1, 163, "4");
dt.Rows.Add(1, 184, "5");
//ID=2
dt.Rows.Add(2, 143, "1");
dt.Rows.Add(2, 115, "2");
dt.Rows.Add(2, 152, "3");
dt.Rows.Add(2, 133, "4");
dt.Rows.Add(2, 164, "5");
//ID=3
dt.Rows.Add(3, 173, "1");
dt.Rows.Add(3, 135, "2");
dt.Rows.Add(3, 172, "3");
dt.Rows.Add(3, 123, "4");
dt.Rows.Add(3, 114, "5");
chart1.ChartAreas["Default"].AxisX.Title = "日期/时间";
chart1.ChartAreas["Default"].AxisY.Title = "电量";
chart1.ChartAreas["Default"].AxisX.LabelStyle.Interval = 1; List<int> ids = new List<int>(3) { 1, 2, 3 }; foreach (int id in ids)
{
DataRow[] data = dt.Select(string.Format("ID={0}", id.ToString())); Series series = chart1.Series.Add("电量值" + id);
series.ChartType = SeriesChartType.Line; series.Points.DataBindXY(data, "Date", data, "Val");
}这个是例子代码;你参考修改下;
---------------------------------------
你把你获取数据合并DataTable (dtTemp.Merge(ds.Tables[0]);)这个代码去掉;
然后把创建图表曲线的代码,放到循环内,让每获取一个ID,就生成一条曲线;主要是生成序列和赋值的代码;
--------------------------------------protected void Button3_Click(object sender, EventArgs e)
{
DataTable dtTemp = new DataTable(); for (int j = 0; j < lstWcq.Items.Count; j++)
{
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["table_1"]); SqlCommand myCommand = new SqlCommand("dian", myConnection);
myCommand.CommandType = CommandType.StoredProcedure; myConnection.Open(); myCommand.Parameters.Add(new SqlParameter("@sj1", SqlDbType.DateTime));
myCommand.Parameters["@sj1"].Value = TextBox1.Text.Trim(); myCommand.Parameters.Add(new SqlParameter("@sj2", SqlDbType.DateTime));
myCommand.Parameters["@sj2"].Value = TextBox2.Text.Trim(); myCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.NChar));
myCommand.Parameters["@id"].Value = lstWcq.Items[j].ToString(); SqlDataAdapter da = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
da.Fill(ds);
//----------------------------------- Series series = chart1.Series.Add("电量值" + lstWcq.Items[j].ToString());
series.ChartType = SeriesChartType.Line;
series.BorderWidth = 2; DataTable dtSeries = ds.Tables[0];
for (int i = 0; i < dtSeries.Rows.Count; i++)
{
Chart1.Series["电量值"].Points.AddXY(dtSeries.Rows[i][2], dtSeries.Rows[i][1]);
}
//-----------------------------------
dtTemp.Merge(ds.Tables[0]); // GridView1.DataSource = ds;
// GridView1.DataBind(); myConnection.Close(); } GridView1.DataSource = dtTemp;
GridView1.DataBind(); //图表
//Chart1.Series.Clear();
//Series series1 = Chart1.Series.Add("电量值");//此处的线应该和上面的lstWcq.Items[j].ToString();想对应,每增加一个ID随之增加一条线。线应该是分开表示的。 string[] l = new string[2000]; // series1.ChartType = "line";
// series1.BorderWidth = 2;
//坐标值
Chart1.ChartAreas["default"].AxisX.Title = "日期/时间";
Chart1.ChartAreas["default"].AxisY.Title = "电量";
Chart1.ChartAreas["Default"].AxisX.LabelStyle.Interval = 1;
//赋值给线
//for (int i = 0; i <= dtTemp.Rows.Count - 1; i++)
//{ // Chart1.Series["电量值"].Points.AddXY(dtTemp.Rows[i][2], dtTemp.Rows[i][1]); //}
}
Chart1.Series["电量值"].Points.AddXY(dtSeries.Rows[i][2], dtSeries.Rows[i][1]);
错了,要改成这样;
------------------
Chart1.Series["电量值" + lstWcq.Items[j].ToString()].Points.AddXY(dtSeries.Rows[i][2], dtSeries.Rows[i][1]);