[英]C# How to add data to a dynamically added MSChart?
我目前正在嘗試將數據添加到動態創建的圖表中。 我有一個使用以下方法的類(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;
}
}
在另一個類(TestForm)中,我將使用圖表創建面板,如下所示:
source.AddGraph a = new source.AddGraph();
var createdPanel = a.panelAdd(tableLayoutPanel1, ip);
我現在該如何訪問面板內的圖表並執行諸如chart_holder.Series[0].Points.AddXY(1,10);
您可以使用此名稱按給控件提供的名稱進行搜索 。
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
}
}
您必須使用使用來訪問Chart Control :
using System.Windows.Forms.DataVisualization.Charting
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.