//Расчет новых параметров регрессии и построение нового прогноза вместе с линией тренда private void predictionMonths_SelectedIndexChanged(object sender, EventArgs e) { try { try { chart1.Series[0].Points.Clear(); chart1.Series[0].Points.Dispose(); chart1.Series[1].Points.Clear(); chart1.Series[1].Points.Dispose(); chart1.Series[2].Points.Clear(); chart1.Series[2].Points.Dispose(); } catch { } DBConnection.GetSellings(); x.Clear(); y.Clear(); foreach (DataRow row in DBConnection.dtSellings.AsEnumerable()) { x.Add(Convert.ToSingle(row[0])); y.Add(Convert.ToSingle(row[1])); } LinearRegression.calculate_regression(x, y); label20.Text = LinearRegression.mean_x.ToString("0.##"); label21.Text = LinearRegression.mean_y.ToString("0.##"); label22.Text = LinearRegression.std_deviation_x.ToString("0.##"); label23.Text = LinearRegression.std_deviation_y.ToString("0.##"); label2.Text = LinearRegression.covariation.ToString("0.##"); label4.Text = LinearRegression.correlation.ToString("0.##"); for (int i = 0; i < 12; i++) { try { err.Add(y[i] / (y.Sum() / y.Count())); } catch { err.Add(1); } } sales_chart(x, y); prediction(LinearRegression.intercept, LinearRegression.slope, err, Convert.ToInt32(predictionMonths.Text)); int end_x = chart1.Series[0].Points.Count + Convert.ToInt32(predictionMonths.Text); //последняя точка линии тренда trend_line(LinearRegression.intercept, LinearRegression.slope, end_x); chart1.Invalidate(); } catch (Exception ex) { exceptPanel.Visible = true; richTextBox1.Text = ex.ToString(); } }
//Расчет новых параметров регрессии и построение нового графика продаж вместе с линией тренда private void displaySalesGraph_Click(object sender, EventArgs e) { try { try { chart1.Series[0].Points.Clear(); chart1.Series[0].Points.Dispose(); chart1.Series[1].Points.Clear(); chart1.Series[1].Points.Dispose(); chart1.Series[2].Points.Clear(); chart1.Series[2].Points.Dispose(); } catch { } DBConnection.GetSellingsByYear(salesYears.Text); x.Clear(); y.Clear(); foreach (DataRow row in DBConnection.dtSellingsByYear.AsEnumerable()) { x.Add(Convert.ToSingle(row[0])); y.Add(Convert.ToSingle(row[1])); } sales_chart(x, y); LinearRegression.calculate_regression(x, y); label20.Text = LinearRegression.mean_x.ToString("0.##"); label21.Text = LinearRegression.mean_y.ToString("0.##"); label22.Text = LinearRegression.std_deviation_x.ToString("0.##"); label23.Text = LinearRegression.std_deviation_y.ToString("0.##"); label2.Text = LinearRegression.covariation.ToString("0.##"); label4.Text = LinearRegression.correlation.ToString("0.##"); int end_x = Convert.ToInt32(x.Max()); //конечная точка линии тренда trend_line(LinearRegression.intercept, LinearRegression.slope, end_x); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
//Происходит при загрузке формы, первичное построение графика продаж, линии тренда, прогноза private void Stats_Load(object sender, EventArgs e) { try { try { chart1.Series[0].Points.Clear(); chart1.Series[0].Points.Dispose(); chart1.Series[1].Points.Clear(); chart1.Series[1].Points.Dispose(); chart1.Series[2].Points.Clear(); chart1.Series[2].Points.Dispose(); } catch { } DBConnection.GetSellingsYears(); salesYears.DataSource = DBConnection.dtSellingsYears; salesYears.DisplayMember = "year"; salesYears.ValueMember = "year"; DBConnection.GetSellings(); reconnectBtn.Font = new Font(reconnectBtn.Font.Name, 10, FontStyle.Regular | FontStyle.Underline); x.Clear(); y.Clear(); foreach (DataRow row in DBConnection.dtSellings.AsEnumerable()) { x.Add(Convert.ToSingle(row[0])); y.Add(Convert.ToSingle(row[1])); } LinearRegression.calculate_regression(x, y); label20.Text = LinearRegression.mean_x.ToString("0.##"); label21.Text = LinearRegression.mean_y.ToString("0.##"); label22.Text = LinearRegression.std_deviation_x.ToString("0.##"); label23.Text = LinearRegression.std_deviation_y.ToString("0.##"); label2.Text = LinearRegression.covariation.ToString("0.##"); label4.Text = LinearRegression.correlation.ToString("0.##"); for (int i = 0; i < 12; i++) { try { err.Add(y[i] / (y.Sum() / y.Count())); } catch { err.Add(1); } } sales_chart(x, y); chart1.Series.Add("Прогноз"); chart1.Series[1].BorderWidth = 3; chart1.Series[1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; prediction(LinearRegression.intercept, LinearRegression.slope, err, 6); chart1.Series.Add("Линия тренда"); chart1.Series[2].BorderWidth = 3; chart1.Series[2].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; int end_x = chart1.Series[0].Points.Count + chart1.Series[1].Points.Count; trend_line(LinearRegression.intercept, LinearRegression.slope, end_x); chart1.ChartAreas["ChartArea1"].AxisX.Title = "Номер периода"; chart1.ChartAreas["ChartArea1"].AxisY.Title = "Сумма"; } catch (Exception ex) { exceptPanel.Visible = true; richTextBox1.Text = ex.ToString(); } }