我需要你的建議。我想在我的圖表中添加許多 ChartAreas 并將它們垂直對齊在一列中,該列將是其父級的 100% 寬度,并且在添加更多聊天區域時將變得可滾動。現在,我使用此代碼添加更多圖表區域:
private void AddChartArea(int index)
{
string chartAreaName = index.ToString();
chart.ChartAreas.Add(chartAreaName);
chart.Series.Add(new Series());
chart.Series[index].ChartType = SeriesChartType.Line;
chart.Series[index].MarkerStyle = MarkerStyle.Diamond;
chart.Series[index].ChartArea = chartAreaName;
/* Trying to align chart areas vertically */
chart.ChartAreas[index].AlignWithChartArea = chart.ChartAreas[index - 1].Name;
chart.ChartAreas[index].AlignmentStyle = AreaAlignmentStyles.AxesView;
chart.ChartAreas[index].AlignmentOrientation = AreaAlignmentOrientations.Vertical;
}
但是當圖表區域數量 > 3 時,我的圖表區域仍然會這樣:

雖然我希望它是這樣的,但右側有一個垂直滾動條:

uj5u.com熱心網友回復:
因此,正如 TaW 所指出的,我的問題與此類似。但是我做了一些改進來自動調整整體圖表高度。
所以我的圖表被放置到面板中,它的 AutoScroll 為真。然后,每次我創建一個新的 ChartArea 時都會呼叫這個方法:
private void DrawNewChartArea(int index)
{
int chartAreaMinHeight = 200;
chart.Dock = DockStyle.Top;
float width = 99;
float height = 100 / chart.ChartAreas.Count;
float x = 0;
float y = height * index;
if (chartAreaMinHeight * (index 1) > chart.Height)
{
chart.Height = chartAreaMinHeight * (index 1);
// realign height of all the chart areas if we are changing chart height
for (int i = 0; i < chart.ChartAreas.Count; i )
{
float caY = height * i;
chart.ChartAreas[i].Position.Height = height;
chart.ChartAreas[i].Position.Y = caY;
}
}
chart.ChartAreas[index].Position = new ElementPosition(x, y, width, height);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/377598.html
