Ejemplo n.º 1
0
        private void InitializeControls()
        {
            ucTitlebar.OnSymbolChanged += (o, e) =>
            {
                GetSymbolData(e.Value, new Action <string, SymbolData>((sym, data) =>
                {
                    if (data != null && data.Any())
                    {
                        _quoteData = data;
                        Symbol     = e.Value.ToUpper();
                        UpdateTitleBar();
                        UpdateChart();
                        SetupAnnotations(ucToolbar.IsShowNews);
                    }
                }));
            };

            ucToolbar.OnComparisonChanged += (o, e) =>
            {
                if (_comparisonSymbols == null)
                {
                    _comparisonSymbols = new ComparisonSymbols(financialChart1);
                }

                if (e.Value.ToUpper() == Symbol.ToUpper())
                {
                    return;
                }

                GetSymbolData(e.Value, (sym, data) =>
                {
                    if (data != null)
                    {
                        var s = new Symbol(e.Value.ToUpper())
                        {
                            DataSource = data
                        };
                        if (_comparisonSymbols.Add(s, _comparisonBinding, ucToolbar.IsShowMovingAverage))
                        {
                            ChangeMode(true);

                            ucToolbar.AddSeriesController(s, () =>
                            {
                                if (s.Visibility == SeriesVisibility.Visible)
                                {
                                    s.MovingAverage.Visibility = ucToolbar.IsShowMovingAverage ? SeriesVisibility.Visible : SeriesVisibility.Hidden;
                                }
                                var checkedSymbol = from p in _comparisonSymbols where p.Value.Visibility == SeriesVisibility.Visible select p;
                                if (checkedSymbol == null || !checkedSymbol.Any())
                                {
                                    ChangeMode(false);
                                }
                                else
                                {
                                    ChangeMode(true);
                                }
                                UpdateYRange();
                            });

                            if (_comparisonSymbols.Keys.Count > 4)
                            {
                                var key   = ucToolbar.GetFirstSeriesCOntroller();
                                var value = _comparisonSymbols[key];
                                ucToolbar.RemoveSeriesController(value);
                                _comparisonSymbols.Remove(value);
                            }
                            UpdateDataRange();
                        }
                    }
                });
            };

            ucToolbar.OnSelectedIndexChanged += (o, e) =>
            {
                if (rs == null)
                {
                    return;
                }

                DateTime lowValue = DateTime.Now;
                switch (e.Value)
                {
                case 0:
                    lowValue = DateTime.Now.AddMonths(-1);
                    break;

                case 1:
                    lowValue = DateTime.Now.AddMonths(-3);
                    break;

                case 2:
                    lowValue = DateTime.Now.AddMonths(-6);
                    break;

                case 3:
                    lowValue = new DateTime(DateTime.Now.Year, 1, 1);
                    break;

                case 4:
                    lowValue = DateTime.Now.AddYears(-1);
                    break;

                case 5:
                    lowValue = DateTime.Now.AddYears(-3);
                    break;

                case 6:
                    lowValue = DateTime.Now.AddYears(-6);
                    break;

                case 7:
                    lowValue = _quoteData.Min(p => p.Date);
                    break;

                default:
                    break;
                }
                rs.UpperValue = DateTime.Now.ToOADate();
                rs.LowerValue = lowValue.ToOADate();

                ucToolbar.RangeText = FormatRange(lowValue, DateTime.Today);
            };

            ucToolbar.OnReseted += (o, e) =>
            {
                if (_comparisonSymbols != null && _comparisonSymbols.Any())
                {
                    _comparisonSymbols.Clear();


                    ChangeMode(false);

                    UpdateYRange();
                    ucToolbar.RemoveAllSeriesController();
                    var removeItems = (from p in financialChart1.Series where p != fs && p != ma && p != vs select p).ToList();

                    for (int i = 0; i < removeItems.Count; i++)
                    {
                        financialChart1.Series.Remove(removeItems[i]);
                    }
                }
            };

            ucToolbar.OnShowVolumeChanged += (o, e) =>
            {
                if (vs != null)
                {
                    vs.Visibility = e.Value ? SeriesVisibility.Visible : SeriesVisibility.Hidden;
                }
            };

            ucToolbar.OnChartTypeChanged += (o, e) =>
            {
                if (fs == null)
                {
                    return;
                }

                financialChart1.BeginUpdate();
                switch (e.Type)
                {
                case ChartType.Line:
                case ChartType.Area:
                    fs.Style.FillColor = Color.FromArgb(64, _baseSeriesColor);
                    fs.Binding         = Binding;
                    break;

                case ChartType.Candlestick:
                    fs.Style.FillColor = Color.FromArgb(255, _baseSeriesColor);
                    fs.Binding         = "High,Low,Open,Close";
                    break;

                case ChartType.HighLowOpenClose:
                default:
                    fs.Style.FillColor = Color.FromArgb(64, _baseSeriesColor);
                    fs.Binding         = "High,Low,Open,Close";
                    break;
                }
                fs.ChartType = e.Type;
                financialChart1.EndUpdate();
            };

            ucToolbar.OnSaveImage += (o, e) =>
            {
                string filter = string.Empty;
                var    format = ImageFormat.Png;
                switch (e.Value)
                {
                case "PNG":
                    filter = "PNG|*.png";
                    format = C1.Win.Chart.ImageFormat.Png;
                    break;

                case "JPG":
                    filter = "JPG|*.jpg";
                    format = C1.Win.Chart.ImageFormat.Jpg;
                    break;

                case "SVG":
                    filter = "SVG|*.svg";
                    format = C1.Win.Chart.ImageFormat.Svg;
                    break;

                default:
                    filter = "PNG|*.png";
                    format = C1.Win.Chart.ImageFormat.Png;
                    break;
                }

                using (var dialog = new SaveFileDialog())
                {
                    dialog.Filter = filter;
                    if (dialog.ShowDialog() == DialogResult.OK)
                    {
                        string path = dialog.FileName;
                        using (System.IO.FileStream fs = new System.IO.FileStream(dialog.FileName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
                        {
                            financialChart1.SaveImage(fs, format, financialChart1.Width, financialChart1.Height);
                        }
                    }
                }
            };

            ucToolbar.OnMovingAverageVisibilityChanged += (o, e) =>
            {
                if (ma != null)
                {
                    ma.Visibility = e.Value ? SeriesVisibility.Visible : SeriesVisibility.Hidden;
                    if (_comparisonSymbols != null)
                    {
                        foreach (var kv in _comparisonSymbols)
                        {
                            kv.Value.MovingAverage.Visibility = ma.Visibility;
                        }
                    }
                }
            };

            ucToolbar.OnMovingAverageTypeChanged += (o, e) =>
            {
                if (ma != null)
                {
                    ma.Type = e.Type;
                    if (_comparisonSymbols != null)
                    {
                        _comparisonSymbols.Type = e.Type;
                    }
                }
            };

            ucToolbar.OnMovingAveragePeriodChanged += (o, e) =>
            {
                if (ma != null)
                {
                    ma.Period = e.Value;
                    if (_comparisonSymbols != null)
                    {
                        _comparisonSymbols.Period = e.Value;
                    }
                }
            };

            ucToolbar.OnLineMarkerVisibilityChanged += (o, e) => marker.Visible = e.Value;
            ucToolbar.OnNewsVisibilityChanged       += (o, e) => SetupAnnotations(e.Value);
        }