效果图
public partial class 折线图 : Form { string[] strYue = { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" }; string[] strShuLiang = { "100", "68", "87", "67", "10", "50", "90", "60", "20", "30", "78", "56" }; DataTable dt = new DataTable(); public 折线图() { InitializeComponent(); DataColumn dc = new DataColumn("YueFen", typeof(string)); dt.Columns.Add(dc); DataColumn dc1 = new DataColumn("ShuLiang", typeof(string)); dt.Columns.Add(dc1); //加载数据 for (int i = 0; i < 12; i++) { DataRow dr = dt.NewRow(); dr["YueFen"] = strYue[i]; dr["ShuLiang"] = strShuLiang[i]; dt.Rows.Add(dr); } comboBox1.SelectedItem = "上半年"; btn_ChaXun_Click(null,null); } private void btn_ChaXun_Click(object sender, EventArgs e) { List<Point> pos = new List<Point>(); int i1, i2; Bitmap bitM = new Bitmap(this.panel1.Width, this.panel1.Height); Graphics g = Graphics.FromImage(bitM); g.Clear(Color.White); Brush b = new SolidBrush(Color.Blue); Font f = new Font("Arial", 9, FontStyle.Regular); Pen p = new Pen(b); //绘制横线 //找出折线统计图的0,0点 int ZheX = 40; int ZheY = 0; int y = 40; for (int i = 0; i < 6; i++) { g.DrawLine(new Pen(Color.Red), 40, y, 260, y); g.DrawString(i * 20 + "", new Font("宋体", 12, FontStyle.Regular), new SolidBrush(Color.Red), 10, this.panel1.Height - y - 32); y += 40; } ZheY = y - 40; //绘制竖线 for (int i = 0; i < 6; i++) { g.DrawLine(new Pen(Color.Red), ZheX + 40 * i, 20, 40 + 40 * i, 240); // g.DrawString(i * 20 + "", new Font("宋体", 12, FontStyle.Regular), new SolidBrush(Color.Red), 10, this.panel1.Height - y - 32); if (comboBox1.Text == "上半年") { g.DrawString(strYue[i], new Font("宋体", 12, FontStyle.Regular), new SolidBrush(Color.Red), 20 + 40 * i, ZheY + 8); } else { g.DrawString(strYue[6 + i], new Font("宋体", 12, FontStyle.Regular), new SolidBrush(Color.Red), 20 + 40 * i, ZheY + 8); } } if (comboBox1.Text == "上半年") { //加载右侧List listBox1.Items.Clear(); for (int i = 0; i < 6; i++) { listBox1.Items.Add(strYue[i] + " : " + strShuLiang[i] + "箱"); } i1 = 0; i2 = 6; } else { //加载右侧List listBox1.Items.Clear(); for (int i = 6; i < 12; i++) { listBox1.Items.Add(strYue[i] + " : " + strShuLiang[i] + "箱"); } i1 = 6; i2 = 12; } for (int i = i1; i < i2; i++) { //根据新的0,0点和横线的增量获取新的Y值 //新的Y值=数量/绘制的文字数量的增量(0,20,40) * 40(绘制的图中的px的增量,40px) decimal d = (decimal.Parse(dt.Rows[i]["ShuLiang"].ToString()) / 20) * 40; int d1 = (int)d; Point p1; if (i1 >= 6) { p1 = new Point((i + 1-6) * 40, ZheY - d1); } else { p1 = new Point((i + 1) * 40, ZheY - d1); } pos.Add(p1); } //连线 for (int i = 0; i < 5; i++) { g.DrawLine(p, pos[i], pos[i + 1]); } this.panel1.BackgroundImage = bitM; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类