WPF编程,Live Charts使用说明(45)——使用Linq

在此示例中,随着用户更改输入,我们将从假数据库中获取一些记录。 DataBase.Cities仅仅是一个用于存储我们的数据的数组。

数据类:

using System;
 
namespace Wpf.CartesianChart.Linq
{
    public class City
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public double Population { get; set; }
        public double Area { get; set; }
        public string Country { get; set; }
    }
}

后台:

using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Controls;
using LiveCharts;
using LiveCharts.Configurations;
using LiveCharts.Helpers;
 
namespace Wpf.CartesianChart.Linq
{
 
    public partial class LinqExample : UserControl
    {
 
        public LinqExample()
        {
            InitializeComponent();
 
            //lets configure the chart to plot cities
            Mapper = Mappers.Xy<City>()
                .X((city, index) => index)
                .Y(city => city.Population);
 
            //lets take the first 15 records by default;
            var records = DataBase.Cities.OrderByDescending(x => x.Population).Take(15).ToArray();
 
            Results = records.AsChartValues();
            Labels = new ObservableCollection<string>(records.Select(x => x.Name));
 
            MillionFormatter = value => (value/1000000).ToString("N") + "M";
 
            DataContext = this;
        }
 
        public ChartValues<City> Results { get; set; }
        public ObservableCollection<string> Labels { get; set; }
        public Func<double, string> MillionFormatter { get; set; }
 
        public object Mapper { get; set; }
 
        private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
        {
            var q = (Query.Text ?? string.Empty).ToUpper();
 
            var records = DataBase.Cities
                .Where(x => x.Name.ToUpper().Contains(q) || x.Country.ToUpper().Contains(q))
                .OrderByDescending(x => x.Population)
                .Take(15)
                .ToArray();
 
            Results.Clear();
            Results.AddRange(records);
 
            Labels.Clear();
            foreach (var record in records) Labels.Add(record.Name);
 
        }
    }
}

前台:

<UserControl x:Class="Wpf.CartesianChart.Linq.LinqExample"
             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:local="clr-namespace:Wpf.CartesianChart.Linq"
             xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" 
             d:DataContext="{d:DesignInstance local:LinqExample}">
    <Grid>
            <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" Orientation="Horizontal">
            <TextBlock VerticalAlignment="Center" Margin="10 0">Search</TextBlock>
            <TextBox Name="Query" VerticalContentAlignment="Center" Width="100" Height="30" 
                     TextChanged="TextBoxBase_OnTextChanged"></TextBox>
        </StackPanel>
        <lvc:CartesianChart Grid.Row="1" >
            <lvc:CartesianChart.Series>
                <lvc:ColumnSeries Title="2016 Population by City" 
                                  Values="{Binding Results}"
                                  Configuration="{Binding Mapper}"/>
            </lvc:CartesianChart.Series>
            <lvc:CartesianChart.AxisX>
                <lvc:Axis LabelsRotation="-20" Labels="{Binding Labels}" DisableAnimations="True">
                    <lvc:Axis.Separator>
                        <lvc:Separator Step="1"></lvc:Separator>
                    </lvc:Axis.Separator>
                </lvc:Axis>
            </lvc:CartesianChart.AxisX>
            <lvc:CartesianChart.AxisY>
                <lvc:Axis LabelFormatter="{Binding MillionFormatter}"></lvc:Axis>
            </lvc:CartesianChart.AxisY>
        </lvc:CartesianChart>
    </Grid>
</UserControl>

猜你喜欢

转载自blog.csdn.net/qq_43307934/article/details/105670113