Exemplo n.º 1
0
        private void RenderChart(StringBuilder sb, string title, int width, int height,
                                 List <List <FastSeriesPoint> > series, List <string> seriesNames)
        {
            var seriesColors = new [] { Color.DarkBlue, Color.DarkOliveGreen, Color.Firebrick };

            var needYearsInFormat = stat.listEquity[stat.listEquity.Count - 1].time.Year != stat.listEquity[0].time.Year;
            var formatString      = needYearsInFormat ? "dd.MM.yyyy" : "dd MMM";

            var chart = new FastChart.Chart.FastChart
            {
                ShowLegend      = series.Count > 1,
                LegendPlacement = FastChart.Chart.FastChart.ChartLegendPlacement.Справа,
                BorderStyle     = BorderStyle.None,
                DrawMargin      = false
            };

            var axisX = new FastAxis(FastAxisDirection.X, true)
            {
                ColorMainGrid = Color.FromArgb(205, 205, 205),
                ColorSubGrid  = Color.FromArgb(220, 220, 220),
                DrawMainGrid  = true,
                DrawSubGrid   = true,
                LabelFormat   = formatString
            };

            // границы оси Y
            var min = double.MaxValue;
            var max = double.MinValue;

            foreach (var ser in series)
            {
                foreach (var pt in ser)
                {
                    if (min > pt.y)
                    {
                        min = pt.y;
                    }
                    if (max < pt.y)
                    {
                        max = pt.y;
                    }
                }
            }
            min = GetLowerBound(min);

            var axisY = new FastAxis(FastAxisDirection.Y, false)
            {
                ColorMainGrid = Color.FromArgb(205, 205, 205),
                ColorSubGrid  = Color.FromArgb(220, 220, 220),
                DrawMainGrid  = true,
                DrawSubGrid   = true,
                AutoScale100  = false,
                MinValue      = new FastChart.Chart.Cortege2 <double, DateTime>(min, default(DateTime)),
                MaxValue      = new FastChart.Chart.Cortege2 <double, DateTime>(max * 1.05, default(DateTime))
            };


            chart.Axes.Add(axisX);
            chart.Axes.Add(axisY);

            // добавить серии и точки
            for (var i = 0; i < series.Count; i++)
            {
                var ser = new FastSeries(seriesNames[i], FastSeriesType.Линия, axisX, axisY, false)
                {
                    PenLine = new Pen(seriesColors[i], 2f), AntiAlias = true
                };
                ser.points.AddRange(series[i]);
                chart.series.Add(ser);
            }

            chart.Width  = width;
            chart.Height = height;

            // рендерить чарт в картинку
            var img = new Bitmap(width, height);

            chart.DrawToBitmap(img, new Rectangle(0, 0, width - 1, height - 1));

            // включить картинку в HTML
            var str    = BaseWebServer.MakeEmbeddedPictureString(img);
            var imgStr = string.Format(
                "<img src=\"{0}\" alt=\"Доходность по счету\" style=\"cursor:pointer\" /> ", str);

            sb.AppendLine("<br/>");
            sb.AppendLine(imgStr);
            sb.AppendLine(string.Format("    <p style=\"font-weight:bold\">{0}</p>", title));
        }
Exemplo n.º 2
0
        private void RenderChart(StringBuilder sb, string title, int width, int height, 
            List<List<FastSeriesPoint>> series, List<string> seriesNames)
        {
            var seriesColors = new [] {Color.DarkBlue, Color.DarkOliveGreen, Color.Firebrick};

            var needYearsInFormat = stat.listEquity[stat.listEquity.Count - 1].time.Year != stat.listEquity[0].time.Year;
            var formatString = needYearsInFormat ? "dd.MM.yyyy" : "dd MMM";

            var chart = new FastChart.Chart.FastChart
            {
                ShowLegend = series.Count > 1,
                LegendPlacement = FastChart.Chart.FastChart.ChartLegendPlacement.Справа,
                BorderStyle = BorderStyle.None,
                DrawMargin = false
            };

            var axisX = new FastAxis(FastAxisDirection.X, true)
            {
                ColorMainGrid = Color.FromArgb(205, 205, 205),
                ColorSubGrid = Color.FromArgb(220, 220, 220),
                DrawMainGrid = true,
                DrawSubGrid = true,
                LabelFormat = formatString
            };

            // границы оси Y
            var min = double.MaxValue;
            var max = double.MinValue;
            foreach (var ser in series)
            {
                foreach (var pt in ser)
                {
                    if (min > pt.y) min = pt.y;
                    if (max < pt.y) max = pt.y;
                }
            }
            min = GetLowerBound(min);

            var axisY = new FastAxis(FastAxisDirection.Y, false)
            {
                ColorMainGrid = Color.FromArgb(205, 205, 205),
                ColorSubGrid = Color.FromArgb(220, 220, 220),
                DrawMainGrid = true,
                DrawSubGrid = true,
                AutoScale100 = false,
                MinValue = new FastChart.Chart.Cortege2<double, DateTime>(min, default(DateTime)),
                MaxValue = new FastChart.Chart.Cortege2<double, DateTime>(max * 1.05, default(DateTime))
            };

            chart.Axes.Add(axisX);
            chart.Axes.Add(axisY);

            // добавить серии и точки
            for (var i = 0; i < series.Count; i++)
            {
                var ser = new FastSeries(seriesNames[i], FastSeriesType.Линия, axisX, axisY, false)
                              {
                                  PenLine = new Pen(seriesColors[i], 2f), AntiAlias = true
                              };
                ser.points.AddRange(series[i]);
                chart.series.Add(ser);
            }

            chart.Width = width;
            chart.Height = height;

            // рендерить чарт в картинку
            var img = new Bitmap(width, height);
            chart.DrawToBitmap(img, new Rectangle(0, 0, width - 1, height - 1));

            // включить картинку в HTML
            var str = BaseWebServer.MakeEmbeddedPictureString(img);
            var imgStr = string.Format(
                "<img src=\"{0}\" alt=\"Доходность по счету\" style=\"cursor:pointer\" /> ", str);

            sb.AppendLine("<br/>");
            sb.AppendLine(imgStr);
            sb.AppendLine(string.Format("    <p style=\"font-weight:bold\">{0}</p>", title));
        }
        /// <summary>
        /// вернуть полный путь к файлу с картинкой доходности
        /// </summary>
        private string MakeProfitChart(string tempFolder, Size chartSize)
        {
            var chart = new FastChart.Chart.FastChart
            {
                ShowLegend      = true,
                LegendPlacement = FastChart.Chart.FastChart.ChartLegendPlacement.Справа,
                BorderStyle     = BorderStyle.None,
                DrawMargin      = false
            };
            var axisX = new FastAxis(FastAxisDirection.X, true)
            {
                ColorMainGrid = Color.FromArgb(205, 205, 205),
                ColorSubGrid  = Color.FromArgb(220, 220, 220),
                DrawMainGrid  = true,
                DrawSubGrid   = true,
                Step          = new FastChart.Chart.Cortege2 <double, TimeSpan>(0,
                                                                                new TimeSpan(356, 0, 0, 0)),
                LabelFormat = "yyyy"
            };
            var maxProfit = (double)profitDic.Max(p => p.equity);
            var axisY     = new FastAxis(FastAxisDirection.Y, false)
            {
                ColorMainGrid = Color.FromArgb(205, 205, 205),
                ColorSubGrid  = Color.FromArgb(220, 220, 220),
                DrawMainGrid  = true,
                DrawSubGrid   = true,
                AutoScale100  = false,
                MinValue      = new FastChart.Chart.Cortege2 <double, DateTime>(0, default(DateTime)),
                MaxValue      = new FastChart.Chart.Cortege2 <double, DateTime>(maxProfit * 1.1,
                                                                                default(DateTime))
            };

            if (maxProfit > 1000)
            {
                var stepSize = maxProfit < 2000 ? 200 : maxProfit < 4000 ? 500 : 1000;
                axisY.Step = new FastChart.Chart.Cortege2 <double, TimeSpan>(stepSize, default(TimeSpan));
            }

            chart.Axes.Add(axisX);
            chart.Axes.Add(axisY);
            var seriesFund = new FastSeries("УК", FastSeriesType.Линия, axisX, axisY, false)
            {
                PenLine = new Pen(Color.FromArgb(80, 5, 5), 2f), AntiAlias = true
            };

            seriesFund.points.AddRange(profitDic.Select(p => new FastSeriesPoint(p.time, p.equity)).ToList());
            chart.series.Add(seriesFund);
            // серии бенчмарков
            var seriesBmA = new FastSeries(benchmarkA, FastSeriesType.Линия, axisX, axisY, false)
            {
                PenLine = new Pen(Color.FromArgb(5, 80, 5), 2f), AntiAlias = true
            };
            var seriesBmB = new FastSeries(benchmarkB, FastSeriesType.Линия, axisX, axisY, false)
            {
                PenLine = new Pen(Color.FromArgb(5, 5, 80), 2f), AntiAlias = true
            };

            seriesBmA.points.AddRange(MakeBenchmarkProfit1000Points(vamiBenchmarkA));
            seriesBmB.points.AddRange(MakeBenchmarkProfit1000Points(vamiBenchmarkB));
            chart.series.Add(seriesBmA);
            chart.series.Add(seriesBmB);

            chart.Width  = chartSize.Width;
            chart.Height = chartSize.Height;

            var img = new Bitmap(chartSize.Width, chartSize.Height);

            chart.DrawToBitmap(img, new Rectangle(0, 0, chartSize.Width, chartSize.Height));
            var path = string.Format("{0}\\chartProfit1000_{1}.png", tempFolder, DateTime.Now.Millisecond);

            img.Save(path, ImageFormat.Png);
            return(path);
        }
        /// <summary>
        /// вернуть полный путь к файлу с картинкой доходности
        /// </summary>
        private string MakeProfitChart(string tempFolder, Size chartSize)
        {
            var chart = new FastChart.Chart.FastChart
                            {
                                ShowLegend = true,
                                LegendPlacement = FastChart.Chart.FastChart.ChartLegendPlacement.Справа,
                                BorderStyle = BorderStyle.None,
                                DrawMargin = false
                            };
            var axisX = new FastAxis(FastAxisDirection.X, true)
                            {
                                ColorMainGrid = Color.FromArgb(205, 205, 205),
                                ColorSubGrid = Color.FromArgb(220, 220, 220),
                                DrawMainGrid = true,
                                DrawSubGrid = true,
                                Step = new FastChart.Chart.Cortege2<double, TimeSpan>(0,
                                                                                      new TimeSpan(356, 0, 0, 0)),
                                LabelFormat = "yyyy"
                            };
            var maxProfit = (double) profitDic.Max(p => p.equity);
            var axisY = new FastAxis(FastAxisDirection.Y, false)
                            {
                                ColorMainGrid = Color.FromArgb(205, 205, 205),
                                ColorSubGrid = Color.FromArgb(220, 220, 220),
                                DrawMainGrid = true,
                                DrawSubGrid = true,
                                AutoScale100 = false,
                                MinValue = new FastChart.Chart.Cortege2<double, DateTime>(0, default(DateTime)),
                                MaxValue = new FastChart.Chart.Cortege2<double, DateTime>(maxProfit * 1.1,
                                    default(DateTime))
                            };
            if (maxProfit > 1000)
            {
                var stepSize = maxProfit < 2000 ? 200 : maxProfit < 4000 ? 500 : 1000;
                axisY.Step = new FastChart.Chart.Cortege2<double, TimeSpan>(stepSize, default(TimeSpan));
            }

            chart.Axes.Add(axisX);
            chart.Axes.Add(axisY);
            var seriesFund = new FastSeries("УК", FastSeriesType.Линия, axisX, axisY, false)
                             {PenLine = new Pen(Color.FromArgb(80, 5, 5), 2f), AntiAlias = true };
            seriesFund.points.AddRange(profitDic.Select(p => new FastSeriesPoint(p.time, p.equity)).ToList());
            chart.series.Add(seriesFund);
            // серии бенчмарков
            var seriesBmA = new FastSeries(benchmarkA, FastSeriesType.Линия, axisX, axisY, false) { PenLine = new Pen(Color.FromArgb(5, 80, 5), 2f), AntiAlias = true };
            var seriesBmB = new FastSeries(benchmarkB, FastSeriesType.Линия, axisX, axisY, false) { PenLine = new Pen(Color.FromArgb(5, 5, 80), 2f), AntiAlias = true };
            seriesBmA.points.AddRange(MakeBenchmarkProfit1000Points(vamiBenchmarkA));
            seriesBmB.points.AddRange(MakeBenchmarkProfit1000Points(vamiBenchmarkB));
            chart.series.Add(seriesBmA);
            chart.series.Add(seriesBmB);

            chart.Width = chartSize.Width;
            chart.Height = chartSize.Height;

            var img = new Bitmap(chartSize.Width, chartSize.Height);
            chart.DrawToBitmap(img, new Rectangle(0, 0, chartSize.Width, chartSize.Height));
            var path = string.Format("{0}\\chartProfit1000_{1}.png", tempFolder, DateTime.Now.Millisecond);
            img.Save(path, ImageFormat.Png);
            return path;
        }