Beispiel #1
0
 public void ConvertNulls_ShouldFail()
 {
     Assert.ThrowsException <ArgumentException>(() => NumericConverter.ConvertData(null, null));
 }
        protected void CheckSettingsAndFirstRow(DataTable dt, ResultSetComparisonSettings settings)
        {
            if (dt.Rows.Count == 0)
                return;

            var dr = dt.Rows[0];
            for (int i = 0; i < dr.Table.Columns.Count; i++)
            {
                if (!dr.IsNull(i))
                {
                    if (settings.GetColumnType(i) == ColumnType.Numeric && IsNumericField(dr.Table.Columns[i]))
                        continue;

                    var numericConverter = new NumericConverter();
                    if (settings.GetColumnType(i) == ColumnType.Numeric && !(numericConverter.IsValid(dr[i]) || BaseComparer.IsValidInterval(dr[i])))
                    {
                        var exception = string.Format("The column with an index of {0} is expecting a numeric value but the first row of your result set contains a value '{1}' not recognized as a valid numeric value or a valid interval."
                            , i, dr[i].ToString());

                        if (numericConverter.IsValid(dr[i].ToString().Replace(",", ".")))
                            exception += " Aren't you trying to use a comma (',' ) as a decimal separator? NBi requires that the decimal separator must be a '.'.";

                        throw new ResultSetComparerException(exception);
                    }

                    if (settings.GetColumnType(i) == ColumnType.DateTime && IsDateTimeField(dr.Table.Columns[i]))
                        return;

                    if (settings.GetColumnType(i) == ColumnType.DateTime && !BaseComparer.IsValidDateTime(dr[i].ToString()))
                    {
                        throw new ResultSetComparerException(
                            string.Format("The column with an index of {0} is expecting a date & time value but the first row of your result set contains a value '{1}' not recognized as a valid date & time value."
                                , i, dr[i].ToString()));
                    }
                }
            }
        }
Beispiel #3
0
        public void ConvertSameUnitsWithTwoPrefixes_ShouldNotFail()
        {
            var result = NumericConverter.ConvertData("1 kilobyte", "terabyte");

            Assert.AreEqual(result, "0.000000001 terabyte");
        }
Beispiel #4
0
        public void ConvertDifferentUnitsWithTwoPrefixes_ShouldNotFail()
        {
            var result = NumericConverter.ConvertData("13.2 kilobit", "gigabyte");

            Assert.AreEqual(result, "0.00000165 gigabyte");
        }
Beispiel #5
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            NumericConverter converter = new NumericConverter(Convert.ToInt32(DesiredBase.Text));

            ConvertedValue.Text = converter.Convert(Convert.ToInt32(NumberToConvert.Text));
        }
Beispiel #6
0
        public void CreateConverterTest()
        {
            NumericConverter converter = new NumericConverter();

            Assert.IsNotNull(converter);
        }
Beispiel #7
0
        public void CreateConverterWithValueTest(string value)
        {
            NumericConverter converter = new NumericConverter(value);

            Assert.AreEqual(value, converter.Value);
        }
Beispiel #8
0
        public void Draw(Graphics g, RectangleF drawingRectangle)
        {
            if (yAxisNumericConverter == null)
            {
                return;
            }
            if (yAxisScaleCreator == null)
            {
                return;
            }

            Brush fontBrush = new SolidBrush(fontColor);

            g.SmoothingMode     = SmoothingMode.AntiAlias;
            g.TextRenderingHint = TextRenderingHint.AntiAlias;

            // calculate caption sizes
            SizeF xAxisCaptionSize = g.MeasureString(xAxisCaption, font);
            SizeF yAxisCaptionSize = g.MeasureString(yAxisCaption, font);

            // calculate max x axis label size
            switch (xAxisAttribute)
            {
            case DomainAttribute.TimeOfDay:
                xAxisNumericConverter = new TimeConverter(TimeConverter.TimeConverterType.TimeOfDay);
                XAxisScaleCreator     = new TimeScaleCreator(xAxisMinValue, xAxisMaxValue, 15, false);
                break;

            case DomainAttribute.ElapsedTime:
                xAxisNumericConverter = new TimeConverter(TimeConverter.TimeConverterType.ElapsedTime);
                XAxisScaleCreator     = new TimeScaleCreator(xAxisMinValue, xAxisMaxValue, 15, false);
                break;

            case DomainAttribute.Distance:
                xAxisNumericConverter = new NumericConverter();
                XAxisScaleCreator     = new DoubleScaleCreator(xAxisMinValue, xAxisMaxValue, 15, false);
                break;
            }

            SizeF maxXAxisLabelSize = SizeF.Empty;

            for (int i = 0; i < xAxisScaleCreator.NoOfMarkers; i++)
            {
                double value     = xAxisScaleCreator.MarkerValue(i);
                string label     = xAxisNumericConverter.ToString(value);
                SizeF  labelSize = g.MeasureString(label, font);
                maxXAxisLabelSize.Width  = Math.Max(maxXAxisLabelSize.Width, labelSize.Width);
                maxXAxisLabelSize.Height = Math.Max(maxXAxisLabelSize.Height, labelSize.Height);
            }

            // calculate max y axis label size
            SizeF maxYAxisLabelSize = SizeF.Empty;

            for (int i = 0; i < yAxisScaleCreator.NoOfMarkers; i++)
            {
                double value     = yAxisScaleCreator.MarkerValue(i);
                string label     = yAxisNumericConverter.ToString(value);
                SizeF  labelSize = g.MeasureString(label, font);
                maxYAxisLabelSize.Width  = Math.Max(maxYAxisLabelSize.Width, labelSize.Width);
                maxYAxisLabelSize.Height = Math.Max(maxYAxisLabelSize.Height, labelSize.Height);
            }

            // calculate graph drawing rectangle
            float leftMargin = Math.Max(1.5F * yAxisCaptionSize.Height + maxYAxisLabelSize.Width, maxXAxisLabelSize.Width / 2);
            int   width      = (int)(drawingRectangle.Width - leftMargin - maxXAxisLabelSize.Width / 2 - 1);
            int   height     = (int)(drawingRectangle.Height - maxYAxisLabelSize.Height / 2 - maxXAxisLabelSize.Height - xAxisCaptionSize.Height - 1);

            graphDrawingRectangle = new Rectangle(
                (int)(drawingRectangle.Left + leftMargin),
                (int)(drawingRectangle.Top + maxYAxisLabelSize.Height / 2),
                Math.Max(width, 1),
                Math.Max(height, 1));

            // draw graph background
            g.FillRectangle(lineGraphBackgroundBrush, GraphDrawingRectangle);

            // draw the axis labels
            // x axis
            for (int i = 0; i < xAxisScaleCreator.NoOfMarkers; i++)
            {
                double value     = xAxisScaleCreator.MarkerValue(i);
                int    x         = (int)Math.Round(ValueToX(value));
                string label     = xAxisNumericConverter.ToString(value);
                SizeF  labelSize = g.MeasureString(label, font);
                g.DrawString(label, font, fontBrush, new PointF(x - labelSize.Width / 2, GraphDrawingRectangle.Bottom));
                g.DrawLine(gridPen, new Point(x, GraphDrawingRectangle.Top), new Point(x, GraphDrawingRectangle.Bottom));
            }
            // y axis
            for (int i = 0; i < yAxisScaleCreator.NoOfMarkers; i++)
            {
                double value     = yAxisScaleCreator.MarkerValue(i);
                int    y         = (int)Math.Round(ValueToY(value));
                string label     = yAxisNumericConverter.ToString(value);
                SizeF  labelSize = g.MeasureString(label, font);
                g.DrawString(label, font, fontBrush, new PointF(GraphDrawingRectangle.Left - labelSize.Width, y - maxYAxisLabelSize.Height / 2));
                g.DrawLine(gridPen, new Point(GraphDrawingRectangle.Left, y), new Point(GraphDrawingRectangle.Right, y));
            }

            // draw the axis captions
            // x axis
            PointF xAxisCaptionLocation = new PointF(
                GraphDrawingRectangle.Left + (GraphDrawingRectangle.Width - xAxisCaptionSize.Width) / 2,
                GraphDrawingRectangle.Bottom + xAxisCaptionSize.Height);

            g.DrawString(xAxisCaption, font, fontBrush, xAxisCaptionLocation);
            // y axis, rotated 90 degrees
            PointF yAxisCaptionLocation = new PointF(
                GraphDrawingRectangle.Left - 3 * yAxisCaptionSize.Height,
                GraphDrawingRectangle.Bottom - (GraphDrawingRectangle.Height - yAxisCaptionSize.Width) / 2);

            g.ResetTransform();
            g.TranslateTransform(-yAxisCaptionLocation.X, -yAxisCaptionLocation.Y, MatrixOrder.Append);
            g.RotateTransform(-90F, MatrixOrder.Append);
            g.TranslateTransform(yAxisCaptionLocation.X, yAxisCaptionLocation.Y, MatrixOrder.Append);
            g.DrawString(yAxisCaption, font, fontBrush, yAxisCaptionLocation);
            g.ResetTransform();

            // create the line points
            var polygonList = new List <List <PointF> >();

            if (pointsList != null)
            {
                foreach (var points in pointsList)
                {
                    GraphicsPath path    = new GraphicsPath();
                    var          polygon = new List <PointF> {
                        new PointF(ValueToX(points[0].X), GraphDrawingRectangle.Bottom)
                    };
                    foreach (LineGraphPoint p in points)
                    {
                        polygon.Add(new PointF(ValueToX(p.X), ValueToY(p.Y)));
                    }
                    polygon.Add(new PointF(ValueToX(points[points.Count - 1].X), GraphDrawingRectangle.Bottom));
                    path.AddPolygon(polygon.ToArray());
                    polygonList.Add(polygon);
                    g.SetClip(path);

                    RouteLineSettings rls = session.Settings.RouteLineSettingsCollection[yAxisAttribute];

                    // draw the gradient filling
                    Bitmap   templateBitmap     = new Bitmap(32, GraphDrawingRectangle.Height);
                    Graphics templateGraphics   = Graphics.FromImage(templateBitmap);
                    double   colorRangeInterval = rls.ColorRange.EndValue - rls.ColorRange.StartValue;
                    if (colorRangeInterval == 0)
                    {
                        colorRangeInterval = 0.00001;
                    }
                    rls.ColorRange.Gradient.Draw(
                        templateGraphics,
                        new Rectangle(0, 0, templateBitmap.Width, templateBitmap.Height),
                        (yAxisMinValue - rls.ColorRange.StartValue) / colorRangeInterval,
                        1 + (yAxisMaxValue - rls.ColorRange.EndValue) / colorRangeInterval,
                        Gradient.Direction.ReverseVertical);
                    templateGraphics.Dispose();
                    for (int i = 0; i < GraphDrawingRectangle.Width; i += templateBitmap.Width)
                    {
                        g.DrawImage(templateBitmap, GraphDrawingRectangle.Left + i, GraphDrawingRectangle.Top);
                    }
                }
            }

            g.SetClip(GraphDrawingRectangle);

            // draw lap lines
            foreach (Lap lap in session.Laps)
            {
                double xValue = 0;
                switch (xAxisAttribute)
                {
                case DomainAttribute.TimeOfDay:
                    xValue = (double)lap.Time.ToLocalTime().Ticks / TimeSpan.TicksPerSecond;
                    break;

                case DomainAttribute.ElapsedTime:
                    xValue =
                        session.Route.GetAttributeFromParameterizedLocation(
                            WaypointAttribute.ElapsedTime,
                            session.Route.GetParameterizedLocationFromTime(lap.Time)).Value;
                    break;

                case DomainAttribute.Distance:
                    xValue =
                        session.Route.GetAttributeFromParameterizedLocation(
                            WaypointAttribute.Distance,
                            session.Route.GetParameterizedLocationFromTime(lap.Time)).Value;
                    break;
                }

                int x = (int)Math.Round(ValueToX(xValue));
                g.DrawLine(lapPen, x, graphDrawingRectangle.Top, x, graphDrawingRectangle.Bottom);
            }

            // draw the line above the gradient area
            foreach (var polygon in polygonList)
            {
                g.DrawLines(linePen, polygon.ToArray());
            }
            g.ResetClip();
            g.DrawRectangle(borderPen, GraphDrawingRectangle);

            // dispose drawing objects
            fontBrush.Dispose();
        }
        private void Draw()
        {
            if (backBufferGraphics == null || preventRedraw)
            {
                return;
            }

            // use backbuffering
            Graphics g = backBufferGraphics;

            // clear background
            g.Clear(BackColor);

            // gradient
            double intervalLength = (colorRange.EndValue - colorRange.StartValue);
            double startLocation  = 0.0 - (colorRange.StartValue - minValue) / intervalLength;
            double endLocation    = 1.0 + (maxValue - colorRange.EndValue) / intervalLength;

            Gradient.FillCheckerboardRectangle(g, colorRangeRectangle, 8);
            ColorRange.Gradient.Draw(g, colorRangeRectangle, startLocation, endLocation, Gradient.Direction.Horizontal, alphaAdjustment);

            // start value slider
            DrawSlider(new Point(ValueToX(colorRange.StartValue), colorRangeRectangle.Bottom + 1));

            // end value slider
            DrawSlider(new Point(ValueToX(colorRange.EndValue), colorRangeRectangle.Bottom + 1));

            // border
            g.DrawRectangle(Pens.Gray, new Rectangle(colorRangeRectangle.Left - 1, colorRangeRectangle.Top - 1, colorRangeRectangle.Width + 1, colorRangeRectangle.Height + 1));

            // marker lines and labels
            SizeF labelSize;
            int   startY;

            if (NumericConverter == null)
            {
                labelSize = new SizeF(0F, 0F);
                startY    = colorRangeRectangle.Top;
            }
            else
            {
                labelSize = g.MeasureString(NumericConverter.ToString(scaleCreator.LastMarkerValue) + "    ", Font);
                startY    = colorRangeRectangle.Top + (int)labelSize.Height;
            }
            float lastLabelX = (float)colorRangeRectangle.Left - labelSize.Width / 2;

            for (double value = scaleCreator.FirstMarkerValue; value <= scaleCreator.LastMarkerValue; value += scaleCreator.MarkerInterval)
            {
                Pen p = new Pen(Color.FromArgb(192, Color.Black));
                g.DrawLine(
                    p,
                    (int)ValueToX(value),
                    startY,
                    ValueToX(value),
                    (int)colorRangeRectangle.Bottom
                    );
                p.Dispose();
                if (NumericConverter != null)
                {
                    float labelX = (float)ValueToX(value);
                    // only draw label if it is enough space to the last one
                    if (labelX > lastLabelX + labelSize.Width && labelX + labelSize.Width / 2 < (float)colorRangeRectangle.Right)
                    {
                        g.DrawString(
                            "  " + NumericConverter.ToString(value),
                            Font,
                            Brushes.Black,
                            new PointF(labelX - labelSize.Width / 2, (float)colorRangeRectangle.Top)
                            );
                        lastLabelX = labelX;
                    }
                }
            }

            // copy to screen
            sliderPanel.CreateGraphics().DrawImageUnscaled(backBufferBitmap, new Point(0, 0));
        }