List<string> XList = new List<string>
{
"2017-01",
"2017-02",
"2017-03",
"2017-04",
"2017-05",
"2017-06",
"2017-07",
"2017-08",
"2017-09",
"2017-10",
"2017-11",
"2017-12",
"2017-13"
};
List<double> XValues = new List<double>
{
1,
21,
32,
44,
51,
62,
73,
84,
91,
101,
111,
121,
113
};
List<double> XValues2 = new List<double>
{
5,
9,
20,
60,
12,
60,
20,
10,
100,
52,
22,
35,
44
};
List<List<double>> XValueList = new List<List<double>>();
XValueList.Add(XValues);
XValueList.Add(XValues2);
string XText = "時間";
string YText = "人數";
int XCount = XList.Count + 1;
int YCount = 6;
Canvas chartCanvas = (Canvas)clientReportWindow.FindName("chartCanvas");
DrawArrow(chartCanvas, XCount, XText,YText);
DrawScale(chartCanvas, XCount, YCount);
DrawScaleLabel(chartCanvas, XList, XValueList , YCount);
/// <summary>
/// 生成横纵坐标及箭头
/// </summary>
private void DrawArrow(Canvas chartCanvas,int XCount,string XText,string YText)
{
double XPoint = 25 + 45 * XCount;
Line x_axis = new Line();//x轴
Line y_axis = new Line();//y轴
x_axis.Stroke =Brushes.Black;
y_axis.Stroke = Brushes.Black;
x_axis.StrokeThickness = 3;
y_axis.StrokeThickness = 3;
x_axis.X1 = 40;
x_axis.Y1 = 320;
x_axis.X2 = XPoint;
x_axis.Y2 = 320;
y_axis.X1 = 40;
y_axis.Y1 = 320;
y_axis.X2 = 40;
y_axis.Y2 = 30;
chartCanvas.Children.Add(x_axis);
chartCanvas.Children.Add(y_axis);
Line y_scale1 = new Line(); //坐标原点直角
y_scale1.Stroke = Brushes.Black;
y_scale1.StrokeThickness = 1;
y_scale1.X1 = 40;
y_scale1.Y1 = 310;
y_scale1.X2 = 44;
y_scale1.Y2 = 310;
y_scale1.StrokeStartLineCap = PenLineCap.Triangle;
chartCanvas.Children.Add(y_scale1);
Path x_axisArrow = new Path();//x轴箭头
Path y_axisArrow = new Path();//y轴箭头
x_axisArrow.Fill = new SolidColorBrush(Color.FromRgb(0, 0, 0));
y_axisArrow.Fill = new SolidColorBrush(Color.FromRgb(0, 0, 0));
PathFigure x_axisFigure = new PathFigure();
x_axisFigure.IsClosed = true;
x_axisFigure.StartPoint = new Point(XPoint, 316); //路径的起点
x_axisFigure.Segments.Add(new LineSegment(new Point(XPoint, 324), false)); //第2个点
x_axisFigure.Segments.Add(new LineSegment(new Point(XPoint+10, 320), false)); //第3个点
PathFigure y_axisFigure = new PathFigure();
y_axisFigure.IsClosed = true;
y_axisFigure.StartPoint = new Point(36, 30); //路径的起点
y_axisFigure.Segments.Add(new LineSegment(new Point(44, 30), false)); //第2个点
y_axisFigure.Segments.Add(new LineSegment(new 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;
chartCanvas.Children.Add(x_axisArrow);
chartCanvas.Children.Add(y_axisArrow);
TextBlock x_label = new TextBlock();
TextBlock y_label = new TextBlock();
TextBlock o_label = new TextBlock();
x_label.Text = XText;
y_label.Text = YText;
o_label.Text = "0";
Canvas.SetLeft(x_label, XPoint+10);
Canvas.SetLeft(y_label, 20);
Canvas.SetLeft(o_label, 20);
Canvas.SetTop(x_label, 317);
Canvas.SetTop(y_label, 4);
Canvas.SetTop(o_label, 312);
x_label.FontSize = 14;
y_label.FontSize = 14;
o_label.FontSize = 14;
chartCanvas.Children.Add(x_label);
chartCanvas.Children.Add(y_label);
chartCanvas.Children.Add(o_label);
}
/// <summary>
/// 作出x轴和y轴的标尺
/// </summary>
private void DrawScale(Canvas chartCanvas,int XCount,int YCount)
{
double XPoint = 45 * XCount;
for (int i = 1; i < XCount; i++)//作12个刻度
{
//原点 O=(40,320)
Line x_scale = new Line(); //主x轴标尺
x_scale.StrokeEndLineCap = PenLineCap.Triangle;
x_scale.StrokeThickness = 1;
x_scale.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0));
x_scale.X1 = 40 + i * 45;
x_scale.X2 = x_scale.X1;
x_scale.Y1 = 320;
x_scale.StrokeThickness = 3;
x_scale.Y2 = x_scale.Y1 - 8;
chartCanvas.Children.Add(x_scale);
Line x_in = new Line();//x轴轴辅助标尺
x_in.Stroke = Brushes.LightGray;
x_in.StrokeThickness = 0.5;
x_in.X1 = 40 + i * 45;
x_in.Y1 = 320;
x_in.X2 = 40 + i * 45;
x_in.Y2 = 30;
chartCanvas.Children.Add(x_in);
}
for (int j = 0; j < 30; j++)
{
Line y_scale = new Line(); //主Y轴标尺
y_scale.StrokeEndLineCap = PenLineCap.Triangle;
y_scale.StrokeThickness = 1;
y_scale.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0));
y_scale.X1 = 40; //原点x=40
if (j % 5 == 0)
{
y_scale.StrokeThickness = 3;
y_scale.X2 = y_scale.X1 + 8;//大刻度线
}
else
{
y_scale.X2 = y_scale.X1 + 4;//小刻度线
}
y_scale.Y1 = 320 - j * 10; //每10px作一个刻度
y_scale.Y2 = y_scale.Y1;
chartCanvas.Children.Add(y_scale);
}
for (int i = 1; i < YCount; i++)
{
Line y_in = new Line();//y轴辅助标尺
y_in.Stroke = Brushes.LightGray;
y_in.StrokeThickness = 0.5;
y_in.X1 = 40;
y_in.Y1 = 320 - i * 50;
y_in.X2 = XPoint;
y_in.Y2 = 320 - i * 50;
chartCanvas.Children.Add(y_in);
}
}
/// <summary>
/// 添加刻度标签
/// </summary>
private void DrawScaleLabel(Canvas chartCanvas,List<string> XList, List<List<double>> XValueList, int YCount)
{
// 最大值
double Max = 0;
for (int i = 0; i < XValueList.Count; i++)
{
if(Max< XValueList[i].Max()) Max = XValueList[i].Max();
}
double y_Max = Math.Ceiling((Max / 5));
// X坐标值
for (int i = 1; i <= XList.Count; i++)
{
TextBlock x_ScaleLabel = new TextBlock();
x_ScaleLabel.Text = XList[i-1];
x_ScaleLabel.LayoutTransform = new RotateTransform(90);
Canvas.SetLeft(x_ScaleLabel, 25 + 45 * i);
Canvas.SetTop(x_ScaleLabel, 320 + 5);
chartCanvas.Children.Add(x_ScaleLabel);
}
// Y坐标值
for (int i = 1; i < YCount; i++)
{
TextBlock y_ScaleLabel = new TextBlock();
y_ScaleLabel.Text = (i * y_Max).ToString();
Canvas.SetLeft(y_ScaleLabel, 0);
Canvas.SetTop(y_ScaleLabel, 320 - 50 * i);
chartCanvas.Children.Add(y_ScaleLabel);
}
// 多条线
for(int k = 0; k < XValueList.Count; k++)
{
// 折线图坐标点
PointCollection coordinatePoints = new PointCollection();
List<double> XValues = XValueList[k];
SolidColorBrush solidColor = new SolidColorBrush();
switch (k)
{
case 0:
solidColor = Brushes.Red;
break;
case 1:
solidColor = Brushes.Green;
break;
case 2:
solidColor = Brushes.Orange;
break;
case 3:
solidColor = Brushes.Blue;
break;
default:
solidColor = Brushes.Black;
break;
}
// 计算数据点
for (int i = 1; i <= XValues.Count; i++)
{
double xPoint = 40 + 45 * i;
double yPoint = 320 - 50 * (XValues[i - 1] / y_Max);
// 点显示
Ellipse Ellipse = new Ellipse();
Ellipse.Fill = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));
Ellipse.Width = 8;
Ellipse.Height = 8;
Canvas.SetLeft(Ellipse, xPoint-4);
Canvas.SetTop(Ellipse, yPoint);
chartCanvas.Children.Add(Ellipse);
// 线显示
coordinatePoints.Add(new Point(xPoint+5, yPoint+2));
// 值显示
TextBlock EP_Label = new TextBlock();
EP_Label.Foreground = solidColor;
EP_Label.Text = XValues[i - 1].ToString();
Canvas.SetLeft(EP_Label, xPoint);
Canvas.SetTop(EP_Label, yPoint);
chartCanvas.Children.Add(EP_Label);
}
// 绘制连接折线
Polyline curvePolyline = new Polyline();
curvePolyline.Stroke = solidColor;
curvePolyline.StrokeThickness = 2;
curvePolyline.Points = coordinatePoints;
chartCanvas.Children.Add(curvePolyline);
}
}