在c#中创建多个图表

时间:2013-12-10 11:05:06

标签: c#

for (int i = 0; i < 5; i += 1)
            {

                ShowReports(0);

            }


 private void ShowReports(int ComboID)
    { 
                   Graph.Series["Series1"].ChartType = SeriesChartType.Line;
                    Graph.Series["Series1"].BorderWidth = 2;
                    Graph.Series["Series1"].MarkerStyle = MarkerStyle.Circle;
                    Graph.ChartAreas["ChartArea1"].AxisX.LabelStyle.Format = "dd-MMM-yyy";
                    Graph.ChartAreas["ChartArea1"].AxisX.Title = "Date";
                    Graph.ChartAreas["ChartArea1"].AxisY.Title = "Average Score (%) ";

                    Graph.ChartAreas["ChartArea1"].AxisY.Minimum = 0;
                    Graph.ChartAreas["ChartArea1"].AxisY.Maximum = 100;

                    Graph.ChartAreas["ChartArea1"].AxisY.Interval = 10;

                    Graph.Series["Series1"].ToolTip = "Date :#VALX Avg Score(%) :#VALY";

                    Graph.Titles.Add(dtReportDetails.Rows[0].ItemArray[1].ToString());
                    Graph.Titles.Add(SetGraphTitile());

                    Graph.Titles[0].Font = new System.Drawing.Font("Arial", 20);
                    Graph.Titles[0].ForeColor = System.Drawing.Color.Black;

                    Graph.Titles[1].Font = new System.Drawing.Font("Arial", 13);
                    Graph.Titles[1].ForeColor = System.Drawing.Color.Black;

                    Graph.Titles[1].Visible = false;

                    // Graph.Series[0].Points.AddXY(DateTime.Parse(dtReportDetails.Rows[0].ItemArray[4].ToString()), dtReportDetails.Rows[0].ItemArray[5].ToString());
                    Graph.Series[0].XValueMember = dtReportDetails.Rows[0].ItemArray[4].ToString();
                    Graph.Series[0].YValueMembers = dtReportDetails.Rows[0].ItemArray[5].ToString();
                    Graph.Series[0].MarkerStyle = MarkerStyle.Circle;

                    Graph.Legends.Add("Legend1");
                    Graph.Legends[0].Enabled = false;
                    Graph.Legends[0].Docking = Docking.Bottom;
                    Graph.Legends[0].Alignment = System.Drawing.StringAlignment.Center;

                    Graph.DataSource = dv;
                    Graph.DataBind();

                }
                else if (dtReportDetails.Rows[0].ItemArray[7].ToString() == "Bar")
                {


                    Graph.Series["Series1"].ChartType = SeriesChartType.Column;
                    Graph.Series["Series1"].BorderWidth = 2;
                    //Graph.Series["Series1"].MarkerStyle = MarkerStyle.Circle;
                    //Graph.ChartAreas["ChartArea1"].AxisX.LabelStyle.Format = "dd-MMM-yyy";
                    //Graph.ChartAreas["ChartArea1"].AxisX.Title = "Date";
                    //Graph.ChartAreas["ChartArea1"].AxisY.Title = "Average Score (%) ";
                    Graph.Series["Series1"].ToolTip = "(#VALX,#VALY)";

                    Graph.Titles.Add(dtReportDetails.Rows[0].ItemArray[1].ToString());
                    Graph.Titles.Add(SetGraphTitile());

                    Graph.Titles[0].Font = new System.Drawing.Font("Arial", 20);
                    Graph.Titles[0].ForeColor = System.Drawing.Color.Black;

                    Graph.Titles[1].Font = new System.Drawing.Font("Arial", 13);
                    Graph.Titles[1].ForeColor = System.Drawing.Color.Black;

                    Graph.Titles[1].Visible = false;


                    Graph.ChartAreas["ChartArea1"].AxisX.Title = "Learning Domains";
                    Graph.ChartAreas["ChartArea1"].AxisY.Title = "Covered";




                    // Graph.Series[0].Points.AddXY(DateTime.Parse(dtReportDetails.Rows[0].ItemArray[4].ToString()), dtReportDetails.Rows[0].ItemArray[5].ToString());
                    Graph.Series[0].XValueMember = dtReportDetails.Rows[0].ItemArray[4].ToString();
                    Graph.Series[0].YValueMembers = dtReportDetails.Rows[0].ItemArray[5].ToString();
                    //  Graph.Series[0].MarkerStyle = MarkerStyle.Circle;


                    Graph.Legends.Add("Legend1");
                    Graph.Legends[0].Enabled = false;
                    Graph.Legends[0].Docking = Docking.Bottom;
                    Graph.Legends[0].Alignment = System.Drawing.StringAlignment.Center;

                    Graph.DataSource = dv;
                    Graph.DataBind();

                    Random random = new Random();
                    foreach (var item in Graph.Series[0].Points)
                    {
                        System.Drawing.Color c = System.Drawing.Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
                        item.Color = c;

                    }

                    Graph.Series[0]["PointWidth"] = "0.2";
                    Graph.Series[0]["BarLabelStyle"] = "Center";
                    Graph.Series[0]["PixelPointDepth"] = "99";
                    Graph.Series[0]["DrawingStyle"] = "Cylinder";

}

这是我创建图形的代码,为了创建1个图形我没有问题,但创建多个我将得到错误:名为“Legend1”的图表元素已经存在于'LegendCollection'中。因为我是每次创造相同的传说。所以你可以帮助摆脱这个问题。

2 个答案:

答案 0 :(得分:0)

解决图例对象名称的问题应该像通过考虑ComboID参数的字符串设置它一样简单。

string myLegendTitle =“Legend”+ ComboID; Graph.Legends.Add(myLegendTitle);

为了创建多个图表 - 假设您使用Microsoft Chart控件看起来像

for (int i = 0; i < 5; i += 1)
{
     ShowReports(0);
}


private void ShowReports(int ComboID)
{ 
     var myNewGraph = new System.Windows.Forms.DataVisualization.Charting.Chart();
     myNewGraph .Series["Series1"].ChartType = SeriesChartType.Line;
     myNewGraph .Series["Series1"].BorderWidth = 2;
     myParentControl.Controls.Add(myNewGraph);
}

关键点是在ShowReports方法中实例化一个新图表,然后在新的图形/图表对象上专门设置所有属性,就像之前一样。然后确保将控件添加到适当的容器(如果它尚不存在)。

如果您知道您将拥有5个图表,那么另一种方法是在设计时构建它们,并使用切换方法来设置变量,例如。

private void ShowReports(int ComboID)
{ 
     Chart myNewGraph;
     switch (ComboID)
     {
         case 1:
             myNewGraph = Graph1;
             break;
     }
     myNewGraph .Series["Series1"].ChartType = SeriesChartType.Line;
     myNewGraph .Series["Series1"].BorderWidth = 2;
}

答案 1 :(得分:0)

是否为创建5图表的代码..

for(int i=0;i< 6;i++)
{
Chart1.Series.Add("Series1" + t.ToString());
Chart1.ChartAreas.Add("ChartArea1" + t.ToString());
Chart1.Legends.Add("Legend1" + t.ToString());

Chart1.Series[t].ChartArea = "ChartArea1" + t.ToString();
Chart1.Series[t].ChartType = SeriesChartType.Column;
Chart1.Series[t].BorderWidth = 2;

Chart1.Series[t].ToolTip = "(#VALX,#VALY)";
Chart1.ChartAreas["ChartArea1" + t.ToString()].AxisX.Title = "Learning Domains";
Chart1.ChartAreas["ChartArea1" + t.ToString()].AxisY.Title = "Covered";
}