WPF开发者QQ群: 340500857  | 微信群 -> 进入公众号主页 加入组织

欢迎转发、分享、点赞、在看,谢谢~。

01

效果预览

效果预览(更多效果请下载源码体验):

一、PieControl.cs 代码如下

using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using WpfPieControl.Models;namespace WpfPieControl
{public class PieControl: Control{public ObservableCollection<PieSegmentModel> PieSegmentModels{get { return (ObservableCollection<PieSegmentModel>)GetValue(PieSegmentModelsProperty); }set { SetValue(PieSegmentModelsProperty, value); }}public static readonly DependencyProperty PieSegmentModelsProperty =DependencyProperty.Register("PieSegmentModels", typeof(ObservableCollection<PieSegmentModel>), typeof(PieControl), new UIPropertyMetadata(OnPieSegmentModelChanged));private static void OnPieSegmentModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){PieControl pieControl = d as PieControl;if (e.NewValue != null){var array = e.NewValue as ObservableCollection<PieSegmentModel>;double angleNum = 0;foreach (var item in array){var color = new SolidColorBrush((Color)ColorConverter.ConvertFromString(pieControl.ColorArray[array.IndexOf(item)]));item.Color = color;item.StartAngle = angleNum;item.EndAngle = angleNum + item.Value / 100 * 360;angleNum = item.EndAngle;}}}/// <summary>/// colors/// </summary>private string[] ColorArray = new string[] { "#FDC006", "#607E89", "#2095F2", "#F34336" };/// <summary>/// 0~1/// </summary>public double ArcThickness{get { return (double)GetValue(ArcThicknessProperty); }set { SetValue(ArcThicknessProperty, value); }}public static readonly DependencyProperty ArcThicknessProperty =DependencyProperty.Register("ArcThickness", typeof(double), typeof(PieControl), new PropertyMetadata(1.0));static PieControl(){DefaultStyleKeyProperty.OverrideMetadata(typeof(PieControl), new FrameworkPropertyMetadata(typeof(PieControl)));}}
}

二、App.xaml 代码如下

<Style TargetType="{x:Type local:PieControl}"><Setter Property="UseLayoutRounding" Value="True" /><!--<Setter Property="Background" Value="#252525"/>--><Setter Property="Foreground" Value="White"/><Setter Property="Width" Value="250"/><Setter Property="Height" Value="250"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type local:PieControl}"><ItemsControl Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" ItemsSource="{TemplateBinding PieSegmentModels}"Background="{TemplateBinding Background}"><ItemsControl.ItemsPanel><ItemsPanelTemplate><Grid IsItemsHost="True"/></ItemsPanelTemplate></ItemsControl.ItemsPanel><ItemsControl.ItemTemplate><DataTemplate><ed:Arc Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"ArcThickness="{Binding ArcThickness,RelativeSource={RelativeSource FindAncestor,AncestorType=local:PieControl}}" ArcThicknessUnit="Percent"EndAngle="{Binding EndAngle}"StartAngle="{Binding StartAngle}"Stretch="None"ToolTip="{Binding Name}"Stroke="{Binding ColorStroke}"StrokeThickness="2"Fill="{Binding Color}"></ed:Arc></DataTemplate></ItemsControl.ItemTemplate></ItemsControl></ControlTemplate></Setter.Value></Setter>
</Style>

三、MainWindow.xaml 代码如下

<Window x:Class="WpfPieControl.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:WpfPieControl"mc:Ignorable="d"Title="微信公众号:WPF开发者" Height="450" Width="800"><StackPanel><WrapPanel Margin="10"><local:PieControl PieSegmentModels="{Binding PieSegmentModels,RelativeSource={RelativeSource AncestorType=local:MainWindow}}" ArcThickness="1"/><local:PieControl PieSegmentModels="{Binding PieSegmentModels,RelativeSource={RelativeSource AncestorType=local:MainWindow}}" Margin="4,0"ArcThickness="{Binding ElementName=PRAT_Slider,Path=Value}"/><local:PieControl PieSegmentModels="{Binding PieSegmentModels,RelativeSource={RelativeSource AncestorType=local:MainWindow}}" ArcThickness="0.65"/></WrapPanel><Slider Maximum="0.9" Minimum="0.1" x:Name="PRAT_Slider" Margin="10" Width="200"/><Button Content="更新" Click="Button_Click" VerticalAlignment="Bottom" Width="200"/></StackPanel>
</Window>

四、MainWindow.xaml.cs 代码如下

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
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;
using WpfPieControl.Models;namespace WpfPieControl
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{public ObservableCollection<PieSegmentModel> PieSegmentModels{get { return (ObservableCollection<PieSegmentModel>)GetValue(PieSegmentModelsProperty); }set { SetValue(PieSegmentModelsProperty, value); }}public static readonly DependencyProperty PieSegmentModelsProperty =DependencyProperty.Register("PieSegmentModels", typeof(ObservableCollection<PieSegmentModel>), typeof(MainWindow), new PropertyMetadata(null));List<ObservableCollection<PieSegmentModel>> collectionList = new List<ObservableCollection<PieSegmentModel>>();public MainWindow(){InitializeComponent();PieSegmentModels = new ObservableCollection<PieSegmentModel>();var collection1 = new ObservableCollection<PieSegmentModel>();collection1.Add(new PieSegmentModel { Name = "一", Value = 10 });collection1.Add(new PieSegmentModel { Name = "二", Value = 20 });collection1.Add(new PieSegmentModel { Name = "三", Value = 25 });collection1.Add(new PieSegmentModel { Name = "四", Value = 45 });var collection2 = new ObservableCollection<PieSegmentModel>();collection2.Add(new PieSegmentModel { Name = "一", Value = 30 });collection2.Add(new PieSegmentModel { Name = "二", Value = 15 });collection2.Add(new PieSegmentModel { Name = "三", Value = 10 });collection2.Add(new PieSegmentModel { Name = "四", Value = 55 });collectionList.AddRange(new[] { collection1, collection2 });PieSegmentModels = collectionList[0];}bool isRefresh = false;private void Button_Click(object sender, RoutedEventArgs e){if (!isRefresh)PieSegmentModels = collectionList[1];elsePieSegmentModels = collectionList[0];isRefresh = !isRefresh;}}
}

源码地址

github:https://github.com/yanjinhuagood/WPFDevelopers.git

gitee:https://gitee.com/yanjinhua/WPFDevelopers.git

WPF开发者QQ群: 340500857

blogs: https://www.cnblogs.com/yanjinhua

Github:https://github.com/yanjinhuagood

出处:https://www.cnblogs.com/yanjinhua

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

转载请著名作者 出处 https://github.com/yanjinhuagood

WPF实现统计图(饼图仿LiveCharts)相关推荐

  1. WPF系列教程——(一)仿TIM QQ界面 - 简书

    WPF系列教程--(一)仿TIM QQ界面 - 简书 原文: WPF系列教程--(一)仿TIM QQ界面 - 简书 TIM QQ 我们先来看一下TIM QQ长什么样,整体可以将界面分为三个部分 TIM ...

  2. 仿支付宝账单统计图(饼图)

    最近项目里用到的一个统计图,是参考支付宝账单的饼图效果做的,效果图如下: 代码在这里:http://download.csdn.net/detail/the_path/7823625

  3. WPF实现雷达图(仿英雄联盟)

    前言 有小伙伴提出需要实现雷达图. 由于在WPF中没有现成的雷达图控件,所以我们自己实现一个. PS:有更好的方式欢迎推荐. 代码如下 一.创建 RadarChart.cs 菜单继承 Control代 ...

  4. WPF实现时间轴(仿Gitee)

    WPF开发者QQ群: 340500857  | 微信群 -> 进入公众号主页 加入组织 " 前言,接着上一篇圆形菜单." 欢迎转发.分享.点赞.在看,谢谢~. 01 - 效果 ...

  5. android仿tim主界面,WPF系列教程——(一)仿TIM QQ界面

    TIM QQ 我们先来看一下TIM QQ长什么样,整体可以将界面分为三个部分 TIM QQ 1. 准备 阅读本文假设你已经有XAML布局的基础,所以只对部分布局进行说明. 界面上的图标均来自 Mate ...

  6. WPF C#截图功能 仿qq截图

    先上效果图 源码下载地址:http://download.csdn.net/detail/candyvoice/9788099 描述:启动程序,点击窗口button,开始截图,鼠标左键按下拖动,选中任 ...

  7. 看看这套WPF开源基础控件库:WPFDevelopers

    此项目包含了 微信公众号 < WPF开发者> 日常开发分享,欢迎Star. 运行环境 Visual Studio 2019,dotNet Framework 4.0 SDK 欢迎关注微信公 ...

  8. WPF仿微信界面发送消息简易版

    WPF仿微信界面发送消息简易版 参考别的博主的例子用WPF MVVM框架来仿了一个微信聊天界面,做了个发送消息简易功能,下面一起来看看吧! 以下为View视图布局代码,消息对话框的样式直接在这里定义了 ...

  9. WPF编程;上位机编程;C#编程;仿QQ基础实现(一)之界面预览

    简介 一.摘要 1.描述 2.关键字 二.什么是WPF 三.为什么选择WPF 四.仿QQ的登录界面 五.仿QQ联系人界面 六.源码下载 七.其他 八.参考 一.摘要 1.描述 本文主要描述的是如何通过 ...

最新文章

  1. django 快速实现文件上传
  2. 成功从小公司跳槽!35岁的程序员被裁
  3. python嵌入式系统开发_Python在开发实时嵌入式系统中的作用
  4. GDCM:gdcm::Image的测试程序
  5. mysql 秒杀 隔离级别_MySQL 四种隔离级别详解,看完吊打面试官
  6. java爬虫的2种爬取方式(HTTP||Socket)简单Demo(一)
  7. Chapter7-1_Overview of NLP Tasks
  8. 最易懂的layui分页
  9. C#开发微信门户及应用(27)-公众号模板消息管理
  10. caffeine 淘汰策略
  11. python搜索关键词自动提交_简单爬虫:调用百度接口,实现关键词搜索(python_003)...
  12. LNMP的403问题总结
  13. 开源html5游戏-少年行
  14. ixp协议服务器,ipx协议中的“内部网络号”是什么意思?
  15. 51单片机控制双步进电机的魔法师思想
  16. 看山聊Java:Date 与 LocalDate 或 LocalDateTime 互相转换
  17. Lake Shore PT-100铂电阻温度传感器
  18. 用Vue3写个气泡对话框组件
  19. 谷歌时代结束 - Google中国名称已经改回
  20. windows和android双系统平板,Windows平板打造双系统爽玩安卓APP

热门文章

  1. HtmlGenericControl
  2. logcat崩溃_使用logcat抓取Android崩溃日志
  3. c#用canny算子做边缘提取_机器视觉学习(三)边缘检测
  4. nginx-1.13.x源码安装
  5. iOS10 优化APP首次安装网络权限提示方案
  6. dpdk对虚拟化的支持调研
  7. APP访问路径和销售归因分析
  8. 通过SQL Server操作MySQL的步骤和方法
  9. 从Windows XP升级? 这是您需要了解的Windows 7
  10. ipad iphone开发_如何将iPhone或iPad置于恢复模式