PieChart.xaml.cs 6.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
using Microsoft.Expression.Shapes;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace FLY.ControlLibrary
{
    /// <summary>
    /// PieChart.xaml 的交互逻辑
    /// </summary>
    public partial class PieChart : UserControl
    {
        static SolidColorBrush[] defaultColor;
        static int defaultcolor_idx = 0;
        static PieChart()
        {
            defaultColor = new SolidColorBrush[] {
            new SolidColorBrush(System.Windows.Media.Colors.Red),
            new SolidColorBrush(System.Windows.Media.Colors.Green),
            new SolidColorBrush(System.Windows.Media.Colors.Blue),
            new SolidColorBrush(System.Windows.Media.Colors.LightPink),
            new SolidColorBrush(System.Windows.Media.Colors.LightGreen),
            new SolidColorBrush(System.Windows.Media.Colors.LightBlue) };
        }
        public static Brush GetColor()
        {
            Brush b = defaultColor[defaultcolor_idx];
            defaultcolor_idx++;
            if (defaultcolor_idx >= defaultColor.Count())
                defaultcolor_idx = 0;
            return b;
        }

        List<PicChartItem> items = new List<PicChartItem>();

        public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(PieChart),
                new PropertyMetadata((d, e) => {
                    PieChart pc = d as PieChart;
                    pc.ItemsBinding();
                }));

        public IEnumerable ItemsSource
        {
            get
            {
                return (IEnumerable)GetValue(ItemsSourceProperty);
            }
            set
            {
                SetValue(ItemsSourceProperty, value);
                ItemsBinding();
            }
        }

        public static readonly DependencyProperty ItemValueBindingProperty =
            DependencyProperty.Register("ItemValueBinding", typeof(string), typeof(PieChart));
        /// <summary>
        /// 值
        /// </summary>
        public string ItemValueBinding
        {
            get
            {
                return (string)GetValue(ItemValueBindingProperty);
            }
            set
            {
                SetValue(ItemValueBindingProperty, value);
            }
        }

        public static readonly DependencyProperty ItemColorBindingProperty =
            DependencyProperty.Register("ItemColorBinding", typeof(string), typeof(PieChart));
        /// <summary>
        /// 值
        /// </summary>
        public string ItemColorBinding
        {
            get
            {
                return (string)GetValue(ItemColorBindingProperty);
            }
            set
            {
                SetValue(ItemColorBindingProperty, value);
            }
        }

        private void ItemsBinding()
        {
            if (ItemsSource == null)
                return;
            IEnumerator iterator = ItemsSource.GetEnumerator();
            while (iterator.MoveNext())
            {
                object obj = iterator.Current;
                if (obj is INotifyPropertyChanged)
                {
                    ((INotifyPropertyChanged)obj).PropertyChanged += PieChart_PropertyChanged;
                }
            }

            Draw();
        }

        private void PieChart_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if ((e.PropertyName == ItemValueBinding) || (e.PropertyName == ItemColorBinding))
            {
                Draw();
            }
        }
        public PieChart()
        {
            InitializeComponent();
        }

        void Draw()
        {

            if (ItemsSource == null)
                return;
            int idx = 0;
            IEnumerator iterator = ItemsSource.GetEnumerator();
            while (iterator.MoveNext())
            {
                object obj = iterator.Current;
                Type t = obj.GetType();
                System.Reflection.PropertyInfo pi_value = t.GetProperty(ItemValueBinding);
                System.Reflection.PropertyInfo pi_color = t.GetProperty(ItemColorBinding);
                if (pi_value == null)
                    return;
                //if (!(pi_value.PropertyType is System.IConvertible))
                //    return;

                double v = Convert.ToDouble(pi_value.GetValue(obj, null));
                Brush c = null;

                if (pi_color != null)
                    c = (Brush)pi_color.GetValue(obj, null);

                PicChartItem item;
                if (idx < items.Count)
                {
                    item = items[idx];
                }
                else
                {
                    item = new PicChartItem();
                    items.Add(item);
                }

                item.Value = v;
                if (c != null)
                    item.Color = c;
                else
                    item.Color = GetColor();

                idx++;
            }
            //把多余的删除
            if(items.Count != idx)
                items.RemoveRange(idx, items.Count - idx);

            if (items.Count() == 0)
                return;

            RootGrid.Children.Clear();

            double total = items.Sum((item) => { return item.Value; });
           
            double startangle = 0;
            foreach (PicChartItem item in items)
            {

                double angle;
                if (total > 0)
                    angle = item.Value / total * 360;
                else
                    angle = 360 / items.Count;

                Arc a = new Arc()
                {
                    ArcThickness = 200,
                    Stretch = Stretch.None,
                    StartAngle = startangle,
                    EndAngle = startangle + angle,
                    Fill = item.Color
                };
                startangle += angle;
                RootGrid.Children.Add(a);
            }
            
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            Draw();
        }

        class PicChartItem : INotifyPropertyChanged
        {

            /// <summary>
            /// 值
            /// </summary>
            public double Value { get; set; }

 
            /// <summary>
            /// 颜色
            /// </summary>
            public Brush Color { get; set; }


            #region INotifyPropertyChanged 成员
            public event PropertyChangedEventHandler PropertyChanged;
            #endregion
        }
    }

}