项目结构调整

This commit is contained in:
艾竹
2023-04-16 20:11:40 +08:00
parent cbfbf96033
commit 81f91f3f35
2124 changed files with 218 additions and 5516 deletions

View File

@@ -0,0 +1,13 @@
using System;
namespace UWP.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; }
}
}

View File

@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
using Windows.Storage;
namespace UWP.CartesianChart.Linq
{
public static class DataBase
{
public static City[] Cities { get; private set; }
public static async Task Initialize()
{
var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///cities.csv"));
var stream = await file.OpenStreamForReadAsync();
var reader = new StreamReader(stream);
var read = new List<City>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
if (line != null)
{
var values = line.Split(',');
read.Add(new City
{
Name = values[0],
Population = double.Parse(values[1], CultureInfo.InvariantCulture),
Area = double.Parse(values[2], CultureInfo.InvariantCulture),
Country = values[3]
});
}
}
Cities = read.ToArray();
}
}
}

View File

@@ -0,0 +1,39 @@
<Page
x:Class="UWP.CartesianChart.Linq.LinqExample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UWP.CartesianChart.Linq"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:lvc="using:LiveCharts.Uwp"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<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>
</Page>

View File

@@ -0,0 +1,70 @@
using LiveCharts;
using LiveCharts.Configurations;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using LiveCharts.Helpers;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace UWP.CartesianChart.Linq
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class LinqExample : Page
{
public LinqExample()
{
InitializeComponent();
//lets configure the chart to plot cities
Mapper = Mappers.Xy<City>()
.X((city, index) => index)
.Y(city => city.Population);
MillionFormatter = value => (value / 1000000).ToString("N") + "M";
this.Loading += LinqExample_Loading;
DataContext = this;
}
private async void LinqExample_Loading(FrameworkElement sender, Object args)
{
await DataBase.Initialize();
//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));
}
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);
}
}
}