質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

.NET Framework

.NET Framework は、Microsoft Windowsのオペレーティングシステムのために開発されたソフトウェア開発環境/実行環境です。多くのプログラミング言語をサポートしています。

Q&A

1回答

8082閲覧

[C#] chartのX軸に時間を表示させたい

petapetapenpen

総合スコア4

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

.NET Framework

.NET Framework は、Microsoft Windowsのオペレーティングシステムのために開発されたソフトウェア開発環境/実行環境です。多くのプログラミング言語をサポートしています。

0グッド

0クリップ

投稿2020/10/07 00:59

.NET Framework 4.6にてWindowsFormApplicationを作成しています。

やりたい事
>Listが保持しているデータをY軸に追加し、自動でX軸に時間経過データとして1秒ごと加算されたラベルを作成してほしい。

現状
>X軸をHH:mm:ss形式にすることはできた。しかし、データを追加しても00:00:00のままラベルに変化がない。

ソース

C#
//折れ線グラフ
//デフォルトのSeriesをクリア
this.chart1.Series.Clear();
this.chart1.ChartAreas.Clear();
//ChartAreaの追加
this.chart1.ChartAreas.Add(new System.Windows.Forms.DataVisualization.Charting.ChartArea("chart_area"));
//Seriesの追加
const string data1 = "data";
this.chart1.Series.Add(data1);
//折れ線グラフの指定
this.chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
//目盛の設定
this.chart1.ChartAreas[0].AxisX.Minimum = DateTime.MinValue.AddSeconds(00).ToOADate();
this.chart1.ChartAreas[0].AxisX.Interval = 1;
this.chart1.ChartAreas[0].AxisX.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Seconds;
this.chart1.ChartAreas[0].AxisX.IntervalOffsetType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Seconds;
this.chart1.Series[0].XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Time;
this.chart1.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss";
//データの追加
for (int i = 0; i < ListA.Count; i++)
{
this.chart1.Series[data1].Points.AddY(ListA[i]);
}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2020/10/07 03:28

コードは ``` と ``` で囲ってください(``` はバッククォート 3つ)。インデントされて見やすくなるので。
guest

回答1

0

やりたいことが分からないのでハズレかもしれませんが・・・

下の画像(Chart サンプルです)のようなことがしたいのでしょうか?

イメージ説明

画像の[C# Source]タブの中にあるソースを以下に貼っておきます。ハズレでしたら失礼しました。回答欄でないと画像やインデントされたコードが貼れないのでご容赦ください。

using System.Windows.Forms.DataVisualization.Charting; ... private Thread addDataRunner; private Random rand = new Random(); private System.Windows.Forms.DataVisualization.Charting.Chart chart1; public delegate void AddDataDelegate(); public AddDataDelegate addDataDel; ... private void RealTimeSample_Load(object sender, System.EventArgs e) { // create the Adding Data Thread but do not start until start button clicked ThreadStart addDataThreadStart = new ThreadStart(AddDataThreadLoop); addDataRunner = new Thread(addDataThreadStart); // create a delegate for adding data addDataDel += new AddDataDelegate(AddData); } private void startTrending_Click(object sender, System.EventArgs e) { // Disable all controls on the form startTrending.Enabled = false; // and only Enable the Stop button stopTrending.Enabled = true; // Predefine the viewing area of the chart minValue = DateTime.Now; maxValue = minValue.AddSeconds(120); chart1.ChartAreas[0].AxisX.Minimum = minValue.ToOADate(); chart1.ChartAreas[0].AxisX.Maximum = maxValue.ToOADate(); // Reset number of series in the chart. chart1.Series.Clear(); // create a line chart series Series newSeries = new Series( "Series1" ); newSeries.ChartType = SeriesChartType.Line; newSeries.BorderWidth = 2; newSeries.Color = Color.OrangeRed; newSeries.XValueType = ChartValueType.DateTime; chart1.Series.Add( newSeries ); // start worker threads. if ( addDataRunner.IsAlive == true ) { addDataRunner.Resume(); } else { addDataRunner.Start(); } } private void stopTrending_Click(object sender, System.EventArgs e) { if ( addDataRunner.IsAlive == true ) { addDataRunner.Suspend(); } // Enable all controls on the form startTrending.Enabled = true; // and only Disable the Stop button stopTrending.Enabled = false; } /// Main loop for the thread that adds data to the chart. /// The main purpose of this function is to Invoke AddData /// function every 1000ms (1 second). private void AddDataThreadLoop() { while (true) { chart1.Invoke(addDataDel); Thread.Sleep(1000); } } public void AddData() { DateTime timeStamp = DateTime.Now; foreach ( Series ptSeries in chart1.Series ) { AddNewPoint( timeStamp, ptSeries ); } } /// The AddNewPoint function is called for each series in the chart when /// new points need to be added. The new point will be placed at specified /// X axis (Date/Time) position with a Y value in a range +/- 1 from the previous /// data point's Y value, and not smaller than zero. public void AddNewPoint( DateTime timeStamp, System.Windows.Forms.DataVisualization.Charting.Series ptSeries ) { double newVal = 0; if ( ptSeries.Points.Count > 0 ) { newVal = ptSeries.Points[ptSeries.Points.Count -1 ].YValues[0] + (( rand.NextDouble() * 2 ) - 1 ); } if ( newVal < 0 ) newVal = 0; // Add new data point to its series. ptSeries.Points.AddXY( timeStamp.ToOADate(), rand.Next(10, 20)); // remove all points from the source series older than 1.5 minutes. double removeBefore = timeStamp.AddSeconds( (double)(90) * ( -1 )).ToOADate(); //remove oldest values to maintain a constant number of data points while ( ptSeries.Points[0].XValue < removeBefore ) { ptSeries.Points.RemoveAt(0); } chart1.ChartAreas[0].AxisX.Minimum = ptSeries.Points[0].XValue; chart1.ChartAreas[0].AxisX.Maximum = DateTime.FromOADate(ptSeries.Points[0].XValue).AddMinutes(2).ToOADate(); chart1.Invalidate(); } /// Clean up any resources being used. protected override void Dispose( bool disposing ) { if ( (addDataRunner.ThreadState & ThreadState.Suspended) == ThreadState.Suspended) { addDataRunner.Resume(); } addDataRunner.Abort(); if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } ...

投稿2020/10/07 03:36

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問