原文:WPF原生环形图表

版权声明:欢迎转载。转载请注明出处,谢谢 https://blog.csdn.net/wzcool273509239/article/details/56480963

主要利用Canvas的子控件(两个圆环)的相对定位进行实现,目前高度和宽度是写死的,有需要的人可重写成自定义宽度和高度。

需要引用Microsoft.SDK.Expression.Blend,可通过NuGet获得

1. 界面UCCircleChart.xaml

 

<UserControl x:Class="HushuPlatform.CommUserControl.HomeLeft.UCCircleChart"
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:ed="http://schemas.microsoft.com/expression/2010/drawing"
mc:Ignorable="d" Width="250" Height="260">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="210"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Canvas Name="cavans" Height="210" Width="210">
<!--空白 作为背景。。Stroke边线颜色;Fill填充颜色-->
<ed:Arc Canvas.Left="5" Canvas.Top="5" ArcThickness="50" ArcThicknessUnit="Pixel"
HorizontalAlignment="Left" Stretch="None" Stroke="White" Fill="Transparent"
StartAngle="0" EndAngle="360"
VerticalAlignment="Top" Height="200" Width="200"/> <!--填充值 作为白分比,即更改EndAngle值即可。需要数值*3.6。因360度等分100份,每次3.6。Stroke边线颜色;Fill填充颜色-->
<ed:Arc x:Name="charValue" Canvas.Left="5" Canvas.Top="5" ArcThickness="50" ArcThicknessUnit="Pixel"
HorizontalAlignment="Left" Stretch="None" Stroke="White" Fill="White"
StartAngle="0" EndAngle="60"
VerticalAlignment="Top" Height="200" Width="200"/>
<!--中间显示的百分比-->
<TextBlock Name="txtChartValue" Canvas.Left="69" Canvas.Top="84" Text="50%" FontSize="36" Foreground="White" ></TextBlock>
</Canvas>
<TextBlock Name="txtAreaName" Grid.Row="1" Text="12321" Foreground="White" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Bottom"></TextBlock>
</Grid>
</UserControl>

 

 

2. 后台UCCircleChart.xaml.cs

 

using System;
using System.Collections.Generic;
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; namespace HushuPlatform.CommUserControl.HomeLeft
{
/// <summary>
/// 圆环形 Chart
/// </summary>
public partial class UCCircleChart : UserControl
{
/// <summary>
/// 百分比 (参数:percent=10,20,30,50.25 等。如是10%,则传10,依次类推)
/// </summary>
/// <param name="percent"></param>
public UCCircleChart(string percent)
{
InitializeComponent();
if (string.IsNullOrEmpty(percent))
{
percent = "0";
}
double EndAngle = Convert.ToDouble(percent) * 3.6;//360度等分100份,每次3.6
this.charValue.EndAngle = EndAngle;
this.txtChartValue.Text = percent + "%";
txtAreaName.Visibility = Visibility.Collapsed;
} /// <summary>
/// 百分比、名字 (参数:percent=10,20,30,50.25 等。如是10%,则传10,依次类推)
/// </summary>
/// <param name="percent">百分比</param>
/// <param name="areaName"></param>
public UCCircleChart(string percent, string areaName)
{
InitializeComponent();
if (string.IsNullOrEmpty(percent))
{
percent = "0";
}
double EndAngle = Convert.ToDouble(percent) * 3.6;//360度等分100份,每次3.6
this.charValue.EndAngle = EndAngle;
this.txtChartValue.Text = percent + "%";
txtAreaName.Text = areaName;
}
}
}

3.调用

 

      //各区域面积

        private double m_Area = 7000;

        private double m_Area2 = 1000;

        private double m_Area3 = 1000;

        private double m_Area4 = 5500;

 

double total = m_Area + m_Area2 + m_Area3 + m_Area4;

string tmp1 = (m_Area / total * 100).ToString("f0");

wpChart.Children.Add(new HushuPlatform.CommUserControl.HomeLeft.UCCircleChart(tmp1, "优质稻米片区"));

 

4. 补充

前端定义样式用于控制Canvas.Let 和Top

   <UserControl.Resources>

        <Style TargetType="{x:Type ed:Arc}">

            <Setter Property="Canvas.Left" Value="5" />

            <Setter Property="Canvas.Top" Value="5" />

        </Style>

    </UserControl.Resources>

 

后端循环创建arc

   public UCCircleChart(List<SeriesCharModel> list)
{
InitializeComponent();
this.charValue.Visibility = Visibility.Collapsed;
if (list != null && list.Count > 0)
{
double startAngngle = 0;
for (int i = 0; i < list.Count; i++)
{
Microsoft.Expression.Shapes.Arc arc = new Microsoft.Expression.Shapes.Arc();
arc.ArcThickness = 50;
arc.ArcThicknessUnit = Microsoft.Expression.Media.UnitType.Pixel;
arc.VerticalAlignment = VerticalAlignment.Top;
arc.HorizontalAlignment = HorizontalAlignment.Left;
System.Drawing.Color color = GetColor(list[i].CharColor);
arc.Stroke = new SolidColorBrush(System.Windows.Media.Colors.White);
arc.Stretch = Stretch.None;
arc.Fill = new SolidColorBrush(System.Windows.Media.Color.FromArgb(color.A, color.R, color.G, color.B));
arc.Height = 200;
arc.Width = 200;
arc.StartAngle = startAngngle;
arc.EndAngle = arc.StartAngle + list[i].ChartValue;
this.cavans.Children.Add(arc);
startAngngle += arc.EndAngle;//下一个的紧挨着上一个
}
}
} /// <summary>
/// Get color, param like "#000000" or "#00f" or "255,255,255"
/// </summary>
/// <param name="fontColorARGB"></param>
/// <returns></returns>
private System.Drawing.Color GetColor(string fontColorARGB)
{
System.Drawing.ColorConverter convert = new System.Drawing.ColorConverter();
var tmpColor = System.Drawing.ColorTranslator.FromHtml(fontColorARGB);
return tmpColor;
}

 

 

最新文章

  1. geotrellis使用(十三)数据导入BUG解决方案说明
  2. iOS-私有API与runtime
  3. android 得到缩略图
  4. 问题:Virtual Box如何复制镜像
  5. SCons - 简单而强大的项目编译脚本
  6. 《C语言入门很简单》欢乐槽点
  7. 友情提醒:欲开发android5.0以上应用,请全部更新开发工具至最新
  8. mysql常用脚本
  9. SQL2008-字符转数字CAST和CONVERT
  10. IBM Rational ClearCase 部署指南
  11. BZOJ 1367([Baltic2004]sequence-左偏树+中位数贪心)
  12. 1.cocos2dx 3.2环境结构
  13. 第十六篇 基于Bootstarp 仿京东多条件筛选插件的开发(展示上)
  14. go Mutex (互斥锁)和RWMutex(读写锁)
  15. python模块学习之hashlib模块学习
  16. hive sqoop,sqoop-hive import data
  17. php面向对象的封装性
  18. maven安装配置部署建项运行
  19. console的所有用法
  20. widows终端远程连接Linux服务器

热门文章

  1. 第n个质数
  2. jsp js action之间传值
  3. Hibernate 一对多
  4. struts配置问题
  5. Newton均差插值性质证明
  6. hdu 2151
  7. UNIX环境高级编程(5):文件I/O(1)
  8. 每一个程序猿都须要了解的一个SQL技巧
  9. Android之弹出菜单框【注冊上下文菜单】
  10. 巧用select延时