2016-01-25 49 views
0

所以基本上我想有一堆图表的循环创建图表的多个实例(会有相当多的人)通过的回路形成相同的结构。是否有一种方法可以基于它们的名称(A1,A2,A3,A4,B1,B2,B3 ...)的数组来生成它们的全部(也包括图表区域和系列名称),还可以更改每个区域的位置新的一个?通过基于一个名字阵列

我真的很新的C#和将不胜感激任何帮助。希望有一个解决方案。谢谢!

单个图表的代码看起来像这样(有一些行,但我将只显示结构):

public Main() 
{ 
    System.Windows.Forms.DataVisualization.Charting.ChartArea A1_area = 
    new System.Windows.Forms.DataVisualization.Charting.ChartArea(); 

    this.A1 = new System.Windows.Forms.DataVisualization.Charting.Chart(); 

    System.Windows.Forms.DataVisualization.Charting.Series series1 = 
    new System.Windows.Forms.DataVisualization.Charting.Series(); 
    System.Windows.Forms.DataVisualization.Charting.Series series2 = 
    new System.Windows.Forms.DataVisualization.Charting.Series(); 

    A1_area.Position.Auto = false; 
    this.A1.ChartAreas.Add(A1_area); 
    this.A1.Location = new System.Drawing.Point(216, 43); 

    series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedBar100; 
    series1.Name = "Series1"; 
    series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedBar100; 
    series2.Name = "Series2"; 

    this.A1.Series.Add(series1); 
    this.A1.Series.Add(series2); 

    this.A1.Size = new System.Drawing.Size(100, 34); 
    this.A1.TabStop = false; 
    this.A1.Click += new System.EventHandler(this.A1_Click); 
} 

private void A1_Click(object sender, EventArgs e) 
{ 
} 

private System.Windows.Forms.DataVisualization.Charting.Chart A1; 
+0

你有没有考虑过使用数组/图表列表? – Eser

+0

感谢您的回答。是的,我尝试使用列表,但我无法使用它。我搜索了任何类似的例子,但没有找到任何。也许你可以给我一个提示或任何类似的链接? – lumens

+0

'我搜索了任何类似的例子,但没有找到任何'成为第一个创建这样一个令人难以置信的代码.... – Eser

回答

0

首先,你应该把这个线与其他包含语句以便更容易语法:

using System.Windows.Forms.DataVisualization.Charting; 

我也不清楚自己真正想要实现的目标,但是这是我想你问(未经测试):

public void Main() 
    { 
     string[] chartNames = { "A1", "A2", "A3", "A4", "B1", "B2", "B3" }; 
     List<Chart> charts = new List<Chart>; 
     int i = 0; 
     foreach(string cname in chartNames) 
     { 
      i++; 
      ChartArea chartArea = new ChartArea(); 
      Chart thisChart = new Chart(); 
      Series series1 = new Series(); 
      Series series2 = new Series(); 
      chartArea.Position.Auto = false; 
      thisChart.ChartAreas.Add(chartArea); 
      //each chart will be 43 units higher than the last 
      thisChart.Location = new System.Drawing.Point(216, (43*i)); 
      series1.ChartType = SeriesChartType.StackedBar100; 
      series2.ChartType = SeriesChartType.StackedBar100; 
      series1.Name = "Series1"; 
      series2.Name = "Series2"; 
      thisChart.Series.Add(series1); 
      thisChart.Series.Add(series2); 
      thisChart.Size = new System.Drawing.Size(100, 34); 
      thisChart.TabStop = false; 
      //sets the name of this chart to the corresponding string from the array of names 
      thisChart.Name = cname;   
      thisChart.Click += new System.EventHandler(this.chartClick); 
      //add this chart to the list of charts 
      charts.Add(thisChart); 
     }  
    } 

    private void chartClick(Object sender, EventArgs e) 
    { 
     //you can differentiate between charts using the sender.name property 
     Chart senderChart = (Chart)sender; 
     if(senderChart.Name == "A1") 
     { 
      //if the chart with name A1 is clicked... 
     } 
    } 

此外,您还可以使用类似的代码foreach循环内以下,设置区域为特定的名字:

  if(cname == "A1") 
      { 
       //set properties for chart with name A1 
       series1.Name = "A1 series 1"; 
       series2.Name = "A2 series 2"; 
       thisChart.Size = new System.Drawing.Size(200, 50); 
      } 
+0

哇,非常感谢您的时间!这个解决方案完全按照我想要的方式工作。用foreach循环设置名字也应该派上用场。再次感谢你。 – lumens

0

我不能发表评论,但还可以让你知道从哪里开始。我没有使用图表类,但类是类。不知道具体细节我正在暗中刺探。您需要一个数组中的名称列表来创建一个包含1个chartarea,2个具有设定值的系列的图表列表。以阵列中名称命名的图表和图表区域的名称。开始了。你需要一种方法。该方法将返回对象Chart对象的列表。图表属性和图表属性内的ChartArea中的名称属性将用于通过名称引用特定的图表,但整个列表将是您放入的任何内容。在这里,我们继续。首先,我不喜欢长名称空间,所以我会添加下面的行来缩短打字时间。

using System.Windows.Forms.DataVisualization.Charting; 

private List<Chart> MakeCharts(string[] YourArray) 
{ 
    var myChart = new List<Chart>(); 
    Foreach(string chName in YourArray) 
    { 
     ChartArea area = new ChartArea(); 
     area.Name = chName; 
     Chart chart = new Chart(); 
     chart.Name = chName; 
     Series series1 = new Series(); 
     Series series2 = new Series(); 

     area.Position.Auto = false; 
     chart.ChartAreas.Add(area); 
     chart.Location = new System.Drawing.Point(216, 43); 
     series1.ChartType = SeriesChartType.StackedBar100; 
     series1.Name = "Series1"; 
     series2.ChartType = SeriesChartType.StackedBar100; 
     series2.Name = "Series2"; 

     chart.Series.Add(series1); 
     chart.Series.Add(series2); 

     chart.Size = new System.Drawing.Size(100, 34); 
     chart.TabStop = false; 

     //Here is the tricky part. See explanation after code. 
     chart.Click += new System.EventHandler(charts_Click); 
     myChart.Add(chart); 
    } 

    return myChart; 
} 

用它在你的主要发言,如:

var MyListofCharts = MakeCharts(myArrayofNames); 

你可以通过使用一个foreach每个排行榜中的周期或使用LINQ其控制的名字引用特定的。在控制上没有任何东西可以玩,但是对于linq游戏应该是这样的。

var mySelectChart = [Chart]MyListofCharts.First(c => c.Name == "Chart Name"); 

因为我认为这可能会将它作为IEnumerable或其他奇怪的东西返回,所以我将它放回原样。

最后是您的活动。我不知道Chart控件,但是你的事件必须是动态的,因为你无法编程为每个Chart编写一个新的事件处理程序。 click事件中的sender对象应该包含它来自的控件。如果您在点击处于活动状态的内部添加了一个中断并检查发件人对象,则应该能够找到引用其来自的控件的属性。一旦你知道它的类型,你可以将它作为该类型转换出来,并从对象中操作控制权,并且可以为所有图表点击事件设置1个事件处理程序。

+0

谢谢你的时间!我已经尝试过提前一点提出解决方案,但我也会尝试将其中一个用于教育目的,因为我在使用方法方面非常糟糕。谢谢。 – lumens