2016-11-10 116 views
0

我要生成酒吧,如在linkenter image description here範圍欄

不過,我得到這個錯誤:

enter image description here

這裏是我使用的代碼:

 // AxisY 
     chart1.ChartAreas.Add(CA); 
     chart1.ChartAreas[1].AxisY.MajorGrid.Enabled = false; 
     chart1.ChartAreas[1].AxisY.Interval = 1; 

     // AxisX 
     chart1.ChartAreas[1].AxisX.ScrollBar.Enabled = true; 
     chart1.ChartAreas[1].AxisX.ScaleView.Zoomable = true; 
     chart1.ChartAreas[1].AxisX.MajorGrid.Enabled = false; 
     chart1.ChartAreas[1].AxisX.LabelStyle.Format = "yyyy-MM-dd hh:mm:ss"; 

     chart1.ChartAreas[1].AxisX.Interval = 0; 
     chart1.ChartAreas[1].AxisX.IntervalType = DateTimeIntervalType.Years; 

     minDate = new DateTime(2016, 01, 01, 00, 00, 00, 000); 
     maxDate = new DateTime(2016, 12, 01, 00, 00, 00, 000); // or DateTime.Now; 

     chart1.ChartAreas[1].Axes[0].Enabled = AxisEnabled.False; 
     chart1.ChartAreas[1].Axes[1].Enabled = AxisEnabled.False; 

     chart1.ChartAreas[1].BackColor = Color.Transparent; 
     chart1.ChartAreas[1].Position.Height = 100; 
     chart1.ChartAreas[1].Position.Width = 100; 
     chart1.ChartAreas[1].InnerPlotPosition.Height = 90; 
     chart1.ChartAreas[1].InnerPlotPosition.Width = 80; 
     chart1.ChartAreas[1].InnerPlotPosition.X = 10; 

     var series2 = new Series 
     { 
      Name = "S2", 
      Color = Color.Black, 
      ChartType = SeriesChartType.RangeBar, 
      YValueType = ChartValueType.Auto, 
      XValueType = ChartValueType.Auto 
     }; 

     var values2 = new DateTime[3]; 
     values2[0] = minDate.AddMonths(2); 
     values2[1] = minDate.AddMonths(4); 
     values2[2] = minDate.AddMonths(6); 

     series2.Points.AddXY(1, values2[1], values2[2]); 

     series2["PointWidth"] = ".25"; 

     chart1.Series.Add(series2); 

底部軸是日期時間。左邊的軸是固定的,並且有文字作爲標籤。我需要顯示數據的差距。

+0

這是不相關的問題。 – TaW

回答

1

是的,這看起來很奇怪乍一看,錯誤信息並沒有指出你在正確的方向。

原因是,您嘗試爲系列添加點,然後在之前將系列添加到圖表。

RangeBar不能與除條形圖表之外的任何其他圖表類型組合。

因此(?)圖表無法檢查點是否實際上以有效的方式添加,而不是說所以它聲稱您的系列只接受一個y值。

解決方案:只要添加任何DataPoints前添加series2Chart,一切都很好..

+0

如果您對答案感到滿意,請考慮考慮[接受](http://stackoverflow.com/help/accepted-answer)它..! - 我發現你從來沒有這樣做過:在答案的選票下面,點擊左上角的(不可見)複選標記,然後單擊它!它變成綠色,並獲得我們兩個小的聲譽.. – TaW

+0

當然,它會發生:D –