□投稿者/ まさ (5回)-(2024/01/06(Sat) 09:59:28)
|
分類:[.NET 全般]
C# Windowsフォームアプリケーション .NetFramework
宜しくお願い致します
form1で1秒毎に外部機器からデータを呼び出して折れ線グラフで表示しています。
そしてそのデータを日付のファイルにcsvで書き込んでいます(1秒毎ですので1日約9万件)
それをform3で1週間分の棒グラフを表示させています(form1は表示したまま)
データの保存、表示は出来ているのですが、作り方を分かっていなかったので
form3のグラフを集計、表示させている間にform1の折れ線グラフが止まっていました
調べましてform3の集計を非同期で処理させようと思ったのですがうまくいかず
何かいい方法はありましたら教えて頂けないでしょうか
宜しくお願い致します
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.IO;
using System.Globalization;
namespace グラフ表示
{
public partial class Form3 : Form
{
private Form1 form1Instance;
private Dictionary<DateTime, double> dailyTotals;
public Form3(Form1 form1)
{
InitializeComponent();
form1Instance = form1;
timer1 = new Timer();
timer1.Interval = 1000;
timer1.Tick += timer1_Tick;
timer1.Start();
}
private void Form3_Load(object sender, EventArgs e)
{
day7();
line7();
lblDate.Text = "2000/00/00";
lblTime.Text = "00:00:00";
}
private void btnMainMenu_Click(object sender, EventArgs e)
{
var form2 = new Form2(new Form1());
form2.Show();
this.Close();
}
private void timer1_Tick(object sender, EventArgs e)
{
lblDate.Text = DateTime.Now.ToString("yyyy/M/d");
lblTime.Text = DateTime.Now.ToString("HH:mm:ss");
}
private Dictionary<DateTime, double> AggregateDailyTotals(string inputDirectory)
{
Dictionary<DateTime, double> dailyTotals = new Dictionary<DateTime, double>();
foreach (string filePath in Directory.GetFiles(inputDirectory, "*.csv"))
{
ParseCSVFile(filePath, dailyTotals);
}
return dailyTotals;
}
private void ParseCSVFile(string filePath, Dictionary<DateTime, double> dailyTotals)
{
try
{
foreach (string line in File.ReadLines(filePath))
{
string[] fields = line.Split(','); // カンマで分割
string dateString = fields[0];// 日付を取得します(日付がA列にある)
DateTime date;
if (DateTime.TryParseExact(dateString, "yyyy/M/d", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
double value;
if (double.TryParse(fields[2], out value)) // C列値
{
if (dailyTotals.ContainsKey(date))// 日付毎に集計
{
dailyTotals[date] += value;
}
else
{
dailyTotals[date] = value;
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("以下の理由でファイルが開けません。\n" + ex.Message);
}
}
private void day7()
{
string inputDirectory = G.csvDataFilePath;// 入力ディレクトリのパスを指定
dailyTotals = AggregateDailyTotals(inputDirectory);// 集計結果を取得
Dictionary<DateTime, double> last7DaysTotals = GetLast7DaysTotals();// 過去7日間の集計結果を取得
dataGridView1.Rows.Clear(); // DataGridViewに結果を表示
foreach (DateTime date in GetLast7DaysDates())
{
double total = last7DaysTotals.ContainsKey(date) ? last7DaysTotals[date] : 0.0;
dataGridView1.Rows.Add(date.ToString("yyyy/M/d"), total);
}
}
private Dictionary<DateTime, double> GetLast7DaysTotals()
{
DateTime currentDate = DateTime.Now.Date;
DateTime sevenDaysAgo = currentDate.AddDays(-7);
Dictionary<DateTime, double> last7DaysTotals = new Dictionary<DateTime, double>();
foreach (var entry in dailyTotals)
{
if (entry.Key >= sevenDaysAgo && entry.Key <= currentDate)
{
last7DaysTotals[entry.Key] = entry.Value;
}
}
return last7DaysTotals;
}
private IEnumerable<DateTime> GetLast7DaysDates()
{
DateTime currentDate = DateTime.Now.Date;
for (int i = 7; i >= 0; i--)
{
yield return currentDate.AddDays(-i);
}
}
private void line7()
{ // 7日前から本日までの日ごとの集計結果を取得
Dictionary<DateTime, double> last7DaysTotals = GetLast7DaysTotals();
chart1.Series.Clear(); // グラフを初期化
chart1.Titles.Clear();
Series series = new Series("過去8日間の合計");
series.ChartType = SeriesChartType.Column;
foreach (DateTime date in GetLast7DaysDates())
{
double total = last7DaysTotals.ContainsKey(date) ? last7DaysTotals[date] : 0.0;
DataPoint dataPoint = new DataPoint();
dataPoint.SetValueXY(date.ToString("yyyy/M/d"), (total/60));//60
dataPoint.Label = (total/60).ToString("0.0");
series.Points.Add(dataPoint);
}
//グラフ表示
chart1.Series.Add(series);
Title title=chart1.Titles.Add("過去8日間の合計");
chart1.Visible = true;
title.Font = new Font("Arial", 20, FontStyle.Bold);
//chart1.Update();
}
}
}
|
|