using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfGraph
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        // Draw a simple graph.
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            const double margin = 10;
            double xmin = margin;
            double xmax = graphCanvas.Width - margin;
            double ymin = margin;
            double ymax = graphCanvas.Height - margin;
            const double step = 10;

            // Make the X axis.
            GeometryGroup xaxisGeom = new GeometryGroup();
            xaxisGeom.Children.Add(new LineGeometry(
                new Point(0, ymax), new Point(graphCanvas.Width, ymax)));
            for (double x = xmin + step; x <= graphCanvas.Width - step; x += step)
            {
                xaxisGeom.Children.Add(new LineGeometry(
                    new Point(x, ymax - margin / 2),
                    new Point(x, ymax + margin / 2)));
            }

            Path xaxisPath = new Path();
            xaxisPath.StrokeThickness = 1;
            xaxisPath.Stroke = Brushes.Black;
            xaxisPath.Data = xaxisGeom;

            graphCanvas.Children.Add(xaxisPath);

            // Make the Y axis.
            GeometryGroup yaxisGeom = new GeometryGroup();
            yaxisGeom.Children.Add(new LineGeometry(
                new Point(xmin, 0), new Point(xmin, graphCanvas.Height)));
            for (double y = step; y <= graphCanvas.Height - step; y += step)
            {
                yaxisGeom.Children.Add(new LineGeometry(
                    new Point(xmin - margin / 2, y),
                    new Point(xmin + margin / 2, y)));
            }

            Path yaxisPath = new Path();
            yaxisPath.StrokeThickness = 1;
            yaxisPath.Stroke = Brushes.Black;
            yaxisPath.Data = yaxisGeom;

            graphCanvas.Children.Add(yaxisPath);

            // Make some data sets.
            Brush[] brushes = { Brushes.Red, Brushes.Green, Brushes.Blue };
            Random rand = new Random();
            for (int dataset = 0; dataset < 3; dataset++)
            {
                int lastY = rand.Next((int)ymin, (int)ymax);

                PointCollection points = new PointCollection();
                for (double x = xmin; x <= xmax; x += step)
                {
                    lastY = rand.Next(lastY - 10, lastY + 10);
                    if (lastY < ymin) lastY = (int)ymin;
                    if (lastY > ymax) lastY = (int)ymax;
                    points.Add(new Point(x, lastY));
                }

                Polyline polyline = new Polyline();
                polyline.StrokeThickness = 1;
                polyline.Stroke = brushes[dataset];
                polyline.Points = points;

                graphCanvas.Children.Add(polyline);
            }
        }
    }
}
