From 6883c8046d5f0b487b214a31be4026b13ede2a3d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E8=80=81=E4=B9=9D?= <366193849@qq.com>
Date: Thu, 11 Nov 2021 01:12:41 +0800
Subject: [PATCH] =?UTF-8?q?=E5=8A=A8=E7=94=BB=E8=BF=9E=E7=BB=AD=E6=80=A7?=
=?UTF-8?q?=E4=BC=98=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
FastGithub.UI/FlowChart.xaml | 17 +++++++++------
FastGithub.UI/FlowChart.xaml.cs | 38 ++++++++++++++++++++++++---------
2 files changed, 39 insertions(+), 16 deletions(-)
diff --git a/FastGithub.UI/FlowChart.xaml b/FastGithub.UI/FlowChart.xaml
index 7871d10..15bb9dd 100644
--- a/FastGithub.UI/FlowChart.xaml
+++ b/FastGithub.UI/FlowChart.xaml
@@ -15,13 +15,18 @@
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
上行流量
diff --git a/FastGithub.UI/FlowChart.xaml.cs b/FastGithub.UI/FlowChart.xaml.cs
index bbe6f55..6774f84 100644
--- a/FastGithub.UI/FlowChart.xaml.cs
+++ b/FastGithub.UI/FlowChart.xaml.cs
@@ -1,8 +1,8 @@
using LiveCharts;
+using LiveCharts.Configurations;
using LiveCharts.Wpf;
using Newtonsoft.Json;
using System;
-using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows.Controls;
@@ -18,19 +18,24 @@ namespace FastGithub.UI
{
Title = "上行速率",
PointGeometry = null,
- Values = new ChartValues()
+ Values = new ChartValues()
};
private readonly LineSeries writeSeries = new LineSeries()
{
Title = "下行速率",
PointGeometry = null,
- Values = new ChartValues()
+ Values = new ChartValues()
};
- public SeriesCollection Series { get; } = new SeriesCollection();
+ private static DateTime GetDateTime(double timestamp) => new DateTime(1970, 1, 1).Add(TimeSpan.FromMilliseconds(timestamp)).ToLocalTime();
- public List Labels { get; } = new List();
+ private static double GetTimestamp(DateTime dateTime) => dateTime.ToUniversalTime().Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
+
+
+ public SeriesCollection Series { get; } = new SeriesCollection(Mappers.Xy().X(item => item.Timestamp).Y(item => item.Rate));
+
+ public Func XFormatter { get; } = timestamp => GetDateTime(timestamp).ToString("HH:mm:ss");
public Func YFormatter { get; } = value => $"{FlowStatistics.ToNetworkSizeString((long)value)}/s";
@@ -77,16 +82,29 @@ namespace FastGithub.UI
this.textBlockRead.Text = FlowStatistics.ToNetworkSizeString(flowStatistics.TotalRead);
this.textBlockWrite.Text = FlowStatistics.ToNetworkSizeString(flowStatistics.TotalWrite);
- this.readSeries.Values.Add(flowStatistics.ReadRate);
- this.writeSeries.Values.Add(flowStatistics.WriteRate);
- this.Labels.Add(DateTime.Now.ToString("HH:mm:ss"));
+ var timestamp = GetTimestamp(DateTime.Now);
+ this.readSeries.Values.Add(new RateItem(flowStatistics.ReadRate, timestamp));
+ this.writeSeries.Values.Add(new RateItem(flowStatistics.WriteRate, timestamp));
- if (this.Labels.Count > 60)
+ if (this.readSeries.Values.Count > 60)
{
this.readSeries.Values.RemoveAt(0);
this.writeSeries.Values.RemoveAt(0);
- this.Labels.RemoveAt(0);
}
}
+
+ private class RateItem
+ {
+ public double Rate { get; }
+
+ public double Timestamp { get; }
+
+ public RateItem(double rate, double timestamp)
+ {
+ this.Rate = rate;
+ this.Timestamp = timestamp;
+ }
+ }
+
}
}