如何修改C# Chart控件chartArea百分比

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15423080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:46:35  来源:igfitidea点击:

How to modify C# Chart control chartArea percentages

c#mschart

提问by Xantham

If I have a chart control with 2 chartAreasin it, the chart control by default puts the chartAreas on top of each other makes each area take 50% of the available size of the chart control.

如果我有一个包含 2chartAreas的图表控件,默认情况下,图表控件会将图表区域放在彼此的顶部,使每个区域占据图表控件可用大小的 50%。

Is there a way to change the chartAreas' percent so that I could say, have the top chart take 75% of the area, and the bottom chart take 25%?

有没有办法改变图表区域的百分比,以便我可以说,让顶部图表占据 75% 的区域,底部图表占据 25%?

采纳答案by Xantham

So, I eventually found it, but I do not think that it is very well documented. There each chartAreahas the property ChartArea.Position. This property of the type ElementPosition, and contains 4 properties that are relevant to this problem.

所以,我最终找到了它,但我认为它没有很好的记录。每个chartArea都有属性ChartArea.PositionElementPosition类型的此属性,包含与此问题相关的 4 个属性。

Height: Gets or sets the height of a chart element.
Width: Gets or sets the width of a chart element.
X: Gets or sets the relative X-coordinate of the top-left corner of an applicable chart element.
Y: Gets or sets the relative Y-coordinate of the top-left corner of an applicable chart element.

Height:获取或设置图表元素的高度。
Width:获取或设置图表元素的宽度。
X:获取或设置适用图表元素左上角的相对 X 坐标。
Y:获取或设置适用图表元素左上角的相对 Y 坐标。

When you dig deeper, the Heightand Widthproperties are also stated in relative coordinates, such that you can only input 0 - 100.

当您深入挖掘时,HeightWidth属性也以相对坐标表示,因此您只能输入 0 - 100。

Basically, you have to change each height, and each Y to move shift them. After the initial creation, it will not automatically adjust the other numbers.

基本上,你必须改变每个高度,每个 Y 移动它们。初始创建后,不会自动调整其他数字。

For instance, if I just change the Heightof chartArea[1] to something smaller, it will still be anchored where it was previously, as would make sense, leaving a lot of white space below it.

例如,如果我只是将HeightchartArea[1] 的值更改为较小的值,它仍然会锚定在之前的位置,这是有道理的,在其下方留下大量空白。

If I then increase the Heightof chartArea[0], it may draw over chartArea[1] that we just resized. So then I have to set the Yof chartArea[1] to move it down, so that it is not drawn over, and the white space is gone.

如果我再增加HeightchartArea[0] 的值,它可能会覆盖我们刚刚调整大小的chartArea[1]。那么我必须设置YchartArea[1] 来将它向下移动,这样它就不会被绘制过来,并且空白消失了。

So, to get something similar to what I asked in the question, I set it to:

因此,为了获得类似于我在问题中提出的内容,我将其设置为:

chart1.ChartAreas[0].Position.Y = 10;
chart1.ChartAreas[0].Position.Height = 60;
chart1.ChartAreas[1].Position.Y = 70;
chart1.ChartAreas[1].Position.Height = 20;

To make this explanation a bit more clear, I will refer to the Chart control that these chartAreas are in as "the parent".

为了让这个解释更清楚一点,我将这些图表区域所在的图表控件称为“父级”。

These are percentages, but for this example, let us assume that the parent's size is 100 pixels.

这些是百分比,但对于此示例,让我们假设父级的大小为 100 像素。

This sets the first chartArea start displaying at 10 px, and makes it about 60px tall. It then starts displaying the second chartArea at 70px, and makes it about 20px tall.

If this chart was 200px tall, then the proportions would be the same, but the actually pixels would be double (so setting the first chart area to 60 would make it 120px tall).

这会将第一个图表区域设置为 10 像素开始显示,并使其高约 60 像素。然后它开始在 70 像素处显示第二个图表区域,并使其高约 20 像素。

如果此图表高 200 像素,则比例将相同,但实际像素将翻倍(因此将第一个图表区域设置为 60 将使其高 120 像素)。

I did pad this a bit more in my real program, because this has titles overwriting axis labels, but I felt these numbers helped explain it better.

我在我的实际程序中做了更多的填充,因为它的标题覆盖了轴标签,但我觉得这些数字有助于更好地解释它。

回答by user1750310

You could also set the second chart .Y position to the bottom of the first chart. That way you only need to worry about where the first chart is located. The code looks something like this:

您还可以将第二个图表的 .Y 位置设置为第一个图表的底部。这样你只需要担心第一个图表的位置。代码如下所示:

chart1.ChartAreas[0].Position.Y = 10;
chart1.ChartAreas[0].Position.Height = 60;
chart1.ChartAreas[1].Position.Y = chart1.ChartAreas[0].Position.Bottom;
chart1.ChartAreas[1].Position.Height = 20;

You can also pad the .Y position, of course, depending on how much white space you want between the charts. I used this approach for an application I built that uses multiple charts and it guarantees each chart will be properly located regardless of what is done with an individual chart.

当然,您也可以填充 .Y 位置,这取决于您希望图表之间有多少空白。我将这种方法用于我构建的使用多个图表的应用程序,它保证每个图表都将被正确定位,无论对单个图表做了什么。