/// <summary> /// 使用配置的颜色属性重新绘制折线图。 /// </summary> public void DrawChart() { ClearChart(); if (XAxis == null || YAxis == null || XAxis.Count() == 0 || YAxis.Count() == 0) { return; } //绘制坐标 var xCellWidth = (Width - _yAxisWidth) / XAxis.Count(); var yCellHeight = (Height - _xAxisHeight) / YAxis.Count(); DrawAxis(xCellWidth, yCellHeight); //绘制网格线 var canvasWidth = Width - _yAxisWidth; var canvasHeight = Height - _xAxisHeight; DrawGrid(xCellWidth, yCellHeight, canvasWidth, canvasHeight); //绘制点和线 if (Values == null) { return; } DrawPointAndLineAndArea(xCellWidth, yCellHeight); }
/// <summary> /// 绘制网格。 /// </summary> internal void DrawGrid(double xCellWidth, double yCellHeight, double canvasWidth, double canvasHeight) { var path = ""; for (int i = 0; i < XAxis.Count(); i++) { path += $"M {xCellWidth * i},0 V {canvasHeight}"; } for (int i = 0; i < YAxis.Count(); i++) { path += $"M 0,{canvasHeight - yCellHeight * i} H {canvasWidth}"; } gridPath.Data = Geometry.Parse(path); }
/// <summary> /// 绘制点、线和区域 /// </summary> internal void DrawPointAndLineAndArea(double xCellWidth, double yCellHeight) { ScaleTransform scale; if (UsingAnimation) { scale = new ScaleTransform() { ScaleY = 0 } } ; else { scale = new ScaleTransform() { ScaleY = 1 } }; polygon.RenderTransform = scale; polyline.RenderTransform = scale; polygon.Points.Add(new Point(0, Height)); var xAxis = 0.0; for (int i = 0; i < Values.Count(); i++) { var value = Values[i]; var point = new Point(xAxis, (Height - _xAxisHeight - yCellHeight) * (1 - value) + yCellHeight); polyline.Points.Add(point); polygon.Points.Add(point); var ellipse = new Ellipse() { Width = 10, Height = 10, Margin = new Thickness(-5, -5, 0, 0), Fill = LineBrush, ToolTip = ValueTips == null ? null : ValueTips.Count() > i ? ValueTips[i] : null, }; if (UsingAnimation) { Canvas.SetTop(ellipse, Height - _xAxisHeight); ellipse.BeginAnimation(Canvas.TopProperty, GetDoubleAnimation((Height - _xAxisHeight - yCellHeight) * (1 - value) + yCellHeight, 1)); } else { Canvas.SetTop(ellipse, (Height - _xAxisHeight - yCellHeight) * (1 - value) + yCellHeight); } Canvas.SetLeft(ellipse, xAxis); pointCanvas.Children.Add(ellipse); xAxis += xCellWidth; if (xAxis / xCellWidth >= XAxis.Count()) { break; } } polygon.Points.Add(new Point((xAxis - xCellWidth), Height)); scale.BeginAnimation(ScaleTransform.ScaleYProperty, GetDoubleAnimation(1, 1)); }