创建图表在同一时间c#

本文关键字:同一时间 创建 | 更新日期: 2023-09-27 18:06:11

我想同时做一些图表。我做到了,只有我在组合框中选择参数,然后点击按钮,然后创建图形。现在我需要保存一些参数,然后点击按钮创建两个,三个图形。怎么做呢?我使用MS Chart,其中从datagridview输入日期。

private void graph2()
    {
        chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
        chart1.Series[1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
        chart1.Series[2].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
        chart1.Series[3].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
        if (metroComboBox1.Text == "Name 1")
        {
            //chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;

            for (int i = 0; i < metroGrid2.Rows.Count - 1; i++)
            {
                foreach (DataGridViewRow row in metroGrid2.Rows)
                {
                    int x = 0;
                    Int32.TryParse(metroGrid2.Rows[i].Cells[4].Value.ToString(), out x);
                    DateTime dt;

                    DateTime.TryParse(metroGrid2.Rows[i].Cells[5].Value.ToString(), out dt);
                    chart1.Series[0].Points.AddXY(metroGrid2.Rows[i].Cells[5].Value.ToString(), metroGrid2.Rows[i].Cells[4].Value.ToString());
                    if (metroComboBox3.Text == "Text 1")
                    {
                        chart1.Series[0].Color = Color.Red;
                    }
                    if (metroComboBox3.Text == "Text 2")
                    {
                        chart1.Series[0].Color = Color.Green;
                    }
                    //chart1.Series[0].Points[5].Color = Color.Blue;
                    //Console.WriteLine(chart1.Series[0].Points.AddXY(metroGrid1.Rows[i].Cells[5].Value.ToString(), metroGrid1.Rows[i].Cells[4].Value.ToString()));
                }
            }
        }
        if (metroComboBox1.Text == "Name 2")
        {
            for (int i = 0; i < metroGrid2.Rows.Count - 1; i++)
            {
                foreach (DataGridViewRow row in metroGrid2.Rows)
                {
                    int x = 0;
                    Int32.TryParse(metroGrid2.Rows[i].Cells[4].Value.ToString(), out x);
                    DateTime dt;

                    DateTime.TryParse(metroGrid2.Rows[i].Cells[5].Value.ToString(), out dt);
                    chart1.Series[1].Points.AddXY(metroGrid2.Rows[i].Cells[5].Value.ToString(), metroGrid2.Rows[i].Cells[4].Value.ToString());
                    if (metroComboBox3.Text == "Text1")
                    {
                        chart1.Series[1].Color = Color.Red;
                    }
                    if (metroComboBox3.Text == "text2")
                    {
                        chart1.Series[1].Color = Color.Green;
                    }
                    //Console.WriteLine(chart1.Series[00].Points.AddXY(metroGrid1.Rows[i].Cells[5].Value.ToString(), metroGrid1.Rows[i].Cells[4].Value.ToString()));
                }
            }
        }

........

创建图表在同一时间c#

    private void graph2()
    {
        chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
        chart1.Series[1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
        chart1.Series[2].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
        chart1.Series[3].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
        setChart();
    }
    private void setChart()
    {
        switch (metroComboBox1.SelectedText.ToString())
        {
            case "Name 1":
                finalizeChart();
                break;
            case "Name 2":
                finalizeChart();
                break;
        }
    }
    private void finalizeChart()
    {
        for (int i = 0; i < metroGrid2.Rows.Count - 1; i++)
        {
                                int x = 0;
                Int32.TryParse(metroGrid2.Rows[i].Cells[4].Value.ToString(), out x);
                DateTime dt;

                DateTime.TryParse(metroGrid2.Rows[i].Cells[5].Value.ToString(), out dt);
                chart1.Series[i].Points.AddXY(metroGrid2.Rows[i].Cells[5].Value.ToString(), metroGrid2.Rows[i].Cells[4].Value.ToString());
                if (metroComboBox3.Text == "Text 1")
                {
                    chart1.Series[i].Color = Color.Red;
                }
                if (metroComboBox3.Text == "Text 2")
                {
                    chart1.Series[i].Color = Color.Green;
                }
                //chart1.Series[0].Points[5].Color = Color.Blue;
                //Console.WriteLine(chart1.Series[0].Points.AddXY(metroGrid1.Rows[i].Cells[5].Value.ToString(), metroGrid1.Rows[i].Cells[4].Value.ToString()));
        }
    }