Пример #1
0
        public void Render(PlotDimensions dims, Bitmap bmp, bool lowQuality = false)
        {
            PointF[] points = new PointF[xs.Length];
            for (int i = 0; i < xs.Length; i++)
            {
                points[i] = new PointF(dims.GetPixelX(xs[i]), dims.GetPixelY(ys[i]));
            }

            using (Graphics gfx = GDI.Graphics(bmp, lowQuality))
                using (Brush fillBrush = GDI.Brush(Color.FromArgb((byte)(255 * fillAlpha), fillColor)))
                    using (Pen outlinePen = GDI.Pen(lineColor, (float)lineWidth))
                    {
                        if (fill)
                        {
                            gfx.FillPolygon(fillBrush, points);
                        }

                        if (lineWidth > 0)
                        {
                            gfx.DrawPolygon(outlinePen, points);
                        }
                    }
        }
Пример #2
0
        public static void Box(PlotDimensions dims, Bitmap bmp, bool lowQuality, Population pop, Random rand,
                               double popLeft, double popWidth, Color color, Position position, BoxFormat boxFormat,
                               HorizontalAlignment errorAlignment = HorizontalAlignment.Right)
        {
            // adjust edges to accomodate special positions
            if (position == Position.Hide)
            {
                return;
            }
            if (position == Position.Left || position == Position.Right)
            {
                popWidth /= 2;
            }
            if (position == Position.Right)
            {
                popLeft += popWidth;
            }

            double errorMaxPx, errorMinPx;
            double yPxTop, yPxBase;
            double yPx;

            if (boxFormat == BoxFormat.StdevStderrMean)
            {
                errorMaxPx = dims.GetPixelY(pop.mean + pop.stDev);
                errorMinPx = dims.GetPixelY(pop.mean - pop.stDev);
                yPxTop     = dims.GetPixelY(pop.mean + pop.stdErr);
                yPxBase    = dims.GetPixelY(pop.mean - pop.stdErr);
                yPx        = dims.GetPixelY(pop.mean);
            }
            else if (boxFormat == BoxFormat.OutlierQuartileMedian)
            {
                errorMaxPx = dims.GetPixelY(pop.maxNonOutlier);
                errorMinPx = dims.GetPixelY(pop.minNonOutlier);
                yPxTop     = dims.GetPixelY(pop.Q3);
                yPxBase    = dims.GetPixelY(pop.Q1);
                yPx        = dims.GetPixelY(pop.median);
            }
            else
            {
                throw new NotImplementedException();
            }

            // make cap width a fraction of available space
            double capWidthFrac = .38;
            double capWidth     = popWidth * capWidthFrac;

            // contract edges slightly to encourage padding between elements
            double edgePaddingFrac = 0.2;

            popLeft  += popWidth * edgePaddingFrac;
            popWidth -= (popWidth * edgePaddingFrac) * 2;
            double     leftPx  = dims.GetPixelX(popLeft);
            double     rightPx = dims.GetPixelX(popLeft + popWidth);
            RectangleF rect    = new RectangleF(
                x: (float)leftPx,
                y: (float)yPxTop,
                width: (float)(rightPx - leftPx),
                height: (float)(yPxBase - yPxTop));

            // determine location of errorbars and caps
            double capPx1, capPx2, errorPxX;

            switch (errorAlignment)
            {
            case HorizontalAlignment.Center:
                double centerX = popLeft + popWidth / 2;
                errorPxX = dims.GetPixelX(centerX);
                capPx1   = dims.GetPixelX(centerX - capWidth / 2);
                capPx2   = dims.GetPixelX(centerX + capWidth / 2);
                break;

            case HorizontalAlignment.Right:
                errorPxX = dims.GetPixelX(popLeft + popWidth);
                capPx1   = dims.GetPixelX(popLeft + popWidth - capWidth / 2);
                capPx2   = dims.GetPixelX(popLeft + popWidth);
                break;

            case HorizontalAlignment.Left:
                errorPxX = dims.GetPixelX(popLeft);
                capPx1   = dims.GetPixelX(popLeft);
                capPx2   = dims.GetPixelX(popLeft + capWidth / 2);
                break;

            default:
                throw new NotImplementedException();
            }

            using (Graphics gfx = GDI.Graphics(bmp, lowQuality))
                using (Pen pen = GDI.Pen(Color.Black))
                    using (Brush brush = GDI.Brush(color))
                    {
                        // draw the box
                        gfx.FillRectangle(brush, rect.X, rect.Y, rect.Width, rect.Height);
                        gfx.DrawRectangle(pen, rect.X, rect.Y, rect.Width, rect.Height);

                        // draw the line in the center
                        gfx.DrawLine(pen, rect.X, (float)yPx, rect.X + rect.Width, (float)yPx);

                        // draw errorbars and caps
                        gfx.DrawLine(pen, (float)errorPxX, (float)errorMinPx, (float)errorPxX, rect.Y + rect.Height);
                        gfx.DrawLine(pen, (float)errorPxX, (float)errorMaxPx, (float)errorPxX, rect.Y);
                        gfx.DrawLine(pen, (float)capPx1, (float)errorMinPx, (float)capPx2, (float)errorMinPx);
                        gfx.DrawLine(pen, (float)capPx1, (float)errorMaxPx, (float)capPx2, (float)errorMaxPx);
                    }
        }
Пример #3
0
        public static void Bar(PlotDimensions dims, Bitmap bmp, bool lowQuality, Population pop, Random rand,
                               double popLeft, double popWidth, Color color, Position position, bool useStdErr = false)
        {
            // adjust edges to accomodate special positions
            if (position == Position.Hide)
            {
                return;
            }
            if (position == Position.Left || position == Position.Right)
            {
                popWidth /= 2;
            }
            if (position == Position.Right)
            {
                popLeft += popWidth;
            }

            // determine the center point and calculate bounds
            double centerX = popLeft + popWidth / 2;
            double xPx     = dims.GetPixelX(centerX);
            double yPxTop  = dims.GetPixelY(pop.mean);
            double yPxBase = dims.GetPixelY(0);

            double errorMaxPx, errorMinPx;

            if (useStdErr)
            {
                errorMaxPx = dims.GetPixelY(pop.mean + pop.stdErr);
                errorMinPx = dims.GetPixelY(pop.mean - pop.stdErr);
            }
            else
            {
                errorMaxPx = dims.GetPixelY(pop.mean + pop.stDev);
                errorMinPx = dims.GetPixelY(pop.mean - pop.stDev);
            }

            // make cap width a fraction of available space
            double capWidthFrac = .38;
            double capWidth     = popWidth * capWidthFrac;
            double capPx1       = dims.GetPixelX(centerX - capWidth / 2);
            double capPx2       = dims.GetPixelX(centerX + capWidth / 2);

            // contract edges slightly to encourage padding between elements
            double edgePaddingFrac = 0.2;

            popLeft  += popWidth * edgePaddingFrac;
            popWidth -= (popWidth * edgePaddingFrac) * 2;
            double leftPx  = dims.GetPixelX(popLeft);
            double rightPx = dims.GetPixelX(popLeft + popWidth);

            RectangleF rect = new RectangleF((float)leftPx, (float)yPxTop, (float)(rightPx - leftPx), (float)(yPxBase - yPxTop));

            using (Graphics gfx = GDI.Graphics(bmp, lowQuality))
                using (Pen pen = GDI.Pen(Color.Black))
                    using (Brush brush = GDI.Brush(color))
                    {
                        gfx.FillRectangle(brush, rect.X, rect.Y, rect.Width, rect.Height);
                        gfx.DrawRectangle(pen, rect.X, rect.Y, rect.Width, rect.Height);
                        gfx.DrawLine(pen, (float)xPx, (float)errorMinPx, (float)xPx, (float)errorMaxPx);
                        gfx.DrawLine(pen, (float)capPx1, (float)errorMinPx, (float)capPx2, (float)errorMinPx);
                        gfx.DrawLine(pen, (float)capPx1, (float)errorMaxPx, (float)capPx2, (float)errorMaxPx);
                    }
        }
Пример #4
0
        public void Render(PlotDimensions dims, Bitmap bmp, bool lowQuality = false)
        {
            using (Graphics gfx = GDI.Graphics(bmp, lowQuality))
                using (Pen backgroundPen = GDI.Pen(dataBackgroundColor))
                    using (Pen outlinePen = GDI.Pen(outlineColor, outlineSize))
                        using (Brush brush = GDI.Brush(Color.Black))
                            using (Brush fontBrush = GDI.Brush(centerTextColor))
                                using (Font sliceFont = GDI.Font(null, sliceFontSize))
                                    using (Font centerFont = GDI.Font(null, centerFontSize))
                                        using (StringFormat sfCenter = new StringFormat()
                                        {
                                            LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center
                                        })
                                        {
                                            double[] proportions = values.Select(x => x / values.Sum()).ToArray();

                                            AxisLimits2D limits         = GetLimits();
                                            double       centreX        = limits.xCenter;
                                            double       centreY        = limits.yCenter;
                                            float        diameterPixels = .9f * Math.Min(dims.DataWidth, dims.DataHeight);

                                            // record label details and draw them after slices to prevent cover-ups
                                            double[] labelXs      = new double[values.Length];
                                            double[] labelYs      = new double[values.Length];
                                            string[] labelStrings = new string[values.Length];

                                            RectangleF boundingRectangle = new RectangleF(
                                                dims.GetPixelX(centreX) - diameterPixels / 2,
                                                dims.GetPixelY(centreY) - diameterPixels / 2,
                                                diameterPixels,
                                                diameterPixels);

                                            if (donutSize > 0)
                                            {
                                                GraphicsPath graphicsPath               = new GraphicsPath();
                                                float        donutDiameterPixels        = (float)donutSize * diameterPixels;
                                                RectangleF   donutHoleBoundingRectangle = new RectangleF(
                                                    dims.GetPixelX(centreX) - donutDiameterPixels / 2,
                                                    dims.GetPixelY(centreY) - donutDiameterPixels / 2,
                                                    donutDiameterPixels,
                                                    donutDiameterPixels);
                                                graphicsPath.AddEllipse(donutHoleBoundingRectangle);
                                                Region excludedRegion = new Region(graphicsPath);
                                                gfx.ExcludeClip(excludedRegion);
                                            }

                                            double start = -90;
                                            for (int i = 0; i < values.Length; i++)
                                            {
                                                // determine where the slice is to be drawn
                                                double sweep       = proportions[i] * 360;
                                                double sweepOffset = explodedChart ? -1 : 0;
                                                double angle       = (Math.PI / 180) * ((sweep + 2 * start) / 2);
                                                double xOffset     = explodedChart ? 3 * Math.Cos(angle) : 0;
                                                double yOffset     = explodedChart ? 3 * Math.Sin(angle) : 0;

                                                // record where and what to label the slice
                                                double sliceLabelR = 0.35 * diameterPixels;
                                                labelXs[i] = (boundingRectangle.X + diameterPixels / 2 + xOffset + Math.Cos(angle) * sliceLabelR);
                                                labelYs[i] = (boundingRectangle.Y + diameterPixels / 2 + yOffset + Math.Sin(angle) * sliceLabelR);
                                                string sliceLabelValue      = (showValues) ? $"{values[i]}" : "";
                                                string sliceLabelPercentage = showPercentages ? $"{proportions[i] * 100:f1}%" : "";
                                                string sliceLabelName       = (showLabels && groupNames != null) ? groupNames[i] : "";
                                                labelStrings[i] = $"{sliceLabelValue}\n{sliceLabelPercentage}\n{sliceLabelName}".Trim();

                                                ((SolidBrush)brush).Color = colors[i];
                                                gfx.FillPie(brush: brush,
                                                            x: (int)(boundingRectangle.X + xOffset),
                                                            y: (int)(boundingRectangle.Y + yOffset),
                                                            width: boundingRectangle.Width,
                                                            height: boundingRectangle.Height,
                                                            startAngle: (float)start,
                                                            sweepAngle: (float)(sweep + sweepOffset));

                                                if (explodedChart)
                                                {
                                                    gfx.DrawPie(
                                                        pen: backgroundPen,
                                                        x: (int)(boundingRectangle.X + xOffset),
                                                        y: (int)(boundingRectangle.Y + yOffset),
                                                        width: boundingRectangle.Width,
                                                        height: boundingRectangle.Height,
                                                        startAngle: (float)start,
                                                        sweepAngle: (float)(sweep + sweepOffset));
                                                }
                                                start += sweep;
                                            }

                                            ((SolidBrush)brush).Color = Color.White;
                                            for (int i = 0; i < values.Length; i++)
                                            {
                                                if (!string.IsNullOrWhiteSpace(labelStrings[i]))
                                                {
                                                    gfx.DrawString(labelStrings[i], sliceFont, brush, (float)labelXs[i], (float)labelYs[i], sfCenter);
                                                }
                                            }

                                            if (outlineSize > 0)
                                            {
                                                gfx.DrawEllipse(
                                                    outlinePen,
                                                    boundingRectangle.X,
                                                    boundingRectangle.Y,
                                                    boundingRectangle.Width,
                                                    boundingRectangle.Height);
                                            }

                                            gfx.ResetClip();

                                            if (centerText != null)
                                            {
                                                gfx.DrawString(centerText, centerFont, fontBrush, dims.GetPixelX(0), dims.GetPixelY(0), sfCenter);
                                            }

                                            if (explodedChart)
                                            {
                                                // draw a background-colored circle around the perimeter to make it look like all pieces are the same size
                                                backgroundPen.Width = 20;
                                                gfx.DrawEllipse(
                                                    pen: backgroundPen,
                                                    x: boundingRectangle.X,
                                                    y: boundingRectangle.Y,
                                                    width: boundingRectangle.Width,
                                                    height: boundingRectangle.Height);
                                            }
                                        }
        }
Пример #5
0
 private void RenderAxis(PlotDimensions dims, Bitmap bmp, bool lowQuality)
 {
     using (Graphics gfx = GDI.Graphics(bmp, lowQuality))
         using (Pen pen = GDI.Pen(Color.Black))
             using (Brush brush = GDI.Brush(Color.Black))
                 using (Font axisFont = GDI.Font(null, 12))
                     using (StringFormat right_centre = new StringFormat()
                     {
                         Alignment = StringAlignment.Far, LineAlignment = StringAlignment.Center
                     })
                         using (StringFormat centre_top = new StringFormat()
                         {
                             Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Near
                         })
                         {
                             double offset   = -2;
                             double minScale = Math.Min(dims.PxPerUnitX, dims.PxPerUnitY);
                             gfx.DrawString($"{AxisOffsets[0]:f3}", axisFont, brush, dims.GetPixelX(0), dims.GetPixelY(offset), centre_top);
                             gfx.DrawString($"{AxisOffsets[0] + AxisMultipliers[0]:f3}", axisFont, brush, new PointF((float)((width * minScale) + dims.GetPixelX(0)), dims.GetPixelY(offset)), centre_top);
                             gfx.DrawString($"{AxisOffsets[1]:f3}", axisFont, brush, dims.GetPixelX(offset), dims.GetPixelY(0), right_centre);
                             gfx.DrawString($"{AxisOffsets[1] + AxisMultipliers[1]:f3}", axisFont, brush, new PointF(dims.GetPixelX(offset), dims.GetPixelY(0) - (float)(height * minScale)), right_centre);
                         }
 }
Пример #6
0
        public void Render(PlotDimensions dims, Bitmap bmp, bool lowQuality = false)
        {
            int    numGroups = normalized.GetUpperBound(0) + 1;
            int    numCategories = normalized.GetUpperBound(1) + 1;
            double sweepAngle = 2 * Math.PI / numCategories;
            double minScale = new double[] { dims.PxPerUnitX, dims.PxPerUnitX }.Min();
            PointF origin = new PointF(dims.GetPixelX(0), dims.GetPixelY(0));

            double[] radii = new double[] { 0.25 * minScale, 0.5 * minScale, 1 * minScale };

            using (Graphics gfx = GDI.Graphics(bmp, lowQuality))
                using (Pen pen = GDI.Pen(webColor))
                    using (Brush brush = GDI.Brush(Color.Black))
                        using (StringFormat sf = new StringFormat()
                        {
                            LineAlignment = StringAlignment.Center
                        })
                            using (Font font = GDI.Font())
                            {
                                for (int i = 0; i < radii.Length; i++)
                                {
                                    gfx.DrawEllipse(pen, (int)(origin.X - radii[i]), (int)(origin.Y - radii[i]), (int)(radii[i] * 2), (int)(radii[i] * 2));
                                    StringFormat stringFormat = new StringFormat()
                                    {
                                        Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Far
                                    };
                                    gfx.DrawString($"{normalizedMax * radii[i] / minScale:f1}", new Font(FontFamily.GenericSansSerif, 8), brush, origin.X, (float)(-radii[i] + origin.Y), stringFormat);
                                }

                                for (int i = 0; i < numCategories; i++)
                                {
                                    PointF destination = new PointF((float)(1.1 * Math.Cos(sweepAngle * i - Math.PI / 2) * minScale + origin.X), (float)(1.1 * Math.Sin(sweepAngle * i - Math.PI / 2) * minScale + origin.Y));
                                    gfx.DrawLine(pen, origin, destination);

                                    if (categoryNames != null)
                                    {
                                        PointF textDestination = new PointF(
                                            (float)(1.3 * Math.Cos(sweepAngle * i - Math.PI / 2) * minScale + origin.X),
                                            (float)(1.3 * Math.Sin(sweepAngle * i - Math.PI / 2) * minScale + origin.Y));

                                        if (Math.Abs(textDestination.X - origin.X) < 0.1)
                                        {
                                            sf.Alignment = StringAlignment.Center;
                                        }
                                        else
                                        {
                                            sf.Alignment = dims.GetCoordinateX(textDestination.X) < 0 ? StringAlignment.Far : StringAlignment.Near;
                                        }
                                        gfx.DrawString(categoryNames[i], font, brush, textDestination, sf);
                                    }
                                }

                                for (int i = 0; i < numGroups; i++)
                                {
                                    PointF[] points = new PointF[numCategories];
                                    for (int j = 0; j < numCategories; j++)
                                    {
                                        points[j] = new PointF(
                                            (float)(normalized[i, j] * Math.Cos(sweepAngle * j - Math.PI / 2) * minScale + origin.X),
                                            (float)(normalized[i, j] * Math.Sin(sweepAngle * j - Math.PI / 2) * minScale + origin.Y));
                                    }

                                    ((SolidBrush)brush).Color = fillColors[i];
                                    pen.Color = lineColors[i];
                                    gfx.FillPolygon(brush, points);
                                    gfx.DrawPolygon(pen, points);
                                }
                            }
        }