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 InteractiveChart : UserControl
    {
        Random rnd = new Random();

        public InteractiveChart()
        {
            this.InitializeComponent();

            c1chart.View.AxisX.ScrollBar = new AxisScrollBar() { Visibility = Visibility.Collapsed };
            c1chart.View.AxisY.ScrollBar = new AxisScrollBar() { Visibility = Visibility.Collapsed };

            c1chart.ActionLeave += new EventHandler(Actions_Leave);

            CreataSampleData(3, 200);

            c1chart.View.AxisX.Scale = 0.5;
            c1chart.View.AxisY.MinScale = 1;
            
            c1chart.ActionUpdateDelay = 0;
            c1chart.ManipulationMode = ManipulationModes.Scale |
              ManipulationModes.TranslateX | ManipulationModes.TranslateY | ManipulationModes.TranslateInertia;

            c1chart.GestureDoubleTap = GestureDoubleTapAction.Scale;
            c1chart.GesturePinch = GesturePinchAction.Scale;
            c1chart.GestureSlide = GestureSlideAction.Translate;
            UpdateScrollbars();

        }

        void Actions_Leave(object sender, EventArgs e)
        {
            UpdateScrollbars();
        }

        void CreataSampleData(int nser, int npts)
        {
            c1chart.BeginUpdate();
            DataSeriesCollection sc = c1chart.Data.Children;
            sc.Clear();

            for (int iser = 0; iser < nser; iser++)
            {
                double phase = 0.05 + 0.1 * rnd.NextDouble();
                double amp = 1 + 5 * rnd.NextDouble();

                DataSeries ds = new DataSeries();
                double[] vals = new double[npts];
                for (int i = 0; i < npts; i++)
                    vals[i] = amp * Math.Sin(i * phase);

                ds.ValuesSource = vals;
                ds.Label = "S " + iser.ToString();
                ds.ConnectionStrokeThickness = 2;

                sc.Add(ds);
            }
            c1chart.EndUpdate();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            c1chart.BeginUpdate();
            c1chart.View.AxisX.Scale = 1;
            c1chart.View.AxisX.Value = 0.5;
            c1chart.View.AxisY.Scale = 1;
            c1chart.View.AxisY.Value = 0.5;

            UpdateScrollbars();
            c1chart.EndUpdate();
        }

        private void rb_Checked(object sender, RoutedEventArgs e)
        {
            if (c1chart == null)
                return;

            c1chart.BeginUpdate();

            if (sender == rbX)
            {
                // limit scale of Y-axis
                c1chart.View.AxisY.MinScale = 1;
                c1chart.View.AxisY.Scale = 1;
                c1chart.View.AxisY.Value = 0.5;
                c1chart.View.AxisX.MinScale = 0.001;
            }
            else if (sender == rbY)
            {
                // limit scale of X-axis
                c1chart.View.AxisX.MinScale = 1;
                c1chart.View.AxisX.Scale = 1;
                c1chart.View.AxisX.Value = 0.5;
                c1chart.View.AxisY.MinScale = 0.001;
            }
            else
            {
                // allow smaller scales for both axes
                c1chart.View.AxisX.MinScale = 0.001;
                c1chart.View.AxisY.MinScale = 0.001;
            }

            UpdateScrollbars();
            c1chart.EndUpdate();
        }

        private void UpdateScrollbars()
        {
            double sx = c1chart.View.AxisX.Scale;
            AxisScrollBar sbx = (AxisScrollBar)c1chart.View.AxisX.ScrollBar;
            if (sx >= 1.0)
                sbx.Visibility = Visibility.Collapsed;
            else
                sbx.Visibility = Visibility.Visible;

            double sy = c1chart.View.AxisY.Scale;
            AxisScrollBar sby = (AxisScrollBar)c1chart.View.AxisY.ScrollBar;
            if (sy >= 1.0)
                sby.Visibility = Visibility.Collapsed;
            else
                sby.Visibility = Visibility.Visible;
        }
    }
}
