You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1031 lines
39 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Arction.Wpf.Charting;
using Arction.Wpf.Charting.Axes;
using Arction.Wpf.Charting.SeriesXY;
using Arction.Wpf.Charting.Views.ViewXY;
using Microsoft.Win32;
using StartServerWPF.Modules.MseedChart.Models;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using SharpDX.Direct2D1;
using SharpDX.DirectWrite;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http.Headers;
using System.Reflection;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using System.Xml.Linq;
using Prism.Regions;
using static System.Collections.Specialized.BitVector32;
using ZhaoXi.Advanced.MyRedis.Service;
using ServiceStack;
using ServiceStack.Redis;
using Arction.Licensing;
using System.Runtime.InteropServices;
namespace StartServerWPF.Modules.MseedChart.ViewModels
{
public class ChartPlotRealDataViewModel : BindableBase, INavigationAware
{
Mseed2asciiApi.LoopCallbackHandler loopCallback;
public ChartPlotRealDataViewModel()
{
_dispatcher = Application.Current.Dispatcher;
_wavesModel = new WavesModel();
(Application.Current.MainWindow as System.Windows.Window).Closing += ApplicationClosingDispose;
IntervalTime=100;
CreateChart();
loopCallback = new Mseed2asciiApi.LoopCallbackHandler(Mseed2AsciiEvent);
Mseed2asciiApi.MseedDatasCallFun(loopCallback);
GC.KeepAlive(loopCallback);
}
#region 字段
Dispatcher _dispatcher;
public int CurPoints;
int _channelCount = 0;
int _samplingFrequency = 500; // 采样频率 (Hz).
WavesModel _wavesModel;
private int _lChartCount = 1;
public ConcurrentQueue<StationModel> smList=new ConcurrentQueue<StationModel>();
public List<AxisY> _chartAxisY=new List<AxisY>();
private DispatcherTimer dispatcherTimer=new DispatcherTimer();
#endregion
#region 属性
private string _title = "数据中心";
public string Title
{
get { return _title; }
set
{
SetProperty(ref _title, value);
}
}
private bool _isRealtimeData =false;
public bool IsRealtimeData
{
get { return _isRealtimeData; }
set
{
SetProperty(ref _isRealtimeData, value);
}
}
private bool _isMultFiles;
public bool IsMultFiles
{
get { return _isMultFiles; }
set
{
SetProperty(ref _isMultFiles, value);
}
}
private int _xaisInterval;
public int XaisInterval
{
get { return _xaisInterval; }
set
{
SetProperty(ref _xaisInterval, value);
}
}
/// <summary>
/// 波形控件数量
/// </summary>
public int LChartCount
{
get { return _lChartCount; }
set { _lChartCount = value; }
}
private LightningChart _lChartAll;
/// <summary>
/// 所有波形
/// </summary>
public LightningChart LChartALL
{
get { return _lChartAll; }
set { _lChartAll = value; }
}
private FrameworkElement childContent;
public FrameworkElement ChildContent
{
get { return childContent; }
set { childContent = value; }
}
private Grid gridChart;
public Grid GridChart
{
get { return gridChart; }
set { gridChart = value; }
}
private StationEventJson _currentEventTime;
public StationEventJson CurrentEventTime
{
get { return _currentEventTime; }
set { _currentEventTime = value; }
}
private List<StationAxis> _stationsName;
public List<StationAxis> StationsName
{
get { return _stationsName; }
set {
SetProperty(ref _stationsName, value);
}
}
private string _currentTime;
public string CurrentTime
{
get { return _currentTime; }
set {
SetProperty(ref _currentTime, value);
}
}
private YAxisAutoPlacement _yaxisPlacement;
public YAxisAutoPlacement YaxisPlacement
{
get { return _yaxisPlacement; }
set
{
LChartALL.ViewXY.AxisLayout.YAxisAutoPlacement = value;
int distancetoX =_wavesModel.SetYasixPlacement(LChartALL);
foreach (var item in LChartALL.ViewXY.YAxes)
{
item.Title.DistanceToAxis = distancetoX;
}
SetProperty(ref _yaxisPlacement, value);
}
}
private int _IntervalTime=10;
public int IntervalTime
{
get { return _IntervalTime; }
set
{
if (value >=10)
{
dispatcherTimer.Interval = TimeSpan.FromMilliseconds(value);
}
SetProperty(ref _IntervalTime, value);
}
}
private bool _SingleChannel=false;
public bool SingleChannel
{
get { return _SingleChannel; }
set
{
if(!value)
{
YasixZENVisible("SHZ", false);
YasixZENVisible("SHN", false);
YasixZENVisible("SHE", false);
}
else
{
YasixZENVisible("SHZ", true);
YasixZENVisible("SHN", true);
YasixZENVisible("SHE", true);
YasixZENVisible(SelectChannel, false);
}
SetProperty(ref _SingleChannel, value);
}
}
private string _SelectChannel;
public string SelectChannel
{
get { return _SelectChannel; }
set
{
if (SingleChannel)
{
//隐藏上当前轴
YasixZENVisible(_SelectChannel, true);
//显示选择轴
YasixZENVisible(value, false);
}
SetProperty(ref _SelectChannel, value);
}
}
#endregion
#region 事件
public DelegateCommand LoadedCommand => new DelegateCommand(Loaded);
public DelegateCommand UnloadedCommand => new DelegateCommand(UnLoaded);
public DelegateCommand<object> AxesYVisibleCommand => new DelegateCommand<object>(AxesYVisible);
public DelegateCommand<object> FileSelectorCommand => new DelegateCommand<object>(FileSelector);
public DelegateCommand<object> OtimeSortCommand => new DelegateCommand<object>(OtimeSort);
public DelegateCommand IntervalSureCommand => new DelegateCommand(IntervalSure);
public DelegateCommand<object> RealTimeDataCommand => new DelegateCommand<object>(RealTimeData);
Dictionary<string,int> subNameList;
private void Loaded()
{
//N08,N25,N26
string station = "N02,N05,N06,N08,N09,N10,N11,N12,N13,N14,N15,N16,N19,N20,N22,N23,N24,N25,N26";//,N07,N08,N09,N10,N11,N12,N13,N14,N15,N16,N17,N18,N19,N20,N21,N22,N23,N24,N25,N26,N27";
var stations = station.Split(',');
smList = _wavesModel.ReadWavesFromJson(stations);
}
private void UnLoaded()
{
}
private void AxesYVisible(object isCheck)
{
if (_lChartAll != null)
{
bool yAxesVisible = ((bool)isCheck == true);
_lChartAll.BeginUpdate();
foreach (AxisY yAxis in _lChartAll.ViewXY.YAxes)
{
yAxis.Visible = yAxesVisible;
}
_lChartAll.EndUpdate();
}
}
private void FileSelector(object obj)
{
IsMultFiles = Convert.ToBoolean(obj);
OpenFileDialog openFileDialog = new OpenFileDialog
{
RestoreDirectory = true,
Filter = ".mseed|*.mseed|.json|*.json|.txt|*.txt",
};
if (openFileDialog.ShowDialog() == true)
{
string asciiSavePath = openFileDialog.FileName.Replace("Mseed", "Txt");
asciiSavePath = Path.ChangeExtension(asciiSavePath, ".txt");
string JsonPath = Path.ChangeExtension(openFileDialog.FileName, "Json");
Stopwatch st = new Stopwatch();
st.Start();
//读取.json文件
CurrentEventTime = _wavesModel.ReadChartJsonFile(JsonPath);
//读取.mseed文件
smList = _wavesModel.ReadMseedFile(openFileDialog.FileName, asciiSavePath, IsMultFiles);
_channelCount = smList.Count * 3;
CurrentTime = smList.First().BeginTime.ToShortDateString();
StationsName = smList.Select(a => new StationAxis{Name= a.Name,IsChecked=true,SelectCommand =new DelegateCommand<object>(StationsNameVisible)}).ToList();
StartChart();
st.Stop();
Debug.WriteLine("统计时间StartChart************{0}", st.Elapsed);
FeedDatasToChart();
if (CurrentEventTime != null)
{
_wavesModel.CreateAxisYEventTime(CurrentEventTime, LChartALL.ViewXY, smList.ToList());
}
}
}
private void OtimeSort(object isCheck)
{
if (_lChartAll != null&& (_chartAxisY.Count!=0))
{
LChartALL.BeginUpdate();
bool yAxesVisible = ((bool)isCheck == true);
ViewXY v = _lChartAll.ViewXY;
IOrderedEnumerable<LineCollection> lines;
if (yAxesVisible)
{
lines = v.LineCollections.Where(a => a.Tag != null && a.Tag.ToString().Contains("SHZ")).OrderBy(a => a.Lines[0].AX);
}
else
{
lines = v.LineCollections.Where(a => a.Tag != null && a.Tag.ToString().Contains("SHZ")).OrderByDescending(b => b.Lines[0].AX); ;
}
int number = v.YAxes.Count / (_chartAxisY.Count / 3);
foreach (var item in lines.Reverse())
{
int index = item.AssignYAxisIndex;
for (int i = 0; i < number; i++)
{
if (index == -1) continue; //隐藏轴不需要处理
AxisY axisY = v.YAxes[index + i];
v.YAxes.RemoveAt(index + i);
v.YAxes.Insert(i, axisY);
}
}
LChartALL.EndUpdate();
}
}
/// <summary>
/// Y轴的显示和隐藏
/// </summary>
/// <param name="content">SHZ,SHN,SHE</param>
/// <param name="yAxesVisible">true 隐藏轴false显示轴</param>
private void YasixZENVisible(string content, bool yAxesVisible)
{
if (_lChartAll != null)
{
_lChartAll.BeginUpdate();
ViewXY v = _lChartAll.ViewXY;
string conStr = (content == "SHZ") ? ".Z" : (content == "SHN") ? ".N" : ".E";
for (int i = 0; i <_chartAxisY.Count; i++)
{
AxisY axisY = _chartAxisY[i];
if (axisY.Title.Text.Contains(conStr))
{
if (yAxesVisible)
{
//隐藏轴
v.YAxes.Remove(axisY);
}
else
{
//显示轴
var axis= v.YAxes.Where(y => y.Title.Text==axisY.Title.Text).FirstOrDefault();
if (axis != null)
{
//找到相同的轴不用处理
continue;
}
string str = axisY.Title.Text.Replace(conStr, "");
int yIndex = 0;
if (content == "SHZ")
{
yIndex = v.YAxes.FindIndex(y => y.Title.Text.Contains(str));
}
else if (content == "SHE")
{
yIndex = v.YAxes.FindLastIndex(y => y.Title.Text.Contains(str));
if (yIndex != -1) yIndex += 1;
}
else
{
yIndex = v.YAxes.FindIndex(y => y.Title.Text.Contains(str));
if (yIndex != -1)
{
if (v.YAxes[yIndex].Title.Text.Contains(".Z"))
{
yIndex = yIndex + 1;
}
}
}
//如果没有查到,直接添加到前面,如果为容器最大索引,只能添加到后面
if (yIndex == -1 || (yIndex == v.YAxes.Count))
{
yIndex = v.YAxes.Count;
v.YAxes.Add(axisY);
}
else
{
v.YAxes.Insert(yIndex, axisY);
}
int sampleIndex = v.SampleDataSeries.FindIndex(y => y.Title.Text == _chartAxisY[i].Title.Text);
v.SampleDataSeries[sampleIndex].AssignYAxisIndex = yIndex;
int lineIndex = v.LineCollections.FindIndex(a => a.Title.Text.Contains(_chartAxisY[i].Title.Text));
if (lineIndex != -1)
{
v.LineCollections[lineIndex].AssignYAxisIndex = yIndex;
}
}
}
}
_lChartAll.EndUpdate();
}
}
private void IntervalSure()
{
if (_lChartAll != null)
{
_lChartAll.BeginUpdate();
if (XaisInterval >= 2)
{
_lChartAll.ViewXY.XAxes[0].MajorDivCount = XaisInterval;
}
_lChartAll.EndUpdate();
}
}
private void StationsNameVisible(object obj)
{
var station = obj as StationAxis;
if (_lChartAll != null)
{
_lChartAll.BeginUpdate();
bool yAxesVisible = (station.IsChecked == false);
ViewXY v = _lChartAll.ViewXY;
var yAxisList= _chartAxisY.Where(y => y.Title.Text.Contains(station.Name));
foreach (var item in yAxisList)
{
if (yAxesVisible)
{
v.YAxes.Remove(item);
}
else
{
v.YAxes.Add(item);
//SampleDataSeries查找数据对应的Y轴索引, 重新分配到对应的轴
int sampleIndex = v.SampleDataSeries.FindIndex(s => s.Title.Text == item.Title.Text);
v.SampleDataSeries[sampleIndex].AssignYAxisIndex = v.YAxes.Count()-1;
//LineCollections查找数据对应的Y轴索引, 重新分配到对应的轴
int lineIndex = v.LineCollections.FindIndex(s => s.Title.Text.Contains(item.Title.Text));
if (lineIndex != -1)
{
v.LineCollections[lineIndex].AssignYAxisIndex = v.YAxes.Count() - 1;
}
}
}
_lChartAll.EndUpdate();
}
}
private void RealTimeData(object isCheck)
{
_channelCount = smList.Count * 3;
CurrentTime = smList.First().BeginTime.ToShortDateString();
StationsName = smList.Select(a => new StationAxis { Name = a.Name, IsChecked = true, SelectCommand = new DelegateCommand<object>(StationsNameVisible) }).ToList();
if (_lChartAll != null)
{
bool isStartRealData = (Convert.ToBoolean(isCheck) == true);
if (smList.Count == 0)
{
return;
}
_lChartAll.BeginUpdate();
if (isStartRealData)
{
_data = CreateInputData(_channelCount, _samplingFrequency);
StartChart();
//Set real-time monitoring automatic old data destruction
LChartALL.ViewXY.DropOldSeriesData = true;
_pointsAppended = LChartALL.ViewXY.XAxes[0].Minimum;
dispatcherTimer.Tick -= CompositionTarget_Rendering;
dispatcherTimer.Tick += CompositionTarget_Rendering;
LChartALL.ViewXY.XAxes[0].ScrollMode = XAxisScrollMode.Scrolling;
dispatcherTimer.Start();
using (RedisListService service = new RedisListService())
{
foreach (var subName in subNameList)
{
service.UnSubscribeFromChannels(subName.Key);
}
}
foreach (var subName in subNameList)
{
Task.Run(() =>
{
Debug.WriteLine($"注册{1}:{subName.Key}");
using (RedisListService service = new RedisListService())
{
service.Subscribe(subName.Key, (c, message, iRedisSubscription) =>
{
Debug.WriteLine($"注册{1}{c}");
Mseed2asciiApi.bufferMseedData(message.Length, message);
});
}
});
}
}
else
{
dispatcherTimer.Stop();
LChartALL.ViewXY.XAxes[0].ScrollMode = XAxisScrollMode.None;
dispatcherTimer.Tick -= CompositionTarget_Rendering;
using (RedisListService service = new RedisListService())
{
foreach (var subName in subNameList)
{
service.UnSubscribeFromChannels(subName.Key);
}
}
}
_lChartAll.EndUpdate();
}
}
private void Mseed2AsciiEvent(AsciiDataStruct asciiData)
{
byte[] bytes = new byte[asciiData.numsamples * asciiData.samplesize];
// IntPtr bufferHandler = Marshal.AllocHGlobal((int)asciiData.datasize);
Marshal.Copy(asciiData.datasamples, bytes, 0, bytes.Length);
List<double> lines = new List<double>();
string[] sid = asciiData.sid.Split('_');
string name = $"HA.{sid[1]}.{sid[2]}.{sid[3]}{sid[4]}{sid[5]}";
if (asciiData.sampletype == 'i')
{
for (int index = 0; index < bytes.Length; index += 4)
{
var a = BitConverter.ToInt32(bytes, index);
lines.Add(a);
}
}
string lineStr= String.Join(",", lines);
//else if (asciiData.sampletype == 'f')
//{
// for (int index = 0; index < bytes.Length; index += 4)
// {
// var b = BitConverter.ToSingle(bytes, index);
// lines.Add(b);
// }
//}
//else if (asciiData.sampletype == 'd')
//{
// for (int index = 0; index < bytes.Length; index += 8)
// {
// var b = BitConverter.ToDouble(bytes, index);
// lines.Add(b);
// }
//}
using (RedisListService service = new RedisListService())
{
// string data = System.Text.Encoding.ASCII.GetString(bytes);
service.RPush(name, lineStr);
}
}
#endregion
private void CreateChart()
{
if (LChartALL != null)
{
LChartALL = null;
}
LChartALL = new LightningChart();
LChartALL.Title.Align = ChartTitleAlignment.TopCenter;
LChartALL.ChartRenderOptions.DeviceType = RendererDeviceType.AutoPreferD11;
LChartALL.ChartRenderOptions.LineAAType2D = LineAntiAliasingType.QLAA;
LChartALL.Title.Text = "事件波形";
LChartALL.Title.Font = new WpfFont("等线", 20);
//轴属性和布局
LChartALL.ViewXY.AxisLayout.YAxesLayout = YAxesLayout.Stacked;
LChartALL.ViewXY.AxisLayout.SegmentsGap = 0;
LChartALL.ViewXY.ZoomPanOptions.PanDirection = PanDirection.Horizontal;
LChartALL.ViewXY.ZoomPanOptions.WheelZooming = WheelZooming.Horizontal;
LChartALL.ViewXY.AxisLayout.YAxisAutoPlacement = YAxisAutoPlacement.AllRight;
LChartALL.ViewXY.AxisLayout.YAxisTitleAutoPlacement = false;
LChartALL.ViewXY.AxisLayout.AutoAdjustMargins = false;
LChartALL.ViewXY.ZoomPanOptions.DevicePrimaryButtonAction = UserInteractiveDeviceButtonAction.Pan;
LChartALL.ViewXY.ZoomPanOptions.DeviceSecondaryButtonAction = UserInteractiveDeviceButtonAction.None;
LChartALL.ViewXY.ZoomPanOptions.WheelZooming = WheelZooming.Horizontal;
// 反锯齿系数。值0和1都不会应用反锯齿
LChartALL.ChartRenderOptions.AntiAliasLevel = 0;
//X轴设置
LChartALL.ViewXY.XAxes[0].AllowScrolling = true;
LChartALL.ViewXY.XAxes[0].PanningEnabled = true;
LChartALL.ViewXY.XAxes[0].MajorGrid.Pattern = LinePattern.Solid;
LChartALL.ViewXY.XAxes[0].ValueType = AxisValueType.DateTime;
LChartALL.ViewXY.XAxes[0].KeepDivCountOnRangeChange = true;
//LChartALL.ViewXY.XAxes[0].MajorDiv =6;
LChartALL.ViewXY.XAxes[0].LabelsColor = Colors.Black;
LChartALL.ViewXY.XAxes[0].AutoDivSpacing = false;
LChartALL.ViewXY.XAxes[0].MajorDivCount = 5;
XaisInterval = 5;
//图表背景颜色
LChartALL.ViewXY.GraphBackground.Color = Colors.White;
LChartALL.ViewXY.GraphBackground.GradientColor = Colors.White;
LChartALL.ChartBackground.GradientFill = GradientFill.Solid;
LChartALL.ChartBackground.Color = Color.FromArgb(0, 0, 0, 0);
GridChart = new Grid();
GridChart.ColumnDefinitions.Add(new ColumnDefinition());
GridChart.Name = "chartGrid";
GridChart.Children.Add(LChartALL);
this.ChildContent = GridChart;
}
private void StartChart()
{
DisposeAllAndClear(_chartAxisY);
subNameList = new Dictionary<string, int>();
ViewXY v = LChartALL.ViewXY;
LChartALL.SizeChanged -= LChartALL_SizeChanged;
LChartALL.SizeChanged += LChartALL_SizeChanged;
LChartALL.ViewXY.Zoomed -= ViewXY_Zoomed;
LChartALL.ViewXY.Zoomed += ViewXY_Zoomed;
LChartALL.ViewXY.Panned -= ViewXY_Panned;
LChartALL.ViewXY.Panned += ViewXY_Panned;
DisposeAllAndClear(v.YAxes);
DisposeAllAndClear(v.SampleDataSeries);
DisposeAllAndClear(v.LineCollections);
v.Margins=new Thickness(60, 30, 60, 60);
v.YAxes.AddRange(_wavesModel.CreateYAxisChart(smList.ToList(), LChartALL));
v.LegendBoxes[0].Position = LegendBoxPositionXY.RightCenter;
v.LegendBoxes[0].Offset.SetValues(-10, 180);
v.LegendBoxes[0].Layout = LegendBoxLayout.Vertical;
v.LegendBoxes[0].Visible = false;
v.LegendBoxes[0].Shadow.Color = Colors.Transparent;
v.LegendBoxes[0].BorderColor = Colors.Transparent;
v.AutoSpaceLegendBoxes = true;
v.AxisLayout.SegmentsGap = 3;
v.LegendBoxes[0].Shadow.Visible = false;
StationModel stationModel = smList.First();
var beginTime= stationModel.BeginTime;
if (AxisValueType.DateTime == v.XAxes[0].ValueType)
{
//设置X轴的开始时间
v.XAxes[0].AutoFormatLabels = false;
v.XAxes[0].LabelsTimeFormat = "HH:mm:ss.ff";
v.XAxes[0].DateOriginYear = beginTime.Year;
v.XAxes[0].DateOriginDay = beginTime.Day;
v.XAxes[0].DateOriginMonth = beginTime.Month;
DateTime MaxDateTime = beginTime.AddSeconds(_samplingFrequency == 500 ? 30 : 60);
v.XAxes[0].SetRange(v.XAxes[0].DateTimeToAxisValue(beginTime), v.XAxes[0].DateTimeToAxisValue(MaxDateTime));
}
double firstSampleTimeStamp = v.XAxes[0].DateTimeToAxisValue(beginTime);
int count = stationModel.Dzne.Count;
int number= smList.Count*3;
for (int i = 0; i < number; i++)
{
int seriesIndex = i;
AxisY axisY = v.YAxes[seriesIndex];
axisY.LabelsColor = Colors.Black;
axisY.Title.Shadow.DropColor = Colors.Transparent;
// axisY.Title.Shadow.ContrastColor = Colors.Transparent;
axisY.Title.AllowDragging = true;
axisY.AllowAutoYFit = true;
axisY.Units.Visible = false;
axisY.LabelsFont.Size = 10;
axisY.Title.Angle = 0;
axisY.MinorGrid.Visible = false;
axisY.AutoDivSpacing = false;
axisY.MajorDivCount = 2;
// axisY.MajorDiv = 1;
axisY.MinorDivTickStyle.Visible = false;
axisY.MajorGrid.Visible = true;
axisY.MajorGrid.Pattern = LinePattern.Solid;
axisY.MajorDivTickStyle.Alignment = seriesIndex % 2 == 0 ? Alignment.Near : Alignment.Far;
// axisY.MajorDivTickStyle.LineLength = 6;
axisY.MajorDivTickStyle.Color = Colors.Black;
axisY.Title.Color = Colors.Black;
axisY.Title.Font = new WpfFont("Segoe UI", 10, false, false);
axisY.PanningEnabled = false;
axisY.AllowScrolling = true;
axisY.AllowScaling = true;
SampleDataSeries series = new SampleDataSeries(v, v.XAxes[0], axisY);
series.ShowInLegendBox = false;
series.FirstSampleTimeStamp = firstSampleTimeStamp;
series.SamplingFrequency = _samplingFrequency;
series.Title.Text = axisY.Title.Text;
series.SampleFormat = SampleFormat.DoubleFloat;
string subName = string.Empty;
string name = axisY.Title.Text.Substring(2,3);
if (seriesIndex % 3 == 0)
{
subName = $"HA.{name}.06.SHZ";
series.LineStyle.Color = System.Windows.Media.Colors.DeepSkyBlue;
}
else if (seriesIndex % 3 == 1)
{
subName = $"HA.{name}.06.SHN";
series.LineStyle.Color = System.Windows.Media.Colors.OrangeRed;
}
else if (seriesIndex % 3 == 2)
{
subName = $"HA.{name}.06.SHE";
series.LineStyle.Color = System.Windows.Media.Colors.ForestGreen;
}
subNameList.Add(subName,i);
series.LineStyle.Width = 0.2;
series.ScrollModePointsKeepLevel = 1;
//series.ScrollingStabilizing = true;
v.SampleDataSeries.Add(series);
_chartAxisY.Add(axisY);
}
}
private void ViewXY_Panned(object sender, PannedXYEventArgs e)
{
foreach (var item in LChartALL.ViewXY.LineCollections)
{
double b = LChartALL.ViewXY.XAxes[0].ValueToCoordD(item.Lines[0].AX) - 80;
item.Title.Offset.SetValues((int)b, 3);
}
}
private void ViewXY_Zoomed(object sender, ZoomedXYEventArgs e)
{
foreach (var item in LChartALL.ViewXY.LineCollections)
{
double b = LChartALL.ViewXY.XAxes[0].ValueToCoordD(item.Lines[0].AX) - 80;
item.Title.Offset.SetValues((int)b, 3);
}
}
private void LChartALL_SizeChanged(object sender, SizeChangedEventArgs e)
{
int distancetoX = _wavesModel.SetYasixPlacement(LChartALL);
foreach (var item in LChartALL.ViewXY.YAxes)
{
item.Title.DistanceToAxis = distancetoX;
}
foreach (var item in LChartALL.ViewXY.LineCollections)
{
double b = LChartALL.ViewXY.XAxes[0].ValueToCoordD(item.Lines[0].AX)-80;
item.Title.Offset.SetValues((int)b, 3);
}
}
private void FeedDatasToChart()
{
_data = new double[smList.Count * 3][];
try
{
for (int channelIndex = 0; channelIndex < smList.Count; channelIndex++)
{
_data[channelIndex * 3] = smList.ElementAt(channelIndex).dz.ToArray();
_data[channelIndex * 3 + 1] = smList.ElementAt(channelIndex).dn.ToArray();
_data[channelIndex * 3 + 2] = smList.ElementAt(channelIndex).de.ToArray();
}
// Invoke FeedNewDataToChart.
_dispatcher.Invoke(() =>
{
LChartALL.BeginUpdate();
for (int channelIndex = 0; channelIndex < _channelCount; channelIndex++)
{
LChartALL.ViewXY.SampleDataSeries[channelIndex].AddSamples(_data[channelIndex], true);
}
LChartALL.EndUpdate();
});
}
catch (Exception ex)
{
throw ex;
}
}
private void ApplicationClosingDispose(object sender, CancelEventArgs e)
{
if (LChartALL != null)
{
LChartALL.Dispose();
LChartALL = null;
}
}
private void DisposeAllAndClear<T>(List<T> list) where T : IDisposable
{
if (list == null)
{
return;
}
while (list.Count > 0)
{
int lastInd = list.Count - 1;
T item = list[lastInd]; // take item ref from list.
list.RemoveAt(lastInd); // remove item first
if (item != null)
{
(item as IDisposable).Dispose(); // then dispose it.
}
}
}
public void Dispose()
{
gridChart.Children.Clear();
if (LChartALL != null)
{
LChartALL.Dispose();
LChartALL = null;
}
}
#region 实时数据
private void CompositionTarget_Rendering(object sender, EventArgs e)
{
RenderNextFrame();
}
private void RenderNextFrame()
{
if (_lChartAll == null)
{
return;
}
_dispatcher.Invoke(() =>
{
//FeedData(/*chartTitleText*/);
});
}
int _iRound = 0;
double _pointsAppended = 0;
double[][] _data;
int PreGenerateDataForRoundCount = 6;
//Y axis minimum
private const double YMin = 0;
//Y axis maximum
private const double YMax = 10;
private double[][] CreateInputData(int seriesCount, int appendCountPerRound)
{
//Create input data for all series.
double[][] data = new double[seriesCount][];
// System.Threading.Tasks.Parallel.For(0, seriesCount, (seriesIndex) =>
for (int seriesIndex = 0; seriesIndex < seriesCount; seriesIndex++)
{
int dataPointCount = PreGenerateDataForRoundCount * appendCountPerRound;
double[] seriesData = new double[dataPointCount];
double seriesIndexPlus1 = seriesIndex + 1;
Random rand = new Random((int)DateTime.Now.Ticks / (seriesIndex + 1));
double y = 50;
for (int i = 0; i < dataPointCount; i++)
{
y = y - 0.05 + rand.NextDouble() / 10.0;
if (y > YMax)
{
y = YMax;
}
if (y < YMin)
{
y = YMin;
}
seriesData[i] = (float)y;
}
data[seriesIndex] = seriesData;
}//);
return data;
}
private void FeedData()
{
if (_lChartAll != null)
{
_lChartAll.BeginUpdate();
//Append data to series
using (RedisListService service = new RedisListService())
{
foreach (var item in subNameList)
{
string data= service.BlockingDequeueItemFromList(item.Key, TimeSpan.FromMilliseconds(500));
if(data == null)
{
System.Diagnostics.Debug.WriteLine("***********注册:{0},没有收到数据,time:{1}", item.Key, DateTime.Now);
continue;
}
// byte[] bytes = System.Text.Encoding.ASCII.GetBytes(data);
// List<double> thisSeriesData = new List<double>();
var thisSeriesData = data.Split(',').Select(a=>Convert.ToDouble(a)).ToArray();
// if (asciiData.sampletype == 'i')
{
//for (int index = 0; index < bytes.Length; index += 4)
//{
// int a = BitConverter.ToInt32(bytes, index);
// thisSeriesData.Add(a);
//}
}
_lChartAll.ViewXY.SampleDataSeries[subNameList[item.Key]].AddSamples(thisSeriesData, false);
System.Diagnostics.Debug.WriteLine("***********index:{0}, pointCount:{1},time:{2}", subNameList[item.Key],
_lChartAll.ViewXY.SampleDataSeries[subNameList[item.Key]].PointCount, DateTime.Now);
}
}
//System.Threading.Tasks.Parallel.For(0, _channelCount, (seriesIndex) =>
//{
// double[] thisSeriesData = _data[seriesIndex];
// double[] dataToAppendNow = new double[_samplingFrequency];
// Array.Copy(thisSeriesData, 0, dataToAppendNow, 0, _samplingFrequency);
// System.Diagnostics.Debug.WriteLine("***********index:{0}, pointCount:{1},time:{2}", seriesIndex,
// _lChartAll.ViewXY.SampleDataSeries[seriesIndex].PointCount, DateTime.Now);
//});
_pointsAppended += 1;
//Set X axis real-time scrolling position
double lastX = _pointsAppended;
_lChartAll.ViewXY.XAxes[0].ScrollPosition = lastX;
//Update sweep bands
if (_lChartAll.ViewXY.XAxes[0].ScrollMode == XAxisScrollMode.Sweeping)
{
//Dark band of old page fading away
double pageLen = _lChartAll.ViewXY.XAxes[0].Maximum - _lChartAll.ViewXY.XAxes[0].Minimum;
double sweepGapWidth = pageLen / 20.0;
_lChartAll.ViewXY.Bands[0].SetValues(lastX - pageLen, lastX - pageLen + sweepGapWidth);
if (_lChartAll.ViewXY.Bands[0].Visible == false)
{
_lChartAll.ViewXY.Bands[0].Visible = true;
}
//Bright new page band
_lChartAll.ViewXY.Bands[1].SetValues(lastX - sweepGapWidth / 6, lastX);
if (_lChartAll.ViewXY.Bands[1].Visible == false)
{
_lChartAll.ViewXY.Bands[1].Visible = true;
}
}
else
{
//Hide sweeping bands if not in sweeping mode
//if (_lChartAll.ViewXY.Bands[0].Visible == true)
//{
// _lChartAll.ViewXY.Bands[0].Visible = false;
//}
//if (_lChartAll.ViewXY.Bands[1].Visible == true)
//{
// _lChartAll.ViewXY.Bands[1].Visible = false;
//}
}
_lChartAll.EndUpdate();
_iRound++;
}
}
public void OnNavigatedTo(NavigationContext navigationContext)
{
var str= navigationContext.Parameters.GetValue<string>("model");
if(str == "实时波形")
{
IsRealtimeData = true;
}
else
{
IsRealtimeData = false;
}
}
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
public void OnNavigatedFrom(NavigationContext navigationContext)
{
}
#endregion
}
}