コード例 #1
0
        public static Bitmap DesignerModeBitmap(Size size, bool drawArrows = false)
        {
            Bitmap bmp = new Bitmap(size.Width, size.Height);

            Graphics gfx = Graphics.FromImage(bmp);

            gfx.Clear(ColorTranslator.FromHtml("#003366"));
            Brush brushLogo         = new SolidBrush(ColorTranslator.FromHtml("#FFFFFF"));
            Brush brushMeasurements = new SolidBrush(ColorTranslator.FromHtml("#006699"));
            Pen   pen = new Pen(ColorTranslator.FromHtml("#006699"), 3);

            pen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
            pen.EndCap   = System.Drawing.Drawing2D.LineCap.Round;
            float arrowSize = 7;
            float padding   = 3;

            // logo
            FontFamily ff = new FontFamily(Fonts.GetDefaultFontName());

            gfx.DrawString("ScottPlot", new Font(ff, 24, FontStyle.Bold), brushLogo, 10, 10);
            var titleSize = GDI.MeasureString(gfx, "ScottPlot", new Font(ff, 24, FontStyle.Bold));

            gfx.DrawString($"version {GetVersionString()}", new Font(ff, 12, FontStyle.Italic), brushLogo, 12, (int)(10 + titleSize.Height * .7));

            if (drawArrows)
            {
                // horizontal arrow
                PointF left   = new PointF(padding, size.Height / 2);
                PointF leftA  = new PointF(left.X + arrowSize, left.Y + arrowSize);
                PointF leftB  = new PointF(left.X + arrowSize, left.Y - arrowSize);
                PointF right  = new PointF(size.Width - padding, size.Height / 2);
                PointF rightA = new PointF(right.X - arrowSize, right.Y + arrowSize);
                PointF rightB = new PointF(right.X - arrowSize, right.Y - arrowSize);
                gfx.DrawLine(pen, left, right);
                gfx.DrawLine(pen, left, leftA);
                gfx.DrawLine(pen, left, leftB);
                gfx.DrawLine(pen, right, rightA);
                gfx.DrawLine(pen, right, rightB);
                gfx.DrawString($"{size.Width}px",
                               new Font(ff, 12, FontStyle.Bold), brushMeasurements,
                               (float)(size.Width * .2), (float)(size.Height * .5));

                // vertical arrow
                PointF top  = new PointF(size.Width / 2, padding);
                PointF topA = new PointF(top.X - arrowSize, top.Y + arrowSize);
                PointF topB = new PointF(top.X + arrowSize, top.Y + arrowSize);
                PointF bot  = new PointF(size.Width / 2, size.Height - padding);
                PointF botA = new PointF(bot.X - arrowSize, bot.Y - arrowSize);
                PointF botB = new PointF(bot.X + arrowSize, bot.Y - arrowSize);
                gfx.DrawLine(pen, top, bot);
                gfx.DrawLine(pen, bot, botA);
                gfx.DrawLine(pen, bot, botB);
                gfx.DrawLine(pen, top, topA);
                gfx.DrawLine(pen, top, topB);
                gfx.RotateTransform(-90);
                gfx.DrawString($"{size.Height}px",
                               new Font(ff, 12, FontStyle.Bold), brushMeasurements,
                               (float)(-size.Height * .4), (float)(size.Width * .5));
            }

            return(bmp);
        }
コード例 #2
0
        public override void Render(Settings settings)
        {
            double[] proportions = values.Select(x => x / values.Sum()).ToArray();

            int outlineWidth      = 1;
            int sliceOutlineWidth = 0;

            if (explodedChart)
            {
                pen.Color         = settings.DataBackground.Colour; // TODO: will fail if data background is transparent
                outlineWidth      = 20;
                sliceOutlineWidth = 1;
            }

            AxisLimits2D limits         = GetLimits();
            double       centreX        = limits.xCenter;
            double       centreY        = limits.yCenter;
            float        diameterPixels = .9f * Math.Min(settings.dataSize.Width, settings.dataSize.Height);
            string       fontName       = Fonts.GetSansFontName();
            float        fontSize       = 12;

            // 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((float)settings.GetPixelX(centreX) - diameterPixels / 2, (float)settings.GetPixelY(centreY) - diameterPixels / 2, diameterPixels, diameterPixels);

            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[i] : "";
                labelStrings[i] = $"{sliceLabelValue}\n{sliceLabelPercentage}\n{sliceLabelName}".Trim();

                brush.Color = colors[i];
                settings.gfxData.FillPie(brush, (int)(boundingRectangle.X + xOffset), (int)(boundingRectangle.Y + yOffset), boundingRectangle.Width, boundingRectangle.Height, (float)start, (float)(sweep + sweepOffset));

                if (explodedChart)
                {
                    pen.Width = sliceOutlineWidth;
                    settings.gfxData.DrawPie(pen, (int)(boundingRectangle.X + xOffset), (int)(boundingRectangle.Y + yOffset), boundingRectangle.Width, boundingRectangle.Height, (float)start, (float)(sweep + sweepOffset));
                }
                start += sweep;
            }

            brush.Color = Color.White;
            var font = new Font(fontName, fontSize);

            for (int i = 0; i < values.Length; i++)
            {
                if (!string.IsNullOrWhiteSpace(labelStrings[i]))
                {
                    settings.gfxData.DrawString(labelStrings[i], font, brush,
                                                (float)labelXs[i], (float)labelYs[i], settings.misc.sfCenterCenter);
                }
            }

            pen.Width = outlineWidth;
            settings.gfxData.DrawEllipse(pen, boundingRectangle.X, boundingRectangle.Y, boundingRectangle.Width, boundingRectangle.Height);
        }
コード例 #3
0
        public PlottableBar(double[] xs, double[] ys, string label,
                            double barWidth, double xOffset,
                            bool fill, Color fillColor,
                            double outlineWidth, Color outlineColor,
                            double[] yErr, double errorLineWidth, double errorCapSize, Color errorColor,
                            bool horizontal, bool showValues, Color valueColor, double[] yOffsets, Color negativeColor
                            )
        {
            if (ys is null || ys.Length == 0)
            {
                throw new ArgumentException("ys must contain data values");
            }

            if (xs is null)
            {
                xs = DataGen.Consecutive(ys.Length);
            }

            if (xs.Length != ys.Length)
            {
                throw new ArgumentException("xs and ys must have same number of elements");
            }

            if (yErr is null)
            {
                yErr = DataGen.Zeros(ys.Length);
            }

            if (yErr.Length != ys.Length)
            {
                throw new ArgumentException("yErr and ys must have same number of elements");
            }

            if (yOffsets is null)
            {
                yOffsets = DataGen.Zeros(ys.Length);
            }


            this.xs           = xs;
            this.ys           = ys;
            this.yErr         = yErr;
            this.xOffset      = xOffset;
            this.label        = label;
            this.verticalBars = !horizontal;
            this.showValues   = showValues;

            this.barWidth     = barWidth;
            this.errorCapSize = errorCapSize;

            this.fill          = fill;
            this.fillColor     = fillColor;
            this.negativeColor = negativeColor;

            this.yOffsets = yOffsets;

            fillBrush  = new SolidBrush(fillColor);
            outlinePen = new Pen(outlineColor, (float)outlineWidth);
            errorPen   = new Pen(errorColor, (float)errorLineWidth);

            valueTextFont  = new Font(Fonts.GetDefaultFontName(), 12);
            valueTextBrush = new SolidBrush(valueColor);
        }