2017-08-21 81 views
0

我試圖重疊兩個圖表區域。他們會共享相同的x值,但Y會有不同的值和尺度。兩個圖表區域的未對齊

這裏是我的代碼結果:

Graph picture

正如你所看到的紅色系列是不符合綠色series.I正在尋找這個網站的答案比對,但無法找到一個工作。有人能解釋我爲什麼不對齊嗎?

代碼:

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 
     } 
    } 
} 

回答

0

一個圖表有AxisX.IsMarginVisible設置爲true,其他爲假,因此不匹配。

但是,如果您試圖繪製重疊系列,爲什麼不將它們添加到同一個圖表區域,而不是經歷所有這些麻煩?

+0

因爲它們的大小不同,所以s1的值從零到幾千,s2從零到一。當我將它們繪製在一起時,s2就變成了一條直線。哪個不好。 我發現迄今爲止唯一的方法是manualny設置第二個ChartArea的比例尺: 'area.Position = new ElementPosition(0,0,100,100);' 'area2.Position = new ElementPosition(4.2f, 0,87.35f,100);' 但我確定有更優雅的解決方案,因爲main只能在兩種情況下工作。 – MrJW

+0

https://stackoverflow.com/questions/15805121/c-sharp-winforms-creating-a-chart-with-multiple-y-axis-3-or-more – zeFrenchy

+0

無論如何,註釋掉''area2.AxisX .IsMarginVisible = false;''或添加''area1.AxisX.IsMarginVisible = true;''應該可以解決您的問題。 – zeFrenchy