using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using C1.Silverlight.Chart;

namespace AxisAggregation
{
  public partial class CategoryAxis1 : UserControl
  {
    public CategoryAxis1()
    {
      InitializeComponent();
      AggregateByName();
    }

    void AggregateByName()
    {
      var keys = new List<string> { "oranges", "apples", "lemons", "grapes" };

      for (int i = 0; i < 2; i++)
      {
        var ds = new DataSeries()
        {
          ItemsSource = SampleItem.CreateSampleData(40),
          ValueBinding = new Binding("Value"),
          Aggregate = Aggregate.Sum,
          Label = "s" + i
        };
        ds.AggregateGroupSelector = (x, y, o) =>
        {
          // index from categories list 
          return keys.IndexOf(((SampleItem)o).Name);
        };

        chart.Data.Children.Add(ds);
      }

      chart.Data.ItemNames = keys;
    }

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

  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;
    }
  }
}
