赞
踩
大数据的展示,Chart图表的方式一目了然。
目标:可显示实现列表对象数据,可切换不同列表显示
<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>
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)); } }
/// <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; } } }
不同形式
觉得有用就请关注赞收藏吧!!!
工控之路,不迷茫
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。