【问题标题】:Dynamically add ChartAreas to Chart in a column and make Chart auto-scrollable in winforms将 ChartAreas 动态添加到 Chart 的列中,并使 Chart 在 winforms 中自动滚动
【发布时间】:2022-01-05 20:45:00
【问题描述】:

我需要你的建议。我想在我的图表中添加许多 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 时,我的图表区域仍然是这样的:

虽然我希望它是这样的,但右侧有一个垂直滚动条:

【问题讨论】:

  • 丽丝so ?

标签: c# .net winforms charts


【解决方案1】:

所以,正如 TaW 所指出的,我的问题类似于this one。但是我做了一些改进,可以自动调整图表的整体高度。

所以我的图表被放入了具有 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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 2020-02-22
    • 1970-01-01
    • 2016-02-09
    • 2011-04-13
    相关资源
    最近更新 更多