using GalaSoft.MvvmLight.Messaging; using IndexFormula.Finance.DataProvider; using IndexFormula.Finance.Win; using Muchinfo.Quote; using Muchinfo.Quote.HistoryClient; using Muchinfo.Quote.SqliteDal; using MuchInfo.Chart.App.Quote; using MuchInfo.Chart.App.ViewModels; using MuchInfo.Chart.Data.EnumTypes; using MuchInfo.Chart.Data.Interfaces; using MuchInfo.Chart.Data.Models; using MuchInfo.Chart.DataAccess; using MuchInfo.Chart.WPF.Primitives; using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Threading; using System.Windows; using System.Windows.Input; using System.Windows.Threading; namespace MuchInfo.Chart.App { /// /// MainWindow.xaml 的交互逻辑 /// public partial class MainWindow : Window { private QuoteComponentProxy _quoteComponentProxy; #region Constructors public MainWindow() { InitializeComponent(); var isfake = ConfigurationManager.AppSettings["IsFakeData"]; if (isfake.ToLower() == "true") { //另外一种更新数据集方法:通过注册周期改变事件而不须要拖动赋值图表的DataSet //this.NewChart.LoadHistoryCycleDataByTimeSpan += new LoadHistoryCycleDataByTimeSpanEventHandler(OnLoadGoodsData); this.NewChart.LoadHistoryCycleDataByCycleType += OnLoadGoodsData; Messenger.Default.Send(string.Empty, "SetCurrentGoods"); tempdataPoints = LoadSVC(); _timerSharingTimer = new DispatcherTimer(); _timerSharingTimer.Interval = new TimeSpan(0, 0, 0, 0,500); _timerSharingTimer.Tick -= _timerSharingTimer_Tick; _timerSharingTimer.Tick += _timerSharingTimer_Tick; _timerSharingTimer.Start(); } else { Messenger.Default.Send(string.Empty, "SetCurrentGoods"); var uri = ConfigurationManager.AppSettings["Quote_Address"]; _quoteComponentProxy = new QuoteComponentProxy(false, uri); _quoteComponentProxy.ResponseHistoryCycle += HistoryClient_ResponseHistoryCycle; this.NewChart.LoadHistoryCycleDataByCycleType += OnLoadHistoryCycleDataByCycleType; } } private void HistoryClient_ResponseHistoryCycle(object sender, HistoryCycleResponseEventArgs e) { var list = e.HisCycle.hisList; var result = (list == null || !list.Any()) ? new List() : list.ToArray().ToDataPointList(); this.Dispatcher.BeginInvoke(new Action(() => { if (this.NewChart.CycleType == CycleType.TimeSharing && e.HisCycle.tSpan == TimeSpan.FromMinutes(1)) { //更新当前商品集合 this.NewChart.UpdateTimeSharingDataPoints(result); } else if (this.NewChart.CycleType == e.HisCycle.cycle.ToCycleType() || (e.HisCycle.cycle == Cycle.Custom && this.NewChart.CurrentTimeFrame == e.HisCycle.tSpan)) { if (this.NewChart.CurrentGoods != null) { if (this.NewChart.CurrentGoods.Symbol == e.HisCycle.Symbol) { //更新当前商品集合 this.NewChart.UpdateDataPoints(result); } else { //更新叠加商品集合 this.NewChart.UpdateComparisonDataPoints(e.HisCycle.Symbol, result); } } } })); } /// /// 测试数据。 /// /// public List TestData(List source) { var outputSource = new List(); Random random = new Random(DateTime.Now.Millisecond); if (source == null) return outputSource; int i = 5; foreach (var lineDataPoint in source) { var bar = lineDataPoint as BarDataPoint; var temdata = random.Next(-1, 1); if (bar != null) { bar.Low += temdata; bar.High += temdata; } outputSource.Add(bar); } return outputSource; } /// /// Called when [load goods data]. /// /// The exchange code. /// The goods code. /// The type. /// The start time. /// The end time. /// The total count. /// ChartDataSet. private List OnLoadHistoryCycleDataByCycleType(int exchangeCode, string goodsCode, CycleType type, TimeSpan timeSpan, DateTime startTime, DateTime endTime, int totalCount) { RetMessageCode messageCode; var newType = type.ToCycle(); var goods = new GoodsInfo(exchangeCode, goodsCode, GoodsType.PreciousMetal, 25, null, 1); HistoryCycle[] list; if (newType == Cycle.Custom) { list = _quoteComponentProxy.GetHistoryCycle(exchangeCode, goodsCode, timeSpan, startTime, endTime, (short)totalCount, out messageCode); } else { list = _quoteComponentProxy.GetHistoryCycle(goods.Symbol, type.ToCycle(), startTime, endTime, (short)totalCount, out messageCode); } return list.ToDataPointList(); } void _timerSharingTimer_Tick(object sender, EventArgs e) { if (_currentIndex > 0) { if (NewChart.CycleType == CycleType.TimeSharing) { var barPoint = tempdataPoints[_currentIndex]; var count = tempdataPoints.Count - _currentIndex; _currentIndex--; barPoint.Date = DateTime.Now.Date.AddMinutes((int)DateTime.Now.TimeOfDay.TotalMinutes); if (barPoint is IBarDataPoint) { //(barPoint as IBarDataPoint).Volume = _random.Next(0, 60); } NewChart.UpdateTimeShareData(barPoint as IBarDataPoint, NewChart.CurrentGoods.GoodsCode); if (NewChart.ComparisonGoodsList != null && NewChart.ComparisonGoodsList.Any()) { var compisonPoint = new BarDataPoint(barPoint.Date, barPoint.Value); compisonPoint.Value += _random.Next(-2, 2); foreach (var goods in NewChart.ComparisonGoodsList) { NewChart.UpdateTimeShareData(compisonPoint as IBarDataPoint, goods.GoodsCode); } } } else { var bar = tempdataPoints.Last(); var randomValue = _random.Next( 95, 105); var lastValue = randomValue*bar.Value/100; var date = DateTime.Now.Date.AddHours(23).AddMinutes(50 + tmpMinutes); var barDataPoint = new BarDataPoint(date, lastValue); var dt = this.DataContext as MainViewModel; dt.TikBarDataPoint = barDataPoint; tmpMinutes++; } } } private int tmpMinutes = 0; private DispatcherTimer _timerSharingTimer; private int _currentIndex = 0; private Random _random = new Random(200); #endregion Constructors #region Methods #region Private Methods /// /// Gets the data set. /// /// Name of the stock. /// The type. /// ChartDataSet. private ChartDataSet GetDataSet(string stockName, CycleType type) { var dataAccess = new FileDataAccess(); //var a = dataAccess.GetDataPoints(stockName, type, DateTime.MinValue, DateTime.Now, 10000); var a = dataAccess.GetDataPoints(stockName, type, DateTime.MinValue, DateTime.Now, 1000); if (a == null) return null; var result = a.Cast().ToList(); var dataSet = new ChartDataSet(result); return dataSet; } /// /// Gets the cycle data. /// /// Name of the stock. /// The time span. /// The start time. /// The end time. /// The total count. /// IList{ILineDataPoint}. private List GetCycleData(string stockName, CycleType cycle, TimeSpan timeSpan, DateTime startTime, DateTime endTime, int totalCount) { //var dataAccess = new FileDataAccess(); //var a = dataAccess.GetDataPoints(stockName, timeSpan, startTime, endTime, totalCount); ////if (a == null) return null; ////var result = a.Cast().ToList(); ////return result; ////return LoadSVC(); // _timerSharingTimer.Start(); if (cycle == CycleType.Minute) //CycleType.TimeSharing) { // _timerSharingTimer.Start(); // return CreateTestPoint(); } else { // _timerSharingTimer.Stop(); return LoadSVC(); // return LoadSVC(timeSpan); } return CreateTestPoint(stockName, endTime, totalCount); //return new List(); } private List LoadSVC() { string fileName = Environment.CurrentDirectory + "\\data\\MSFT.CSV"; var currentDataManager = new YahooCSVDataManager(Path.GetDirectoryName(fileName), Path.GetExtension(fileName)); var provider = currentDataManager.LoadYahooCSV(fileName); var timeSpan = DateTime.Now - DateTime.FromOADate(provider["DATE"][provider.Count - 1]); var dataPoints = new List(); for (int i = 0; i < provider.Count - 1; i++) { var dataPoint = new BarDataPoint( DateTime.FromOADate(provider["DATE"][i]).AddDays(timeSpan.Days), (float)provider["OPEN"][i], (float)provider["HIGH"][i], (float)provider["LOW"][i], (float)provider["CLOSE"][i], (float)provider["VOLUME"][i], 0, 0 ); dataPoints.Add(dataPoint); } dataPoints = dataPoints.Where((item) => item.Date > DateTime.Now.Date.AddDays(-200)).ToList(); return dataPoints; } private List LoadSVC(TimeSpan timeSpan) { string fileName = Environment.CurrentDirectory + "\\data\\MSFT.CSV"; var currentDataManager = new YahooCSVDataManager(Path.GetDirectoryName(fileName), Path.GetExtension(fileName)); var mintues = (int)timeSpan.TotalMinutes; var provider = currentDataManager.LoadYahooCSV(fileName); var dataPoints = new List(); int index = provider.Count - 1; var now = new DateTime(2014, 06, 04, 12, 02, 30, 250); for (int i = 0; i < provider.Count - 1; i += mintues) { index -= mintues; var dataPoint = new BarDataPoint( now.AddMinutes(-index), (float)provider["OPEN"][i], (float)provider["HIGH"][i], (float)provider["LOW"][i], (float)provider["CLOSE"][i], (float)provider["VOLUME"][i], 0, 0 ); dataPoints.Add(dataPoint); } return dataPoints; } private List tempdataPoints; /// /// 创建测试数据 /// /// private List CreateTestPoint(string symbol, DateTime endTime, int totalCount) { var result = new List(); var date = DateTime.Today.AddHours(8); var ramdon = new Random(DateTime.Now.Millisecond); if (tempdataPoints != null) { var index = tempdataPoints.Count - 1; _currentIndex = tempdataPoints.Count - 1; result = tempdataPoints.Where(z => z.Date <= endTime).OrderByDescending(z => z.Date).Take(totalCount).ToList(); } return result; } /// /// Gets the tik data. /// /// Name of the stock. /// The start time. /// The end time. /// The total count. /// IList{ILineDataPoint}. private List GetTikData(string stockName, DateTime startTime, DateTime endTime, int totalCount) { var dataAccess = new FileDataAccess(); var a = dataAccess.GetDataPoints(stockName, new TimeSpan(0, 1, 0), startTime, endTime, totalCount); if (a == null) return null; var result = a.Cast().ToList(); return result; } /// /// Called when [load goods data]. /// /// The exchange code. /// The goods code. /// The time span. /// The start time. /// The end time. /// The total count. /// ChartDataSet. private List OnLoadGoodsData(int exchangeCode, string goodsCode, CycleType cycle, TimeSpan timeSpan, DateTime startTime, DateTime endTime, int totalCount) { var result = GetCycleData(exchangeCode + goodsCode, cycle, timeSpan, startTime, endTime, totalCount); return result; } /// /// Called when [load history tik price data]. /// /// The exchange code. /// The goods code. /// The start time. /// The end time. /// The total count. /// IList{ILineDataPoint}. private List OnLoadHistoryTikPriceData(int exchangeCode, string goodsCode, DateTime startTime, DateTime endTime, int totalCount) { var result = GetTikData(exchangeCode + goodsCode, startTime, endTime, totalCount); return result; } #endregion Private Methods #endregion Methods private void BtnOpen_OnClick(object sender, RoutedEventArgs e) { Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(SetLanguage.languageName); FormulaSourceEditor.Open("", ""); } } }