当前位置:编程学习 > C#/ASP.NET >>

求助 画了个饼状图,但显示的比例有问题

今天做了个饼状图控件,我用6个相同的数据测试,计算的结果是各占16.667%,六分之一,这是正确的,但是在图上显示的却不大小不一,为什么?
本人是新手,第一次发帖,不知道怎么发图片。。。。。。
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.Imaging;
using System.Drawing.Drawing2D;
using Microsoft.JScript;


namespace kongjianlx
{
    public partial class tjzhu :UserControl
    {
        private MapTypes mapType; //图表类型 
        private int xuan = 1;
        private float beiy = 10;
        private float beix = 1;
        private string biaoti = "要输入的标题";
        private int biaotizihao=10;
        private int biaotizigao = 12;
        private int gsx = 13;
        private int gsy = 11;
        private Point jiao = new Point(6, 5);
        private int zihao = 10;
        private string dany = "Y轴单位";
        private string danx = "X轴单位";
        private float[]  zhi=new float [] {80,90};
        private string[] zhimiaoshu = new string[] { "产量A", "产量B", "产量c", "产量d", "产量e", "产量f", "产量g", "产量h", "产量i", "产量j", "产量k", "产量l", "产量m" };



        

      private void tjzhu_Paint(object sender, PaintEventArgs e)
        {
            int i;

            if (mapType == MapTypes.折线图)
            { 
                jiao.X = 0;
                jiao.Y = 0;
            }

            float[] zhi = new float[] { 60,60,60,60,60,60};// 测试数据,图的分档等于数据的个数

   
            Graphics g = e.Graphics; 
            Bitmap bmap = new Bitmap(this.Size.Width, this.Size.Height);                 /////定义图像大小;
            Graphics gph = Graphics.FromImage(bmap);        ////定义或创建gdi绘图对像;
            gph.Clear(Color.White);//Yellow);//////////////////1111
          
            System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.FromArgb(80, 80, 80, 80));

            if (mapType == MapTypes.饼状图)
            {
                 int x = bmap.Width/10;
                 int y = bmap.Height/10;
                 int a = bmap.Width / 5 * 3;
                 int b = bmap.Height / 5 * 3;
                 Point yd = new Point(a / 2 + x, b / 2 + y);
                 Rectangle tempRect = new Rectangle(x, y, a,  b);
                 Rectangle tempRect1 = new Rectangle(x, y+jiao.Y, a, b);
            //     gph.DrawEllipse(Pens.Black,tempRect); 
            //     gph.DrawRectangle(Pens.Black,x, y, a, b);
                 
                
                 float sum = 0;
                 float jiaodu = 0;
                 float tempFloat = 0;
                 float bl = 0;
                


                 for (i = 0; i < zhi.Length; i++)
                 {
                     sum += zhi[i];
                 }
          /*       for (i = 0; i < zhi.Length; i++)
                 {
                     jiaodu = zhi[i] / sum * 360;
                     Color myColor = GetColor(i);
                     Brush myBrush = new SolidBrush(myColor);
                     if (i == zhi.Length - 1)
                     {
                         gph.FillPie(myBrush, tempRect1, tempFloat, 360 - tempFloat);
                         gph.DrawPie(Pens.Black, tempRect1, tempFloat, 360 - tempFloat);
                         //  gph.FillPie(myBrush, tempRect, tempFloat, 360 - tempFloat);
                         //  gph.DrawPie(Pens.Black, tempRect, tempFloat, 360 - tempFloat);
                     }
                     else
                     {
                         gph.FillPie(myBrush, tempRect1, tempFloat, jiaodu);
                         gph.DrawPie(Pens.Black, tempRect1, tempFloat, jiaodu);
                         //     gph.FillPie(myBrush, tempRect, tempFloat, jiaodu);
                         //      gph.DrawPie(Pens.Black, tempRect, tempFloat, jiaodu);
                         tempFloat += jiaodu;
                     }
                 }
                 tempFloat = 0;*/
                 for (i = 0; i < zhi.Length; i++)
                 {
                
                     jiaodu = zhi[i] / sum * 360;


                     Color myColor = GetColor(i);
                     Brush myBrush = new SolidBrush(myColor);
               /*      if (i == zhi.Length - 1)
                     {
                         gph.FillPie(myBrush, tempRect, tempFloat, 360 - tempFloat);
                      //   gph.DrawPie(Pens.Black, tempRect, tempFloat, 360 - tempFloat);

                         float x1 = a / 2 * (float)MathObject.cos(tempFloat * Math.PI / 180) + yd.X;
                         float y1 = b / 2 * (float)MathObject.sin(tempFloat * Math.PI / 180) + yd.Y;
                         gph.DrawLine(Pens.Black, yd.X, yd.Y, x1, y1);

                     }
                     else*/
                     {

                       


                         gph.FillPie(myBrush, tempRect, tempFloat, jiaodu);
                  //       gph.DrawPie(Pens.Black, tempRect, tempFloat, jiaodu);


                     //    float x1 = a / 2 * (float)MathObject.cos(tempFloat * Math.PI / 180) + yd.X;
                     //    float y1 = b / 2 * (float)MathObject.sin(tempFloat * Math.PI / 180) + yd.Y;
                    //     gph.DrawLine(Pens.Black, yd.X, yd.Y, x1, y1);

                         tempFloat += jiaodu;

                     }


                     gph.FillRectangle(myBrush, bmap.Width / 4 * 3, bmap.Height / 10 + i * zihao * 2, zihao, zihao);
                     gph.DrawRectangle(Pens.Black, bmap.Width / 4 * 3, bmap.Height / 10 + i * zihao * 2, zihao, zihao);

                     Rectangle rect = new Rectangle(bmap.Width / 4 * 3 + zihao * 2, bmap.Height / 10 + i * zihao * 2, bmap.Width / 5, zihao * 2);
                     StringFormat sf = new StringFormat();
                     sf.Alignment = StringAlignment.Center;
                     gph.DrawString((100*jiaodu/360).ToString()+"%", new Font("宋体", zihao), Brushes.Black, rect, sf);

                     // */





                 }
      
            
            
            
            
            }

  g.DrawImage(bmap,0,0);
              

        }


--------------------编程问答--------------------       public enum MapTypes
      {

          饼状图,
          折线图,
          柱状图

      }
      public int Xuan
      {
          get
          {
              return xuan;
          }
          set
          {
              xuan = value;
              Invalidate();
          }
      }
      public float Beiy
      {
          get
          {
              return beiy;
          }
          set
          {
              beiy = value;
              Invalidate();
          }
      }
      public float Beix
      {
          get
          {
              return beix;
          }
          set
          {
              beix = value;
              Invalidate();
          }
      }


      
      public string Biaoti
      {
          get
          {
              return biaoti;
          }
          set
          {
              biaoti = value;
              Invalidate();
          }
      }
     
      public int Biaotizihao
      {
          get
          {
              return biaotizihao;
          }
          set
          {
              biaotizihao = value;
              Invalidate();
          }
      }
      public int Biaotizigao
      {
          get
          {
              return biaotizigao;
          }
          set
          {
              biaotizigao = value;
              Invalidate();
          }
      }
   
      public int Gsx
      {
          get
          {
              return gsx;
          }
          set
          {
              gsx = value;
              Invalidate();
          }
      }
    
      public int Gsy
      {
          get
          {
              return gsy;
          }
          set
          {
              gsy = value;
              Invalidate();
          }
      }
      public Point Jiao
      {
          get
          {
              return jiao;
          }
          set
          {
              jiao = value;
              Invalidate();
          }
      }
      public int Zihao
      {
          get
          {
              return zihao;
          }
          set
          {
              zihao = value;
              Invalidate();
          }
      }
      //     [DefaultValue("要输入的标题")]
      public float[] Zhi
      {

          get
          {
              

              return zhi;
          }
          set
          {
              zhi = value;
              Invalidate();
          }
      }
      public string[] Zhimiaoshu
      {

          get
          {
             

              return zhimiaoshu;
          }
          set
          {
              zhimiaoshu = value;
              Invalidate();
          }
      }
      //*/
      //  [DefaultValue("要输入的标题")]
      public string Dany
      {
          get
          {
              return dany;
          }
          set
          {
              dany = value;
              Invalidate();
          }
      }
      public string Danx
      {
          get
          {
              return danx;
          }
          set
          {
              danx = value;
              Invalidate();
          }
      }
      public MapTypes MapType
      {

          get
          {
              return mapType;
          }
          set
          {
              mapType = value;
          }
      }

      public tjzhu()
      {
          InitializeComponent();
          // this.shezhi();

      }

     
      private Color GetColor(int ValueIndex)
      {
          Color ColorTemp;
          switch (ValueIndex)
          {
              case 0: //红色 
                  ColorTemp = Color.FromArgb(235, 0, 0);
                  break;
              case 1:
                  ColorTemp = Color.FromArgb(142, 235, 235);
                  break;
              case 2:
                  ColorTemp = Color.FromArgb(0, 192, 192);
                  break;
              case 3:
                  ColorTemp = Color.FromArgb(235, 216, 177);
                  break;
              case 4:
                  ColorTemp = Color.FromArgb(189, 167, 225);
                  break;
              case 5:
                  ColorTemp = Color.FromArgb(0xff, 0xe4, 0xe1);
                  break;
              case 6:
                  ColorTemp = Color.FromArgb(0x00, 0x00, 0xff);
                  break;
              case 7:
                  ColorTemp = Color.FromArgb(0x80, 0x80, 0x00);
                  break;
              case 8:
                  ColorTemp = Color.FromArgb(0x48, 0xd1, 0xcc);
                  break;
              case 9:
                  ColorTemp = Color.FromArgb(0xbc, 0x8f, 0x8f);
                  break;
              case 10:
                  ColorTemp = Color.FromArgb(0xad, 0xd8, 0xe6);
                  break;
              case 11:
                  ColorTemp = Color.FromArgb(0x94, 0x00, 0xd3);
                  break;
              case 12:
                  ColorTemp = Color.FromArgb(0x00, 0x8b, 0x8b);
                  break;
              case 13:
                  ColorTemp = Color.FromArgb(0xaf, 0xee, 0xee);
                  break;
              case 14:
                  ColorTemp = Color.FromArgb(0x7b, 0x68, 0xee);
                  break;
              case 15:
                  ColorTemp = Color.FromArgb(0xf0, 0xf8, 0xff);
                  break;
              case 16:
                  ColorTemp = Color.FromArgb(0xb8, 0x86, 0x0b);
                  break;
              case 17:
                  ColorTemp = Color.FromArgb(0xff, 0xd7, 0x00);
                  break;
              case 18:
                  ColorTemp = Color.FromArgb(0x32, 0xcd, 0x32);
                  break;
              case 19:
                  ColorTemp = Color.FromArgb(0x00, 0xfa, 0x9a);
                  break;
              default:
                  ColorTemp = Color.Black;
                  break;
          }
          return ColorTemp;
      } 
       
       
    }
}




我还用30,60等角度试过,比例都不准,不知道为什么,希望高手帮下,谢谢
--------------------编程问答-------------------- 大部分情况下是由于小数点引起的误差造成的.尝试修改一下计算公式里的函数 --------------------编程问答-------------------- 同样的数据相差接近1倍,不是小数的问题 --------------------编程问答-------------------- 有做了个实验,图越接近圆形比例就越准,越扁就越失真,有什么方法可以矫正吗?希望高手指点下,本人新手,万分感谢!!!!!!!!! --------------------编程问答-------------------- 画的时候按正圆画,然后再建一个宽度相同但高度只有一半差不多的图,把结果图画上去,就变形了.但还是那句话:小数点影响比例,长期以来我也是对此比较头痛的.

以下是我以前做的,两张图中都有明显的因小数点引起的偏差扩大.


--------------------编程问答--------------------
相同的数据,差的太多了 --------------------编程问答-------------------- 百分比是一样的,中间的两个也太小了
看了你编的图片,我都自卑了,打击我信心啊! --------------------编程问答-------------------- 呵,有什么打击不打击的.我偏向于喜欢去创作人机界面,不擅长做数据.相信自己,我期待你成功.
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,