일부 분석 결과를 그래픽으로 표시하는 데 사용되는 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;
}
}
"ChartName에 변수 전달"이란 무엇입니까? – Magnus
generic 메서드를 만들기 위해'ref' 키워드를 사용하여 비슷한 것을했습니다 – Sayse
ChartName을 시리즈 코드 세그먼트와 XY 다이어그램 세그먼트에 전달하기 위해 변수를 다시 사용하고 싶습니다. 본질적으로 나는 네 개의 차트 모두에 대해 동일한 두 줄의 코드를 사용하고 8 줄의 코드가 ChartName 인 유일한 차이점을 가지고 똑같은 일을하지는 않을 것입니다. –