WPF Chart图标的使用

背景

大数据的展示,Chart图表的方式一目了然。

用法

目标:可显示实现列表对象数据,可切换不同列表显示

1.Nuget程序包安装在这里插入图片描述

2.界面构建

在这里插入图片描述

<Window x:Class="WpfApp1.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:WpfApp1" 
        xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=DotNetProjects.DataVisualization.Toolkit"
        mc:Ignorable="d"
        Title="MainWindow" Height="400" Width="400">
    <Grid>
        <charting:Chart Height="271" Name="chart1" Width="379" >
            <charting:Chart.Series>
                <!--PieSeries ColumnSeries  LineSeries-->
                <charting:ColumnSeries Name="chartDate"
                    DependentValuePath="Value" 
                    IndependentValuePath="Key"
                    Title="Pet Preference" IsSelectionEnabled="True" />
            </charting:Chart.Series>
        </charting:Chart>
        <Button Content="Date1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="42" Width="57" Click="Button_Click"/>
        <Button Content="Date2" HorizontalAlignment="Left" Margin="92,10,0,0" VerticalAlignment="Top" Height="42" Width="57" Click="Button_Click_1"/>
        <Button Content="Date1++" HorizontalAlignment="Center" Margin="0,10,0,0" VerticalAlignment="Top" Height="42" Width="56" Click="Button_Click_2"/>
        <Button Content="Date2++" HorizontalAlignment="Left" Margin="244,10,0,0" VerticalAlignment="Top" Height="42" Width="56" Click="Button_Click_3"/>
    </Grid>
</Window>

3.后台

数据对象构建
public class Map : INotifyPropertyChanged
    {
        public Map()
        {

        }

        public Map(string key, int value)
        {
            this.key = key;
            this.value = value;
        }

        private string key = "";
        private int value = 0;

        public string Key
        {
            get => key;
            set
            {
                key = value;
                RaisePropertyChanged("Key");
            }
        }
        public int Value
        {
            get => value;
            set
            {
                this.value = value;
                RaisePropertyChanged("Value");
            }
        }

        #region Methods
        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
    public class ViewModel
    {
        public ObservableCollection<Map> Pair1 { get; set; }
        public ObservableCollection<Map> Pair2 { get; set; }

        public ViewModel()
        {
            Pair1 = new ObservableCollection<Map>();
            Pair1.Add(new Map("-10%", 40));
            Pair1.Add(new Map("-5%", 50));
            Pair1.Add(new Map("0%", 100));
            Pair1.Add(new Map("+5%", 55));
            Pair1.Add(new Map("+10%", 2));

            Pair2 = new ObservableCollection<Map>();
            Pair2.Add(new Map("-10%", 40));
            Pair2.Add(new Map("-5%", 50));
            Pair2.Add(new Map("0%", 100));
            Pair2.Add(new Map("+5%", 55));
            Pair2.Add(new Map("+10%", 2));
        }
    }
4.对象绑定
/// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ViewModel viewModel = new ViewModel();
        Random random = new Random();
        public MainWindow()
        {
            InitializeComponent();
            chart1.DataContext = new ViewModel();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Binding b2 = new Binding();
            b2.Source = viewModel;
            b2.Path = new PropertyPath("Pair2");
            chartDate.SetBinding(ColumnSeries.ItemsSourceProperty, b2);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Binding b1 = new Binding();
            b1.Source = viewModel;
            b1.Path = new PropertyPath("Pair1");
            chartDate.SetBinding(ColumnSeries.ItemsSourceProperty, b1);
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            foreach (var item in viewModel.Pair1)
            {
                int i = random.Next(0, 100);
                item.Value += i;
            }
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            foreach (var item in viewModel.Pair2)
            {
                int i = random.Next(0, 100);
                item.Value += i;
            }
        }
    }

效果展示

请添加图片描述
不同形式
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

觉得有用就请关注赞收藏吧!!!
工控之路,不迷茫

之前三此发的库http://download.youkuaiyun.com/detail/maiker/9621027可以作废,本次为最新的,其中测试了历史曲线的动态显示,这是对Microsoft WpfToolkit的更新和扩展: 1、将库版本升级到.NET 4.6.1,对命名控件进行了替换处理 2、实现了单数据Chart图的混合颜色显示,可通过修改资源字典调整和添加颜色 3、可控制各种Chart图形的数据值显示 4、坐标轴文本可倾斜显示 5、添加了StepLine图和圆环图,圆环半径比例系数可设定,同时添加了饼图的半径比系数,这样可以更好的控制饼图的标签显示,避免重叠 6、Legend可位于区域四侧,对齐方式也可以设定,以及是否显示, 可控制Chart Title是否显示 7、柱状图缝隙间隔可调整 8、全方面的加入了各种加载动画效果,动画效果可屏蔽,同时原系统自带动画效果,可叠加,这是两种不同的效果,可以互补性的单个使用 9、设计了四种主题颜色,各人可以针对图形颜色和背景色进行更好的搭配,在用户项目中,可以自己添加新的主题。 10、扩展了时间轴的应用,在新的DateTimeChart中可以通过鼠标平移和缩放时间轴,并测试了两个例子用于动态显示历史曲线,一个是外部定时更新,一个是内部定时更新 11、对LineDataPoint样式进行了设计,现在可以选择线图的点样式(如空心圆、五角星、三角形,矩形等),同时这些不同的点样式可以体现在Legend上,从而实现颜色和图形的双重区分。 总而言之,微软的控件库做得很标准,还是很好修改的。 最后一直想在历史曲线中取消点动画(及默认的透明度动画),以便提高执行效率,但微软的那部分动画不熟,总是修改失败。 可以发邮件wuyang26@live.cn讨论。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

手搓人生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值