private void DrawGraph(string graphName) { VFunction.Func f; Enum.TryParse <VFunction.Func>(comboBox1.SelectedValue.ToString(), out f); System.Windows.Forms.DataVisualization.Charting.Series graph = chart.Series.FindByName(graphName); if (graph == null) { graph = chart.Series.Add(graphName); } graph.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; if (graph.Name == "Original") { graph.Color = ColorSet.MainColor; } else { graph.Color = ColorSet.PeakColor; } graph.Points.Clear(); double val = 0, maxVal = 0, yScale = 1; switch (f) { case VFunction.Func.Linear: for (double x = 0; x < 1.05; x += 0.05) { val = VFunction.Linear(x, upRate) * 1000 / settings.AutoControlInterval; maxVal = Math.Max(maxVal, val); graph.Points.AddXY(x, val); } break; case VFunction.Func.SlicedLinear: VFunction.FactorsForSlicedLinear sliceFactors = VFunction.GetFactorsForSlicedLinear(upRate, baseLevel); for (double x = 0; x < 1.05; x += 0.05) { val = VFunction.SlicedLinear(x, upRate, baseLevel, sliceFactors.A, sliceFactors.B) * 1000 / settings.AutoControlInterval; maxVal = Math.Max(maxVal, val); graph.Points.AddXY(x, val); } break; case VFunction.Func.Reciprocal: for (double x = 0; x < 1.05; x += 0.05) { val = VFunction.Reciprocal(x, upRate, kurtosis) * 1000 / settings.AutoControlInterval; maxVal = Math.Max(maxVal, val); graph.Points.AddXY(x, val); } break; case VFunction.Func.FixedReciprocal: for (double x = 0; x < 1.05; x += 0.05) { val = VFunction.FixedReciprocal(x, upRate, kurtosis) * 1000 / settings.AutoControlInterval; maxVal = Math.Max(maxVal, val); graph.Points.AddXY(x, val); } break; default: for (double x = 0; x < 1.05; x += 0.05) { val = upRate * 1000 / settings.AutoControlInterval; graph.Points.AddXY(x, val); } maxVal = 1; break; } if (graph.Name == "Original") { originalMax = maxVal; } else { maxVal = Math.Max(maxVal, originalMax); } for (double i = 1.0; i > 0.00001; i /= 2) { if (maxVal > i) { yScale = i * 2.0; break; } } chart.ChartAreas["Area"].AxisY.Maximum = yScale; if (maxVal > 0.01) { chart.ChartAreas["Area"].AxisY.LabelStyle.Format = "G"; } else { chart.ChartAreas["Area"].AxisY.LabelStyle.Format = "#.#E0"; } myText1.Y = chart.ChartAreas["Area"].AxisY.Maximum; myText2.Y = chart.ChartAreas["Area"].AxisY.Maximum * 0.92; }
/// <summary> /// Draw a graph of decrement function /// </summary> /// <param name="graphName">Unique identification of a visual graph</param> private void DrawGraph(string graphName) { List <Series> exc = new List <Series>(); foreach (Series s in plotView.Model.Series) { if (s.Title == graphName) { exc.Add(s); } } foreach (Series s in exc) { plotView.Model.Series.Remove(s); } exc.Clear(); VFunction.Func f; Enum.TryParse <VFunction.Func>(FunctionSelector.SelectedItem.ToString(), out f); Series exclude = null; foreach (var g in plotView.Model.Series) { if (g.Title == graphName) { exclude = g; } } if (exclude != null) { plotView.Model.Series.Remove(exclude); /*Console.WriteLine("Plot removed");*/ } FunctionSeries graph; switch (f) { case VFunction.Func.Linear: graph = new FunctionSeries(new Func <double, double>((x) => { double res = VFunction.Linear(x, settings.UpRate); // * 1000 / settings.AutoControlInterval; //if (res > 1) { res = 1; } else if (res < 0) { res = 0; } return(res); }), 0, 1, 0.05, graphName); break; case VFunction.Func.SlicedLinear: VFunction.FactorsForSlicedLinear sliceFactors = VFunction.GetFactorsForSlicedLinear(settings.UpRate, settings.TargetLevel); graph = new FunctionSeries(new Func <double, double>((x) => { double res = VFunction.SlicedLinear(x, settings.UpRate, settings.TargetLevel, sliceFactors.A, sliceFactors.B); // * 1000 / settings.AutoControlInterval; //if (res > 1) { res = 1; } else if (res < 0) { res = 0; } return(res); }), 0, 1, 0.05, graphName); break; case VFunction.Func.Reciprocal: graph = new FunctionSeries(new Func <double, double>((x) => { double res = VFunction.Reciprocal(x, settings.UpRate, settings.Kurtosis); // * 1000 / settings.AutoControlInterval; //if (res > 1) { res = 1; } else if (res < 0) { res = 0; } return(res); }), 0, 1, 0.05, graphName); break; case VFunction.Func.FixedReciprocal: graph = new FunctionSeries(new Func <double, double>((x) => { double res = VFunction.FixedReciprocal(x, settings.UpRate, settings.Kurtosis); // * 1000 / settings.AutoControlInterval; //if (res > 1) { res = 1; } else if (res < 0) { res = 0; } return(res); }), 0, 1, 0.05, graphName); break; default: graph = new FunctionSeries(new Func <double, double>((x) => { double res = settings.UpRate; // * 1000 / settings.AutoControlInterval; //if (res > 1) { res = 1; } else if (res < 0) { res = 0; } return(res); }), 0, 1, 0.05, graphName); break; } double maxVal = 0, yScale = 1; if (graph.Title == "Original") { graph.Color = Color(ColorSet.MainColor); originalMax = graph.MaxY; } else { graph.Color = Color(ColorSet.PeakColor); maxVal = Math.Max(graph.MaxY, originalMax); } plotView.Model.Series.Add(graph); plotView.Model.InvalidatePlot(true); for (double i = 1.0; i > 0.00001; i /= 2) { if (maxVal > i) { yScale = i * 2.0; break; } } //plotView.Model.DefaultYAxis.AbsoluteMaximum = yScale; //foreach (var g in plotView.Model.Series) { } //plotView.ZoomAllAxes(yScale); //if (maxVal > 0.01) chart.ChartAreas["Area"].AxisY.LabelStyle.Format = "G"; //else chart.ChartAreas["Area"].AxisY.LabelStyle.Format = "#.#E0"; //myText1.Y = chart.ChartAreas["Area"].AxisY.Maximum; //myText2.Y = chart.ChartAreas["Area"].AxisY.Maximum * 0.92; plotView.InvalidatePlot(); }