Exemplo n.º 1
0
 protected double ParseT(double d, bool IncludeOffset)
 {
   ConvertEventArgsEx e = new ConvertEventArgsEx(d, typeof(double), IncludeOffset);
   if (TempParser != null)
     TempParser(this, e);
   return (double)e.Value;
 }
Exemplo n.º 2
0
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics gfx = e.Graphics;

            Pen axisPen = new Pen(this.ForeColor);
            Brush textBrush = new SolidBrush(this.ForeColor);
            StringFormat sfmt = new StringFormat();
            sfmt.Alignment = StringAlignment.Center;
            sfmt.LineAlignment = StringAlignment.Near;

            RectangleF plotRect = PlotRect(gfx);

            gfx.SmoothingMode = SmoothingMode.None; //Axes look worse when AA'd.

            //Then: Draw the axes:
            //(For now, we will simply draw lines without arrows - to be upgraded in future.)

            gfx.DrawLine(axisPen, new PointF(plotRect.Left, plotRect.Bottom), new PointF(plotRect.Right, plotRect.Bottom));
            gfx.DrawLine(axisPen, new PointF(plotRect.Left, plotRect.Bottom), new PointF(plotRect.Left, plotRect.Top));

            #region X-axis
            {
                FormatterParserProvider prov = null;
                if (!string.IsNullOrEmpty(m_sXUnits) && m_Formatters != null && m_Formatters.ContainsKey(m_sXUnits))
                    prov = m_Formatters[m_sXUnits];

                //Determine if we can fit (MaxChecks) into the graph:
                ConvertEventArgs ceaMaxX = new ConvertEventArgs(m_dMaxXValue, typeof(double));
                if (prov != null) prov.Formatter(this, ceaMaxX);
                string s = ((double)ceaMaxX.Value).ToString("0");

                float sWidth = gfx.MeasureString(s, this.Font).Width;
                int maxPossibleChecks = (int)(plotRect.Width / sWidth) + 1;
                int numChecks = maxPossibleChecks > m_nMaxChecks ? m_nMaxChecks : maxPossibleChecks;
                bool labelChecks = numChecks > 1;

                if (numChecks < 2) numChecks = 2;

                float XFact = plotRect.Width / (numChecks - 1);
                double XFact2 = Range / (numChecks - 1);

                for (int i = 0; i < numChecks; i++)
                {
                    PointF checkTop = new PointF(plotRect.Left + XFact * i, plotRect.Bottom);
                    PointF checkBottom = new PointF(checkTop.X, checkTop.Y + m_nCheckSize);
                    gfx.DrawLine(axisPen, checkTop, checkBottom);
                    if (labelChecks)
                    {
                        ConvertEventArgs val = new ConvertEventArgs(m_dMinXValue + XFact2 * i, typeof(double));
                        if (prov != null)
                            prov.Formatter(this, val);
                        string label = ((double)val.Value).ToString("0");
                        PointF textTop = new PointF(checkBottom.X, checkBottom.Y + m_nSpacer);
                        gfx.DrawString(label, Font, textBrush, textTop, sfmt);
                    }
                }

                if (m_sXLabel != null && !string.IsNullOrEmpty(m_sXLabel.Trim()))
                {
                    PointF textTop = new PointF((plotRect.Left + plotRect.Right) / 2,
                        plotRect.Bottom + m_nCheckSize + 2 * m_nSpacer + gfx.MeasureString("fg", Font).Height);
                    string units = prov != null ? " (" + prov.CurrentCnv + ")" : "";
                    gfx.DrawString(m_sXLabel + units, Font, textBrush, textTop, sfmt);
                }
            }
            #endregion X-Axis

            #region Y-Axis
            GraphSeries selectedSeries = null;
            foreach (GraphSeries gs in m_Datasets)
                if (gs.Selected)
                {
                    selectedSeries = gs; break;
                }

            if (m_bDrawYLabel && selectedSeries != null && !float.IsNaN(selectedSeries.Min) && !float.IsNaN(selectedSeries.Max)
                &&!float.IsInfinity(selectedSeries.Min) && !float.IsInfinity(selectedSeries.Max))
            {
                FormatterParserProvider prov = null;
                if (!string.IsNullOrEmpty(selectedSeries.Units))
                    prov = m_Formatters[selectedSeries.Units];

                Brush textBrush2 = new SolidBrush(selectedSeries.DefaultColour);
                Pen yAxisPen = new Pen(textBrush2);

                sfmt.Alignment = StringAlignment.Far;
                sfmt.LineAlignment = StringAlignment.Center;
                float maxTextWidth = 0;

                if (selectedSeries.Logarithmic)
                #region Logarithmic
                {
                    if (selectedSeries.Min == selectedSeries.Max)
                    {
                        PointF checkRight = new PointF(plotRect.Left, (plotRect.Bottom + plotRect.Top) / 2);
                        PointF checkLeft = new PointF(checkRight.X - m_nCheckSize, checkRight.Y);
                        gfx.DrawLine(yAxisPen, checkRight, checkLeft);
                        ConvertEventArgsEx val = new ConvertEventArgsEx(selectedSeries.Min, typeof(float), false);
                        if (prov != null) prov.Formatter(this, val);
                        string label = ((float)val.Value).ToString("G2");
                        PointF textRight = new PointF(checkLeft.X - m_nSpacer, checkLeft.Y);
                        maxTextWidth = gfx.MeasureString(label, Font).Width;
                        gfx.DrawString(label, Font, textBrush2, textRight, sfmt);
                    } //Single Check
                    else
                    {
                        float graphMin = selectedSeries.Min * selectedSeries.Max > 0 ? selectedSeries.Min : selectedSeries.Max * 1E-6f;
                        ConvertEventArgsEx ceaGraphMin = new ConvertEventArgsEx(graphMin, typeof(double), false);
                        if (prov != null) prov.Formatter(this, ceaGraphMin);
                    
                        bool seriesNegative = graphMin < 0;

                        ConvertEventArgs ceaMin = new ConvertEventArgsEx(seriesNegative ? -selectedSeries.Max : graphMin, typeof(double), false);
                        if (prov != null) prov.Formatter(this, ceaMin);
                        int min = (int)Math.Floor(Math.Log10((double)ceaMin.Value));

                        ConvertEventArgs ceaMax = new ConvertEventArgsEx(seriesNegative ? -graphMin : selectedSeries.Max, typeof(double), false);
                        if (prov != null) prov.Formatter(this, ceaMax);
                        int max = (int)Math.Floor(Math.Log10((double)ceaMax.Value));

                        for (int i = max; i > min; i--)
                        {
                            if (!seriesNegative && Math.Pow(10, i) < (double)ceaGraphMin.Value)
                                continue;
                            else if (seriesNegative && -Math.Pow(10, i) < (double)ceaGraphMin.Value)
                                continue;

                            float y = plotRect.Bottom - plotRect.Height * (float)((i - Math.Log10((seriesNegative ? -1 : 1) * (double)ceaGraphMin.Value)) / Math.Log10((double)ceaMax.Value / (double)ceaGraphMin.Value));
                            PointF checkRight = new PointF(plotRect.Left, y);
                            PointF checkLeft = new PointF(checkRight.X - m_nCheckSize, checkRight.Y);
                            gfx.DrawLine(yAxisPen, checkRight, checkLeft);
                            string label = (seriesNegative ? "-" : "") + "10^" + i;
                            PointF textRight = new PointF(checkLeft.X - m_nSpacer, checkLeft.Y);
                            gfx.DrawString(label, Font, textBrush2, textRight, sfmt);
                            float f = gfx.MeasureString(label, Font).Width;
                            if (f > maxTextWidth)
                                maxTextWidth = f;
                        }
                    }
                }
                #endregion Logarithmic
                else
                #region Linear
                {
                    double SeriesRange = selectedSeries.Max - selectedSeries.Min;

                    
                    float sHeight = gfx.MeasureString("0", this.Font).Height;
                    int maxPossibleChecks = (int)(plotRect.Height / sHeight) + 1;

                    int numChecks = maxPossibleChecks > m_nMaxChecks ? m_nMaxChecks : maxPossibleChecks;

                    bool labelChecks = numChecks > 1;

                    if (numChecks < 2) numChecks = 2;

                    float YFact = plotRect.Height / (numChecks - 1);
                    double YFact2 = SeriesRange / (numChecks - 1);

                    if (SeriesRange != 0)
                    {
                        for (int i = 0; i < numChecks; i++)
                        {
                            PointF checkRight = new PointF(plotRect.Left, plotRect.Bottom - YFact * i);
                            PointF checkLeft = new PointF(checkRight.X - m_nCheckSize, checkRight.Y);
                            gfx.DrawLine(yAxisPen, checkRight, checkLeft);
                            if (labelChecks)
                            {
                                //G2 seems to love to show 100 as 1E2...
                                ConvertEventArgs ceaVal = new ConvertEventArgs(selectedSeries.Min + YFact2 * i, typeof(double));
                                if (prov != null) prov.Formatter(this, ceaVal);
                                double val = (double)ceaVal.Value;
                                string label;
                                if (Math.Abs(val) < 10000 && Math.Abs(val) > 10)
                                    label = val.ToString("F0");
                                else
                                    label = val.ToString("G2");
                                PointF textRight = new PointF(checkLeft.X - m_nSpacer, checkLeft.Y);
                                gfx.DrawString(label, Font, textBrush2, textRight, sfmt);
                                float f = gfx.MeasureString(label, Font).Width;
                                if (f > maxTextWidth)
                                    maxTextWidth = f;
                            }
                        }
                    }
                    else //Draw one check in the centre...
                    {
                        PointF checkRight = new PointF(plotRect.Left, (plotRect.Bottom + plotRect.Top) / 2);
                        PointF checkLeft = new PointF(checkRight.X - m_nCheckSize, checkRight.Y);
                        gfx.DrawLine(yAxisPen, checkRight, checkLeft);

                        ConvertEventArgs ceaVal = new ConvertEventArgs(selectedSeries.Min, typeof(double));
                        if (prov != null) prov.Formatter(this, ceaVal);
                        double val = (double)ceaVal.Value;
                        string label;
                        if (Math.Abs(val) < 10000 && Math.Abs(val) > 10)
                            label = val.ToString("F0");
                        else
                            label = val.ToString("G2");

                        PointF textRight = new PointF(checkLeft.X - m_nSpacer, checkLeft.Y);
                        gfx.DrawString(label, Font, textBrush2, textRight, sfmt);
                        maxTextWidth = gfx.MeasureString(label, Font).Width;
                    }
                }
                #endregion Linear

                if (m_bDrawYLabel)
                {
                    StringFormat ylblFmt = new StringFormat();
                    ylblFmt.Alignment = StringAlignment.Center;
                    ylblFmt.LineAlignment = StringAlignment.Far;

                    float left = plotRect.Left - m_nCheckSize - 2 * m_nSpacer - maxTextWidth;
                    if (left - gfx.MeasureString("0", Font).Height > 0)
                    {
                        gfx.TranslateTransform(left, (plotRect.Bottom + plotRect.Top) / 2);
                        gfx.RotateTransform(-90);
                        string units = prov != null ? " (" + prov.CurrentCnv + ")" : "";
                        gfx.DrawString(selectedSeries.Name + units, Font, textBrush2, 0, 0, ylblFmt);
                        gfx.ResetTransform();
                    }
                }
            }
            #endregion Y-Axis

            gfx.SmoothingMode = SmoothingMode.AntiAlias;

            //First: Draw the unselected items:
            foreach (GraphSeries d in m_Datasets)
                if (!d.Selected)
                    DrawDataSet(d, gfx, plotRect, 2 * m_Datasets.IndexOf(d));
            //Then, draw the selected items:
            foreach (GraphSeries d in m_Datasets)
                if (d.Selected)
                    DrawDataSet(d, gfx, plotRect, 2 * m_Datasets.IndexOf(d));

            base.OnPaint(e);
        }