|
■No102590 (たか さん) に返信 > 魔界の仮面弁士 さん > また続きで教えて頂きたいことがありますので > 宜しくお願い致します
いや指名されましても。
> 表示している最中にcycletime(横軸の値)を変更させたいのですが > どうすればいいでしょうか 方針としては、前回の回答と同じですよ。 「データの蓄積」と「データの表示」を分けて管理するだけです。
public sealed partial class Form1 : Form { // 蓄積するデータ private List<double> upperList = new List<double>(); private List<double> lowerList = new List<double>(); private Random random; private Timer timer;
// チャート private Chart upperChart; private Chart lowerChart;
// チャートに表示するデータ数。今回は TrackBar を利用しましたが、 // 自前で、「CycleTime プロパティ」を設けて管理しても良いでしょう。 private Label labelValue; private TrackBar cycleTimeBar; private const int CycleTimeMinValue = 1; private const int CycleTimeMaxValue = 20; private const int CycleTimeIniValue = 5;
public Form1() { InitializeComponent(); this.random = new Random(); (this.components ?? (this.components = new System.ComponentModel.Container())).Add(this.timer = new Timer() { Interval = 1000 }); this.Controls.Add(this.cycleTimeBar = new TrackBar() { Name = "trackBar", Minimum = CycleTimeMinValue, Maximum = CycleTimeMaxValue, Value = CycleTimeIniValue, Dock = DockStyle.Top }); this.Controls.Add(this.labelValue = new Label() { Name = "labelValue", Text = this.cycleTimeBar.Value.ToString(), Dock = DockStyle.Top }); this.labelValue.DataBindings.Add("Text", this.cycleTimeBar, "Value");
this.upperChart = this.CreateChart(this.cycleTimeBar.Value); this.upperChart.Location = new System.Drawing.Point(280, 250); this.lowerChart = this.CreateChart(this.cycleTimeBar.Value); this.lowerChart.Location = new System.Drawing.Point(280, 530);
this.Controls.AddRange(new[] { upperChart, lowerChart }); this.timer.Tick += this.timer_Tick; this.timer.Start(); this.cycleTimeBar.ValueChanged += delegate { this.UpdateCharts(); }; this.UpdateCharts(); }
private Chart CreateChart(int MaxCycle) { Chart chart = new Chart { Size = new System.Drawing.Size(1200, 250) }; Series series = chart.Series.Add("DataSeries"); series.ChartType = SeriesChartType.Line; series.BorderWidth = 2; chart.ChartAreas.Add(new ChartArea() { AxisY = { Minimum = 0, Maximum = 100 }, AxisX = { Minimum = 0, Maximum = MaxCycle, LabelStyle = { Enabled = true } } }); return chart; }
private void UpdateCharts() { UpdateChart(upperChart, upperList, cycleTimeBar.Value); UpdateChart(lowerChart, lowerList, cycleTimeBar.Value); } private void UpdateChart(in Chart chart, in List<double> list, int cycleTime) { Stack<double> viewData = new Stack<double>(); for (int i = list.Count - 1; i >= (list.Count - cycleTime - 1); i--) { viewData.Push((i < 0) ? double.NaN : list[i]); } Series series = chart.Series["DataSeries"]; var points = series.Points; points.Clear(); chart.ChartAreas[0].AxisX.Maximum = cycleTime; for (int x = 0; x <= cycleTime; x++) { points.AddXY(x, viewData.Pop()); } }
private void timer_Tick(object sender, EventArgs e) { int upperData = random.Next(40, 80); int lowerData = random.Next(0, 100); this.upperList.Add(upperData); this.lowerList.Add(lowerData); this.UpdateCharts(); } }
|