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 PolygonPlot : UserControl
  {
    public PolygonPlot()
    {
      this.InitializeComponent();

      chart.ChartType = ChartType.PolygonFilled;

      chart.CustomPalette = new Color[]
      {
        Color.FromArgb(0xff, 0xe0, 0x00, 0x00),
        Color.FromArgb(0xff, 0xff, 0x00, 0x4c),
        Color.FromArgb(0xff, 0xff, 0xa0, 0x00),
        Color.FromArgb(0xff, 0xff, 0x80, 0xc4),
        Color.FromArgb(0xff, 0x7d, 0x00, 0x00),

        Color.FromArgb(0xa0, 0xe0, 0x00, 0x00),
        Color.FromArgb(0xa0, 0xff, 0x00, 0x4c),
        Color.FromArgb(0xa0, 0xff, 0xa0, 0x00),
        Color.FromArgb(0xa0, 0xff, 0x80, 0xc4),
        Color.FromArgb(0xa0, 0x7d, 0x00, 0x00),

        Color.FromArgb(0x80, 0xe0, 0x00, 0x00),
        Color.FromArgb(0x80, 0xff, 0x00, 0x4c),
        Color.FromArgb(0x80, 0xff, 0xa0, 0x00),
        Color.FromArgb(0x80, 0xff, 0x80, 0xc4),
        Color.FromArgb(0x80, 0x7d, 0x00, 0x00),

        Color.FromArgb(0x40, 0xe0, 0x00, 0x00),
        Color.FromArgb(0x40, 0xff, 0x00, 0x4c),
        Color.FromArgb(0x40, 0xff, 0xa0, 0x00),
        Color.FromArgb(0x40, 0xff, 0x80, 0xc4),
        Color.FromArgb(0x40, 0x7d, 0x00, 0x00),
      };

      chart.View.AxisX.Origin = chart.View.AxisY.Origin = 0.0;
      chart.View.AxisX.Min = chart.View.AxisY.Min = -25.0;
      chart.View.AxisX.Max = chart.View.AxisY.Max = 25.0;

      NewData();
    }

    void NewData()
    {
      chart.BeginUpdate();
      chart.Data.Children.Clear();

      int nser = 80;
      double delta = 2 * Math.PI / nser;

      for (int i = 0; i < nser; i++)
      {
        double t = i * delta;

        var x = Math.Cos(6 * t) * Math.Cos(t) * 20;
        var y = Math.Cos(6 * t) * Math.Sin(t) * 20;

        var d = Math.Sqrt(x * x + y * y);

        chart.Data.Children.Add(CreateRose(100, Math.Round(3 + 0.2 * d), 1 + 0.1 * d,
          x,// 40 * rnd.NextDouble() - 20,
          y //40 * rnd.NextDouble() - 20
          ));
      }
      chart.EndUpdate();
    }

    XYDataSeries CreateRose(int npts, double k, double rad, double x0, double y0)
    {
      var x = new double[npts];
      var y = new double[npts];

      double delta = 2 * Math.PI / npts;

      for (int i = 0; i < npts; i++)
      {
        double t = i * delta;
        x[i] = x0 + Math.Cos(k * t) * Math.Cos(t) * rad;
        y[i] = y0 + Math.Cos(k * t) * Math.Sin(t) * rad;
      }

      return new XYDataSeries() { XValuesSource = x, ValuesSource = y };
    }

    protected override Size MeasureOverride(Size availableSize)
    {
      var size = Math.Min(availableSize.Width, availableSize.Height);
      return base.MeasureOverride(new Size(size, size));
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
      var size = Math.Min(finalSize.Width, finalSize.Height);
      return base.ArrangeOverride(new Size(size, size));
    }
  }
}
