Search code examples
c#winformsmschart

Chart control width with many series


I have this chart with some series in legend and this chart is docking fill in panel. The legend has these properties:

chart1.Legends.Add(seriesName);
chart1.Legends[0].Docking = Docking.Bottom;
chart1.Legends[0].TableStyle = LegendTableStyle.Wide;
chart1.Legends[0].BorderColor = Color.CornflowerBlue;
chart1.Legends[0].BorderDashStyle = ChartDashStyle.Dash;
chart1.Legends[0].BorderWidth = 1;

Chart with only a few series:

enter image description here

With even more series but the same interval date I have this result:

enter image description here

The issue is not the scale of the data but the reduced size of the ChartArea itself.. - How can I fix this?


Solution

  • The reason is in this line:

    chart1.Legends.Add(seriesName);
    

    What it does is adding one totally empty Legend, most likely for each of the Series you add. It is placed at the default position, that is to the right. And if you get enough of them they push the ChartArea back to the left..

    Simply remove the line, as all Series get added to the default Legend anyway. It is only that default Legend your next lines style and position docked to the bottom.

    To demostrate the effect you can add a LegendItem to each:

    Legend l = chart1.Legends.Add(chart1.Legends.Count + ""));
    l.CustomItems.Add(Color.HotPink, chart1.Legends.Count + " *");
    

    Result:

    enter image description here

    As you can see even very few extra Legends push the ChartArea way to the left. Yours are empty, so they don't take as musch room, but still..