using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

using C1.Xaml.Chart;

namespace ChartSamples
{
  public sealed partial class AggregateByRange : UserControl
  {
      public AggregateByRange()
      {
          InitializeComponent();
          CreateSampleChart();
      }

      void CreateSampleChart()
      {

                var keys = new string[] { "<4", "4-8", ">8" };

      for (int i = 0; i < 3; i++)
      {
          var ds = new DataSeries()
          {
              ItemsSource = SampleItem.CreateSampleData(100),
              ValueBinding = new Binding() { Path = new PropertyPath("Value") },
              Aggregate = Aggregate.Count,
              Label = "s" + i,
              AggregateGroupSelector = RangeSelector
          };
        chart.Data.Children.Add(ds);
      }

      chart.Data.ItemNames = keys;
    }

    double RangeSelector(double x, double y, object o)
    {
      var sample = (SampleItem)o;
      if (sample.Value < 4)
        return 0;
      else if (sample.Value <= 8)
        return 1;
      else
        return 2;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
      chart.Data.Children.Clear();
      CreateSampleChart();
    }
  }

  public class SampleItem
  {
      public string Name { get; set; }
      public double Value { get; set; }

      static Random rnd = new Random();

      public static SampleItem[] CreateSampleData(int cnt)
      {
          var names = new string[] { "oranges", "apples", "lemons", "grapes" };
          var array = new SampleItem[cnt];

          for (int i = 0; i < cnt; i++)
          {
              array[i] = new SampleItem() { Value = rnd.Next(1, 10), Name = names[rnd.Next(names.Length)] };
          }

          return array;
      }
  }

}
