一:基礎定義
#region 定義線尾、線頭為箭頭、字型和筆刷
Pen p = new Pen(Color.Black, 1);//定義畫筆 藍色,寬度為1(坐标顯示顔色)
p.EndCap = LineCap.ArrowAnchor;//定義線尾的樣式為箭頭
Pen pk = new Pen(Color.Black, 1);//定義畫筆 黑色,寬度為1(坐标顯示顔色)
Pen pbl = new Pen(Color.Red,1);
pbl.EndCap = LineCap.ArrowAnchor;//定義線尾的樣式為箭頭
StringFormat strF = new StringFormat(StringFormatFlags.DirectionVertical);//定義格式
Font f = new System.Drawing.Font("宋體", 10);//定義字型
SolidBrush bB = new SolidBrush(Color.Black);//定義單色畫刷
SolidBrush bl = new SolidBrush(Color.Red);//定義單色畫刷
#endregion
二:使用前準備,建立一個picturebox,執行個體化一個 Graphics類,關聯它們
Graphics gph;//繪畫區域
Bitmap bmp = new Bitmap(pic_Img.Width, pic_Img.Height);//600, 512
gph = Graphics.FromImage(bmp);
三:執行個體

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace DrawLine
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
#region 定義線尾、線頭為箭頭、字型和筆刷
Pen p = new Pen(Color.Blue, 1);//定義畫筆 藍色,寬度為1(坐标顯示顔色)
p.EndCap = LineCap.ArrowAnchor;//定義線尾的樣式為箭頭
p.StartCap = LineCap.ArrowAnchor;//定義線首的樣式為箭頭
Font f = new System.Drawing.Font("宋體", 10);//定義字型
SolidBrush bB = new SolidBrush(Color.Black);//定義單色畫刷
//SolidBrush bB = new SolidBrush(Color.Blue);
#endregion
string[] month = new string[12] { "一月", "二月", "三月", "四月", "五月", "六月",
"七月", "八月", "九月", "十月", "十一月", "十二月" };
float[] d = new float[12] { 20.5F, 60, 10.8F, 15.6F, 30, 70.9F, 50.3F, 30.7F, 70, 50.4F, 30.8F, 20 };
//畫圖初始化
Bitmap bMap = new Bitmap(526, 500);
Graphics gph = Graphics.FromImage(bMap);
//gph.Clear(Color.White);
PointF cPt = new PointF(40, 420);//中心點
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軸三角形
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軸三角形
gph.DrawString("紅馬車公司月生産量圖表", new Font("宋體", 14)
, Brushes.Black, new PointF(cPt.X + 60, cPt.X));//圖示标題
//畫X軸
gph.DrawLine(Pens.Black, cPt.X, cPt.Y, cPt.Y, cPt.Y);
gph.DrawPolygon(Pens.Black, xPt);
gph.FillPolygon(new SolidBrush(Color.Black), xPt);
gph.DrawString("月份", new Font("宋體", 12), Brushes.Black, new PointF(cPt.Y + 10, cPt.Y + 10));
//畫Y軸
gph.DrawLine(Pens.Black, cPt.X, cPt.Y, cPt.X, cPt.X);
gph.DrawPolygon(Pens.Black, yPt);
gph.FillPolygon(new SolidBrush(Color.Black), yPt);
gph.DrawString("機關(萬)", new Font("宋體", 12), Brushes.Black, new PointF(0, 7));
for (int i = 1; i <= 12; i++)
{
//畫Y軸刻度
if (i < 11)
{
gph.DrawString((i * 10).ToString(), new Font("宋體", 11), Brushes.Black, new PointF(cPt.X - 30, cPt.Y - i * 30 - 6));
gph.DrawLine(Pens.Black, cPt.X - 3, cPt.Y - i * 30, cPt.X, cPt.Y - i * 30);
}
//畫X軸項目
gph.DrawString(month[i - 1].Substring(0, 1), new Font("宋體", 11), Brushes.Black,
new PointF(cPt.X + i * 30 - 5, cPt.Y + 5));
gph.DrawString(month[i - 1].Substring(1, 1), new Font("宋體", 11), Brushes.Black,
new PointF(cPt.X + i * 30 - 5, cPt.Y + 20));
if (month[i - 1].Length > 2) gph.DrawString(month[i - 1].Substring(2, 1), new Font("宋體", 11),
Brushes.Black,
new PointF(cPt.X + i * 30 - 5, cPt.Y + 35));
gph.DrawLine(Pens.Black, cPt.X + i * 30, cPt.Y, cPt.X + i * 30, cPt.Y + 3);
//畫點
gph.DrawEllipse(Pens.Black, cPt.X + i * 30, cPt.Y - d[i - 1] * 3 - 1.5F, 3, 3);
gph.FillEllipse(new SolidBrush(Color.Black), cPt.X + i * 30, cPt.Y - d[i - 1] * 3 - 1.5F,
3, 3);
//畫數值
gph.DrawString(d[i - 1].ToString(), new Font("宋體", 11), Brushes.Black,
new PointF(cPt.X + i * 30, cPt.Y - d[i - 1] * 3));
//畫折線
if (i > 1)
gph.DrawLine(Pens.Red, cPt.X + (i - 1) * 30, cPt.Y - d[i - 2] * 3, cPt.X + i * 30, cPt.Y - d[i - 1] * 3);
}
pictureBox1.Image = bMap;
}
}
}
四:注意事項
如何建立一個有比例關系的坐标系,主要是确定實際距離和像素的關系,一個像素代表多少米的距離
如何生成一個動态比例尺、動态刻度值的坐标系,這就需要确立一個參照點,将這個參照點作為零點,再計算刻度值,從零點的左右兩邊分别标刻度值。标點時,需要确立動态的像素和距離的關系來确定位置
轉載于:https://www.cnblogs.com/hongmaju/p/4271510.html