WinFrom折线图

时间:2023-01-10 17:25:44
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace MyEillipse
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Paint(object sender, PaintEventArgs e)
{
//声明一个string类型的数组用于存储一年中的12个月份
string[] month = new string[] {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
float[] d = new float[] { 20.5F,,10.8F,15.6F,,70.9F,50.3F,30.7F,,50.4F,30.8F,};
//画图初始化
Bitmap bMap = new Bitmap(,);
Graphics gph = Graphics.FromImage(bMap);
gph.Clear(Color.White); PointF cPt = new PointF(, );//中心点
//X轴三角形
PointF[] xPt = new PointF[] { new PointF(cPt.Y + , cPt.Y), new PointF(cPt.Y, cPt.Y-), new PointF(cPt.Y, cPt.Y+) };
//Y轴三角形
PointF[] yPt = new PointF[] { new PointF(cPt.X, cPt.X - ), new PointF(cPt.X - , cPt.X), new PointF(cPt.X + , cPt.X) };
gph.DrawString("某工厂某产品月生产量图表",new Font("宋体",),Brushes.Black,new PointF(cPt.X+,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("宋体", ), Brushes.Black, new PointF(cPt.Y + , cPt.Y + )); //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("宋体", ), Brushes.Black, new PointF(,)); for (int i = ; i <=; i++)
{
//画Y轴刻度
if (i < )
{
gph.DrawString((i * ).ToString(), new Font("宋体", ), Brushes.Black, new PointF(cPt.X - , cPt.Y - i * - ));
gph.DrawLine(Pens.Black, cPt.X - , cPt.Y - i * , cPt.X, cPt.Y - i * );
}
//画X轴
gph.DrawString(month[i - ].Substring(, ), new Font("宋体", ), Brushes.Black, new PointF(cPt.X + i * - , cPt.Y + ));
gph.DrawString(month[i - ].Substring(, ), new Font("宋体", ), Brushes.Black, new PointF(cPt.X + i * - , cPt.Y + )); if (month[i - ].Length>)
gph.DrawString(month[i-].Substring(,),new Font("宋体",),Brushes.Black,new PointF(cPt.X+i*-,cPt.Y+));
//画点
gph.DrawEllipse(Pens.Black,cPt.X+i*-1.5F,cPt.Y-d[i-]*-1.5F,,);
gph.FillEllipse(new SolidBrush(Color.Black),cPt.X+i*-1.5F,cPt.Y-d[i-]*-1.5F,,);
//画数值
gph.DrawString(d[i - ].ToString(), new Font("宋体", ), Brushes.Black, new PointF(cPt.X + i * , cPt.Y - d[i - ] * ));
//画折线 if(i>) gph.DrawLine(Pens.Red,cPt.X+(i-)*,cPt.Y-d[i-]*,cPt.X+i*,cPt.Y-d[i-]*);
}
pictureBox1.Image = bMap;
}
}
}

WinFrom折线图