[英]How to access a chart to add a data point in c#
I am trying to write a class to update chart data.我正在尝试编写 class 来更新图表数据。 I have created the chart through windows forms which has autogenerated the code in Form1.cs and Form1.Designer.cs我已经通过 windows forms 创建了图表,它在 Form1.cs 和 Form1.Designer.cs 中自动生成了代码
Here is what i think is the relevant part from Form1.designer.cs:这是我认为来自 Form1.designer.cs 的相关部分:
private void InitializeComponent()
{
System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint1 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(0D, 2D);
System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint2 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(0D, 3D);
System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint3 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(0D, 2D);
System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint4 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(0D, 25D);
System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint5 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(0D, 2D);
System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint6 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(0D, 3D);
System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint7 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(0D, 0D);
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
this.tableLayoutPanel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit();
this.SuspendLayout();
I have written another class called UpdateGraph.cs which has a method to add an extra point to the graph我写了另一个名为 UpdateGraph.cs 的 class,它有一种方法可以在图中添加一个额外的点
namespace DataLogger
{
class UpdateGraph
{
public void addGraphPoints()
{
chart1.Series.Points.AddXY(0, 10);
}
}
}
The problem is I get an error with message问题是我收到消息错误
The name chart1 does not exist in the current context当前上下文中不存在名称 chart1
So if anyone could explain how I can access the chart to modify the data (or what i should be refering to) I would really appreciate it as I am a bit stumped at the moment.因此,如果有人能解释我如何访问图表来修改数据(或者我应该指的是什么),我会非常感激,因为我现在有点难过。
You could try this one: 您可以尝试以下方法:
using System.Windows.Forms.DataVisualization.Charting;
class UpdateGraph
{
public Chart Chart1 { get; set; }
public UpdateGRaph(Chart chart)
{
Chart1 = chart;
}
public void AddGraphPoints()
{
Chart1.Series.Points.AddXY(0, 10);
}
}
Forms Designer will create a local chart in InitializeComponent() - maybe there's a way to prevent it but I've yet to find it. Forms Designer将在InitializeComponent()中创建一个本地图表-也许有一种方法可以阻止它,但是我还没有找到它。 The trick is to get the chart out of your control hierarchy. 诀窍是使图表脱离您的控制层次结构。 For example, my chart in Designer.cs is added to grpStuff: 例如,将我在Designer.cs中的图表添加到grpStuff中:
this.grpStuff.Controls.Add(chrtStuff);
and its name can be found in Designer.cs also: 其名称也可以在Designer.cs中找到:
chrtStuff.Name = "chartName";
SO - in my code I find the chart by: 所以-在我的代码中,我通过以下方式找到图表:
chrt = (System.Windows.Forms.DataVisualization.Charting.Chart)grpStuff.Controls("chartName");
chart1.Series["Your series name"].Points.AddXY(0, 10); chart1.Series["您的系列名称"].Points.AddXY(0, 10); sorry to be being this fast抱歉这么快
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.