<commit_before>﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Matrix2D = System.Drawing.Drawing2D.Matrix;

namespace ComputerAlgebra.Plotting
{
    /// <summary>
    /// A single plot window.
    /// </summary>
    public class Plot
    {
        protected SeriesCollection series;
        /// <summary>
        /// The series displayed in this plot.
        /// </summary>
        public SeriesCollection Series { get { return series; } }

        /// <summary>
        /// Width of the plot window.
        /// </summary>
        public int Width { get { return form.Width; } set { form.Width = value; } }
        /// <summary>
        /// Height of the plot window.
        /// </summary>
        public int Height { get { return form.Height; } set { form.Height = value; } }

        private double _x0 = -10.0, _x1 = 10.0;
        private double _y0 = double.NaN, _y1 = double.NaN;
        /// <summary>
        /// Plot area bounds.
        /// </summary>
        public double x0 { get { return _x0; } set { _x0 = value; Invalidate(); } }
        public double y0 { get { return _y0; } set { _y0 = value; Invalidate(); } }
        public double x1 { get { return _x1; } set { _x1 = value; Invalidate(); } }
        public double y1 { get { return _y1; } set { _y1 = value; Invalidate(); } }

        private string xlabel = null, ylabel = null;
        /// <summary>
        /// Axis labels.
        /// </summary>
        public string xLabel { get { return xlabel; } set { xlabel = value; Invalidate(); } }
        public string yLabel { get { return ylabel; } set { ylabel = value; Invalidate(); } }

        string title = null;
        /// <summary>
        /// Title of the plot window.
        /// </summary>
        public string Title { get { return title; } set { form.Text = title = value; Invalidate(); } }

        private bool showLegend = true;
        /// <summary>
        /// Show or hide the legend.
        /// </summary>
        public bool ShowLegend { get { return showLegend; } set { showLegend = value; Invalidate(); } }

        protected Thread thread;
        protected Form form = new Form();

        public Plot()
        {
            series = new SeriesCollection();
            series.ItemAdded += (o, e) => Invalidate();
            series.ItemRemoved += (o, e) => Invalidate();

            form = new Form() 
            { 
                Text = "Plot",
                Width = 300, 
                Height = 300,
            };
            form.Paint += Plot_Paint;
            form.SizeChanged += Plot_SizeChanged;

            thread = new Thread(() => Application.Run(form));
            thread.Start();
        }

        private RectangleF PaintTitle(Graphics G, RectangleF Area)
        {
            if (title == null)
                return Area;

            Font font = new Font(form.Font.FontFamily, form.Font.Size * 1.5f, FontStyle.Bold);<commit_msg>Add hack to "fix" race condition<commit_after>﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Matrix2D = System.Drawing.Drawing2D.Matrix;

namespace ComputerAlgebra.Plotting
{
    /// <summary>
    /// A single plot window.
    /// </summary>
    public class Plot
    {
        protected SeriesCollection series;
        /// <summary>
        /// The series displayed in this plot.
        /// </summary>
        public SeriesCollection Series { get { return series; } }

        /// <summary>
        /// Width of the plot window.
        /// </summary>
        public int Width { get { return form.Width; } set { form.Width = value; } }
        /// <summary>
        /// Height of the plot window.
        /// </summary>
        public int Height { get { return form.Height; } set { form.Height = value; } }

        private double _x0 = -10.0, _x1 = 10.0;
        private double _y0 = double.NaN, _y1 = double.NaN;
        /// <summary>
        /// Plot area bounds.
        /// </summary>
        public double x0 { get { return _x0; } set { _x0 = value; Invalidate(); } }
        public double y0 { get { return _y0; } set { _y0 = value; Invalidate(); } }
        public double x1 { get { return _x1; } set { _x1 = value; Invalidate(); } }
        public double y1 { get { return _y1; } set { _y1 = value; Invalidate(); } }

        private string xlabel = null, ylabel = null;
        /// <summary>
        /// Axis labels.
        /// </summary>
        public string xLabel { get { return xlabel; } set { xlabel = value; Invalidate(); } }
        public string yLabel { get { return ylabel; } set { ylabel = value; Invalidate(); } }

        string title = null;
        /// <summary>
        /// Title of the plot window.
        /// </summary>
        public string Title { get { return title; } set { form.Text = title = value; Invalidate(); } }

        private bool showLegend = true;
        /// <summary>
        /// Show or hide the legend.
        /// </summary>
        public bool ShowLegend { get { return showLegend; } set { showLegend = value; Invalidate(); } }

        protected Thread thread;
        protected Form form = new Form();

        public Plot()
        {
            series = new SeriesCollection();
            series.ItemAdded += (o, e) => Invalidate();
            series.ItemRemoved += (o, e) => Invalidate();

            form = new Form() 
            { 
                Text = "Plot",
                Width = 300, 
                Height = 300,
            };
            form.Paint += Plot_Paint;
            form.SizeChanged += Plot_SizeChanged;

            // The sleep is a lame hack to avoid errors when using the properties
            // to change the form in a racy way.
            thread = new Thread(() => { Thread.Sleep(100); Application.Run(form); });
            thread.Start();
        }

        private RectangleF PaintTitle(Graphics G, RectangleF Area)
        {
            if (title == null)
                return Area;

            Font font = new Font(form.Font.FontFamily, form.Font.Size * 1.5f, FontStyle.Bold);
{'fraction_non_alphanumeric': 0.12581954417733374, 'fraction_numerical': 0.010771152044957853, 'mean_word_length': 1.7592592592592593, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2991727', 'n_tokens_mistral': 1807, 'n_tokens_neox': 1734, 'n_words': 564}