简体   繁体   English

Windows 窗体 C# 如何使图表的网格线变为正方形?

[英]Windows forms C# how to make the grid lines of a chart square?

I have a chart in windows forms and I want the gridlines to be squared.我有一个 Windows 窗体中的图表,我希望网格线是平方的。 The gridline is anchored to bottom, top, left and right so it resizes with the screen.网格线固定在底部、顶部、左侧和右侧,因此它会随着屏幕调整大小。 How do I make the grid lines always square and make the whole chart resize with the screen?如何使网格线始终为方形并使整个图表随屏幕调整大小?

图形图像

I have tried setting the width and height to be the same, but it doesn't work since the series names of the chart are on the right.我曾尝试将宽度和高度设置为相同,但它不起作用,因为图表的系列名称在右侧。

EDIT 1: Here is the full uncensored code:编辑 1:这是完整的未经审查的代码:

chart1.ChartAreas[0].AxisY.Minimum = 0;
chart1.ChartAreas[0].AxisY.Maximum = max;
chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.ChartAreas[0].AxisX.Maximum = max;
chart1.ChartAreas[0].AxisX.LabelStyle.Format = "0";
chart1.ChartAreas[0].AxisY.LabelStyle.Format = "0";
chart1.ChartAreas[0].AxisX.Interval = 1;
chart1.ChartAreas[0].AxisY.Interval = 1;

chart1.ChartAreas[0].AxisX.IntervalAutoMode = IntervalAutoMode.FixedCount;
chart1.ChartAreas[0].RecalculateAxesScale();
for (int i = 0; i < points.ToArray().Length; i++)
    dt.Rows.Add(pointsArr[i, 0], pointsArr[i, 1]);
chart1.DataSource = dt;

chart1.Series["תחום הפתרונות האפשריים"].BorderWidth = 0;
float[] OptimalPoint = CalculateOptimalPt(convertEq(z), ListArToAr(points));
if (OptimalPoint[0] == 0)
{
    for (int i = 0; i < 2; i++)
    {
        DataPoint dp = new DataPoint();
        dp.SetValueXY(i, OptimalPoint[1]);
        if (i > 0) dp.Color = Color.Transparent;
        chart1.Series["פיתרון אופטימלי"].Points.Add(dp);
    }
}
else
    chart1.Series["פיתרון אופטימלי"].Points.AddXY(OptimalPoint[0], OptimalPoint[1]);
chart1.Series["פיתרון אופטימלי"].Points[0].MarkerSize = 10;
chart1.Series["תחום הפתרונות האפשריים"].XValueMember = "X_Value";
chart1.Series["תחום הפתרונות האפשריים"].YValueMembers = "Y_Value";
chart1.Series["תחום הפתרונות האפשריים"].ChartType = SeriesChartType.Area;
panel1.Visible = false;
panel2.Visible = true;

You can do it by anchoring the chart only to the Top and Left and calculating and setting the Width and Height yourself when Form size changes.您可以通过将图表仅锚定到TopLeft并在表单大小更改时自己计算和设置WidthHeight

To do so we get fundamental data of the chart in the form constructor.为此,我们在表单构造函数中获取图表的基本数据。

private readonly Size _innerMargin = new Size(183, 55); // Estimated
private readonly Size _outerMargin;
private readonly float _aspectRatio;

public Form1()
{
    InitializeComponent();

    _outerMargin = Size - chart1.Size;
    Size innerSize = chart1.Size - _innerMargin;

    _aspectRatio = (float)innerSize.Width / innerSize.Height;
}

_innerMargin is the estimated total difference between the chart size and the plot area with the gridlines. _innerMargin是图表大小和带有网格线的绘图区域之间的估计总差异。 I actually got it from a screenshot and measured it in a graphics application.我实际上是从屏幕截图中得到的,并在图形应用程序中对其进行了测量。

_outerMargin is the difference of the form size and the chart control size. _outerMargin是表单大小和图表控件大小的差值。

This calculation of the initial _aspectRatio assumes the grid lines build perfect squares when the form opens.初始_aspectRatio这种计算假定当表单打开时网格线构建完美的正方形。 Instead, you could set this aspect ratio from the known number of squares in X and Y:相反,您可以根据 X 和 Y 中已知的正方形数量设置此纵横比:

_aspectRatio = 16f / 16f; // From your example image.

In the Form_Resize event handler, we then set the new size of the chart.Form_Resize事件处理程序中,我们然后设置图表的新大小。 Depending on whether the current aspect ratio (calculated from the theoretical new maximum plot area size) is less than or greater than the original aspect ratio, the height or the width of the chart determines the maximum chart size.根据当前纵横比(根据理论新的最大绘图区域大小计算)是小于还是大于原始纵横比,图表的高度或宽度决定了最大图表尺寸。 The other dimension must be calculated so that the aspect ratio of plot area remains the same.必须计算另一个维度,以便绘图区域的纵横比保持不变。

private void Form1_Resize(object sender, EventArgs e)
{
    Size maxChartSize = Size - _outerMargin;
    Size innerSize = maxChartSize - _innerMargin;

    double currentAspectRatio = (float)innerSize.Width / innerSize.Height;
    if (currentAspectRatio < _aspectRatio) {
        int chartWidth = Width - _outerMargin.Width;
        chart1.Width = chartWidth;
        chart1.Height = (int)((chartWidth - _innerMargin.Width) / _aspectRatio + _innerMargin.Height);
    } else {
        int chartHeight = Height - _outerMargin.Height;
        chart1.Height = chartHeight;
        chart1.Width = (int)((chartHeight - _innerMargin.Height) * _aspectRatio + _innerMargin.Width);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM