首页 > 其他分享 >WPF LiveCharts.Wpf

WPF LiveCharts.Wpf

时间:2024-08-28 16:37:37浏览次数:7  
标签:LiveCharts System rnd new using WPF Next Wpf 1000

//xaml
<Window x:Class="WpfApp299.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:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
        xmlns:local="clr-namespace:WpfApp299"
        mc:Ignorable="d" WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button HorizontalAlignment="Right" VerticalAlignment="Top" Width="200" Height="50" Click="Button_Click" Panel.ZIndex="2">Refresh</Button>
        <lvc:CartesianChart Series="{Binding BooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" LegendLocation="Right">
            <lvc:CartesianChart.AxisX>
                <lvc:Axis Title="Salesman" Labels="{Binding Labels,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
            </lvc:CartesianChart.AxisX>
            <lvc:CartesianChart.AxisY>
                <lvc:Axis Title="Sold Apps" LabelFormatter="{Binding Formatter}"/>
            </lvc:CartesianChart.AxisY>
        </lvc:CartesianChart>
    </Grid>
</Window>




//cs
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
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 LiveCharts;
using LiveCharts.Wpf;
using Newtonsoft.Json;

namespace WpfApp299
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {

        public MainWindow()
        {
            InitializeComponent();
            rnd = new Random();
            this.DataContext = this;
            InitData();
        }

        
        private void InitData()
        {           
            BooksCollection = new SeriesCollection();
            BooksCollection.Add(new ColumnSeries
            {
                Title = "2015",
                Values=new ChartValues<double> { rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1,1000) }
            });

            BooksCollection.Add(new ColumnSeries
            {
                Title = "2016",
                Values = new ChartValues<double> { rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000) }
            });

            BooksCollection.Add(new ColumnSeries
            {
                Title = "2017",
                Values = new ChartValues<double> { rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000) }
            });

            BooksCollection.Add(new ColumnSeries
            {
                Title = "2018",
                Values = new ChartValues<double> { rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000) }
            });

            BooksCollection.Add(new ColumnSeries
            {
                Title = "2019",
                Values = new ChartValues<double> { rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000), rnd.Next(1, 1000) }
            });
            
            Labels = new List<string>();
            for(int i=0;i<5;i++)
            {
                Labels.Add($"SalesMan_{rnd.Next(1,100)}");
            }
        }

        private Random rnd { get; set; }

        private SeriesCollection booksCollection;
        public SeriesCollection BooksCollection
        {
            get
            {
                return booksCollection;
            }
            set
            {
                booksCollection = value;
                OnPropertyChanged(nameof(BooksCollection));
            }
        }

        private List<string> labels;
        public List<string> Labels
        {
            get
            {
                return labels;
            }
            set
            {
                if(value!=labels)
                {
                    labels = value;
                    OnPropertyChanged(nameof(Labels));
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        
        private void OnPropertyChanged(string propName)
        {
            var handler = PropertyChanged;
            if(handler!=null)
            {
                handler?.Invoke(this, new PropertyChangedEventArgs(propName));
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Dispatcher.BeginInvoke(new Action(() =>
            {
                InitData();
            }));
            //Task.Run(() =>
            //{
            //    InitData();
            //});
        }
    }

    public class  BookTrim
    {
        public string Title { get; set; }
        public string Values { get; set; }
    }
}

 

 

 

 

 

 

标签:LiveCharts,System,rnd,new,using,WPF,Next,Wpf,1000
From: https://www.cnblogs.com/Fred1987/p/18385050

相关文章

  • FastReport .NET WPF 24.2.17 Crack
    FastReport.NETWPFAsetofhigh-performancecomponentsforcreatingreportsanddocumentsinWPFprojectsBuyTryforfreeWhatreportscanFastReport.NETWPFdo?Practicallyany:invoices,financialreports,productcatalogswithcolorprofilesuppor......
  • WPF 如何利用Blend给Button添加波纹效果
    先看一下效果吧:如果不会写动画或者懒得写动画,就直接交给Blend来做吧;其实Blend操作起来很简单,有点类似于在操作PS,我们只需要设置关键帧,鼠标点来点去就可以了,Blend会自动帮我们生成我们想要的动画效果.第一步:要创建一个空的WPF项目第二步:右键我们的项目,在最下方有一个,在Blend......
  • 一起搭WPF架构之界面绑定显示
    一起搭WPF架构之界面绑定显示1前言2定义文件3定义属性4控制器使用5界面内容绑定6界面效果总结1前言之前的许多介绍,已经完成界面搭建的熟悉内容,现在在搭建的基础上完成简单的界面切换。2定义文件我们在已有项目中需要定义两个CS文件,在这个两个CS文件中,我......
  • 一起搭WPF之列表数据绑定
    一起搭WPF之列表数据绑定1前言2数据绑定2.1前端2.2后端实现2.2.1界面后台2.2.2模型与逻辑3问题3.2解决总结1前言之前已经简单介绍了列表的大致设计,在设计完列表界面后,我们可以开展列表的数据绑定,在前端显示我们的数据,对列表进行数据输入。那么让我们开......
  • WPF 自定义路由事件的实现
    路由事件通过EventManager,RegisterRoutedEvent方法注册,通过AddHandler和RemoveHandler来关联和解除关联的事件处理函数;通过RaiseEvent方法来触发事件;通过传统的CLR事件来封装后供用户使用。如何实现自定义路由事件,可以参考MSDN官网上的文档:如何:创建自定义路由事件下面的这个......
  • WPF 数据校验
    一、新建NameValidationRule类publicclassNameValidationRule:ValidationRule{publicoverrideValidationResultValidate(objectvalue,CultureInfocultureInfo){varlength=value.ToString().Length;if(length......
  • WPF C# split picture into small pieces and show in grid cells
    usingSystem;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;using......
  • WPF 模板
    一、数据模板继承了ItemConrol的控件对象(如ListView、ListBox、DataGrid、TabControl等等),都可以使用数据模板DataTemplate。数据模板的作用在于决定每个Item中的数据的展示形式。普通控件通过Template属性来定义模板,而子项容器控件则通过ItemTemplate属性来定义子项模板。先创......
  • WPF中如何根据数据类型使用不同的数据模板
    我们在将一个数据集合绑定到列表控件时,有时候想根据不同的数据类型,显示为不同的效果。例如将一个文件夹集合绑定到ListBox时,系统文件夹显示为不同的效果,就可以使用模板选择器功能。WPF提供了一个模板选择器类型DataTemplateSelector,它可以根据数据对象和数据绑定元素来选择 Dat......
  • WPF 路由事件
    一、什么是路由事件?根据MSDN定义:功能定义:路由事件是一种可以针对元素树中的多个侦听器(而不是仅针对引发该事件的对象)调用处理程序的事件。实现定义:路由事件是由类的实例支持的CLR事件,RoutedEvent由事件WindowsPresentationFoundation(WPF)系统处理。典型的WPF应......