user3079666
user3079666

Reputation: 1165

Dynamically add Chart to Windows Forms - shows up blank

I'm trying to add a Chart control dynamically to a Form (using C#, this should all be .NET 4.0), but it's always blank (only the background color shows). I tried the same code in a Form that already has a control and it works, so I imagine it's some initialization function I should call (I tried Invalidate() on the control and Refresh() on both control and the panel it's being placed in, made no difference). I went through the few similar posts I found, tried throwing in any other commands they used (BeginInit() is from one such post) but no luck so far. Any ideas?

BTW I want to display 6-9 charts (position, speed and acceleration in 3D space) so I'd rather add them dynamically than have 9 sets of assignments. Here's the code that adds the charts to the panel:

foreach (KeyValuePair<string, List<double>> p in b.storedValues)
{
    Control c = getChartForData(p);
    panel1.Controls.Add(c);
    c.Invalidate();
    c.Refresh();
    break;
}

And the function that creates each chart:

private Chart getChartForData(KeyValuePair<string, List<double>> data)
        {
            Chart c = new Chart();
            ((System.ComponentModel.ISupportInitialize)c).BeginInit();
            c.Series.Clear();
            c.BackColor = Color.White;
            c.Height = 300;
            c.Width = 500;
            c.Palette = ChartColorPalette.Bright;
            Series s = new Series(data.Key);
            s.ChartType = SeriesChartType.Spline;
            double maxValue = 0;
            //NOTE: Going logarithmic on this, too big numbers
            for (int i = 0; i < data.Value.Count; i++)
            {
                maxValue = Math.Max(Math.Log10(data.Value[i]), maxValue);
            }
            for (int i = 0; i < data.Value.Count; i++)
            {
                s.Points.AddXY(i,Math.Log10(data.Value[i]) * c.Height / maxValue);
            }
            c.Series.Add(s);
            return c;
        }

Many thanks in advance.

Upvotes: 0

Views: 1458

Answers (1)

gnud
gnud

Reputation: 78528

When you create a Chart yourself, in code, it does not contain any ChartArea. Therefore, nothing is displayed.

I'm guessing that the designer generates some code to initialize a default chart area when you drag and drop a chart control onto the form.

Also, you should really let the chart control handle the layout, instead of calculating the desired position of each point based on the height of the chart control.

I would go as simple as possible to get something that's working, and then you can tweak the range of the axis afterwards. You can also set an axis to be logarithmic.

Start with trying out this minimal version, and make sure that displays something, before you complicate things. This works for me.

private Chart getChartForData(string key, List<double> data)
{
    Chart c = new Chart();      

    Series s = new Series(key);
    s.ChartType = SeriesChartType.Spline;

    for (int i = 0; i < data.Count; i++)
    {       
        s.Points.AddXY(i, data[i]);
    }       
    c.Series.Add(s);

    var area = c.ChartAreas.Add(c.ChartAreas.NextUniqueName()); 
    s.ChartArea = area.Name;
    // Here you can tweak the axis of the chart area - min and max value,
    // where they display "ticks", and so on.
    return c;
}

Upvotes: 2

Related Questions