【问题标题】:Add Chart as Control from separate class. Chart not drawing从单独的类中添加图表作为控件。图表不画
【发布时间】: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


【解决方案1】:

您要添加MyChart 的表单没有任何内容可绘制,因为您将所有数据添加到MyChart.chart1,这是您在ChartControl 类中创建的附加字段。

您正在处理chart1 中的所有“图表”数据,但是用于绘制内容的所有 WinForm 代码都在您在 ChartControl 中扩展的 Chart 类中,它不知道 @987654327 是什么@ 是(或者它甚至存在)。

我的猜测是您正在创建这种类型的“包装器”类以将特定样式应用于图表。如果是这种情况,您需要确保直接操作从 Chart 继承的 ChartControl 属性,而不是创建自定义属性或字段(除非您打算覆盖绘制方法以使用它们)。

示例构造函数:

public ChartControl()
{
    ChartArea chartArea1 = new ChartArea();
    Legend legend1 = new Legend();
    Series series1 = new Series();
    chartArea1.Name = "ChartArea1";
    this.ChartAreas.Add(chartArea1);
    this.Name = "chart1";
    series1.ChartArea = "ChartArea1";
    series1.Legend = "Legend1";
    series1.Name = "Series1";
    this.Series.Add(series1);
    this.Text = "chart1";
}

正如@TaW 所提到的——我同意——这并不是一个很好的设计方法,但仍然可以工作(尽管我没有测试过这段代码)。

【讨论】:

  • 感谢 Taw 和 @Aelarion 的清晰解释。你纠正了我对继承应该如何工作的误解。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-20
  • 1970-01-01
  • 2017-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多