Exemple #1
0
        void chart_Paint(object sender, PaintEventArgs e)
        {
            //ComputeAxis();
            try
            {
                Graphics  gfx    = e.Graphics;
                Rectangle bounds = e.ClipRectangle;
                gfx.FillRectangle(Brushes.White, bounds);
                System.Drawing.Font f = new Font("Tahoma", 8);
                Pen BlackPen          = new Pen(Color.Black, 1f);
                Pen DarkGrayPen       = new Pen(Color.DarkGray, 1f);
                Pen LightGrayPen      = new Pen(Color.LightGray, 1f);
                int graphs            = Graphs.Count;
                int BottomSize        = 80;
                int TopSize           = 15;
                int Rightsize         = 30;

                int g_index = 0;

                // Max axis index
                int MaxAxisIndex = 0;
                foreach (PlotterGraph g in Graphs)
                {
                    if (g.Y_Axis.Count > MaxAxisIndex)
                    {
                        MaxAxisIndex = g.Y_Axis.Count;
                    }
                }

                double x_axis_min  = x_min;
                double x_axis_max  = x_max;
                double x_axis_step = 0;

                // How many charts?
                foreach (PlotterGraph g in Graphs)
                {
                    double height = bounds.Height - TopSize - BottomSize;

                    height /= graphs;

                    double base_y = height * g_index + TopSize;
                    height -= 9;

                    double base_x = 10 + 50 * MaxAxisIndex - 50 * g.Y_Axis.Count;

                    // Draw all y-axis
                    foreach (PlotterYAxis axis in g.Y_Axis)
                    {
                        double Max_Min = axis.max - axis.min;

                        double Draw_Min = Math.Ceiling(axis.min * 1.0 / axis.step) * axis.step;
                        double Draw_Max = Math.Floor(axis.max * 1.0 / axis.step) * axis.step;

                        axis.step = Math.Abs(axis.step);
                        if (Draw_Min == Draw_Max)
                        {
                            if (axis.step > Draw_Max - Draw_Min)
                            {
                                axis.step         = 1;
                                axis.step_divider = axis.step / 5.0;
                            }
                            Draw_Max += axis.step;
                        }
                        else
                        {
                            if (axis.step > Draw_Max - Draw_Min)
                            {
                                axis.step         = Draw_Max - Draw_Min;
                                axis.step_divider = axis.step / 5.0;
                            }
                        }
                        for (double v = Draw_Min; v <= Draw_Max; v += axis.step)
                        {
                            double __y = base_y + height * (1 - (v - axis.min) / (Max_Min));

                            if (v != Draw_Max)
                            {
                                for (double v2 = v; v2 <= v + axis.step; v2 += axis.step_divider)
                                {
                                    double __y2 = base_y + height * (1 - (v2 - axis.min) / (Max_Min));
                                    gfx.DrawLine(BlackPen, base_x + 43, __y2, base_x + 48, __y2);
                                }
                            }

                            gfx.DrawLine(BlackPen, base_x + 38, __y, base_x + 48, __y);

                            gfx.DrawString(Math.Round(v, axis.digits_comma).ToString(), f, Brushes.Black, base_x + 10, __y - 5);
                        }

                        gfx.DrawLine(BlackPen, base_x + 48, base_y, base_x + 48, base_y + height); // left
                        base_x += 50;
                    }


                    // Draw line alongside..
                    gfx.DrawLine(BlackPen, bounds.Width - Rightsize, base_y, bounds.Width - Rightsize, base_y + height);     // right
                    gfx.DrawLine(DarkGrayPen, base_x - 50 + 48, base_y, bounds.Width - Rightsize, base_y);                   // top
                    gfx.DrawLine(DarkGrayPen, base_x - 50 + 48, base_y + height, bounds.Width - Rightsize, base_y + height); // bottom

                    // Plot data
                    foreach (PlotterCurves c in g.Curves)
                    {
                        PlotterYAxis yx = g.Y_Axis[c.YAxis];

                        Best_Time = 0;
                        double Best_Value = 0;
                        double Best_Y = 0;
                        double Best_X = 0;
                        double Best_dX = 78990;
                        double pp_x = 0, pp_y = 0;
                        Pen    line = new Pen(c.LineColor, Convert.ToSingle(c.LineThickness));
                        foreach (KeyValuePair <double, double> point in c.Data)
                        {
                            if (point.Key >= x_axis_min && point.Key <= x_axis_max)
                            {
                                double p_x = (point.Key - x_axis_min) / (x_axis_max - x_axis_min);
                                double p_y = (point.Value - yx.min) / (yx.max - yx.min);
                                if (p_y > 1)
                                {
                                    p_y = 1;
                                }
                                if (p_y < 0)
                                {
                                    p_y = 0;
                                }
                                if (double.IsInfinity(p_y))
                                {
                                    p_y = 0.5;
                                }
                                if (double.IsNaN(p_y))
                                {
                                    p_y = 0.5;
                                }
                                if (double.IsInfinity(p_x))
                                {
                                    p_x = 0.5;
                                }
                                if (double.IsNaN(p_x))
                                {
                                    p_x = 0.5;
                                }
                                p_y = 1 - p_y;

                                p_y *= height;
                                p_x *= (bounds.Width - Rightsize) - (base_x - 50 + 48);

                                p_x += base_x;
                                p_y += base_y;

                                double dx = Math.Abs(p_x - Mouse_X);


                                // clip outside boundaries.)
                                if (p_y <= base_y)
                                {
                                    p_y = base_y;
                                }
                                if (p_y >= base_y + height)
                                {
                                    p_y = base_y + height;
                                }


                                if (dx < Best_dX)
                                {
                                    Best_dX    = dx;
                                    Best_X     = p_x;
                                    Best_Time  = point.Key;
                                    Best_Value = point.Value;
                                    Best_Y     = p_y;
                                }

                                if (pp_x != 0 && pp_y != 0)
                                {
                                    gfx.DrawLine(line, pp_x, pp_y, p_x, p_y);
                                }

                                pp_x = p_x;
                                pp_y = p_y;
                            }
                        }

                        if (Mouse_X > base_x && Mouse_X < bounds.Width - Rightsize)
                        {
                            gfx.DrawString(Math.Round(Best_Value, yx.digits_comma).ToString(), f, new SolidBrush(c.LineColor), Best_X, Best_Y - 15);
                            gfx.FillEllipse(new SolidBrush(Color.DarkBlue), Best_X - 3, Best_Y - 3, 6, 6);
                            TimeCursor = Best_Time;
                        }
                    }

                    g_index++;
                    if (Mouse_X > base_x && Mouse_X < bounds.Width - Rightsize)
                    {
                        gfx.DrawLine(new Pen(Color.DarkBlue, 1f), Mouse_X, TopSize, Mouse_X, bounds.Height - BottomSize - Graphs.Count * 5);
                    }
                }

                double bx = 10 + 50 * MaxAxisIndex;

                double time_stepsize = 5;
                double dt            = x_max - x_min;

                if (dt > 0.1)
                {
                    time_stepsize = 0.01;
                }
                if (dt > 0.5)
                {
                    time_stepsize = 0.05;
                }
                if (dt > 1)
                {
                    time_stepsize = 0.1;
                }
                if (dt > 2.5)
                {
                    time_stepsize = 0.25;
                }
                if (dt > 5)
                {
                    time_stepsize = 0.5;
                }
                if (dt > 10)
                {
                    time_stepsize = 1;
                }
                if (dt > 20)
                {
                    time_stepsize = 2;
                }
                if (dt > 30)
                {
                    time_stepsize = 2.5;
                }
                if (dt > 60)
                {
                    time_stepsize = 5;
                }
                if (dt > 2 * 60)
                {
                    time_stepsize = 10;
                }
                if (dt > 3 * 60)
                {
                    time_stepsize = 20;
                }

                for (double time = x_min; time <= x_max; time += time_stepsize)
                {
                    if (x_min == x_max)
                    {
                        continue;
                    }
                    float dutycycle = Convert.ToSingle(((time - x_min) / (x_max - x_min)) * ((bounds.Width - Rightsize) - (bx - 50 + 48)) + bx);

                    StringFormat strf = new StringFormat();
                    strf.FormatFlags = StringFormatFlags.DirectionVertical;

                    gfx.DrawString(time.ToString("00.00"), f, Brushes.Black, dutycycle - 8f,
                                   Convert.ToSingle(bounds.Height - BottomSize + 5), strf);
                    gfx.DrawLine(BlackPen, dutycycle, Convert.ToSingle(bounds.Height - BottomSize - 3), dutycycle,
                                 Convert.ToSingle(bounds.Height - BottomSize - 9));
                }

                ThreadPool.QueueUserWorkItem(new WaitCallback(_FireDrawn));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MessageBox.Show(ex.StackTrace);
            }
        }
        public void Load(string file)
        {
            // read it
            if (!File.Exists(file))
            {
                throw new Exception("File doesn't exist");
            }
            string[] FileData = File.ReadAllLines(file);

            int GraphIndex = 0;

            foreach (string line in FileData)
            {
                string[] line_structure = new string[0], line_data = new string[0];
                if (line.Contains("="))
                {
                    line_structure = line.Trim().Split("=".ToCharArray(), 2);
                    if (line_structure.Length == 2)
                    {
                        line_data = line_structure[1].Split(",".ToCharArray());
                    }
                }

                if (line.Trim() == "[Chart]")
                {
                    // A new chart
                    _graphs.Add(new PlotterGraph());
                    GraphIndex = _graphs.Count - 1;
                }

                if (line_structure.Length == 2)
                {
                    // A new curve of yaxis
                    switch (line_structure[0])
                    {
                    case "Curve":

                        PlotterCurves c = new PlotterCurves();

                        c.Legend    = line_data[0];
                        c.LineColor = Color.FromName(line_data[1]);
                        Int32.TryParse(line_data[2], out c.LineThickness);
                        Int32.TryParse(line_data[3], out c.YAxis);

                        if (GraphIndex > -1)
                        {
                            _graphs[GraphIndex].Curves.Add(c);
                        }
                        break;

                    case "Yaxis":

                        PlotterYAxis y = new PlotterYAxis();

                        if (line_data.Length == 1)
                        {
                            y.auto = true;
                        }
                        else
                        {
                            bool.TryParse(line_data[0], out y.auto);
                            Double.TryParse(line_data[1], out y.min);
                            Double.TryParse(line_data[2], out y.max);
                            Double.TryParse(line_data[3], out y.step);
                            Double.TryParse(line_data[4], out y.step_divider);
                            Int32.TryParse(line_data[5], out y.digits_comma);
                        }

                        if (GraphIndex > -1)
                        {
                            _graphs[GraphIndex].Y_Axis.Add(y);
                        }
                        break;
                    }
                }
            }
        }
        public void Load(string file)
        {
            // read it
            if(!File.Exists(file))
                throw new Exception("File doesn't exist");
            string[] FileData = File.ReadAllLines(file);

            int GraphIndex = 0;
            foreach(string line in FileData)
            {
                string[] line_structure = new string[0], line_data = new string[0];
                if (line.Contains("="))
                {
                    line_structure = line.Trim().Split("=".ToCharArray(), 2);
                    if (line_structure.Length == 2) line_data = line_structure[1].Split(",".ToCharArray());
                }

                if(line.Trim()=="[Chart]")
                {
                    // A new chart
                    _graphs.Add(new PlotterGraph());
                    GraphIndex = _graphs.Count - 1;
                }

                if(line_structure.Length == 2)
                {
                    // A new curve of yaxis
                    switch(line_structure[0])
                    {
                        case "Curve":

                            PlotterCurves c = new PlotterCurves();

                            c.Legend = line_data[0];
                            c.LineColor = Color.FromName(line_data[1]);
                            Int32.TryParse(line_data[2], out c.LineThickness);
                            Int32.TryParse(line_data[3], out c.YAxis);

                            if(GraphIndex>-1)
                            _graphs[GraphIndex].Curves.Add(c);
                            break;

                        case "Yaxis":

                            PlotterYAxis y = new PlotterYAxis();

                            if (line_data.Length == 1)
                                y.auto = true;
                            else
                            {
                                bool.TryParse(line_data[0], out y.auto);
                                Double.TryParse(line_data[1], out y.min);
                                Double.TryParse(line_data[2], out y.max);
                                Double.TryParse(line_data[3], out y.step);
                                Double.TryParse(line_data[4], out y.step_divider);
                                Int32.TryParse(line_data[5], out y.digits_comma);
                            }

                            if (GraphIndex > -1)
                                _graphs[GraphIndex].Y_Axis.Add(y);
                            break;
                    }

                }

            }
        }