WPF繪製折線圖
繪製折線圖的函式如下:
protected void DrawingLine(double drawwidth, double drawheight,double[] value,DateTime[] datetime)
{
double farTop = 10;
double farBottom = 10;
double farLeft = 10;
double farRight = 10;
double useHeight = drawheight - farTop - farBottom;
double useWidth = drawwidth - farLeft - farRight;
LineGeometry myLineX = new LineGeometry();
myLineX.StartPoint = new Point(farLeft, useHeight+farTop);
myLineX.EndPoint = new Point(useWidth, useHeight + farTop);
Path myPathX = new Path();
myPathX.Stroke = Brushes.Black;
myPathX.StrokeThickness = 2;
myPathX.Data = myLineX;
gridParent.Children.Add(myPathX);
LineGeometry myLineY = new LineGeometry();
myLineY.StartPoint = new Point(farLeft, farTop);
myLineY.EndPoint = new Point(farLeft, useHeight + farTop);
Path myPathY = new Path();
myPathY.Stroke = Brushes.Black;
myPathY.StrokeThickness = 2;
myPathY.Data = myLineY;
gridParent.Children.Add(myPathY);
double[] dtTime=new double[5];
double maxTime = datetime[0].Ticks; ;
double minTime = datetime[0].Ticks; ;
for (int i = 0; i < datetime.Length; i++)
{
dtTime[i] = datetime[i].Ticks;
if (dtTime[i] < minTime)
{
minTime = dtTime[i];
}
if (dtTime[i] > maxTime)
{
maxTime = dtTime[i];
}
}
double maxValue = value[0];
double minValue = value[0];
for (int i = 0; i < value.Length; i++)
{
if (value[i] < minValue)
{
minValue = value[i];
}
if (value[i] > maxValue)
{
maxValue = value[i];
}
}
double everVlaue = (useHeight - 10) / (maxValue - minValue); //每一個刻度數值用everVlaue個頁面px畫素來表示;
double everTime = (useWidth-40) / (maxTime - minTime);
Point pt1 = new Point(farLeft + 10, useHeight - 10+farTop);
Point[] values = new Point[5];
values[0]=pt1;
for (int i = 1; i < value.Length; i++)
{
Point pt = new Point(pt1.X + (dtTime[i] - dtTime[0]) * everTime, pt1.Y - (value[i] - value[0]) * everVlaue);
values[i] = pt;
}
StreamGeometry geometry = new StreamGeometry();
using (StreamGeometryContext ctx = geometry.Open())
{
ctx.BeginFigure(values[0], true, false);
EllipseGeometry ellipseOne = new EllipseGeometry();
ellipseOne.Center = values[0];
ellipseOne.RadiusX = 3;
ellipseOne.RadiusY = 3;
Path myPathellipseOne = new Path();
myPathellipseOne.Stroke = Brushes.Black;
myPathellipseOne.StrokeThickness = 1;
myPathellipseOne.Fill = Brushes.LightBlue;
myPathellipseOne.Data = ellipseOne;
myPathellipseOne.ToolTip = values[0].Y.ToString();
gridParent.Children.Add(myPathellipseOne);
for (int i = 1; i < values.Length; i++)
{
ctx.LineTo(values[i], true, false);
EllipseGeometry ellipse = new EllipseGeometry();
ellipse.Center = values[i];
ellipse.RadiusX = 3;
ellipse.RadiusY = 3;
Path myPathellipse = new Path();
myPathellipse.Stroke = Brushes.Black;
myPathellipse.StrokeThickness = 1;
myPathellipse.Fill = Brushes.LightBlue;
myPathellipse.Data = ellipse;
myPathellipse.ToolTip = value[i].ToString() + " " + datetime[i].ToString();
gridParent.Children.Add(myPathellipse);
}
}
geometry.FillRule = FillRule.Nonzero;
geometry.Freeze();
Path myPath = new Path();
myPath.Stroke = Brushes.Red;
myPath.StrokeThickness = 1;
myPath.Data = geometry;
gridParent.Children.Add(myPath);
}
用以下函式呼叫繪製曲線函式——傳入要繪製的數值:
void Window1_Loaded(object sender, RoutedEventArgs e)
{
double drawheight = (this.Height) * 0.5-10;
double drawwidth = this.Width;
double[] value = new double[5] {1,3,6,8,5};
DateTime[] datetime = new DateTime[5] { DateTime.Parse("2012-6-30 10:20"),
DateTime.Parse("2012-6-30 10:30"),DateTime.Parse("2012-6-30 10:40"),
DateTime.Parse("2012-6-30 10:50"),DateTime.Parse("2012-6-30 11:00")
};
DrawingLine(drawwidth, drawheight,value,datetime);
}