Пример #1
0
        public void InitializeChart(string chartAreaName, string seriesName, IQuoteBasic quote)
        {
            if (quote == null || quote.Count <= 1)
            {
                return;
            }

            var series = ChartSeries.FindByName(seriesName);

            if (series == null)
            {
                return;
            }

            var seriesInfo = _dicChartAreaToSeriesInfo[chartAreaName].Find(s => s.SeriesName == seriesName);

            if (seriesInfo == null)
            {
                return;
            }

            var max_index = quote.Count - 1;
            var min_index = Math.Max(0, max_index - _maxNumbars);
            var times     = quote.Time.GetRange(min_index, max_index - min_index + 1).Select(t => t.GetUTCFromUnixTime().ToOADate()).ToList();
            var high      = quote.High.GetRange(min_index, max_index - min_index + 1).ToList();
            var low       = quote.Low.GetRange(min_index, max_index - min_index + 1).ToList();
            var open      = quote.Open.GetRange(min_index, max_index - min_index + 1).ToList();
            var close     = quote.Close.GetRange(min_index, max_index - min_index + 1).ToList();

            _chart.InvokeIfRequired(c => series.Points.Clear());
            _chart.InvokeIfRequired(c => series.Points.DataBindXY(times, high, low, open, close));

            seriesInfo.MinTimeData = DateTime.FromOADate(times.FirstOrDefault()).GetUnixTimeFromUTC();
            seriesInfo.MaxTimeData = DateTime.FromOADate(times.LastOrDefault()).GetUnixTimeFromUTC();

            this.SetAxisRangeForChartArea(chartAreaName, series, seriesInfo);
        }
Пример #2
0
        public void UpdateChart(string chartAreaName, string seriesName, IQuoteBasic quote)
        {
            var series = ChartSeries.FindByName(seriesName);

            if (series == null)
            {
                return;
            }

            var seriesInfo = _dicChartAreaToSeriesInfo[chartAreaName].Find(s => s.SeriesName == seriesName);

            if (seriesInfo == null)
            {
                return;
            }

            var sindex = quote.Count - 1;

            while (sindex >= 0 && quote.Time[sindex] > seriesInfo.MaxTimeData)
            {
                --sindex;                                                                //index must be -1 or quote.Time[index] <= maxTimeData
            }
            ++sindex;

            var num = quote.Count - sindex;

            if (num <= 0)
            {
                return;
            }

            if (num > 1)
            {
                var points = series.Points.ToList();
                var lst    = Enumerable.Range(sindex, num).Select(i => new DataPoint(quote.Time[i].GetUTCFromUnixTime().ToOADate(), new List <double> {
                    quote.High[i], quote.Low[i], quote.Open[i], quote.Close[i]
                }.ToArray()));
                points.AddRange(lst);
                _chart.InvokeIfRequired(c => series.Points.DataBindXY(
                                            points.Select(l => l.XValue).ToList(),
                                            points.Select(l => l.YValues[0]).ToList(),
                                            points.Select(l => l.YValues[1]).ToList(),
                                            points.Select(l => l.YValues[2]).ToList(),
                                            points.Select(l => l.YValues[3]).ToList()));
            }
            else //num == 1
            {
                _chart.InvokeIfRequired(c => series.Points.AddXY(quote.LastTime.GetUTCFromUnixTime().ToOADate(), quote.High.Last(), quote.Low.Last(), quote.Open.Last(), quote.Close.Last()));
            }

            //remove first elements if gets too much data
            if (series.Points.Count >= _maxNumbars * 10)
            {
                var points = series.Points.ToList();
                points.RemoveRange(0, 9 * _maxNumbars);
                _chart.InvokeIfRequired(c => series.Points.DataBindXY(
                                            points.Select(l => l.XValue).ToList(),
                                            points.Select(l => l.YValues[0]).ToList(),
                                            points.Select(l => l.YValues[1]).ToList(),
                                            points.Select(l => l.YValues[2]).ToList(),
                                            points.Select(l => l.YValues[3]).ToList()));
            }

            //add the new data to series
            seriesInfo.MinTimeData = DateTime.FromOADate(series.Points.FirstOrDefault().XValue).GetUnixTimeFromUTC();
            seriesInfo.MaxTimeData = DateTime.FromOADate(series.Points.LastOrDefault().XValue).GetUnixTimeFromUTC();

            this.SetAxisRangeForChartArea(chartAreaName, series, seriesInfo);
        }