【发布时间】:2021-05-01 02:05:14
【问题描述】:
我正在添加一个图表作为在单独的类中创建的控件。图表的背景绘制,但图表本身不绘制。有人能指出我的错误在哪里吗?我尝试过诸如 BringToFront、Anchoring、Dock.Fill、Invalidate 之类的东西。
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
ChartControl MyChart;
public Form1()
{
InitializeComponent();
MyChart = new ChartControl();
this.Controls.Add(MyChart);
MyChart.chart1.Series[0].Points.AddXY(1.4, 1.3);
MyChart.chart1.Series[0].Points.AddXY(1.7, 1.9);
}
}
}
图表类
using System.Windows.Forms.DataVisualization.Charting;
namespace WindowsFormsApp2
{
public class ChartControl : Chart
{
public Chart chart1;
public ChartControl()
{
ChartArea chartArea1 = new ChartArea();
Legend legend1 = new Legend();
Series series1 = new Series();
chart1 = new Chart();
((System.ComponentModel.ISupportInitialize)chart1).BeginInit();
SuspendLayout();
chartArea1.Name = "ChartArea1";
chart1.ChartAreas.Add(chartArea1);
legend1.Name = "Legend1";
chart1.Legends.Add(legend1);
chart1.Location = new System.Drawing.Point(0, 0);
chart1.Name = "chart1";
series1.ChartArea = "ChartArea1";
series1.Legend = "Legend1";
series1.Name = "Series1";
chart1.Series.Add(series1);
chart1.Size = new System.Drawing.Size(300, 300);
chart1.TabIndex = 0;
chart1.Text = "chart1";
((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit();
ResumeLayout(false);
}
}
}
【问题讨论】:
-
ChartControl已经是一个图表。为什么要添加另一个您使用但实际上无法显示的public Chart chart1?删除它并将MyChart.chart1.Series[0].Points.AddXY(1.4, 1.3);替换为MyChart.Series[0].Points.AddXY(1.4, 1.3);等.. 并将chartArea1.Name = "ChartArea1";替换为Name = "ChartArea1";等..!
标签: c# winforms charts data-visualization