问题描述
我有一个包含多个图表区域的图表.当我按下按钮时,正在创建一个新的图表区域等.我的问题是,在添加一些图表区域后,我得到以下结果:我希望每个图表区域只在一列中,像这样一个接一个:
I have a chart with multiple chart areas. When I press a button a new chart area is being created etc.My problem is that after adding some chart areas I get the following result :I want to have each chart area in only one column, one after the other like this :
这可能吗?
动态添加图表区域
左侧是添加了 3 个图表区域的图表,右侧是具有 4 个区域的图表.
on the left it is the chart with 3 chart areas added and the right is the chart with 4 areas.
推荐答案
这里是您如何实现您所需要的,也可以参考这里的您的问题:
https://stackoverflow.com/questions/67124754/dynamically-add-chart-areas-in-vertical-alignment
Here is how you can achive what you need, also referring for your question here:
https://stackoverflow.com/questions/67124754/dynamically-add-chart-areas-in-vertical-alignment
使用 Position
属性,请阅读代码中的注释:
Use the Position
property and please read comments inside the code:
private void button1_Click(object sender, EventArgs e)
{
List<ChartArea> Areas = new List<ChartArea>();
int numberOfAreas = 5;
chart1.Legends[0].Enabled = false;
for (int k = 1; k <= numberOfAreas; k++) // YOU WANT 5 and not 4 AREAS - changed k< to k<=
{
var S1 = new Series();
chart1.Series.Add(S1);
S1.Name = k.ToString();
for (int j = 0; j < 100; j += 10) S1.Points.AddXY(j, j / 10);
S1.Color = Color.Transparent;
Areas.Add(new ChartArea(k.ToString()));
chart1.ChartAreas.Add(Areas[k - 1]); // IT IS k-1 and not k - we start counting from 0
S1.ChartArea = k.ToString();
chart1.ChartAreas[k - 1].AlignWithChartArea = chart1.ChartAreas[k - 1].Name;
chart1.ChartAreas[k - 1].AlignmentStyle = AreaAlignmentStyles.Position;
chart1.ChartAreas[k - 1].AlignmentOrientation = AreaAlignmentOrientations.Vertical;
}
// NOW THE IMPORTANT PART
float currentHeight = 0;
foreach (var itm in Areas)
{
itm.Position.Height = 100 / numberOfAreas; // Note: the valus are in percenteges and not absolute pixels
itm.Position.Y = currentHeight;
itm.Position.X = 5;
itm.Position.Width = 95;
currentHeight += 100 / numberOfAreas;
}
}
输出:
这篇关于一列中有多个图表区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!