WPF统计(饼图,折线,柱图)控件与调用

1:柱图

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
             xmlns:dxdb="http://schemas.devexpress.com/winfx/2008/xaml/demobase"
             xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/charts"   
             xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
             xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib"
             xmlns:local="clr-namespace:IShiTong.Project.WpfChart.Common"
             mc:Ignorable="d"
             d:DesignHeight="419" x:Name="thiself"  d:DesignWidth="475" Foreground="#67FFFFFF" Background="#67FFFFFF" BorderBrush="#67FFFFFF">
  
                                  MouseLeave="chart_MouseLeave" EnableAnimation="True"  BorderThickness="0" Height="Auto" Width="Auto" FontWeight="Bold" FontSize="14">
       
       
           
               
                   
                       
                           
                       

                   

               

               
                   
                       
                           
                               
                           
                       

                   

               

               
                   
                   

               

               
               

           

         
       

       
           
       

       
           
           
       

       
           
       

   

cs文件、

public Bar3DControlExtend()
        {
            InitializeComponent();
         
            //this.SetStyle(ControlStyles.AllPaintingInWmPaint | //不擦除背景 ,减少闪烁
            //                  ControlStyles.OptimizedDoubleBuffer | //双缓冲
            //                  ControlStyles.UserPaint | //使用自定义的重绘事件,减少闪烁
            //                  ControlStyles.ResizeRedraw |
            //                  ControlStyles.Selectable |
            //                  ControlStyles.SupportsTransparentBackColor,
            //                  true);
            this.TitleDate = DateTime.Now.ToString("yyyy年MM月dd日");
            this.Loaded += new RoutedEventHandler(Bar3DControlExtend_Loaded);
            this.chart.Palette = new IShiTongPalette();
        }
        public ChartControl CurrentChart
        {
            get { return this.chart; }
            set { this.chart = value; }
        }
        void Bar3DControlExtend_Loaded(object sender, RoutedEventArgs e)
        {
            this.chart.Animate();
        }

        public BarSideBySideSeries2D GetBaseBarSerie(string name,string displayname)
        {
            BarSideBySideSeries2D BarSeries = new BarSideBySideSeries2D();
            //BarSeries.Name = name;
            BarSeries.DisplayName = displayname;
            BarSeries.AnimationAutoStartMode = AnimationAutoStartMode.SetStartState;
            BarSeries.Model=new GlassCylinderBar2DModel();
            SeriesLabel label = new SeriesLabel(){Indent=20,ResolveOverlappingMode= ResolveOverlappingMode.Default,ConnectorThickness=1, Visible=true};
            BarSeries.Label = label;

            return BarSeries;
        }


        public void InitData(List> datasource, string displayname, string x, string y)
        {
            foreach (var item in datasource)
            {
                if (item.Count > 0)
                {
                   
                    string disname = Utils.GetValueByName(item[0], displayname);
                    BarSideBySideSeries2D line = this.GetBaseBarSerie(disname, disname);
                    foreach (var child in item)
                    {
                        string xvalue = Utils.GetValueByName(child, x);
                        if (!string.IsNullOrWhiteSpace(xvalue))
                        {
                            if (xvalue.Length > 5)
                            {
                                StringBuilder sb = new StringBuilder();
                                sb.AppendLine(xvalue.Substring(0, 5));
                                sb.AppendLine(xvalue.Substring(5, xvalue.Length - 5));
                                xvalue = sb.ToString();
                            }
                        }
                        string yvalue = Utils.GetValueByName(child, y);
                        line.Points.Add(new SeriesPoint(xvalue, double.Parse(yvalue)));
                    }
                    this.chart.Diagram.Series.Add(line);
                }
            }
        }

       
        public string TitleText
        {
            get { return (string)GetValue(TitleTextProperty); }
            set { SetValue(TitleTextProperty, value); }
        }
        public static readonly DependencyProperty TitleTextProperty =
            DependencyProperty.Register("TitleText", typeof(string), typeof(Bar3DControlExtend));
        //readonly


        public string TitleDate
        {
            get { return (string)GetValue(TitleDateProperty); }
            set { SetValue(TitleDateProperty, value); }
        }
        public static readonly DependencyProperty TitleDateProperty =
            DependencyProperty.Register("TitleDate", typeof(string), typeof(Bar3DControlExtend));


2 饼图

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
    xmlns:dxdb="http://schemas.devexpress.com/winfx/2008/xaml/demobase"
    xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/charts"
    xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
    xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib"
     xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
       xmlns:wf ="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"                 
    xmlns:local="clr-namespace:IShiTong.Project.WpfChart.Common"
          x:Name="thiself" Foreground="#67FFFFFF" Background="#67FFFFFF" BorderBrush="#67FFFFFF" OverridesDefaultStyle="False" Opacity="1">
   
   
       
    
           
           
           
           
               
           

       

   

   
       
           
               
           

       

   

   

       
          
                                          QueryChartCursor="chart_QueryChartCursor" BorderThickness="0" IsManipulationEnabled="True" SizeChanged="chart_SizeChanged" EnableAnimation="True" FontWeight="Bold" FontSize="14">
              
               
                   
                       
                           
                               
                                   
                               

                               
                                   
                                   
                                       
                                           
                                       

                                   

                               

                           

                       

                   

               

               
                   
                       
                           
                               
                                   
                               

                           

                       

                       
                           
                               
                                   
                                       
                                           
                                       

                                       
                                           
                                       

                                   

                               

                             
                               
                                   
                               

                               
                                   
                               

                               
                                   
                               

                               
                                   
                                       
                                           
                                       

                                   
                               

                         
                           

                       

                   

               

               
                   
                   
               

               
                   
               

               
                   
               

           
       
   
   
 


cs文件

       const int clickDelta = 200;

        bool isLeftMouseButtonReleased = true;
        int mouseDownTime;

        public override ChartControl ActualChart { get { return chart; } }

        public Pie3D()
        {
            InitializeComponent();
            this.TitleDate = DateTime.Now.ToString("yyyy年MM月dd日");
            this.Loaded += new RoutedEventHandler(Pie3DControl_Loaded);
            this.ModuleAppear += new RoutedEventHandler(Pie3DControl_ModuleAppear);
           
        }
        public ChartControl CurrentChart
        {
            get { return this.chart; }
            set { this.chart = value; }
        }
        public string TitleText
        {
            get { return (string)GetValue(TitleTextProperty); }
            set { SetValue(TitleTextProperty, value); }
        }
        public static readonly DependencyProperty TitleTextProperty =
            DependencyProperty.Register("TitleText", typeof(string), typeof(Pie3D));
        //readonly
        public string TitleDate
        {
            get { return (string)GetValue(TitleDateProperty); }
            set { SetValue(TitleDateProperty, value); }
        }
        public static readonly DependencyProperty TitleDateProperty =
            DependencyProperty.Register("TitleDate", typeof(string), typeof(Pie3D));
  

        void Pie3DControl_ModuleAppear(object sender, RoutedEventArgs e)
        {
           
            this.chart.Animate();
        }

        void Pie3DControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.RaiseModuleAppear();
          
        }
        bool IsClick(int mouseUpTime)
        {
            return mouseUpTime - mouseDownTime < clickDelta;
        }
       
        void chart_MouseDown(object sender, MouseButtonEventArgs e)
        {
        }
        void chart_MouseUp(object sender, MouseButtonEventArgs e)
        {
        }


        void chart_QueryChartCursor(object sender, QueryChartCursorEventArgs e)
        {
         
        }

        private void chart_SizeChanged(object sender, SizeChangedEventArgs e)
        {
          
        }


        private PieSeries3D GetBaseBarSerie(string name, string displayname)
        {
            PieSeries3D pie = new PieSeries3D() { ArgumentScaleType = ScaleType.Qualitative, DepthTransform = 0.5, HoleRadiusPercent = 0, AnimationAutoStartMode = AnimationAutoStartMode.PlayOnce };
            PointOptions option = new PointOptions() { PointView = PointView.ArgumentAndValues };
            pie.PointOptions = option;
            PercentOptions po = new PercentOptions();
            po.ValueAsPercent = false;
            PieSeries3D.SetPercentOptions(option, po);

            pie.Model = new SemiRectanglePie3DModel();

            pie.Label = new SeriesLabel() { Visible = true, Indent = 20, RenderMode = LabelRenderMode.CustomShape, ResolveOverlappingMode = ResolveOverlappingMode.JustifyAllAroundPoint };
            pie.LegendPointOptions = option;
            PieSeries3D.SetLabelPosition(pie.Label, PieLabelPosition.Inside);
            return pie;

        }
        public void InitData(List> datasource, string displayname, string x, string y)
        {//"Name","X","Y"
            string disname = Utils.GetValueByName(datasource[0], displayname);
            PieSeries3D pie = this.GetBaseBarSerie(displayname, displayname);
            foreach (var item in datasource)
            {
                if (item.Count > 0)
                {
                 
                    foreach (var child in item)
                    {
                        string xvalue = Utils.GetValueByName(child, x);
                        if (!string.IsNullOrWhiteSpace(xvalue))
                        {
                            if (xvalue.Length > 5)
                            {
                                StringBuilder sb = new StringBuilder();
                                sb.AppendLine(xvalue.Substring(0, 5));
                                sb.AppendLine(xvalue.Substring(5, xvalue.Length - 5));
                                xvalue =sb.ToString();
                            }
                        }
                        string yvalue = Utils.GetValueByName(child, y);
                        if (datasource.Count == 1)
                        {
                            pie.Points.Add(new SeriesPoint( xvalue, double.Parse(yvalue)));
                        }
                        else
                        {
                            pie.Points.Add(new SeriesPoint(xvalue, double.Parse(yvalue)));
                        }
                    }
                  
                }
            }
            this.chart.Diagram.Series.Add(pie);
        }


3 折线

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
             xmlns:dxdb="http://schemas.devexpress.com/winfx/2008/xaml/demobase"
             xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/charts"   
             xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
             xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib"
             xmlns:local="clr-namespace:IShiTong.Project.WpfChart.Common"
             mc:Ignorable="d"
            d:DesignHeight="419" x:Name="thiself"  d:DesignWidth="475" Foreground="#67FFFFFF" Background="#67FFFFFF" BorderBrush="#67FFFFFF"
            
             >
   
       
                            Background="{Binding Path=Label.Background}"
                BorderBrush="{Binding Path=Label.BorderBrush}"
                BorderThickness="{Binding Path=Label.BorderThickness}">
               
                   
               

               
           

       

   

   
       
           
       

       
           
           
       

       
           
               
                   
                       
                           
                       

                   

               

               
                   
                   

               

               
                  
                   
                       
                           
                       

                       
                           
                       

                   

               

               
                   
                
               

           

       
       
           
               
                   
               

           

       

   


cs文件

        public Line2DControl()
        {
            InitializeComponent();
            this.TitleDate = DateTime.Now.ToString("yyyy年MM月dd日");
            this.Loaded += new RoutedEventHandler(Line2DControl_Loaded);
            this.chart.Palette = new IShiTongPalette();
        }


   
       
        void Line2DControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.chart.Animate();
        }
        public ChartControl CurrentChart
        {
            get { return this.chart; }
            set {
                this.chart = value;
               
            }
        }
        public LineSeries2D CreateBaseSerie()
        {
            LineSeries2D LineSeries = new LineSeries2D();
            //创建line动画
            Line2DStretchFromNearAnimation lineanimation = new Line2DStretchFromNearAnimation();
            lineanimation.Duration = new TimeSpan(0, 0, 2);
            LineSeries.SeriesAnimation = lineanimation;
            //创建point动画
            Marker2DSlideFromTopAnimation pointanimation = new Marker2DSlideFromTopAnimation();
            pointanimation.Duration = new TimeSpan(0, 0, 2);
            LineSeries.PointAnimation = pointanimation;

            SeriesLabel lable = new SeriesLabel();
            MarkerSeries2D.SetAngle(lable, 90);
            LineSeries.Label = lable;
            return LineSeries;
        }

        public string TitleText
        {
            get { return (string)GetValue(TitleTextProperty); }
            set { SetValue(TitleTextProperty, value); }
        }
        public static readonly DependencyProperty TitleTextProperty =
            DependencyProperty.Register("TitleText", typeof(string), typeof(Line2DControl));
        //readonly
        public string TitleDate
        {
            get { return (string)GetValue(TitleDateProperty); }
            set { SetValue(TitleDateProperty, value); }
        }
        public static readonly DependencyProperty TitleDateProperty =
            DependencyProperty.Register("TitleDate", typeof(string), typeof(Line2DControl));

     

        public void InitData(List> datasource,string displayname,string x,string y)
        {
            //if (!string.IsNullOrWhiteSpace(x))
            //{
            //    if (x.Length > 5)
            //    {
            //        StringBuilder sb = new StringBuilder();
            //        sb.AppendLine(x.Substring(0, 5));
            //        sb.AppendLine(x.Substring(5, x.Length - 5));
            //        x = sb.ToString();
            //    }
            //}
            foreach (var item in datasource)
            {
                if (item.Count > 0)
                {
                     LineSeries2D line = this.CreateBaseSerie();
                     line.DisplayName = Utils.GetValueByName(item[0], displayname);
                    foreach (var child in item)
                    {
                        line.Points.Add(new SeriesPoint(Utils.GetValueByName(child, x), double.Parse(Utils.GetValueByName(child, y))));
                    }
                    this.chart.Diagram.Series.Add(line);
                }
            }
        }

        public DataTemplate GetResource()
        {
            return this.Resources["AxisXLabelTemplate"] as DataTemplate;
        }


4 调用方式

 public string ProduceImageByEntity(int showinfr, List pushResultList, long moduleid, string titleText, int key, System.Windows.Forms.Integration.ElementHost elementHost)
        {
            string datepath = FileImagePath();
            string savepath = cacheDirectory + titleText + "&" + key.ToString() + datepath + ".txt";
            lock (obj)
            {
                elementHost.Size = this.Size;
                elementHost.Name = "elementHost";
                if (showinfr == Helpers.SystemConst.INDEXONE)
                {
                    WpfChart.Common.Bar3DControlExtend bar3d = new WpfChart.Common.Bar3DControlExtend();
                    bar3d.CurrentChart.EnableAnimation = false;
                    bar3d.TitleText = titleText;
                    bar3d.InitData(ChangeLx(pushResultList, showinfr), "Pushitemname", "Xvalue", "Yvalue");
                    elementHost.Child = bar3d;
                }
                else if (showinfr == Helpers.SystemConst.INDEXTWO)
                {
                    WpfChart.Common.Pie3D Pie3D = new WpfChart.Common.Pie3D();
                    Pie3D.CurrentChart.EnableAnimation = false;
                    Pie3D.TitleText = titleText;
                    Pie3D.InitData(ChangeLx(pushResultList, showinfr), "Pushitemname", "Xvalue", "Yvalue");
                    elementHost.Child = Pie3D;
                }
                else if (showinfr == Helpers.SystemConst.INDEXTHREE)
                {
                    WpfChart.Common.Line2DControl Line2D = new WpfChart.Common.Line2DControl();
                    Line2D.TitleText = titleText;
                    Line2D.CurrentChart.EnableAnimation = false;
                    Line2D.InitData(ChangeLx(pushResultList, showinfr), "Pushitemname", "Xvalue", "Yvalue");
                    elementHost.Child = Line2D;
                }
                else if (showinfr == Helpers.SystemConst.INDEXFOUR)
                {
                    WpfChart.Common.StackedBar3DControl Stacker3d = new WpfChart.Common.StackedBar3DControl(); ;
                    Stacker3d.CurrentChart.EnableAnimation = false;
                    Stacker3d.TitleStackedBar3dText = titleText;
                    Stacker3d.InitData(ChangeLx(pushResultList, showinfr), "Pushitemname", "Xvalue", "Yvalue");
                    elementHost.Child = (System.Windows.UIElement)Stacker3d;
                }
            }
            using (Bitmap tobit = new Bitmap(this.Width, this.Height))
            {
                this.Controls.Add(elementHost);
                this.DrawToBitmap(tobit, new Rectangle(0, 0, this.Width, this.Height));
                if (!string.IsNullOrWhiteSpace(savepath))
                {
                    byte[] bitbyte =Helpers.CPlusHelper.SerializeObject(tobit);
                    if (bitbyte != null)
                    {
                        File.WriteAllBytes(savepath, bitbyte);
                    }
                }
              
                //tobit.Save(savepath);
            }
            foreach (System.Windows.Forms.Integration.ElementHost control in this.Controls)
            {
                if (!control.IsDisposed)
                {
                    control.Dispose();
                }
            }
            this.Controls.Remove(elementHost);
            System.GC.Collect();
            return savepath;
        }

     


        private List> ChangeLx(List list, int showfrm)
        {
            List> changelist = new List>();
            if (list == null || list.Count <= Helpers.SystemConst.INDEXZERO)
            {
                return changelist;
            }
            if (showfrm.Equals(Helpers.SystemConst.INDEXTHREE))
            {
                IEnumerable> eventLst = list.OrderBy(evnt => evnt.Pushitemname).ToList().GroupBy(evnt => evnt.Pushitemname).ToList();
                foreach (IGrouping eventGroup in eventLst)
                {
                    List lt = eventGroup.ToList();
                    List listobj = new List();
                    foreach (PushResult entity in lt)
                    {
                        object obj = new ValueTable(entity.Pushitemname, entity.Xvalue, double.Parse(entity.YValue));
                        listobj.Add(obj);
                    }
                    changelist.Add(listobj);
                }
            }
            else
            {
                foreach (var item in list)
                {
                    List listobj = new List();
                    object obj = new ValueTable(item.Pushitemname, item.Xvalue, double.Parse(item.YValue));
                    listobj.Add(obj);
                    changelist.Add(listobj);
                }
            }
            return changelist;
        }


        private void PushGdiBar_Load(object sender, EventArgs e)
        {

        }
    }

    #region 统计项数据形式集
    class ValueTable
    {
        private string pushResult;
        public string PushResult
        {
            get { return pushResult; }
            set { this.pushResult = value; }
        }
        private string xvalue;
        public string Xvalue
        {
            get { return xvalue; }
            set { this.xvalue = value; }
        }

        private double yvale;
        public double Yvalue
        {
            get { return yvale; }
            set { this.yvale = value; }
        }
        public ValueTable(string pushResult, string xvalue, double yvale)
        {
            this.pushResult = pushResult;
            this.xvalue = xvalue;
            this.yvale = yvale;
        }

    }
    #endregion


实体

 //返回结果
    [Serializable]
    [DataContract]
    public class PushResult
    {
        public PushResult()
        { }
        public PushResult(string pushitemname, string xvalue, string yvalue)
        {
            this.pushitemname = pushitemname;
            this.xvalue = xvalue;
            this.yvalue = yvalue;
        }
        ///


        ///推送项名称
        ///

        private string pushitemname;
        public string Pushitemname
        {
            get { return pushitemname; }
            set { this.pushitemname = value; }
        }

        ///


        /// X轴值
        ///

        private string xvalue;
        public string Xvalue
        {
            get { return xvalue; }
            set { this.xvalue = value; }
        }

        ///


        /// Y轴点值
        ///

        private string yvalue;
        public string YValue
        {
            get { return yvalue; }
            set { this.yvalue = value; }
        }
    }





你可能感兴趣的:(c#,wpf)