码迷,mamicode.com
首页 > 其他好文 > 详细

快速生成折线图及代码详解

时间:2014-05-07 17:18:20      阅读:364      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   tar   

快速生成折线图时,只需要修改代码中的以下数据:

1、Y轴刻度个数:Ycounts

2、Y轴最小刻度数:YminValue

3、横坐标:数组mouth

4、标题:strTopic

5、用户数据:数组d

6、[可选]修改背景色:代码中27行改为所需要的颜色即可

完整代码:

bubuko.com,布布扣
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Text;
 7 using System.Windows.Forms;
 8 
 9 namespace Test11
10 {
11     public partial class Form1 : Form
12     {
13         public Form1()
14         {
15             InitializeComponent();
16         }
17 
18         private void Form1_Paint(object sender, PaintEventArgs e)
19         {
20             int Ycounts = 9;//Y轴刻度个数
21             int YminValue = 20;//Y轴最小值
22             string[] month = new string[8] { "1至3天", "4至7天", "8至14天", "15至21天", "22至28天", "29至39天", "36至42天", "42天以后" };
23             float[] d = new float[8] { 36, 35, 33.5F, 31.5F, 29, 27, 26, 21 };
24             //画图初始化
25             Bitmap bMap = new Bitmap(500, 500);
26             Graphics gph = Graphics.FromImage(bMap);
27             gph.Clear(Color.White);
28             PointF cPt = new PointF(40, 420);//中心点
29             PointF[] xPt = new PointF[3] { new PointF(cPt.Y + 15, cPt.Y), new PointF(cPt.Y, cPt.Y - 8), new PointF(cPt.Y, cPt.Y + 8) };//X轴三角形
30             PointF[] yPt = new PointF[3] { new PointF(cPt.X, cPt.X - 15), new PointF(cPt.X - 8, cPt.X), new PointF(cPt.X + 8, cPt.X) };//Y轴三角形
31             string strTopic = "畜禽温度时刻图表";
32             gph.DrawString(strTopic, new Font("宋体", 14), Brushes.Black, new PointF((cPt.Y-strTopic.Length*14)/2, cPt.X*2f/3));//图表标题,居中对齐
33             //画X轴
34             gph.DrawLine(Pens.Black, cPt.X, cPt.Y, cPt.Y, cPt.Y);
35             gph.DrawPolygon(Pens.Black, xPt);
36             gph.FillPolygon(new SolidBrush(Color.Black), xPt);
37             gph.DrawString("天数", new Font("宋体", 12), Brushes.Black, new PointF(cPt.Y + 12, cPt.Y + 12));//12为字体大小
38             //画Y轴
39             gph.DrawLine(Pens.Black, cPt.X, cPt.Y, cPt.X, cPt.X);
40             gph.DrawPolygon(Pens.Black, yPt);
41             gph.FillPolygon(new SolidBrush(Color.Black), yPt);
42             gph.DrawString("单位(摄氏度)", new Font("宋体", 12), Brushes.Black, new PointF(0, 6));//6为字体大小的一半
43 
44             for (int i = 0; i < Ycounts; i++)
45             {
46                 //画Y轴刻度
47                 string strY = (i * 2 + YminValue).ToString();//Y轴刻度值
48                 gph.DrawString(strY, new Font("宋体", 11), Brushes.Black, new PointF(cPt.X - strY.Length*11, cPt.Y - i * (cPt.Y-cPt.X)/Ycounts-11*2f/3));//11为字体大小
49                 if (i != 0)
50                 {
51                     gph.DrawLine(Pens.Black, cPt.X + 3, cPt.Y - i * (cPt.Y - cPt.X) / Ycounts , cPt.X, cPt.Y - i * (cPt.Y - cPt.X) / Ycounts);
52                 }
53             }
54 
55             for (int i = 1; i <= month.Length; i++)
56             {
57                 //画X轴项目
58                 int positionWordTian=month[i-1].IndexOf("");
59                 int positionWordZhi = month[i - 1].IndexOf("");
60                 int py = 0;
61                 if (positionWordZhi > 0)
62                 {
63                     gph.DrawString(month[i - 1].Substring(0, positionWordZhi).PadLeft(2), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X + (i - 1) * (cPt.Y-cPt.X)/month.Length-11, cPt.Y));
64                     gph.DrawString(month[i - 1].Substring(positionWordZhi, 1), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X + (i - 1) * (cPt.Y - cPt.X) / month.Length-11, cPt.Y + 15));//15为字体比例大小,下同
65                     gph.DrawString(month[i - 1].Substring(positionWordZhi + 1, positionWordTian - positionWordZhi - 1).PadLeft(2), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X + (i - 1) * (cPt.Y - cPt.X) / month.Length-11, cPt.Y + 30));
66                     py = 30;
67                 }
68                 else
69                 {
70                     gph.DrawString(month[i - 1].Substring(0, positionWordTian), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X + (i - 1) * (cPt.Y - cPt.X) / month.Length-11, cPt.Y));
71                 }
72                 for (int j = positionWordTian; j < month[i - 1].Length;j++ )
73                     gph.DrawString(month[i - 1].Substring(j, 1), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X + (i - 1) * (cPt.Y - cPt.X) / month.Length-11, cPt.Y + (j - positionWordTian + 1) * 15 + py));
74                 //画X轴刻度
75                 if (i != 0)
76                 {
77                     gph.DrawLine(Pens.Black, cPt.X + (i - 1) * (cPt.Y - cPt.X) / month.Length, cPt.Y-3, cPt.X + (i - 1) * (cPt.Y - cPt.X) / month.Length, cPt.Y);
78                 }
79                 //画点
80                 float YpointPosition = (cPt.Y - cPt.X) / (Ycounts * 2f);
81                 gph.DrawEllipse(Pens.Black, cPt.X + (i - 1) * (cPt.Y - cPt.X) / month.Length, cPt.Y - (d[i - 1] - YminValue) * YpointPosition - 1.5F, 3, 3);
82                 gph.FillEllipse(new SolidBrush(Color.Black), cPt.X + (i - 1) * (cPt.Y - cPt.X) / month.Length, cPt.Y - (d[i - 1] - YminValue) * YpointPosition - 1.5F, 3, 3);
83                 //画折线
84                 if (i > 1) gph.DrawLine(Pens.Red, cPt.X + (i - 2) * (cPt.Y - cPt.X) / month.Length, cPt.Y - (d[i - 2] - YminValue) * YpointPosition - 1.5F, cPt.X + (i - 1) * (cPt.Y - cPt.X) / month.Length, cPt.Y - (d[i - 1] - YminValue) * YpointPosition - 1.5F);
85             }
86             pictureBox1.Image = bMap;
87         }
88     }
89 }
bubuko.com,布布扣

 效果图如下:

bubuko.com,布布扣

快速生成折线图及代码详解,布布扣,bubuko.com

快速生成折线图及代码详解

标签:style   blog   class   code   java   tar   

原文地址:http://www.cnblogs.com/xuhang/p/3712731.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!