如何删除图表中的白色区域?

时间:2016-09-20 19:07:23

标签: c# winforms charts

如何在图表中删除白色区域(1,2)并减小轴(3)的宽度(C#,Visual Studio 2013)。图表宽度约为16000像素。 PS:如果图表宽度较短(1000-2000像素),则没有白色区域,轴的宽度正常。 enter image description here

1 个答案:

答案 0 :(得分:0)

大的白色空间是按比例放大的距离。

放大Chart宽度时,可以将位置设置为较小的值。

请注意,您可以定位的元素的相关属性是ElementPosition类型和那个..

  • ..其值不是像素,而是各个容器的百分比。
  • ..初始值全部设置为0,表示Automatic

因此,您需要在每次调整图表大小时计算位置,并且最初无法设置单个属性,因为其他属性仍在0

可以定位这些元素:

  • Chartarea(s)
  • (每个)InnerPlotPosition
  • Chartarea
  • Legend(s)
  • 其他一些人,例如Annotations,我们不需要这里

您还可以将Major - 和MinorTickMarks的尺寸从Auto设置为合适的数值。以下是一个适用于Chart.Width 16,000像素的示例:

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;

enter image description here

另请注意,我无法找到任何方法来定位YAxis标签;所以它通常会偏向左边。不过,您可以在DrawString事件中Paint

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);
}

我在这里也使用了一些合适的值,但你会想要为其他尺寸制作出新的值!

但是:我不确定你是否应该这样放大图表。相反,我认为你应该允许用户在缩放图表中缩放滚动