快速生成扇形图只需修改数组budgetAll和actualAll
代码如下:
1 //设置笔刷样式
2 SolidBrush[] sbrush =
3 {
4 new SolidBrush(Color.Wheat),//背景色
5 new SolidBrush(Color.Gold),//其它
6 new SolidBrush(Color.Red),//交通
7 new SolidBrush(Color.Green),//饮食
8 new SolidBrush(Color.Blue),//门票
9 new SolidBrush(Color.Brown)//住宿
10 };
11 Rectangle rect1 = new Rectangle(0, 0, 200, 200);
12 Rectangle rect2 = new Rectangle(10, 10, 180, 180);
13 Graphics g1 = panel1.CreateGraphics();//预算
14 Graphics g2 = panel2.CreateGraphics();//实际支出
15 g1.FillEllipse(sbrush[0], rect1);
16 g2.FillEllipse(sbrush[0], rect1);
17 g1.FillEllipse(sbrush[1], rect2);
18 g2.FillEllipse(sbrush[1], rect2);
19 string[] budgetAll = budget.Split(‘,‘);
20 string[] actualAll = actual.Split(‘,‘);
21 int sumBudget = 0;
22 int sumActual = 0;
23 for (int i = 0; i < budgetAll.Length; i++)
24 {
25 sumBudget +=Int32.Parse(budgetAll[i]);
26 sumActual += Int32.Parse(actualAll[i]);
27 }
28
29 int angleBud = 30;//预算起始角度
30 int angleAct=30;//实际起始角度
31 int tempBud, tempAct;
32 for (int i = 0; i < budgetAll.Length-1; i++)
33 {
34 tempBud=(int)(Int32.Parse(budgetAll[i]) * 360 / sumBudget);
35 tempAct=(int)(Int32.Parse(actualAll[i]) * 360 / sumActual);
36 g1.FillPie(sbrush[i + 2], rect2, angleBud, tempBud);
37 g2.FillPie(sbrush[i + 2], rect2, angleAct, tempAct);
38 angleBud += tempBud;
39 angleAct += tempAct;
40 }
效果图如下:
时间: 2024-10-16 11:45:25