gpt4 book ai didi

c# - Mschart 从数据库中获取多个系列

转载 作者:太空宇宙 更新时间:2023-11-03 20:28:28 24 4
gpt4 key购买 nike

您好,我正在尝试从 sql 填充 mschart,但即使系列具有不同的值,图表也是相同的。所以所有系列都有相同的线相互重叠。

        sqlConnection.Open();
SqlCommand cmd = new SqlCommand(query, sqlConnection);
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 300;
rdr = cmd.ExecuteReader();
DataTable table = new DataTable();

// Add three columns to the table.
table.Columns.Add("Channel", typeof(String));
table.Columns.Add("Date", typeof(String));
table.Columns.Add("Value", typeof(Int32));


// Add data rows to the table.
while (rdr.Read())
{

table.Rows.Add(new object[] { rdr[0], rdr[1], rdr[2] });

}
chart1.DataSource = table;
//add series
for (int i = 0; i < table.Rows.Count; i++)
{
if (chart1.Series.Where(x => x.Name == table.Rows[i][0].ToString()).Count() > 0)
{

}
else
{
chart1.Series.Add(table.Rows[i][0].ToString());

}
}
//set x,y axis for each series
for (int i = 0; i < chart1.Series.Count; i++)
{

chart1.Series[i].XValueMember = "Date";
chart1.Series[i].YValueMembers = "Value";

chart1.Series[i].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;

}
chart1.DataBind();

最佳答案

找到另一种没有数据绑定(bind)的方法

   #region get values from database
string query = "declare @start datetime, @end datetime select @start = '" + start + "', @end = '" + end + "' select Name,DataDT,Rating,DataGroupID from DATA inner join Channels on Channels.ID = DATA.ChannelID where DataDT >= @start and DataDT < @end and ChannelID<'4'";
SqlDataReader rdr = null;
sqlCon = new SqlConnection(Properties.Settings.Default.ConectionString);
sqlConnection.Open();
SqlCommand cmd = new SqlCommand(query, sqlConnection);
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 300;
rdr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
DataTable table = new DataTable();
// Add three columns to the table.
table.Columns.Add("Channel", typeof(String));
table.Columns.Add("Date", typeof(String));
table.Columns.Add("Value", typeof(Int32));

// Add data rows to the table.
while (rdr.Read())
{
table.Rows.Add(new object[] { rdr[0], rdr[1], rdr[2] });
}
sqlCon.Close();


#endregion



// Create a data series


for (int i = 0; i < table.Rows.Count; i++)
{
if (chart1.Series.Where(x => x.Name == table.Rows[i][0].ToString()).Count() > 0)
{

}
else
{
Series series = new Series(table.Rows[i][0].ToString());
chart1.Series.Add(series);

}
}
for (int i = 0; i < table.Rows.Count; i++)
{
foreach (Series series in chart1.Series)
{
if (table.Rows[i][0].ToString() == series.Name)
{
series.Points.AddXY(Convert.ToDateTime(table.Rows[i][1]).ToShortTimeString(),Convert.ToDouble(table.Rows[i][2]));
}

}
}
//Set Properties for chart series
for (int i = 0; i < chart1.Series.Count; i++)
{

chart1.Series[i].XValueMember = "Date";
chart1.Series[i].YValueMembers = "Value";

chart1.Series[i].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
chart1.Series[i].XValueType = ChartValueType.Time;
chart1.Series[i].ChartType = SeriesChartType.Line;

chart1.Series[i].MarkerStyle = MarkerStyle.Star10;
chart1.Series[i].MarkerSize = 8;
chart1.Series[i].MarkerColor = chart1.Series[0].BorderColor;
chart1.Series[i].BorderWidth = 3;
chart1.Series[i].IsValueShownAsLabel = true;
chart1.Series[i].ToolTip = "#VALY => #AXISLABEL";
}

chart1.ChartAreas[0].AxisX.IsMarginVisible = true;
}

关于c# - Mschart 从数据库中获取多个系列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8985635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com