WPF DevExpress ChartControl使用之PieChart

摘要:
饼图比XY图简单一点。基本上是一样的。没有X和Y坐标轴,就没有第二个坐标。这有点简单。

饼状图要比XYDiagram要简单一点,大体上也是那些东西,没有了X、Y坐标轴,也就没有了第二坐标,要简单一点。
WPF饼状图
PieChartControl.xaml

<UserControl x:Class="WpfControl.PieChartControl"
         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:dxc="http://schemas.devexpress.com/winfx/2008/xaml/charts"
         mc:Ignorable="d">
    <Grid>
        <dxc:ChartControl x:Name="chart"
                          MouseDown="chart_MouseDown"
                          MouseMove="chart_MouseMove"
                          MouseUp="chart_MouseUp">
            <dxc:ChartControl.Titles>
                <dxc:Title x:Name="title" Content="Title" Dock="Top" HorizontalAlignment="Center" VerticalAlignment="Top"/>
            </dxc:ChartControl.Titles>
            <dxc:SimpleDiagram2D x:Name="diagram2D">
            </dxc:SimpleDiagram2D>
            <dxc:ChartControl.Legend>
                <dxc:Legend HorizontalPosition="Right"/>
            </dxc:ChartControl.Legend>
        </dxc:ChartControl>
    </Grid>
</UserControl>

PieChartControl.cs

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using DevExpress.Xpf.Charts;
using Dugufeixue.Common;
namespace WpfControl
{
    /// <summary>
    /// PieChartControl.xaml 的交互逻辑
    /// </summary>
    public partial class PieChartControl : UserControl
    {
        const int clickDelta = 200;
        DateTime mouseDownTime;
        bool rotate;
        Point startPosition;
        public PieChartControl(DataEntity de, bool isUseColor)
        {
            InitializeComponent();
            LoadPieChart(de, isUseColor);
        }
        public void LoadPieChart(DataEntity de, bool isUseColor)
        {
            Series series= new PieSeries2D();
            //设置饼状图Label样式
            SeriesLabel label = new SeriesLabel();
            label.Indent = 15;
            label.TextPattern = "{A}: {VP:P2}";
            series.Label = label;
            foreach (KeyValuePair<string, double> kvp in de.Dic)
            {
                //这里和XYDiagram不一样,饼状图只能用SeriesPoint添加点来创建Series,没找到其他的好方法
                SeriesPoint point = new SeriesPoint(kvp.Key, kvp.Value);
                if (isUseColor)
                {
                    SolidColorBrush brush;
                    if (kvp.Key.Contains("运行"))
                    {
                        brush = new SolidColorBrush(Color.FromRgb(0x00, 0xFF, 0x00));
                    }
                    else if (kvp.Key.Contains("启动"))
                    {
                        brush = new SolidColorBrush(Color.FromRgb(0x00, 0xFF, 0x00));
                    }
                    else if (kvp.Key.Contains("正常"))
                    {
                        brush = new SolidColorBrush(Color.FromRgb(0x00, 0xFF, 0x00));
                    }
                    else if (kvp.Key.Contains("停止"))
                    {
                        brush = new SolidColorBrush(Color.FromRgb(0xFF, 0x00, 0x00));
                    }
                    else if (kvp.Key.Contains("启炉"))
                    {
                        brush = new SolidColorBrush(Color.FromRgb(0x00, 0xFF, 0x00));
                    }
                    else if (kvp.Key.Contains("停炉"))
                    {
                        brush = new SolidColorBrush(Color.FromRgb(0xFF, 0x00, 0x00));
                    }
                    else if (kvp.Key.Contains(""))
                    {
                        brush = new SolidColorBrush(Color.FromRgb(0x00, 0xFF, 0x00));
                    }
                    else if (kvp.Key.Contains(""))
                    {
                        brush = new SolidColorBrush(Color.FromRgb(0xFF, 0x00, 0x00));
                    }
                    else if (kvp.Key.Contains("故障"))
                    {
                        brush = new SolidColorBrush(Color.FromRgb(0xFF, 0xFF, 0x00));
                    }
                    else if (kvp.Key.Contains("需监管"))
                    {
                        brush = new SolidColorBrush(Color.FromRgb(0x00, 0x00, 0xFF));
                    }
                    else if (kvp.Key.Contains("反馈"))
                    {
                        brush = new SolidColorBrush(Color.FromRgb(0x00, 0x00, 0xFF));
                    }
                    else if (kvp.Key.Contains("高料位报警"))
                    {
                        brush = new SolidColorBrush(Color.FromRgb(0xFF, 0xC0, 0xCB));
                    }
                    else if (kvp.Key.Contains("无状态"))
                    {
                        brush = new SolidColorBrush(Color.FromRgb(0xCC, 0xCC, 0xCC));
                    }
                    else if (kvp.Key.Contains(""))
                    {
                        brush = new SolidColorBrush(Color.FromRgb(0xCC, 0xCC, 0xCC));
                    }
                    else
                    {
                        brush = new SolidColorBrush(Color.FromRgb(0xCC, 0xCC, 0xCC));
                    }
                    //设置每一个扇形的不同颜色
                    point.Brush = brush;
                }
                series.Points.Add(point);
            }
            series.LabelsVisibility = true;
            PieSeries2D.SetLabelPosition(series.Label,PieLabelPosition.Outside);
            //设置Legend的格式
            series.LegendTextPattern = "{A}: {VP:P2}";
            diagram2D.Series.Add(series);
            title.Content = de.Ytitle;
        }
        //设置饼状图的动画效果
        bool IsClick(DateTime mouseUpTime)
        {
            return (mouseUpTime - mouseDownTime).TotalMilliseconds < clickDelta;
        }
        double CalcAngle(Point p1, Point p2)
        {
            Point center = new Point(chart.Diagram.ActualWidth / 2, chart.ActualHeight / 2);
            Point relativeP1 = new Point(p1.X - center.X, p1.Y - center.Y);
            Point relativeP2 = new Point(p2.X - center.X, p2.Y - center.Y);
            double angleP1Radian = Math.Atan2(relativeP1.X, relativeP1.Y);
            double angleP2Radian = Math.Atan2(relativeP2.X, relativeP2.Y);
            double angle = (angleP2Radian - angleP1Radian) * 180 / (Math.PI * 2);
            if (angle > 90)
                angle = 180 - angle;
            else if (angle < -90)
                angle = 180 + angle;
            return angle;
        }
        void chart_MouseUp(object sender, MouseButtonEventArgs e)
        {
            ChartHitInfo hitInfo = chart.CalcHitInfo(e.GetPosition(chart));
            rotate = false;
            if (hitInfo == null || hitInfo.SeriesPoint == null || !IsClick(DateTime.Now))
                return;
            double distance = PieSeries.GetExplodedDistance(hitInfo.SeriesPoint);
            Storyboard storyBoard = new Storyboard();
            DoubleAnimation animation = new DoubleAnimation();
            animation.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 300));
            animation.To = distance > 0 ? 0 : 0.3;
            storyBoard.Children.Add(animation);
            Storyboard.SetTarget(animation, hitInfo.SeriesPoint);
            Storyboard.SetTargetProperty(animation, new PropertyPath(PieSeries.ExplodedDistanceProperty));
            storyBoard.Begin();
        }
        void chart_MouseDown(object sender, MouseButtonEventArgs e)
        {
            mouseDownTime = DateTime.Now;
            Point position = e.GetPosition(chart);
            ChartHitInfo hitInfo = chart.CalcHitInfo(position);
            if (hitInfo != null && hitInfo.SeriesPoint != null)
            {
                rotate = true;
                startPosition = position;
            }
        }
        void chart_MouseMove(object sender, MouseEventArgs e)
        {
            Point position = e.GetPosition(chart);
            ChartHitInfo hitInfo = chart.CalcHitInfo(position);
            if (hitInfo == null)
                return;
            if (rotate && !IsClick(DateTime.Now))
            {
                PieSeries2D series = chart.Diagram.Series[0] as PieSeries2D;
                double angleDelta = CalcAngle(startPosition, position);
                startPosition = position;
            }
        }
    }
}

免责声明:文章转载自《WPF DevExpress ChartControl使用之PieChart》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Socket编程 (一)Unity灯光详解下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

安装qmake与环境变量解析

如果你已经有了qmake,可以跳过这里,请看10分钟学会使用qmake。手动安装qmake在手工连编Qt之前,下面这些环境变量必须被设置:QMAKESPEC这个必须设置为你所使用的系统的平台和编译器的组合。当编译完成时,qmake已经可以使用了。这里对添加环境变量时,是在path里头添加,还是new一个变量有点疑惑。而如果是new的话,当我们在为程序添加路径...

流控制、FlowControl

作用就是防止网络拥堵时导致的“丢包”问题,大致的工作原理就是当链路两端的设备有一端忙不过来了,他会给另外一端的设备发一个暂停发包的命令,通过这种方式来缓解压力,解决丢包问题。看上去流控制应该是个非常好的防止丢包的方法,但是为什么我们还要在无盘上关闭他呢?...

Navicat数据存放位置和备份数据库路径设置

navicat数据库存储在哪里?有了这样的问题,让我们来解决这个问题。默认情况下安装Navicat,默认情况下也安装MySQL,数据库存储在默认用户的目录中。选择安装目录时,还可以选择数据的位置。很多人此时只是设置了MySQL的安装位置。...

Windows怎么从命令行下载文件

具体步骤如下:1.打开cmd。exeWin+R或git的bush接口。2.启动powershell。2.在命令行中输入startpowershell以启动powershell。3.下载操作。1.在powershell中输入$client=newobjectSystem.Net.WebClient3.2。exe文件,然后输入$client。下载文件('X','...

Python读取JSON数据操作实例解析

为了遵循JSON规范,您应该只编写Python列表和字典。JSON编码格式与Python语法几乎相同,只是存在一些细微差异。...

uniapp原生插件开发及打包发布

二.调试原生插件:1.使用hx创建uniapp项目2.创建vue页面或者nvue,在js中第一行获取插件对象vartestModule=uni.requireNativePlugin3.然后使用插件对象就可以调用到插件中的方法4.在hx上的菜单上:发行-原生app-本地打包-生成本地app打包资源;5.打包完成会在控制台上输出打包信息,我们关注的是打包路径;...