예제 #1
0
        /// <summary>
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="bounds"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        public override void Plot(Graphics graphics, Rectangle bounds, double min, double max)
        {
            if (Bars == null || ChartControl == null)
            {
                return;
            }

            // plot error if data not complete
            if (textBrush.Color != ChartControl.AxisColor || textFont != ChartControl.Font)
            {
                textBrush.Color = ChartControl.AxisColor;
                textFont        = ChartControl.Font;

                SizeF errorSize = graphics.MeasureString(errorData, textFont);
                errorTextWidth  = errorSize.Width + 5;
                errorTextHeight = errorSize.Height + 5;
            }

            if (priorDayHLC == HLCCalculationMode.CalcFromIntradayData)
            {
                DateTime lastBarDate = (Bars.Count == 0) ? Cbi.Globals.MaxDate : ChartControl.EquidistantBars ?
                                       Bars.GetTime(Math.Min(Bars.Count - 1, ChartControl.LastBarPainted)).Date
                                                                                : Bars.GetTime(Math.Min(Bars.Count - 1, Bars.GetBar(ChartControl.LastBarTimePainted))).Date;
                if ((lastBarDate == Cbi.Globals.MaxDate) ||
                    (Bars.BarsType.BuiltFrom == PeriodType.Minute &&
                     ((pivotRangeType == PivotRange.Monthly && Bars.Count > 0 && Bars.GetTime(0).AddSeconds(-1).Date >= new DateTime(lastBarDate.Year, lastBarDate.Month, 1).AddMonths(-1)) ||
                      (pivotRangeType == PivotRange.Weekly && Bars.Count > 0 && Bars.GetTime(0).AddSeconds(-1).Date >= lastBarDate.AddDays(-(((int)lastBarDate.DayOfWeek) + 1) % 7).AddDays(-7)) ||
                      (pivotRangeType == PivotRange.Daily && Bars.Count > 0 && Bars.GetTime(0).AddSeconds(-1).Date >= lastBarDate.AddDays(-1)))) ||
                    (Bars.BarsType.BuiltFrom != PeriodType.Minute &&
                     ((pivotRangeType == PivotRange.Monthly && Bars.Count > 0 && Bars.GetTime(0).Date >= new DateTime(lastBarDate.Year, lastBarDate.Month, 1).AddMonths(-1)) ||
                      (pivotRangeType == PivotRange.Weekly && Bars.Count > 0 && Bars.GetTime(0).Date >= lastBarDate.AddDays(-(((int)lastBarDate.DayOfWeek) + 1) % 7).AddDays(-7)) ||
                      (pivotRangeType == PivotRange.Daily && Bars.Count > 0 && Bars.GetTime(0).Date >= lastBarDate.AddDays(-1)))))
                {
                    graphics.DrawString(errorData, ChartControl.Font, textBrush, bounds.X + bounds.Width - errorTextWidth, bounds.Y + bounds.Height - errorTextHeight, stringFormatNear);
                }
            }
            else if (priorDayHLC == HLCCalculationMode.DailyBars && existsHistDailyData)
            {
                DateTime lastBarDate = (dailyBars.Count == 0) ? Cbi.Globals.MaxDate : ChartControl.EquidistantBars ?
                                       dailyBars.GetTime(Math.Min(dailyBars.Count - 1, ChartControl.LastBarPainted)).Date
                                        : dailyBars.GetTime(Math.Min(dailyBars.Count - 1, dailyBars.GetBar(ChartControl.LastBarTimePainted))).Date;
                if ((lastBarDate == Cbi.Globals.MaxDate) ||
                    (pivotRangeType == PivotRange.Monthly && dailyBars.GetTime(0).Date >= new DateTime(lastBarDate.Year, lastBarDate.Month, 1).AddMonths(-1)) ||
                    (pivotRangeType == PivotRange.Weekly && dailyBars.GetTime(0).Date >= lastBarDate.AddDays(-(((int)lastBarDate.DayOfWeek) + 1) % 7).AddDays(-7)) ||
                    (pivotRangeType == PivotRange.Daily && dailyBars.GetTime(0).Date >= lastBarDate.AddDays(-1)))
                {
                    graphics.DrawString(errorData, ChartControl.Font, textBrush, bounds.X + bounds.Width - errorTextWidth, bounds.Y + bounds.Height - errorTextHeight, stringFormatNear);
                }
            }

            float textHeight = ChartControl.Font.GetHeight();

            for (int seriesCount = 0; seriesCount < Values.Length; seriesCount++)
            {
                SolidBrush     brush = brushes[seriesCount];
                int            firstBarIdxToPaint = -1;
                int            lastX            = -1;
                int            lastY            = -1;
                SmoothingMode  oldSmoothingMode = graphics.SmoothingMode;
                Gui.Chart.Plot plot             = Plots[seriesCount];
                DataSeries     series           = (DataSeries)Values[seriesCount];

                for (int i = newSessionBarIdxArr.Count - 1; i >= 0; i--)
                {
                    int prevSessionBreakIdx = (int)newSessionBarIdxArr[i];
                    if (prevSessionBreakIdx <= this.LastBarIndexPainted)
                    {
                        firstBarIdxToPaint = prevSessionBreakIdx;
                        break;
                    }
                }

                using (GraphicsPath path = new GraphicsPath())
                {
                    if (brush.Color != plot.Pen.Color)
                    {
                        brush = new SolidBrush(plot.Pen.Color);
                    }

                    for (int idx = this.LastBarIndexPainted; idx >= Math.Max(this.FirstBarIndexPainted, this.LastBarIndexPainted - Width); idx--)
                    {
                        if (idx - Displacement < 0 || idx - Displacement >= Bars.Count || (!ChartControl.ShowBarsRequired && idx - Displacement < BarsRequired))
                        {
                            continue;
                        }
                        else if (!series.IsValidPlot(idx))
                        {
                            continue;
                        }

                        if (idx < firstBarIdxToPaint)
                        {
                            break;
                        }

                        double val = series.Get(idx);
                        int    x   = ChartControl.GetXByBarIdx(BarsArray[0], idx);
                        int    y   = ChartControl.GetYByValue(this, val);

                        if (lastX >= 0)
                        {
                            if (y != lastY)                             // Problem here is, that last bar of old day has date of new day
                            {
                                y = lastY;
                            }
                            path.AddLine(lastX - plot.Pen.Width / 2, lastY, x - plot.Pen.Width / 2, y);
                        }
                        lastX = x;
                        lastY = y;
                    }

                    graphics.SmoothingMode = SmoothingMode.AntiAlias;
                    graphics.DrawPath(plot.Pen, path);
                    graphics.SmoothingMode = oldSmoothingMode;
                    graphics.DrawString(plot.Name, ChartControl.Font, brush, lastX, lastY - textHeight / 2, stringFormatFar);
                }
            }
        }
예제 #2
0
        //
        //  This Plot() method has been tweaked so it will color in two different colors
        //  in-between the two plots of Values[3] and Values[4].
        //
        public override void Plot(Graphics graphics, Rectangle bounds, double min, double max)
        {
            base.Plot(graphics, bounds, min, max);

            if (Bars == null || ChartControl == null)
            {
                return;
            }

            SolidBrush brush;                                                                           // Set current brush color here.

            SolidBrush brushUP   = new SolidBrush(Color.FromArgb(bandAreaColorOpacity * 20, bandAreaColorUp));
            SolidBrush brushDOWN = new SolidBrush(Color.FromArgb(bandAreaColorOpacity * 20, bandAreaColorDown));

            int           barWidth         = ChartControl.ChartStyle.GetBarPaintWidth(ChartControl.BarWidth);
            SmoothingMode oldSmoothingMode = graphics.SmoothingMode;
            GraphicsPath  path             = new GraphicsPath();

            DataSeries series0 = (DataSeries)Values[0];                 // Color in between these two plots.
            DataSeries series1 = (DataSeries)Values[1];

            brush = brushUP;                                                    // Start with the upwards color.
            int  barcount = 0;                                                  // Start with leftmost bar.
            bool firstbar = true;                                               // Plotting the first bar.

            while (barcount < ChartControl.BarsPainted)                         // Continue until all bars have been painted.
            {
                int count = 0;                                                  // Counter for innner loop.
                for (int seriesCount = 0; seriesCount < 2; seriesCount++)
                {
                    int            lastX  = -1;
                    int            lastY  = -1;
                    DataSeries     series = (DataSeries)Values[seriesCount];
                    Gui.Chart.Plot plot   = Plots[seriesCount];

                    for (count = barcount; count < ChartControl.BarsPainted; count++)
                    {
                        int idx = ChartControl.LastBarPainted - ChartControl.BarsPainted + 1 + count;
                        if (idx < 0 || idx >= Input.Count || (!ChartControl.ShowBarsRequired && idx < BarsRequired))
                        {
                            continue;
                        }

                        double val = series.Get(idx);                                                           // Get next y-value to be plotted.
                        if (val == 0)                                                                           // If nothing to plot...
                        {
                            continue;                                                                           // ...ignore the enrtry.
                        }
                        int x = (int)(ChartControl.CanvasRight - ChartControl.BarMarginRight - barWidth / 2
                                      + (count - ChartControl.BarsPainted + 1) * ChartControl.BarSpace) + 1;

                        int y = (int)((bounds.Y + bounds.Height) - ((val - min) / Gui.Chart.ChartControl.MaxMinusMin(max, min)) * bounds.Height);

                        double val0 = series0.Get(idx);
                        double val1 = series1.Get(idx);
                        if (((val0 > val1) && (brush != brushUP)) ||                            // Now going in wrong direction?
                            ((val0 < val1) && (brush != brushDOWN)))
                        {                                                                       // Yes.  Done with this loop.
                            if (lastX >= 0)                                                     // Was there a last point?
                            {                                                                   // Yes.  Connect it to the position half-way to this one.
                                path.AddLine(lastX - plot.Pen.Width / 2, lastY, (x + lastX - plot.Pen.Width) / 2, (lastY + y) / 2);
                                // Plot vertex of cross-over of the lines (1/2 way point).
                            }
                            break;                                                                                              // Done, exit inner loop to change color.
                        }

                        if (firstbar == false)                                                      // Is this the first plotted bar of the chart?
                        {                                                                           // No.  Plot all bars after the first one.
                            if (count == barcount)                                                  // First bar after direction change (and color swap)?
                            {                                                                       // Yes.  Add line segment for cross-over, 1/2 bar back.
                                double valm1 = series.Get(idx - 1);                                 // Get prior y-value to be plotted.
                                lastX = x - ChartControl.BarSpace / 2;                              // Back up 1/2 a bar for x-value.
                                lastY = (y + (int)((bounds.Y + bounds.Height) - ((valm1 - min) / Gui.Chart.ChartControl.MaxMinusMin(max, min)) * bounds.Height)) / 2;
                            }

                            path.AddLine(lastX - plot.Pen.Width / 2, lastY, x - plot.Pen.Width / 2, y);                 // Connect last point to this one.
                        }
                        firstbar = false;                                                                               // No longer the first bar.
                        lastX    = x;                                                                                   // Save current position for next time, so we can connect the dots.
                        lastY    = y;
                    }
                    path.Reverse();                                                                                     // Go back the other direction.
                }
                graphics.SmoothingMode = SmoothingMode.AntiAlias;
                graphics.FillPath(brush, path);
                path.Reset();                                                           // Eliminate points already colored.

                barcount = count;                                                       // Get ready to process next segment.
                brush    = (brush == brushUP) ? brushDOWN : brushUP;                    // Switch colors for next segment.
            }
            graphics.SmoothingMode = oldSmoothingMode;                                  // Restore smoothing mode before exiting.
        }