labnet
labnet

Reputation: 5

Add Chart as Control from separate class. Chart not drawing

I'm adding a chart as a control created in a separate class. The background of the chart paints, but the chart itself is not painting. Can someone point out where my error is? I've tried things like 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);
        }
    }
}

The Chart Class

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);
        }
    }
}

Upvotes: 0

Views: 268

Answers (1)

Aelarion
Aelarion

Reputation: 417

The form that you're adding MyChart to has nothing to draw because you add all of your data to MyChart.chart1, which is an additional field you've created in the ChartControl class.

You're manipulating all the "chart" data in chart1, but all of the WinForm code to draw content is in the Chart class you're extending in ChartControl, which has no clue what chart1 is (or that it even exists).

My guess is you're creating this type of "wrapper" class to apply a particular style to the chart. If that's the case you need to make sure you're directly manipulating ChartControl properties that are inherited from Chart, not creating custom properties or fields (unless you intend to override paint methods to make use of them).

Example constructor:

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";
}

As @TaW mentioned -- and I agree -- is not really a great design approach, but should work nonetheless (although I haven't tested this code).

Upvotes: 2

Related Questions