LinqExample.cs 2.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using LiveCharts;
using LiveCharts.Configurations;
using LiveCharts.Helpers;
using LiveCharts.Wpf;
using Separator = LiveCharts.Wpf.Separator;

namespace Winforms.Cartesian.Linq
{
    public partial class LinqExample : Form
    {
        public LinqExample()
        {
            InitializeComponent();
            
            //lets configure the chart to plot cities
            var 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 = records.Select(x => x.Name).ToList();

            cartesianChart1.Series = new SeriesCollection
            {
                new ColumnSeries
                {
                    Configuration = mapper,
                    Values = Results
                }
            };

            cartesianChart1.AxisY.Add(new Axis
            {
                LabelFormatter = value => (value/1000000).ToString("N") + "M"
            });
            cartesianChart1.AxisX.Add(new Axis
            {
                Labels = Labels,
                DisableAnimations = true,
                LabelsRotation = 20,
                Separator = new Separator
                {
                    Step = 1
                }
            });
        }

        public ChartValues<City> Results { get; set; }
        public List<string> Labels { get; set; }

        private void textBox1_TextChanged(object sender, System.EventArgs e)
        {
            var q = ((TextBox) sender).Text ?? string.Empty;
            q = q.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);

        }
    }
}