两个图表区域的错位

时间:2017-08-21 07:43:59

标签: c# asp.net visual-studio graph

我试图重叠两个图表区域。它们将共享相同的x值,但Y将具有不同的值和比例。

以下是我的代码的结果:

Graph picture

正如你所看到的,红色系列与绿色系列不一致。我正在搜索这个网站寻找答案,但找不到一个有用的。有人可以解释我为什么不对齐吗?

代码:

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace TestGraph
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            #region Data

            // Creating first series
            Series s1 = new Series();
            s1.Name = "Values";
            s1.ChartType = SeriesChartType.Column;
            s1.XValueType = ChartValueType.DateTime;
            s1.Color = Color.Green;
            s1.BorderWidth = 2;

            // Hard Coding test values
            DataPoint[] values =
            {
                new DataPoint(new DateTime(2017, 8, 1).ToOADate(), 10),
                new DataPoint(new DateTime(2017, 8, 2).ToOADate(), 11),
                new DataPoint(new DateTime(2017, 8, 3).ToOADate(), 12),
                new DataPoint(new DateTime(2017, 8, 4).ToOADate(), 13),
            };

            // Adding vales to s1
            foreach (DataPoint p in values)
            {
                s1.Points.Add(p);
            }

            // Creating second series
            Series s2 = new Series();
            s2.Name = "Values 2";
            s2.ChartType = SeriesChartType.Column;
            s2.XValueType = ChartValueType.DateTime;
            s2.Color = Color.Red;
            s2.BorderWidth = 2;

            // Hard Coding test values
            DataPoint[] values2 =
            {
                new DataPoint(new DateTime(2017, 8, 1).ToOADate(), 0.1),
                new DataPoint(new DateTime(2017, 8, 2).ToOADate(), -0.2),
                new DataPoint(new DateTime(2017, 8, 3).ToOADate(), -0.7),
                new DataPoint(new DateTime(2017, 8, 4).ToOADate(), 13),
            };

            // Adding vales to s2
            foreach (DataPoint p in values2)
            {
                s2.Points.Add(p);
            }

            #endregion
            #region Charts

            // Initializing chart
            Chart mainChart = new Chart();
            ChartArea area = new ChartArea();
            ChartArea area2 = new ChartArea();

            Controls.Add(mainChart);
            mainChart.Dock = DockStyle.Fill;

            // Adding areas to mainChart
            mainChart.ChartAreas.Add(area);
            mainChart.ChartAreas.Add(area2);

            // Adding series to areas
            s1.ChartArea = area.Name;
            s2.ChartArea = area2.Name;

            mainChart.Series.Add(s1);
            mainChart.Series.Add(s2);

            // Aligning areas
            // Overlapping area2 with area
            area2.AlignmentStyle = AreaAlignmentStyles.All;
            area2.AlignmentOrientation = AreaAlignmentOrientations.All;
            area2.AlignWithChartArea = area.Name;

            // Scale actualization
            area2.RecalculateAxesScale();
            area.RecalculateAxesScale();

            // Defining Y scale
            area2.AxisY.Maximum = 2;
            area2.AxisY.Minimum = -2;

            area2.BackColor = Color.Transparent;

            // Disabling unnecessary graphics
            area2.BackGradientStyle = GradientStyle.None;
            area2.AxisX.IsMarginVisible = false;

            area2.AxisX.LabelStyle.Enabled = false;
            area2.AxisY.LabelStyle.Enabled = false;

            area2.AxisX.Enabled = AxisEnabled.False;
            area2.AxisY.Enabled = AxisEnabled.False;

            // Resizing chart back to 100%
            area.Position = new ElementPosition(0, 0, 100, 100);

            #endregion
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的某个图表AxisX.IsMarginVisible设置为true,另一个设置为false,因此不匹配。

但是,如果您试图绘制重叠系列,为什么不将它们添加到同一图表区域而不是经历所有这些麻烦?