網(wǎng)站首頁 編程語言 正文
每日一笑
下班和實(shí)習(xí)生一起回家,公交站等車,一乞丐把碗推向?qū)嵙?xí)生乞討。這時(shí),實(shí)習(xí)生不慌不忙的說了句:“我不要你的錢,你這錢來的也不容易。”?
前言?
有小伙伴需要統(tǒng)計(jì)圖。
效果預(yù)覽(更多效果請(qǐng)下載源碼體驗(yàn))
一、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="微信公眾號(hào):WPF開發(fā)者" 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];
else
PieSegmentModels = collectionList[0];
isRefresh = !isRefresh;
}
}
}
原文鏈接:https://www.cnblogs.com/yanjinhua/p/15353711.html
相關(guān)推薦
- 2022-12-09 ReactQuery系列React?Query?實(shí)踐示例詳解_React
- 2022-12-29 React點(diǎn)擊事件的兩種寫法小結(jié)_React
- 2022-10-01 React?Hook中useState更新延遲問題及解決_React
- 2022-11-06 pandas?dataframe?drop函數(shù)介紹_python
- 2022-12-13 sql索引失效的情況以及超詳細(xì)解決方法_MsSql
- 2022-08-25 python數(shù)學(xué)建模之三大模型與十大常用算法詳情_python
- 2022-07-27 利用Python中的內(nèi)置open函數(shù)讀取二進(jìn)制文件_python
- 2022-04-20 python錯(cuò)誤提示:Errno?2]?No?such?file?or?directory的解決方法
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支