同时创建图表c#

时间:2016-09-08 19:14:10

标签: c# charts graphic

我想同时做一些图表。我做到了,只有我在组合框中选择参数,然后单击按钮然后创建图形。现在我需要保存一些参数,然后点击按钮创建两个,三个图形。这是怎么回事?我使用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()));
                }
            }
        }

........

1 个答案:

答案 0 :(得分:0)

    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()));

        }
    }