private static void DrawYScale(Graphics g2, Pen blackPen, Rectangle spectrumRec)
        {
            int fontHeight = (int)g2.MeasureString("M", bigFont).Height;

            RectangleTransform rt2 = new RectangleTransform(spectrumRec, spectrumRec.Width, 100);

            g2.DrawLine(blackPen, new Point(spectrumRec.Left, spectrumRec.Bottom), new Point(spectrumRec.Left, spectrumRec.Top));
            for (int i = 0; i <= 100; i += 2)
            {
                int curPercent = rt2.GetTransformY(i);
                if (i % 10 == 0)
                {
                    g2.DrawLine(blackPen, spectrumRec.Left, curPercent, spectrumRec.Left - 10, curPercent);
                    string number   = i.ToString();
                    float  strWidth = GetStringWidth(g2, bigFont, number);
                    g2.DrawString(number, bigFont, Brushes.Black, new PointF(spectrumRec.Left - 10 - strWidth, curPercent - fontHeight / 2));
                }
                else
                {
                    g2.DrawLine(blackPen, spectrumRec.Left, curPercent, spectrumRec.Left - 5, curPercent);
                }
            }

            g2.DrawVerticalString(bigFont, Brushes.Black, spectrumRec.Left - 10 - GetStringWidth(g2, bigFont, "100") - fontHeight / 2, (spectrumRec.Top + spectrumRec.Bottom) / 2, "Intensity");
        }
        public void Test()
        {
            Rectangle          rec = new Rectangle(100, 10000, 500, 400);
            RectangleTransform rt  = new RectangleTransform(rec, 2000, 2000);

            Assert.AreEqual(100 + 250, rt.GetTransformX(1000));
            Assert.AreEqual(10000 + 400 - 100, rt.GetTransformY(500));
        }
        public void Draw(Graphics g2, Rectangle rec, IIdentifiedPeptideResult sr)
        {
            g2.CompositingQuality = CompositingQuality.HighQuality;
            g2.SmoothingMode      = SmoothingMode.AntiAlias;
            g2.TextRenderingHint  = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;

            Rectangle newRec = new Rectangle(rec.Left + 60, rec.Top, rec.Width - 80, rec.Height - 50 - bigFont.Height);

            Pen blackPen = new Pen(Color.Black, 1);

            Rectangle spectrumRec = GetSpectrumRectangle(g2, sr.ExperimentalPeakList, newRec);

            var maxMz =
                (from peak in sr.ExperimentalPeakList
                 orderby peak.Mz descending
                 select peak.Mz).FirstOrDefault();

            double maxMzWidth = ((int)maxMz / 100 + 1) * 100.0;

            int yFontShift = (int)(g2.MeasureString("M", bigFont).Height / 2);

            RectangleTransform rt = new RectangleTransform(spectrumRec, maxMzWidth, sr.ExperimentalPeakList.FindMaxIntensityPeak().Intensity);

            DrawXScale(g2, blackPen, spectrumRec, rt);

            DrawYScale(g2, blackPen, spectrumRec);

            foreach (MatchedPeak peak in sr.ExperimentalPeakList)
            {
                Brush brush;
                Pen   pen;
                if (peak.Matched)
                {
                    Color color = IonColorMap[peak.PeakType];
                    brush = new SolidBrush(color);
                    pen   = new Pen(brush);
                }
                else
                {
                    brush = Brushes.Black;
                    pen   = blackPen;
                }

                int x = rt.GetTransformX(peak.Mz);

                int y = rt.GetTransformY(peak.Intensity);

                g2.DrawLine(pen, new Point(x, spectrumRec.Bottom), new Point(x, y));

                if (peak.Matched)
                {
                    DrawVerticalIonName(g2, brush, x - yFontShift, y, peak.Information);
                }
            }
        }
        private static Rectangle GetSpectrumRectangle(Graphics g2, PeakList <MatchedPeak> peaks, Rectangle rec)
        {
            MatchedPeak maxIntensityPeak = peaks.FindMaxIntensityPeak();

            MatchedPeak highestPeak = maxIntensityPeak;

            RectangleTransform rt = new RectangleTransform(rec, 1.0, highestPeak.Intensity);
            int top = rt.GetTransformY(highestPeak.Intensity);
            int highestTextLength = 0;

            if (highestPeak.Matched)
            {
                highestTextLength = 10 + (int)GetStringWidth(g2, bigFont, highestPeak.Information);
                top -= highestTextLength;
            }

            foreach (MatchedPeak peak in peaks)
            {
                if (!peak.Matched)
                {
                    continue;
                }

                int textLength  = 10 + (int)GetStringWidth(g2, bigFont, peak.Information);
                int totalLength = rt.GetTransformY(peak.Intensity) - textLength;

                if (totalLength < top)
                {
                    highestTextLength = textLength;
                    top         = totalLength;
                    highestPeak = peak;
                }
            }

            int peakIntensityLength = rec.Height - highestTextLength;
            int recHeight           = (int)(peakIntensityLength * maxIntensityPeak.Intensity / highestPeak.Intensity);

            return(new Rectangle(rec.Left, rec.Bottom - recHeight, rec.Width, recHeight));
        }
        private static void DrawXScale(Graphics g2, Pen blackPen, Rectangle spectrumRec, RectangleTransform rt)
        {
            int fontHeight = (int)g2.MeasureString("M", bigFont).Height;

            g2.DrawLine(blackPen, new Point(spectrumRec.Left, spectrumRec.Bottom), new Point(spectrumRec.Right, spectrumRec.Bottom));
            for (int i = 0; i < 10000; i += 50)
            {
                int curMz = rt.GetTransformX(i);
                if (curMz > spectrumRec.Right)
                {
                    break;
                }

                if (i % 100 == 0)
                {
                    g2.DrawLine(blackPen, curMz, spectrumRec.Bottom, curMz, spectrumRec.Bottom + 10);
                    string number   = i.ToString();
                    float  strWidth = GetStringWidth(g2, bigFont, number);
                    g2.DrawString(number, bigFont, Brushes.Black, new PointF(curMz - strWidth / 2, spectrumRec.Bottom + 10));
                }
                else
                {
                    g2.DrawLine(blackPen, curMz, spectrumRec.Bottom, curMz, spectrumRec.Bottom + 5);
                }
            }

            float mzWidth = GetStringWidth(g2, bigFont, "M/Z");

            g2.DrawString("M/Z", bigFont, Brushes.Black, new PointF((spectrumRec.Right + spectrumRec.Left - mzWidth) / 2, spectrumRec.Bottom + 10 + fontHeight));
        }