vs2010chart控件-饼图

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 ChartControl = System.Windows.Forms.DataVisualization.Charting;
namespace 图形
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            ChartPie();
        }
        /// <summary>
        /// 3D饼图显示
        /// </summary>
        private void ChartPie()
        {
            chart1.Series.Clear();
            //简单测试数据
            string[] data = new string[] { "香蕉", "西瓜", "草莓", "火龙果", "水蜜桃" };
            //对应的数量
            double[] value = new double[] { 100, 200, 400, 600, 800 };
            string name = "PieChart";
            //添加个一图表
            chart1.Series.Add(name);
            //添加一个图标Y值类型
            chart1.Series[name].YValueType = ChartControl.ChartValueType.Double;
            //设置图形类型为圆形
            chart1.Series[name].ChartType = ChartControl.SeriesChartType.Pie;
            //圆图显示角度
            chart1.ChartAreas[0].Area3DStyle.Inclination = 20;
            //使用颜色
            chart1.Palette = ChartControl.ChartColorPalette.Pastel;
            for (int i = 0; i < data.Length; i++)
            {
                //计算比例
                double num = (int)((value[i] / 2100)*100);
                
                double[] number = new double[] { num };
                //添加一个数据点
                chart1.Series[name].Points.Add();
                //对应商品名
                chart1.Series[name].Points[i].LegendText = data[i];
                //Y赋值
                chart1.Series[name].Points[i].YValues = number;
                //数据点上显示分文字
                chart1.Series[name].Points[i].AxisLabel = number[0] + "%";
                //鼠标移到图形上的提示
                chart1.Series[name].Points[i].ToolTip = data[i];
                //最大数量的分离显示
                if (value[i] > 600)
                    chart1.Series[name].Points[i].CustomProperties = "Exploded=True";
            }
        }
        /// <summary>
        /// 单击分离图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chart1_MouseClick(object sender, MouseEventArgs e)
        {
            //判断鼠标按下是否有数据点 不存在返回-1 存在返回下标
            ChartControl.HitTestResult f = chart1.HitTest(e.X, e.Y);
            //获取数据点的下标
            int p = f.PointIndex;
            //存在
            if (p > -1)
            {
                //获取是否分离
                string check = chart1.Series["PieChart"].Points[p].CustomProperties;
                if (check != ("Exploded=True"))
                    chart1.Series["PieChart"].Points[p].CustomProperties = "Exploded=True";
                else
                    chart1.Series["PieChart"].Points[p].CustomProperties = "Exploded=none";
            }
        }     
        
    }
}
效果图如下

转载于:https://www.cnblogs.com/fz-code/archive/2012/07/07/2580423.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值