Files
MetroGauges-Old/MetroGauges/WpfPreview.xaml.cs

385 lines
15 KiB
C#
Raw Permalink Normal View History

2026-02-23 17:02:55 +08:00
using MetroGauges.General;
using MetroGauges.Model;
using MetroGauges.ViewModel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace MetroGauges
{
/// <summary>
/// WpfPreview.xaml 的交互逻辑
/// </summary>
public partial class WpfPreview : Window
{
private PointCollection othCdPts;
private PointCollection pgCdPts;
private PointCollection revOthCdPts;
private PointCollection revPgCdPts;
public WpfPreview()
{
InitializeComponent();
}
public WpfPreview(DgViewModel<KineModel> k)
{
InitializeComponent();
DrawScale();
DrawScaleLabel();
DrawKinePoint(k);
DrawCurve();
//DrawArrow();
//chartCanvas.Children.Add(DrawPLGraph(vm));
}
public WpfPreview(DgViewModel<LiEquiModel> l)
{
InitializeComponent();
DrawScale();
DrawScaleLabel();
DrawLiEquiPoint(l);
DrawCurve();
//DrawArrow();
//chartCanvas.Children.Add(DrawPLGraph(vm));
}
public WpfPreview(DgViewModel<CurEquiModel> c)
{
InitializeComponent();
DrawScale();
DrawScaleLabel();
DrawCurEquiPoint(c);
DrawCurve();
//DrawArrow();
//chartCanvas.Children.Add(DrawPLGraph(vm));
}
private void WindowClose_Click(object sender, RoutedEventArgs e)
{
Close();
}
//private Polyline DrawPLGraph(DataViewModel vm)
//{
// PointCollection ptCol = new PointCollection();
// for (int i = 0; i < vm.Items.Count; i++)
// {
// DataModel dm = vm.Items[i];
// if (true)
// {
// }
// System.Windows.Point pt = new System.Windows.Point(dm.X / 10, -dm.Y / 10);
// ptCol.Add(pt);
// }
// RotateTransform rotateTransform = new RotateTransform(0);
// TranslateTransform transTransform = new TranslateTransform(350, 550);
// TransformGroup group = new TransformGroup();
// group.Children.Add(transTransform);
// group.Children.Add(rotateTransform);
// Polyline pl = new Polyline()
// {
// Points = ptCol,
// StrokeThickness = 2,
// Stroke = new SolidColorBrush(Colors.Red),
// RenderTransformOrigin = new System.Windows.Point(0, 0),
// RenderTransform = group
// };
// //pl.RenderTransform = rotateTransform;
// //pl.RenderTransform = transTransform;
// return pl;
//}
/// <summary>
/// 作出箭头
/// </summary>
//private void DrawArrow()
//{
// Path x_axisArrow = new Path();//x轴箭头
// Path y_axisArrow = new Path();//y轴箭头
// x_axisArrow.Fill = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));
// y_axisArrow.Fill = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));
// PathFigure x_axisFigure = new PathFigure();
// x_axisFigure.IsClosed = true;
// x_axisFigure.StartPoint = new System.Windows.Point(480, 276); //路径的起点
// x_axisFigure.Segments.Add(new LineSegment(new System.Windows.Point(480, 284), false)); //第2个点
// x_axisFigure.Segments.Add(new LineSegment(new System.Windows.Point(490, 280), false)); //第3个点
// PathFigure y_axisFigure = new PathFigure();
// y_axisFigure.IsClosed = true;
// y_axisFigure.StartPoint = new System.Windows.Point(36, 30); //路径的起点
// y_axisFigure.Segments.Add(new LineSegment(new System.Windows.Point(44, 30), false)); //第2个点
// y_axisFigure.Segments.Add(new LineSegment(new System.Windows.Point(40, 20), false)); //第3个点
// PathGeometry x_axisGeometry = new PathGeometry();
// PathGeometry y_axisGeometry = new PathGeometry();
// x_axisGeometry.Figures.Add(x_axisFigure);
// y_axisGeometry.Figures.Add(y_axisFigure);
// x_axisArrow.Data = x_axisGeometry;
// y_axisArrow.Data = y_axisGeometry;
// this.chartCanvas.Children.Add(x_axisArrow);
// this.chartCanvas.Children.Add(y_axisArrow);
//}
/// <summary>
/// 绘制点
/// </summary>
/// <param name="vm"></param>
private void DrawKinePoint(DgViewModel<KineModel> vm)
{
othCdPts = new PointCollection();
pgCdPts = new PointCollection();
revOthCdPts = new PointCollection();
revPgCdPts = new PointCollection();
ObservableCollection<KineModel> dm = vm.Items;
for (int i = 0; i < dm.Count; i++)
{
if (dm[i].Position == PositionI.Pantograph)
{
pgCdPts.Add(new System.Windows.Point(350 + dm[i].X / 10, 550 - dm[i].Y / 10));
revPgCdPts.Add(new System.Windows.Point(350 - dm[i].X / 10, 550 - dm[i].Y / 10));
}
else
{
othCdPts.Add(new System.Windows.Point(350 + dm[i].X / 10, 550 - dm[i].Y / 10));
revOthCdPts.Add(new System.Windows.Point(350 - dm[i].X / 10, 550 - dm[i].Y / 10));
}
}
for (int i = 0; i < dm.Count; i++)
{
Ellipse dataEllipse = new Ellipse
{
Fill = new SolidColorBrush(Color.FromRgb(28, 120, 135)),
Width = 8,
Height = 8,
ToolTip = dm[i].Name + "(" + dm[i].X + "," + dm[i].Y + ")",
//ToolTip = String.Format("{0}{1}{2}{3}{4}{5}", dm[i].Name, "(", dm[i].X, ",", dm[i].Y, ")")
};
Canvas.SetLeft(dataEllipse, 350 + dm[i].X / 10 - 4);//-4是为了补偿圆点的大小到精确的位置
Canvas.SetTop(dataEllipse, 550 - dm[i].Y / 10 - 4);
chartCanvas.Children.Add(dataEllipse);
}
//else
//{
// ObservableCollection<LiEquiModel> dm = ((LiEquiViewModel)vm).Items;
//}
}
private void DrawLiEquiPoint(DgViewModel<LiEquiModel> vm)
{
othCdPts = new PointCollection();
pgCdPts = new PointCollection();
revOthCdPts = new PointCollection();
revPgCdPts = new PointCollection();
ObservableCollection<LiEquiModel> dm = vm.Items;
for (int i = 0; i < dm.Count; i++)
{
if (dm[i].Position2 == PositionII.Pantograph)
{
pgCdPts.Add(new System.Windows.Point(350 + dm[i].X / 10, 550 - dm[i].Y / 10));
revPgCdPts.Add(new System.Windows.Point(350 - dm[i].X / 10, 550 - dm[i].Y / 10));
}
else
{
othCdPts.Add(new System.Windows.Point(350 + dm[i].X / 10, 550 - dm[i].Y / 10));
revOthCdPts.Add(new System.Windows.Point(350 - dm[i].X / 10, 550 - dm[i].Y / 10));
}
}
for (int i = 0; i < dm.Count; i++)
{
Ellipse dataEllipse = new Ellipse
{
Fill = new SolidColorBrush(Color.FromRgb(28, 120, 135)),
Width = 8,
Height = 8,
ToolTip = dm[i].Name + "(" + dm[i].X + "," + dm[i].Y + ")",
//ToolTip = String.Format("{0}{1}{2}{3}{4}{5}", dm[i].Name, "(", dm[i].X, ",", dm[i].Y, ")")
};
Canvas.SetLeft(dataEllipse, 350 + dm[i].X / 10 - 4);//-4是为了补偿圆点的大小到精确的位置
Canvas.SetTop(dataEllipse, 550 - dm[i].Y / 10 - 4);
chartCanvas.Children.Add(dataEllipse);
}
//else
//{
// ObservableCollection<LiEquiModel> dm = ((LiEquiViewModel)vm).Items;
//}
}
private void DrawCurEquiPoint(DgViewModel<CurEquiModel> vm)
{
othCdPts = new PointCollection();
pgCdPts = new PointCollection();
revOthCdPts = new PointCollection();
revPgCdPts = new PointCollection();
ObservableCollection<CurEquiModel> dm = vm.Items;
for (int i = 0; i < dm.Count; i++)
{
if (dm[i].Position == PositionI.Pantograph)
{
pgCdPts.Add(new System.Windows.Point(350 + dm[i].X / 10, 550 - dm[i].Y / 10));
revPgCdPts.Add(new System.Windows.Point(350 - dm[i].X / 10, 550 - dm[i].Y / 10));
}
else
{
othCdPts.Add(new System.Windows.Point(350 + dm[i].X / 10, 550 - dm[i].Y / 10));
revOthCdPts.Add(new System.Windows.Point(350 - dm[i].X / 10, 550 - dm[i].Y / 10));
}
}
for (int i = 0; i < dm.Count; i++)
{
Ellipse dataEllipse = new Ellipse
{
Fill = new SolidColorBrush(Color.FromRgb(28, 120, 135)),
Width = 8,
Height = 8,
ToolTip = dm[i].Name + "(" + dm[i].X + "," + dm[i].Y + ")",
//ToolTip = String.Format("{0}{1}{2}{3}{4}{5}", dm[i].Name, "(", dm[i].X, ",", dm[i].Y, ")")
};
Canvas.SetLeft(dataEllipse, 350 + dm[i].X / 10 - 4);//-4是为了补偿圆点的大小到精确的位置
Canvas.SetTop(dataEllipse, 550 - dm[i].Y / 10 - 4);
chartCanvas.Children.Add(dataEllipse);
}
//else
//{
// ObservableCollection<LiEquiModel> dm = ((LiEquiViewModel)vm).Items;
//}
}
private void DrawPolyline(PointCollection ptcol, Brush brush)
{
Polyline pl = new Polyline
{
Stroke = brush,
StrokeThickness = 2,
Points = ptcol,
};
chartCanvas.Children.Add(pl);
}
/// <summary>
/// 绘制曲线
/// </summary>
private void DrawCurve()
{
DrawPolyline(pgCdPts, Brushes.Blue);
DrawPolyline(othCdPts, Brushes.Blue);
DrawPolyline(revPgCdPts, Brushes.Blue);
DrawPolyline(revOthCdPts, Brushes.Blue);
}
/// <summary>
/// 作出x轴和y轴的标尺
/// </summary>
private void DrawScale()
{
for (int i = 0; i < 33; i += 1)//作480个刻度因为当前x轴长 480px每10px作一个小刻度还预留了一些小空间
{
//原点 O=(350,550)
Line x_scale = new Line
{
StrokeEndLineCap = PenLineCap.Triangle,
StrokeThickness = 1,
Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0)),
X1 = 350 + i * 10 //原点x=10,每10px作1个刻度
};
x_scale.X2 = x_scale.X1; //在x轴上的刻度线起点和终点相同
x_scale.Y1 = 550; //与原点坐标的y=550相同
if (i % 5 == 0)//每5个刻度添加一个大刻度
{
x_scale.StrokeThickness = 3;//把刻度线加粗一点
x_scale.Y2 = x_scale.Y1 - 8;//刻度线长度为8px
}
else
{
x_scale.Y2 = x_scale.Y1 - 4;//刻度线长度为4px
}
chartCanvas.Children.Add(x_scale);
}
for (int i = 0; i < 53; i += 1)//由于y轴短一些所以在此作出判断只作25个刻度
{
//作出Y轴的刻度
Line y_scale = new Line
{
StrokeEndLineCap = PenLineCap.Triangle,
StrokeThickness = 1,
Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0)),
X1 = 350 //原点x=350在y轴上的刻度线的起点与原点相同
};
if (i % 5 == 0)
{
y_scale.StrokeThickness = 3;
y_scale.X2 = y_scale.X1 + 8;//刻度线长度为4px
}
else
{
y_scale.X2 = y_scale.X1 + 4;//刻度线长度为8px
}
y_scale.Y1 = 550 - i * 10; //每10px作一个刻度
y_scale.Y2 = y_scale.Y1; //起点和终点y坐标相同
chartCanvas.Children.Add(y_scale);
}
}
/// <summary>
/// 添加刻度标签
/// </summary>
private void DrawScaleLabel()
{
for (int i = 1; i < 7; i++)//7 个标签,一共
{
TextBlock x_ScaleLabel = new TextBlock
{
Text = (i * 500).ToString()//只给大刻度添加标签每50px添加一个标签
};
Canvas.SetLeft(x_ScaleLabel, 350 + 5 * 10 * i - 12);//350是原点的坐标-12是为了让标签看的位置居中一些
Canvas.SetTop(x_ScaleLabel, 550 + 2);//让标签字往下移一点
this.chartCanvas.Children.Add(x_ScaleLabel);
}
for (int i = 1; i < 15; i++)
{
TextBlock y_ScaleLabel = new TextBlock
{
Text = (i * 500).ToString()
};
Canvas.SetLeft(y_ScaleLabel, 350 - 35); //-35px是字体大小的偏移
Canvas.SetTop(y_ScaleLabel, 550 - 5 * 10 * i - 6); //650px是原点的坐标同样-6是为了让标签不要上坐标轴叠上
this.chartCanvas.Children.Add(y_ScaleLabel);
}
}
private void ChartCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
}
}