2013-02-12 4 views
3

일부 분석 결과를 그래픽으로 표시하는 데 사용되는 4 개의 차트 컨트롤이있는 C# WinForms 응용 프로그램이 있습니다. ,변수를 사용하여 차트에 액세스하십시오.

  • 필요한 시리즈를 만들
  • 추출물 :

    나는 코드가 나는에 코드 블록을 정의했다 & 재사용 코드를보다 효율적으로하지만 시도, 각 그래프에 대한 작업이 데이터베이스의 데이터 &은 해당 계열에 결과를 할당합니다.

  • 차트에 시리즈를 추가하십시오.
  • 차트 모양을 사용자 정의하십시오.

위의 모든 작업은 디자인 타임에 데이터가 없기 때문에 동적으로 수행됩니다.

내가 사용을 다시 찾고 작업 코드는 다음과 같습니다

// Add both series to the chart. 
ChartName.Series.AddRange(new Series[] { series1, series2 }); 

// Cast the chart's diagram to the XYDiagram type, to access its axes. 
XYDiagram diagram = (XYDiagram)ChartName.Diagram; 
나는 내가 다시 사용하기 위해 차트의 각을 통과 할 수 변수에 ChartName 객체를 변경하려면

암호. 같은 뭔가 (작동하지 않는이주의) : -

var VChart = this.Controls.Find(ChartName, true); 

// Add both series to the chart. 
VChart.Series.AddRange(new Series[] { series1, series2 }); 

// Cast the chart's diagram to the XYDiagram type, to access its axes. 
XYDiagram diagram = (XYDiagram)VChart.Diagram; 

모든 아이디어, 힌트, 팁, 등등 ChartName에 변수를 전달하는 방법-에 감상 할 수있다.

전체 코드 : 당신은 당신이 다른 차트, 때마다 당신의 일상적인 네 가지 번 호출 할 수 있도록 변수로 하드 ChartName을 교체하라는 것 같은

void Generate_Chart() 
    { 
     // Create two stacked bar series. 
     Series series1 = new Series("Data", ViewType.Bar); 
     Series series2 = new Series("Ben", ViewType.Line); 

     try 
     {      
      using (var cmd = new SQLiteCommand(m_dbConnection)) 
      for (int i = LoopMin; i < LoopMax; i++) 
      { 
       // Retrieve the actual calculated values from the database 
       cmd.CommandText = "SELECT " + Chart_SourceActualValue + " FROM " + Chart_SourceTable + " WHERE Value = " + i + ""; 
       Chart_SeriesA_Value = Convert.ToInt32(cmd.ExecuteScalar()); 

       // Retrieve the expected values from the database 
       cmd.CommandText = "SELECT " + Chart_BenExpValue + " FROM " + Chart_SourceTable + " WHERE Value = " + i + ""; 
       Chart_SeriesB_Value = Convert.ToInt32(cmd.ExecuteScalar()); 

       // Add the dynamically created values to a series point for the chart 
       series1.Points.Add(new SeriesPoint(i, Chart_SeriesA_Value)); 
       series2.Points.Add(new SeriesPoint(i, Chart_SeriesB_Value)); 
      } 
     } 
     catch (Exception) 
     {     
      throw; 
     } 

     // Add both series to the chart. 
     //this.Controls.Find(varChart, true) 
     ChartName.Series.AddRange(new Series[] { series1, series2 }); 

     // Remove the GridLines from the chart for better UI 
     // Cast the chart's diagram to the XYDiagram type, to access its axes. 
     XYDiagram diagram = (XYDiagram)ChartName.Diagram; 
     // Customize the appearance of the axes' grid lines. 
     diagram.AxisX.GridLines.Visible = false; 
     }   
} 
+0

"ChartName에 변수 전달"이란 무엇입니까? – Magnus

+0

generic 메서드를 만들기 위해'ref' 키워드를 사용하여 비슷한 것을했습니다 – Sayse

+0

ChartName을 시리즈 코드 세그먼트와 XY 다이어그램 세그먼트에 전달하기 위해 변수를 다시 사용하고 싶습니다. 본질적으로 나는 네 개의 차트 모두에 대해 동일한 두 줄의 코드를 사용하고 8 줄의 코드가 ChartName 인 유일한 차이점을 가지고 똑같은 일을하지는 않을 것입니다. –

답변

1

소리가 난다. 난 당신의 코드를 촬영하고 차트 제어와 설정의 전역 변수의 일부를 대체하고 당신이 함수에 전달할 매개 변수했습니다 : 원본을 사용하여이 같은이 메서드를 호출 결국, 그런

void Generate_Chart(DevExpress.XtraCharts.ChartControl chartCtrl, 
        string chart_sourceActualValue, 
        string chart_sourceTable, 
        string chart_benExpValue 
        ) 
    { 
     // Create two stacked bar series. 
     Series series1 = new Series("Data", ViewType.Bar); 
     Series series2 = new Series("Ben", ViewType.Line); 

     try 
     {      
      using (var cmd = new SQLiteCommand(m_dbConnection)) 
      for (int i = LoopMin; i < LoopMax; i++) 
      { 
       // Retrieve the actual calculated values from the database 
       cmd.CommandText = "SELECT " + sourceActualValue + " FROM " + 
        chart_sourceTable + " WHERE Value = " + i + ""; 
       Chart_SeriesA_Value = Convert.ToInt32(cmd.ExecuteScalar()); 

       // Retrieve the expected values from the database 
       cmd.CommandText = "SELECT " + chart_benExpValue + " FROM " + 
         chart_sourceTable + " WHERE Value = " + i + ""; 
       Chart_SeriesB_Value = Convert.ToInt32(cmd.ExecuteScalar()); 

       // Add the dynamically created values 
       // to a series point for the chart 
       series1.Points.Add(new SeriesPoint(i, Chart_SeriesA_Value)); 
       series2.Points.Add(new SeriesPoint(i, Chart_SeriesB_Value)); 
      } 
     } 
     catch (Exception) 
     {     
      throw; 
     } 

     // Add both series to the chart. 
     chartCtrl.Series.AddRange(new Series[] { series1, series2 }); 

     // Remove the GridLines from the chart for better UI 
     // Cast the chart's diagram to the XYDiagram type, to access its axes. 
     XYDiagram diagram = (XYDiagram)chartCtrl.Diagram; 
     // Customize the appearance of the axes' grid lines. 
     diagram.AxisX.GridLines.Visible = false; 
     }   
} 

값을 인수로 사용하십시오.

void Generate_Chart(ChartName, Chart_SourceActualValue, Chart_SourceTable, 
        Chart_BenExpValue); 

// call it three other times passing in the different specifics for that chart. e.g. 
void Generate_Chart(SomeOtherChartName, SomeOtherChart_SourceActualValue, 
        SomeOhterChart_SourceTable, SomeOtherChart_BenExpValue); 

..... 
관련 문제