我有应用程序接收实时数据并使用计时器在我的图表控制图上显示此数据:
MyObject obj = null;
Series seriesTraffic = null:
private void LoadGraph()
{
seriesTraffic = new Series();
seriesTraffic.Color = Color.Blue;
seriesTraffic.ChartType = SeriesChartType.Spline;
seriesTraffic.BorderWidth = 2;
chart1.Series.Add(seriesTraffic);
chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.White;
chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.White;
chart1.ChartAreas[0].AxisX.LabelStyle.Enabled = false;
chart1.ChartAreas[0].AxisY.LabelStyle.Enabled = false;
chart1.ChartAreas[0].AxisX.IsStartedFromZero = true;
chart1.ChartAreas[0].AxisX.IntervalOffsetType = DateTimeIntervalType.Number;
}
private void chartTimer_Tick(object sender, EventArgs e)
{
var timer = (System.Windows.Forms.Timer)sender;
MyObject wf2 = (MyObject)timer.Tag;
lblChartMbitSec.Text = string.Format("{0} Mbit/sec", (wf2.BitsPerSecond * 0.000001).ToString("0.##"));
if (seriesTraffic.Points.Count() > 150)
seriesTraffic.Points.RemoveAt(0);
seriesTraffic.Points.Add(wf.BitsPerSecond * 0.000001);
chart1.ResetAutoValues();
}
我的对象包含2个properties
,我希望我的图表显示为单独的:
Tnis是我的Combobox SelectedIndexChanged event
:
private void cbStatistics_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbStatistics.SelectedIndex == 0)
timerChart.Tag = wf;
else if (cbStatistics.SelectedIndex == 1)
timerChart.Tag = wf;
}
当这个选定的索引改变时,我想显示我的其他图表,我该怎么做?
答案 0 :(得分:2)
我会创建两个系列对象,并在每个计时器滴答时更新它们 - 一个用于每秒位数,一个用于每秒数据包:
private void LoadGraph()
{
seriesBps = new Series("bps");
seriesBps.Color = Color.Blue;
seriesBps.ChartType = SeriesChartType.Spline;
seriesBps.BorderWidth = 2;
chart1.Series.Add(seriesBps);
seriesPps = new Series("pps");
seriesPps.Color = Color.Blue;
seriesPps.ChartType = SeriesChartType.Spline;
seriesPps.BorderWidth = 2;
chart1.Series.Add(seriesPps);
// set up axes as you already do
}
然后在你的计时器刻度线处理程序中:
private void chartTimer_Tick(object sender, EventArgs e)
{
if (seriesBps.Points.Count() > 150)
seriesBps.Points.RemoveAt(0);
seriesBps.Points.Add(wf.BitsPerSecond * 0.000001);
if (seriesPps.Points.Count() > 150) seriesPps.Points.RemoveAt(0);
seriesPps.Points.Add(wf.PacketsPerSecond);
chart1.ResetAutoValues();
}
并且,在您的组合框事件处理程序中,启用所需的系列:
private void cbStatistics_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbStatistics.SelectedIndex == 0)
{
seriesBps.Enabled = true;
seriesPps.Enabled = false;
lblChartMbitSec.Text = string.Format("{0} Mbit/sec", (wf2.BitsPerSecond * 0.000001).ToString("0.##")); // and similarly for the other case below
}
else if (cbStatistics.SelectedIndex == 1)
{
seriesBps.Enabled = false;
seriesPps.Enabled = true;
}
chart1.ResetAutoValues();
}
在LoadGraph()
方法中的两个系列上设置启用标志,以对应组合框的初始状态。