How to remove white areas in Chart? [closed]

2019-03-05 13:18发布

问题:

How can I remove white areas (1,2) and reduce width of axis (3) in chart (C#, Visual Studio 2013). Width of chart about 16000px. PS: If width of chart short (1000-2000 px) there are no white areas and width of axis is normally.

回答1:

The large white spaces are the proportionally enlarged distances.

You can set the positions to smaller values when you enlarge the Chart width.

Note that relevant property of the elements you can position is of type ElementPosition and that..

  • ..its values are not in pixels but in percentages of the respective containers.
  • ..the initial values are all set to 0, which means Automatic.

So you need to calculate the position each time you resize the chart and you can't initially set a single property since the others are still at 0.

These elements can be positioned:

  • The Chartarea(s)
  • The InnerPlotPosition of (each) Chartarea
  • The Legend(s)
  • a few others, like Annotations, we don't need here

You can also set the sizes of the Major- and MinorTickMarks from Auto to a suitable numeric value. Here is an example that work here for a Chart.Width of 16,000 pixels:

ChartArea ca = chart1.ChartAreas[0];
Legend L = chart1.Legends[0];

ca.Position = new ElementPosition(0.2f, 5, 99, 90);
ca.InnerPlotPosition = new ElementPosition(0.3f, 1, 99.5f, 90);
L.Position = new ElementPosition(99.03f, 5, 0.75f, 22);

ca.AxisY.MajorTickMark.Size = 0.15f;

ChartArea ca = chart1.ChartAreas[0];
ca.Position.X = 0.1f;
ca.InnerPlotPosition.X = 0.3f;

Axis ay = ca.AxisY;
ay.MajorTickMark.Size = 0.1f;

Also note that I can't see any way to position the YAxis label; so it will usually be off to the left. You can DrawString it in the Paint event, though:

private void chart1_Paint(object sender, PaintEventArgs e)
{
    Axis ay = chart1.ChartAreas[0].AxisY;
    Graphics g = e.Graphics;
    g.TranslateTransform(-20, 180);
    g.RotateTransform(270);
    using (SolidBrush brush = new SolidBrush(ay.TitleForeColor))
        g.DrawString(ay.Title, ay.TitleFont, brush, 22, 22);
}

I use some suitable values here as well but you will want to work out new ones for other sizes!


But: I'm not sure whether you should enlarge the chart this way. Instead I believe you should allow the user to zoom in and scroll in the zoomed chart!