gpt4 book ai didi

c# - 使用变量访问图表

转载 作者:行者123 更新时间:2023-11-30 17:55:40 27 4
gpt4 key购买 nike

我有一个 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 的想法、提示、技巧等都将不胜感激。

完整代码:

    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,这样您就可以调用您的例程四次不同的时间,每次都使用不同的图表。我已经采用了您的代码并替换了图表控件和设置的一些全局变量,并将它们作为您传递给函数的参数:

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

.....

关于c# - 使用变量访问图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14837148/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com