简体   繁体   English

C#如何将数据添加到动态添加的MSChart?

[英]C# How to add data to a dynamically added MSChart?

I'm currently trying to add data to my chart that I created dynamically. 我目前正在尝试将数据添加到动态创建的图表中。 I've got a class (AddGraph) with following method: 我有一个使用以下方法的类(AddGraph):

public class AddGraph
{
    public string Name { get; set; }
    Random R = new Random();
    System.Windows.Forms.DataVisualization.Charting.Chart chart_holder = new System.Windows.Forms.DataVisualization.Charting.Chart();
    System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();

    public Panel panelAdd(TableLayoutPanel fp, string hostNameIP)
    {
        Panel p = new Panel();
        p.Name = hostNameIP;
        Name = p.Name;
        p.Anchor = (AnchorStyles.Left | AnchorStyles.Right);
        p.Size = new Size(fp.ClientSize.Width, 100);
        p.Dock = DockStyle.Top;

        //tablelayoutpanel - definition
        fp.Controls.Add(p);
        fp.Controls.SetChildIndex(p, 0);
        fp.HorizontalScroll.Visible = false;
        fp.HorizontalScroll.Maximum = 0;
        fp.AutoScroll = false;
        fp.AutoScroll = true;

        //insert title
        chart_holder.Titles.Add(hostNameIP);
        chart_holder.Titles[0].Alignment = ContentAlignment.TopLeft;

        chart_holder.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom);
        chart_holder.Size = p.Size;
        chart_holder.ChartAreas.Add(chartArea1);
        chart_holder.Series.Add("Series1");
        chart_holder.BackColor = Color.Transparent;

        chart_holder.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
        chart_holder.Series[0].ChartArea = chart_holder.ChartAreas[0].Name;

        chart_holder.ChartAreas[0].BackColor = Color.Transparent;
        chart_holder.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
        chart_holder.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dot;

        p.Controls.Add(chart_holder);
        fp.Invalidate();

        return p;
    }
}

In another class (TestForm) I'm creating the panels with the chart like following: 在另一个类(TestForm)中,我将使用图表创建面板,如下所示:

source.AddGraph a = new source.AddGraph();
var createdPanel = a.panelAdd(tableLayoutPanel1, ip);

How can I access now the chart inside the panel and do something like chart_holder.Series[0].Points.AddXY(1,10); 我现在该如何访问面板内的图表并执行诸如chart_holder.Series[0].Points.AddXY(1,10);

You can search by the name that you gave to the control by using this. 您可以使用此名称按给控件提供的名称进行搜索

foreach (Control c in fp.Controls)
{
    if (c is Chart)
    {
        Chart ChartControl = (Chart)c;

        if (ChartControl.Name.Equals("Chart Name"))      
            //Code to modify Chart values
    }
}

You MUST use this using , to access the Chart Control : 必须使用使用来访问Chart Control

using System.Windows.Forms.DataVisualization.Charting

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM