re.Segments.Add(new LineSegment(new Point(600, 324), false)); //第2个点
41 x_axisFigure.Segments.Add(new LineSegment(new Point(610, 320), false)); //第3个点
42 PathFigure y_axisFigure = new PathFigure();
43 y_axisFigure.IsClosed = true;
44 y_axisFigure.StartPoint = new Point(36, 30); //路径的起点
45 y_axisFigure.Segments.Add(new LineSegment(new Point(44, 30), false)); //第2个点
46 y_axisFigure.Segments.Add(new LineSegment(new Point(40, 20), false)); //第3个点
47 PathGeometry x_axisGeometry = new PathGeometry();
48 PathGeometry y_axisGeometry = new PathGeometry();
49 x_axisGeometry.Figures.Add(x_axisFigure);
50 y_axisGeometry.Figures.Add(y_axisFigure);
51 x_axisArrow.Data = x_axisGeometry;
52 y_axisArrow.Data = y_axisGeometry;
53 this.chartCanvas.Children.Add(x_axisArrow);
54 this.chartCanvas.Children.Add(y_axisArrow);
55
56 TextBlock x_label =new TextBlock();
57 TextBlock y_label =new TextBlock();
58 TextBlock o_label =new TextBlock();
59 x_label.Text = "月";
60 y_label.Text = "元";
61 o_label.Text = "0";
62 Canvas.SetLeft(x_label, 610);
63 Canvas.SetLeft(y_label, 20);
64 Canvas.SetLeft(o_label, 20);
65 Canvas.SetTop(x_label, 317);
66 Canvas.SetTop(y_label, 4);
67 Canvas.SetTop(o_label, 312);
68 x_label.FontSize = 14;
69 y_label.FontSize = 14;
70 o_label.FontSize = 14;
71 this.chartCanvas.Children.Add(x_label);
72 this.chartCanvas.Children.Add(y_label);
73 this.chartCanvas.Children.Add(o_label);
74
75 }
标尺,X轴以45为间隔单位,Y轴以10px为单位,且没5格显示一个大标尺
1 /// <summary>
2 /// 作出x轴和y轴的标尺
3 /// </summary>
4 private void DrawScale()
5 {
6 for (int i = 1; i < 13; i++)//作12个刻度
7 {
8 //原点 O=(40,320)
9 Line x_scale = new Line(); //主x轴标尺
10 x_scale.StrokeEndLineCap = PenLineCap.Triangle;
11 x_scale.StrokeThickness = 1;
12 x_scale.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0));
13 x_scale.X1 = 40 + i * 45;
14 x_scale.X2 = x_scale.X1;
15 x_scale.Y1 = 320;
16 x_scale.StrokeThickness = 3;
17 x_scale.Y2 = x_scale.Y1 - 8;
18 this.chartCanvas.Children.Add(x_scale);
19
20 Line x_in = new Line();//x轴轴辅助标尺
21 x_in.Stroke = System.Windows.Media.Brushes.LightGray;
22 x_in.StrokeThickness = 0.5;
23 x_in.X1 = 40 + i * 45;
24 x_in.Y1 = 320;
25 x_in.X2 = 40 + i * 45;
26 x_in.Y2 = 30;
27 this.chartCanvas.Children.Add(x_in);
28 }
29 for (int j = 0; j < 30; j++ )
30 {
31 Line y_scale = new Line(); //主Y轴标尺
32 y_scale.StrokeEndLineCap = PenLineCap.Triangle;
33 y_scale.StrokeThickness = 1;
34 y_scale.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0));
35
36 y_scale.X1 = 40; //原点x=40
37 if (j % 5 == 0)
38 {
39 y_scale.StrokeThickness = 3;
40 y_scale.X2 = y_scale.X1 + 8;//大刻度线
41 }
42 else
43 {
44 y_scale.X2 = y_scale.X1 + 4;//小刻度线
45 }
46
47 y_scale.Y1 = 320 - j * 10; //每10px作一个刻度
48 y_scale.Y2 = y_scale.Y1;
49 this.chartCanvas.Children.Add(y_scale);
50 }
51 for (int i = 1; i < 6; i++)
52 {
53 Line y_in = new Line();//y轴辅助标尺
54 y_in.Stroke = System.Windows.Media.Brushes.LightGray;
55 y_in.StrokeThickness = 0.5;
56 y_in.X1 = 40;
57 y_in.Y1 = 320 - i * 50;
58 y_in.X2 = 600;
59 y_in.Y2 = 320 - i * 50;
60 this.chartCanvas.Children.Add(y_in);
61 }
62
63 }
刻度标签的话,X轴是固定的,并且其中用到了一个把阿拉伯数字转换为中文的方法 NumberToChinese(),
Y轴标尺标签,是用出入的 list<double>,计算出最大值再向上取100整,再分成五份,每份的值就是五个标签了
list最大值向上取100的方法:(除100向上取整再乘100)
Math.Ceiling(list.Max() / 100) * 100