Nathan_Sav
Nathan_Sav

Reputation: 8531

C# Creating Custom Chart Class

It's been a while since I did something like this, however I am trying to create a custom chart class derived from the DataVisualization.Chart class, I have the following

public class clsCustomChart:System.Windows.Forms.DataVisualization.Charting.Chart
{

    public clsCustomChart(string strChartTitle, double[] dblX, double[] dblY)
    {
        //  Create the chart

        //  Create the chart
        Chart chartReturn = new Chart();
        chartReturn.BackColor = Color.FromArgb(50, Color.DarkGray);
        chartReturn.BorderlineDashStyle = ChartDashStyle.Solid;
        chartReturn.BorderlineColor = Color.Black;
        chartReturn.Width = 300;
        chartReturn.Height = 300;

        //  Create the legend
        Legend l = new Legend("Legend");
        l.Docking = Docking.Bottom;
        l.BackColor = Color.Transparent;
        chartReturn.Legends.Add(l);

        //  Create the chart area
        ChartArea a = new ChartArea("ChartArea1");
        a.Area3DStyle.Enable3D = false;
        a.Area3DStyle.WallWidth = 0;
        a.BackColor = Color.FromArgb(100, Color.Black);

        chartReturn.ChartAreas.Add(a);

        //  Create the axis
        a.AxisX.LineColor = Color.Silver;
        a.AxisX.MajorGrid.Enabled = true;
        a.AxisX.MinorGrid.Enabled = false;
        a.AxisX.MajorGrid.LineColor = Color.FromArgb(50, Color.Black);
        a.AxisX.LabelStyle.Font = new System.Drawing.Font("Arial", 8F);

        a.AxisY.LineColor = Color.Silver;
        a.AxisY.MajorGrid.Enabled = true;
        a.AxisY.MinorGrid.Enabled = false;
        a.AxisY.MajorGrid.LineColor = Color.FromArgb(50, Color.Black);
        a.AxisY.LabelStyle.Font = new System.Drawing.Font("Arial", 8F);

        //  Chart title
        chartReturn.Titles.Add(new Title(strChartTitle));

        //  Add the data
        //  Create the data series
        Series s = new Series("IN");
        s.ChartType = SeriesChartType.Line;

        dblX.ToList<double>().ForEach(x => { s.Points.Add(x); });
        s.Color = Color.FromArgb(200, Color.Red);
        s.BorderWidth = 3;

        Series s2 = new Series("OUT");
        s2.ChartType = SeriesChartType.Line;

        dblY.ToList<double>().ForEach(x => { s2.Points.Add(x); });
        s2.Color = Color.FromArgb(200, Color.Green);
        s2.BorderWidth = 3;

        chartReturn.Series.Add(s);
        chartReturn.Series.Add(s2);

        chartReturn.SaveImage("c:/test/" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".jpeg", ChartImageFormat.Jpeg);

    }

}

The code that is in the custom chart is all tested and working fine, when creating as a chart object, and the custom class saves the chart as image fine.

However, when I try this in a form

Chart C = (Chart)new clsCustomChart("TEST",x,y);

this.Controls.Add(C);

I don't get the chart...... can anyone advise.....

TIA

Upvotes: 0

Views: 2550

Answers (1)

TaW
TaW

Reputation: 54433

//  Create the chart
Chart chartReturn = new Chart(); 

This creates a chart which you then style and throw away.

Delete it and replace chartReturn with this!

Also you may want to provide a parameterless constructor in case you ever want to place it on a form via the designer..

Upvotes: 2

Related Questions