C#绘制饼状图和柱状图的方法
作者:CodingByMe 发布时间:2023-12-19 15:42:59
标签:C#,饼状图,柱状图
本文实例为大家分享了C#绘制饼状图和柱状图的具体代码,供大家参考,具体内容如下
#代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TEST3._2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
BarMap();
}
private void BitMap()//饼状图
{
int[] saleNum = { 300, 500, 400 };
int sum = 0, threeNum = 0, fourNum = 0, fiveNum = 0;
for (int i = 0; i < saleNum.Length; i++)
{
sum += saleNum[i];
if (i == 0)
threeNum = saleNum[0];
else if (i == 1)
fourNum = saleNum[1];
else
fiveNum = saleNum[2];
}
int height = pictureBox1.Height, width = pictureBox1.Width;
Bitmap bitmap = new Bitmap(width, height);
Graphics g = Graphics.FromImage(bitmap);
g.Clear(Color.White);
Pen pen1 = new Pen(Color.Red);
Brush brush1 = new SolidBrush(Color.PowderBlue);
Brush brush2 = new SolidBrush(Color.Blue);
Brush brush3 = new SolidBrush(Color.Wheat);
Brush brush4 = new SolidBrush(Color.Orange);
Font font1 = new Font("Couriter New", 16, FontStyle.Bold);
Font font2 = new Font("Couriter New", 10);
g.FillRectangle(brush1, 0, 0, width, height);
g.DrawString("每月销售占比饼状图", font1, brush2, new Point(70, 20));
int piex = 100, piey = 60, piew = 200, pieh = 200;
float angle1 = Convert.ToSingle((360 / Convert.ToSingle(sum)) * Convert.ToSingle(threeNum));
float angle2 = Convert.ToSingle((360 / Convert.ToSingle(sum)) * Convert.ToSingle(fourNum));
float angle3 = Convert.ToSingle((360 / Convert.ToSingle(sum)) * Convert.ToSingle(fiveNum));
g.FillPie(brush2, piex, piey, piew, pieh, 0, angle1);
g.FillPie(brush3, piex, piey, piew, pieh, angle1, angle2);
g.FillPie(brush4, piex, piey, piew, pieh, angle1 + angle2, angle3);
g.DrawRectangle(pen1, 50, 300, 310, 130);
g.FillRectangle(brush2, 90, 320, 20, 10);
g.DrawString(string.Format("3月份销量占比:{0:P2}", Convert.ToSingle(threeNum) / Convert.ToSingle(sum)), font2, brush2, 120, 320);
g.FillRectangle(brush3, 90, 360, 20, 10);
g.DrawString(string.Format("4月份销量占比:{0:P2}", Convert.ToSingle(fourNum) / Convert.ToSingle(sum)), font2, brush2, 120, 360);
g.FillRectangle(brush4, 90, 400, 20, 10);
g.DrawString(string.Format("5月份销量占比:{0:P2}", Convert.ToSingle(fiveNum) / Convert.ToSingle(sum)), font2, brush2, 120, 400);
this.groupBox1.Text = "饼状图";
this.pictureBox1.Width = bitmap.Width;
this.pictureBox1.Height = bitmap.Height;
this.pictureBox1.BackgroundImage = bitmap;
}
private void BarMap()//柱状图
{
int[] saleNum = { 300, 500, 400 };
int sum = saleNum[0]+ saleNum[1]+ saleNum[2];
float[] Y_Num ={ Convert.ToSingle(saleNum[0]) / Convert.ToSingle(sum),Convert.ToSingle(saleNum[1]) / Convert.ToSingle(sum),
Convert.ToSingle(saleNum[2]) / Convert.ToSingle(sum) };
int height = pictureBox1.Height, width = pictureBox1.Width;
Bitmap image = new Bitmap(width, height);
//创建Graphics类对象
Graphics g = Graphics.FromImage(image);
try
{
//清空图片背景色
g.Clear(Color.White);
Font font = new Font("Arial", 10, FontStyle.Regular);
Font font1 = new Font("宋体", 20, FontStyle.Bold);
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),Color.Blue, Color.BlueViolet, 1.2f, true);
Font font2 = new System.Drawing.Font("Arial", 10, FontStyle.Bold);
SolidBrush mybrush = new SolidBrush(Color.Red);
SolidBrush mybrush2 = new SolidBrush(Color.Green);
Pen mypen = new Pen(brush, 1);
//绘制线条
//绘制横向线条
int x = 100;
Pen mypen1 = new Pen(Color.Blue, 2);
x = 60;
g.DrawLine(mypen1, x, 0, x, 300);
//绘制纵向线条
int y = 0;
for (int i = 0; i <11; i++)
{
g.DrawLine(mypen, 45, y, 60, y);
y = y + 30;
}
g.DrawLine(mypen1, 60, y-30, 620, y-30);
//x轴
String[] n = { "3月份", "4月份", "5月份"};
x = 100;
for (int i = 0; i < 3; i++)
{
g.DrawString(n[i].ToString(), font, Brushes.Blue, x, 300); //设置文字内容及输出位置
Console.WriteLine(300 - Y_Num[i] * 100 * 3);
g.FillRectangle(mybrush, x, 300 - Y_Num[i] * 100 * 3, 20, Y_Num[i] * 100 * 3);
g.DrawString(Y_Num[i].ToString(), font2, Brushes.Green, x, 300 - Y_Num[i] * 100 * 3 - 15);
x = x + 100;
}
//y轴
String[] m = {"0","0.10","0.20", "0.30", "0.40", "0.50", "0.60", "0.70", " 0.80"," 0.90", " 1.00" };
y = 0;
for (int i = 10; i >= 0; i--)
{
g.DrawString(m[i].ToString(), font, Brushes.Blue, 20, y); //设置文字内容及输出位置
y = y + 30;
}
//绘制标识
Font font3 = new System.Drawing.Font("Arial", 10, FontStyle.Regular);
g.DrawRectangle(new Pen(Brushes.Blue), 170, 390, 250, 50); //绘制范围框
g.FillRectangle(Brushes.Red, 200, 410, 20, 10); //绘制小矩形
g.DrawString("月销量占比", font3, Brushes.Red, 292, 408);
this.button1.Text = "查看饼状图";
this.groupBox1.Text = "柱状图";
this.pictureBox1.Width = image.Width;
this.pictureBox1.Height = image.Height;
this.pictureBox1.BackgroundImage = image;
}
catch { }
}
private void Button1_Click(object sender, EventArgs e)
{
BitMap();
this.button1.Visible =false;//隐藏button
}
}
}
来源:https://blog.csdn.net/qq_15042311/article/details/89724877
0
投稿
猜你喜欢
- 前言昨晚想在Android应用中增加一个int映射到String的字典表,使用HashMap实现的时候,Eclipse给出了一个警告,昨晚项
- @RequestBody的作用@RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的),所以只能发送
- 百度百科说法:Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务
- 概述在使用Spring Boot的时候我们经常使用actuator,健康检查,bus中使用/refresh等。这里记录如何使用注解的方式自定
- 题目一链表题——操作链表根据给定的链表按照指定条件删除其中节点并返回新的头节点具体题目如下解法/**
- 线程安全当多个线程访问某个类时,不管运行时环境采用何种调度方式或者这些进程将如何交替执行,并且在主调代码中不需要任何额外的同步或协调,这个类
- fastjson转换对象实体@JsonProperty不生效项目场景请求第三方应用 返回json数据问题描述第三方返回的数据中,存在java
- 见过一句夸张的话,叫做“没有阅读过jdk源码的人不算学过java”。从今天起开始精读源码。而适合精读的源码无非就是java.io,.util
- 本文实例讲述了Java实现矩阵加减乘除及转制等运算功能。分享给大家供大家参考,具体如下:Java初学,编写矩阵预算程序,当做工具,以便以后写
- 前言兄弟们,刚刚又给seata社区修了一个BUG,有用户提了issue反应TransactionHook在某些情况下不会被调用:相关issu
- HttpClient模拟浏览器登录后发起请求浏览器实现这个效果需要如下几个步骤: 1请求一个需要登录的页
- 前言1、下面是一个效果展示;2、先抱怨一下,在博客上面的抄袭真的非常严重,为了实现一个图片滑动验证,我搜索了挺久的资料,不过内容翻来覆去就是
- 前言最近写了一篇博客是关于 使用Jenkins来构建SVN+Maven项目 ,这里使用的的代码版本工具是SVN,但是事实上也有很多公司使用G
- Object是所有类的父类,也就是说java中所有的类都是直接或者间接继承自Object类。比如你随便创建一个classA,虽然没有明说,但
- 本项目是前端vue3,后端springboot开发 需求为:前端导入表格,后端处理表格存储数据,点击按钮可以导出表格。上传效果:前端点击上传
- 前言 红包文化源远流长。从古时的红色纸包,到手
- 1.引入如下依赖<dependency> <groupId>org.spri
- 本文实例为大家分享了Java实现扑克牌程序的具体代码,供大家参考,具体内容如下思路:在实现之前,先要想好步骤,思路清晰才不会出错。要实现一副
- 最近的项目中要实现一个聊天的功能,类似于斗鱼TV的聊天室功能,与服务器端人商量后决定用WebSocket来做,但是在这之前我只知道Socke
- 本文实例讲述了Java * 和AOP应用。分享给大家供大家参考,具体如下:一 点睛 * 在AOP(Aspect Orient Progr