コード例 #1
0
        protected void DrawSeries(Bitmap bufferBitmap, float vMin, float vMax)
        {
            if (bufferBitmap == null)
            {
                return;
            }
            int   w = bufferBitmap.Width;
            int   h = bufferBitmap.Height;
            float x1 = ChartPadding.Left, x2 = w - ChartPadding.Right;
            float y1 = ChartPadding.Top, y2 = h - ChartPadding.Bottom;
            int   n = TickCount, m = CategoryCount;

            using (Graphics graphics = Graphics.FromImage(bufferBitmap))
                using (StringFormat stringFormat1 = new StringFormat()
                {
                    Alignment = StringAlignment.Far, LineAlignment = StringAlignment.Far
                })
                    using (StringFormat stringFormat2 = new StringFormat()
                    {
                        Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center
                    })
                        using (SolidBrush brush1 = new SolidBrush(Color.Black))
                            using (SolidBrush brush2 = new SolidBrush(Color.White))
                                using (Pen pen1 = new Pen(Color.Black, 2f))
                                    using (Pen pen2 = new Pen(Color.Gray, 1f)
                                    {
                                        DashStyle = DashStyle.Dash
                                    })
                                    {
                                        Padding chartPadding = ChartPadding;
                                        SizeF   innerSize    = InnerSize;
                                        graphics.Clip = new Region(new RectangleF(chartPadding.Left, chartPadding.Top, innerSize.Width + 1f, innerSize.Height + 1f));
                                        int i = 0;
                                        foreach (KeyValuePair <object, List <float> > keyValuePair in Items)
                                        {
                                            object       key    = keyValuePair.Key;
                                            List <float> values = keyValuePair.Value;

                                            Color color = ColorUtilities.GetDarkColor(i);
                                            pen1.Color   = color;
                                            brush1.Color = color;
                                            {
                                                PointF q1, q2 = new PointF(float.NaN, float.NaN);
                                                bool   b3 = false;
                                                for (int j = 0; j < n; j++) // tick
                                                {
                                                    float y = GetValue(values, j);
                                                    if (float.IsNaN(y))
                                                    {
                                                        b3 = false;
                                                    }
                                                    else
                                                    {
                                                        q1 = q2;
                                                        q2 = ToPointF(bufferBitmap, j, n, ToSingle((y - vMin) / (vMax - vMin)));
                                                        if (PointValid(q1) && PointValid(q2))
                                                        {
                                                            DrawConnection(graphics, pen1, pen2, q1, q2, b3 ? InnerConnection : OuterConnection);
                                                        }
                                                        else if (PointValid(q2))
                                                        {
                                                            DrawPoint(graphics, pen1, brush1, brush2, stringFormat2, q2, j);
                                                        }
                                                        b3 = true;
                                                    }
                                                }
                                                graphics.DrawString($"[{i + 1}]", DefaultFont, brush1, q2.X, q2.Y, stringFormat1);
                                            }
                                            if (ViewPoints)
                                            {
                                                PointF q2 = new PointF(0, 0);
                                                for (int j = 0; j < n; j++) // tick
                                                {
                                                    float y = GetValue(values, j);
                                                    if (float.IsNaN(y))
                                                    {
                                                        continue;
                                                    }
                                                    q2 = ToPointF(bufferBitmap, j, n, ToSingle((y - vMin) / (vMax - vMin)));
                                                    if (PointValid(q2))
                                                    {
                                                        DrawPoint(graphics, pen1, brush1, brush2, stringFormat2, q2, j);
                                                    }
                                                }
                                            }
                                            i++;
                                        }
                                    }
        }
コード例 #2
0
        public static void DrawLegend(
            Bitmap bitmap,
            float minValue, float maxValue, string format,
            ColorGradientEnum colorGradient, bool inverseGradient,
            params string[] lines)
        {
            if (float.IsNaN(minValue) || float.IsNaN(maxValue))
            {
                return;
            }
            const int a = 12; // distance from right legend to right bitmap
            const int b = 12; // distance from bottom legend to bottom bitmap
            const int l = 16; // height of a text line
            const int h = 60; // height of the color gradient indicator
            const int w = 64; // width
            const int d = 2;  // distance from y to text string
            int       n = lines != null ? lines.Length : 0;
            float     x1 = bitmap.Width - a, x0 = x1 - w, x2 = x0;
            float     y4 = bitmap.Height - b, y3 = y4 - l, y2 = y3 - h, y1 = y2 - l, y0 = y1 - n * l;

            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                // background
                using (SolidBrush backgroundBrush = new SolidBrush(Color.FromArgb(0xF0, 0xF0, 0xF0)))
                {
                    graphics.FillRectangle(backgroundBrush, x0, y0, w, h + (2 + n) * l);
                }

                // color gradient
                using (Pen gradientPen = new Pen(Color.Black, 1f))
                {
                    for (int i = 0; i <= h; i++)
                    {
                        float z = maxValue > minValue ? (float)i / h : 0.5f;
                        gradientPen.Color = ColorUtilities.GetColor(z, colorGradient, inverseGradient);
                        float y = y2 + i;
                        graphics.DrawLine(gradientPen, x0, y, x1, y);
                    }
                }

                // text
                using (Pen textPen = new Pen(Color.Black, 1f))
                    using (SolidBrush textBrush = new SolidBrush(Color.Black))
                        using (StringFormat stringFormat = new StringFormat
                        {
                            Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Near
                        })
                        {
                            Font font = Control.DefaultFont;
                            textBrush.Color = Color.Black;
                            textPen.Color   = Color.Black;
                            for (int i = 0; i < n; i++)
                            {
                                graphics.DrawString(lines[i], font, textBrush, x2, y0 + i * l + d, stringFormat);
                                graphics.DrawRectangle(textPen, x0, y0 + i * l, w, l);
                            }
                            graphics.DrawString(minValue.ToString(format), font, textBrush, x2, y1 + d, stringFormat);
                            graphics.DrawString(maxValue.ToString(format), font, textBrush, x2, y3 + d, stringFormat);
                            graphics.DrawRectangle(textPen, x0, y1, w, h + 2 * l);
                        }
            }
        }