0

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

3
  • can you add the DataVisualization Chart control to the window form and see how the IDE is adding the code? From there, you can replace the Chart type with your own derived type. Commented Jun 13, 2016 at 14:06
  • Are you trying to "return" a value from a constructor? The chart you are defining is nested within your custom control, and not the custom control itself. Commented Jun 13, 2016 at 14:09
  • Sorry, I don't understand? I've used my code from the class in the form, on form_load, and it draws the chart fine. Commented Jun 13, 2016 at 14:11

1 Answer 1

2
//  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..

Sign up to request clarification or add additional context in comments.

1 Comment

I tried, this, first of all, which is what I thought it would be, but I got a readonly, as I was tring to create this, many thanks, I knew my memory wasn't that bad :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.