首页 > 其他分享 >【Chart控件】饼状图

【Chart控件】饼状图

时间:2022-12-10 14:23:06浏览次数:37  
标签:控件 Windows Chart System KeyValuePair new using 状图

WPF没有系统的Chart控件,因此要使用第三方的chart控件库

1、在Nuget中添加DotNetProjects.DataVisualization.Toolkit。

Chart控件结构

饼状图

效果

 

 

xaml

<Window x:Class="testControl.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:testControl"
        xmlns:chart="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=DotNetProjects.DataVisualization.Toolkit" xmlns:visualizationToolkit="clr-namespace:System.Windows.Controls.DataVisualization;assembly=DotNetProjects.DataVisualization.Toolkit"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style x:Key="PieChartLegendItemStyle" TargetType="{x:Type chart:LegendItem}">
            <Setter Property="IsTabStop" Value="False" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type chart:LegendItem}">
                        <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                            <StackPanel Orientation="Horizontal">
                                <Rectangle Width="8" Height="8" Fill="{Binding Background}" Stroke="{Binding BorderBrush}" StrokeThickness="0" Margin="0,0,3,0" />
                                <visualizationToolkit:Title Content="{TemplateBinding Content}" />
                            </StackPanel>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="PieSeriesStyle1" TargetType="{x:Type chart:PieSeries}">
            <Setter Property="IsTabStop" Value="False"/>
            <Setter Property="Margin" Value="0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type chart:PieSeries}">
                        <Canvas x:Name="PlotArea" Margin="0" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>

        <!--表格-->
        <chart:Chart  Height="271" Name="chart1" Width="379"  BorderThickness="0" >
            <!--表格的LegEnd-->
                <chart:Chart.LegendStyle>
                    <Style TargetType="visualizationToolkit:Legend">
                        <Setter Property="Visibility" Value="Collapsed"/>
                        <Setter Property="Width" Value="0"/>
                        <Setter Property="Height" Value="0"/>
                    </Style>
                </chart:Chart.LegendStyle>

            <!--表格的绘图区域 -->
            <chart:Chart.PlotAreaStyle>
                    <Style TargetType="Grid">
                        <Setter Property="Panel.Background" Value="Transparent"/>
                    </Style>
                </chart:Chart.PlotAreaStyle>

            <!--表格的序列-->
            <chart:Chart.Series >
                <!--饼状图-->
                <chart:PieSeries   ItemsSource="{Binding}"
                    DependentValuePath="Value"
                    IndependentValuePath="Key"
                    Title="Pet Preference" IsSelectionEnabled="True"   />
            </chart:Chart.Series>
        </chart:Chart>
 
    </Grid>
</Window>

 

C# 代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.DataVisualization.Charting;
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 testControl
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            chart1.DataContext = new KeyValuePair<string, int>[] {
                                    new KeyValuePair<string, int>("Dog", 30),
                                    new KeyValuePair<string, int>("Cat", 25),
                                    new KeyValuePair<string, int>("Rat", 5),
                                    new KeyValuePair<string, int>("Hampster", 8),
                                    new KeyValuePair<string, int>("Rabbit", 12) };
        }
      
    }


}

 

标签:控件,Windows,Chart,System,KeyValuePair,new,using,状图
From: https://www.cnblogs.com/cdaniu/p/16971521.html

相关文章

  • MFC--StaticText静态文本框控件
    CStatic类提供了一个Windows静态控件的性能。一个静态控件用来显示一个文本字符串、框、矩形、图标、光标、位图或增强的图元文件。它可以被用来作为标签,框,或用来分隔其它的......
  • WinForm(三)揭开可视化控件的面纱
    WinForm所见即所得的UI设计框架,开发效率确实有所提升,同时降低了编程门槛,让WinForm更普及。拖拖拽拽就能设计出一个界面,那么我们拖拽的这些东西是什么?它们是什么原理?。......
  • WinForm(三)揭开可视化控件的面纱
    WinForm所见即所得的UI设计框架,开发效率确实有所提升,同时降低了编程门槛,让WinForm更普及。拖拖拽拽就能设计出一个界面,那么我们拖拽的这些东西是什么?它们是什么原理?。......
  • MAUI新生5.2-样式外观:控件状态样式VisualState
    (当前版本V7.0.94,VisualState有bug)控件状态指控件当前处于什么使用状态,如禁用、聚焦、鼠标悬停等等,当控件进入到某种状态时,可以通过【附加属性】【VisualStateManager.Visu......
  • Echarts-常见配置
    series:系列列表。每个系列通过type决定自己的图标类型xAxis:直角坐标系grid中的x轴yAxis:直角坐标系grid中的y轴grid:直角坐标系内回执网格这个网格距离topbottom......
  • ASP.NET 2.0中使用webpart系列控件
    <v:shapetypeid="_x0000_t75"coordsize="21600,21600"o:spt="75"o:preferrelative="t"path="m@4@5l@4@11@9@11@9@5xe"filled="f"stroked="f">原文发表在​​http://......
  • echarts-1.不渲染的问题
    1问题1.1今日开发中遇到同时设置两个echarts,切换的时候其中一个不渲染,一个渲染成功一个渲染失败。2排查原因2.1先是排查echarts是否选中,确认已选中。2.2然后排查e......
  • echarts的option有哪些配置项
    正在思考,请等待......全局配置项:backgroundColor、textStyle、title、tooltip、legend、dataZoom、visualMap、toolbox、animation、parallel、parallelAxis、calendar、......
  • FusionCharts Free-使用JS加载-flash图表
    使用JS加载 这个JS类文件就在FusionChartsFree>JSClass文件夹下,我们把它拷贝到c:FusionChartsFusionCharts下面。现在,我们把原来的Chart.html复制一份,命名为JSCh......
  • layui时间控件 laydate 重置失效
    layui时间控件laydate重置失效​​问题描述​​​​页面代码​​​​问题处理​​问题描述layui日期控件laydate引入页面后,页面开始时间小于结束时间,结束时间最大为当前时......