用主要和次要y轴绘制线图c#

问题描述 投票:7回答:1

我一直在研究用c#绘制图表的方法。我有一个特定的要求,绘制一个图表,其中包含y轴和x轴以及第二个y轴。我已经尝试过使用excel Interop但是没有找到解决方案。我已经开始使用MSChart组件但是没有达到任何数据我的数据与...合作

index lines branches
1      20     5
2      30     8
3      34     6

我想在x轴上绘制索引,在左y轴上绘制线的比例,在右y轴上绘制分支的比例。

我正在使用.net版本2.0和3.5,如果这有帮助

c# mschart excel-interop
1个回答
12
投票

创建系列时,将YAxisType属性设置为AxisType.PrimaryAxisType.Secondary

        var lines = new Series("lines");
        lines.ChartType = SeriesChartType.Line;
        lines.Points.Add(new DataPoint(1, 20));
        lines.Points.Add(new DataPoint(2, 30));
        lines.Points.Add(new DataPoint(3, 34));
        lines.YAxisType = AxisType.Primary;
        chart1.Series.Add(lines);

        var branches = new Series("branches");
        branches.ChartType = SeriesChartType.Line;
        branches.Points.Add(new DataPoint(1, 5));
        branches.Points.Add(new DataPoint(2, 6));
        branches.Points.Add(new DataPoint(3, 8));
        branches.YAxisType = AxisType.Secondary;
        chart1.Series.Add(branches);

这会产生一个这样的图表,听起来就像你追求的那样。下面的示例有点难看,它有主要和次要y值的行等,但您可以通过设置图表控件的属性以您希望的方式清理它。

© www.soinside.com 2019 - 2024. All rights reserved.