2013-09-01 17 views
0

我創建了一個簡單的Microsoft Forms應用程序,該應用程序將使用Microsoft .Net圖表控件庫顯示股票圖表。我可以成功地顯示使用LINQ股票行動,SQL:Microsoft .NET圖表組件打破在股票圖表上做簡單移動平均線

IEnumerable<Quote> quotes = Store.Quotes.Where(q => q.Ticker == "MSFT" && q.Date > DateTime.Parse("7/01/2013")); 
MainChart.Series["SeriesTop"].Points.Clear(); 
MainChart.Series["SeriesTop"].Points.DataBindXY(quotes, "Date", quotes, "Low,High,Open,Close"); 
MainChart.Series["SeriesTop"].LegendText = quotes.First().Ticker; 

但是,如果我添加了一個簡單的移動平均線,我得到一個大紅色的X,而不是圖表,也不例外或其他信息,這將有助於。這是代碼,打破它行:

MainChart.DataManipulator.FinancialFormula(FinancialFormula.MovingAverage, "5", "SeriesTop", "AvgTop"); 

我使用Visual Studio檢查「AvgTop」系列,它看起來確定我的內容,但圖表將不顯示。

感謝, 肯

+0

大紅色的X *是一個例外,它恰好被抓住並處理。使用調試+異常並勾選拋出複選框以查看CLR異常,以便調試器在拋出時停止。調用堆棧應該提供一些見解。 –

+0

我也必須進入工具>選項>調試,並取消選中「啓用只是我的代碼」,但做到這一點後,我能夠看到異常消息:Series'SeriesTop'和系列'AvgTop'必須對齊才能執行操作。該系列目前有不同數量的數據點。 –

+0

榮譽,這是真正的程序員選項。 –

回答

1

我DataManipulator.InsertEmptyPoints(按http://msdn.microsoft.com/en-us/library/dd456677.aspx)和大紅色的X打得四處走了,但在週末得到了空洞填充數據,我不想這樣,我想週末(和其他非交易日)從圖表中消失(請參閱Series.IsXValueIndexed = true)。所以我推出我自己的方法來對準兩個數據系列:

public static void AlignSeries(Series seriesA, Series seriesB) 
    { 
     var aligned = seriesA.Points.GroupJoin(seriesB.Points, a => a.XValue, b => b.XValue, (a, b) => new { a = a, b = b.SingleOrDefault() }).ToArray(); 
     DataPointCollection bCollection = seriesB.Points; 
     bCollection.Clear(); 
     foreach (var pair in aligned) 
     { 
      DataPoint bPoint = new DataPoint(); 
      bPoint.XValue = pair.a.XValue; 
      if (null != pair.b) 
      { 
       bPoint.YValues = pair.b.YValues; 
      } 
      else 
      { 
       bPoint.IsEmpty = true; 
      } 
      bCollection.Add(bPoint); 
     } 
    } 

我當然希望有人用更多的智慧比我可以推薦一個更好的方法或者說我錯過了一個API調用。