/// <summary> /// Sets the numerical markers on the x and y axis. /// </summary> /// <param name="numAxisX">number of markers on x axis</param> /// <param name="numAxisY">number of markers on y axis</param> /// <param name="axisX">function to transform index value to any marker</param> /// <param name="axisY">function to transform value to any marker</param> /// <returns>this Builder for chaining</returns> /// <exception cref="ArgumentException">if any argument is negative</exception> public Builder SetAxisMarkers(int numAxisX, int numAxisY, Func <int, string> axisX, Func <float, string> axisY) { NumMarkersAxisX = ValidationUtil.RequireNonNegative(numAxisX, $"numAxisX({numAxisX}) cannot be negative."); NumMarkersAxisY = ValidationUtil.RequireNonNegative(numAxisY, $"numAxisY({numAxisY}) cannot be negative."); AxisX = ValidationUtil.RequireNonNull(axisX); AxisY = ValidationUtil.RequireNonNull(axisY); return(this); }
/// <summary> /// Sets a grid in the graph. /// <para> /// Note that the last call to any AddGrid method is applied, rest is ignored. /// </para> /// </summary> /// <param name="grid">the grid lines information</param> /// <param name="numVertical">number of vertical lines</param> /// <param name="numHorizontal">number of horizontal lines</param> /// <returns>this Builder for chaining</returns> /// <exception cref="ArgumentException">if grid is null</exception> public Builder SetGrid(LineInfo grid, int numVertical, int numHorizontal) { Grid = ValidationUtil.RequireNonNull(grid); NumVertical = ValidationUtil.RequireNonNegative(numVertical, $"numAxisY({numVertical}) cannot be negative."); NumHorizontal = ValidationUtil.RequireNonNegative(numHorizontal, $"numHorizontal({numHorizontal}) cannot be negative."); AgainstMarkersX = false; AgainstMarkersY = false; return(this); }
/// <summary> /// Sets the number of lines. This will result in crash if they cannot fit. Give -1 to mean as many as possible. /// <para> /// Defaults to -1 and a .5 thickness line with RGB(66, 66, 66) with width of 1f. /// </para> /// </summary> /// <param name="numLines">the number of lines</param> /// <param name="lineWidth">the width of the lines</param> /// <param name="lineInfo">the line info</param> /// <returns>this Builder for chaining</returns> public Builder SetLines(int numLines, float lineWidth, LineInfo lineInfo) { if (numLines != -1) { ValidationUtil.RequireNonNegative(numLines, $"numLines({numLines}) must be greater or equal to -1"); } ValidationUtil.RequireBetween(lineWidth, 0, 1, $"lineWidth({lineWidth}) must be in the interval [0, 1]"); ValidationUtil.RequireNonNull(lineInfo); NumLines = numLines; LineWidth = lineWidth; LineInfo = lineInfo; return(this); }
/// <summary> /// Adds axis limits to the graph and thus to all lines added. Note that the xAxis is indexes that acts as a "valid" /// index window for all lines. /// </summary> /// <param name="xAxis">the limits on the x-axis; defaults to null, meaning to fit the lines</param> /// <param name="yAxis">the limits on the y-axis; defaults to null, meaning to fit the lines</param> /// <returns>this Builder for chaining</returns> /// <exception cref="ArgumentException">if any lower limit is equal or greater than the upper limit or lower x axis limit is negative</exception> public Builder SetAxisLimits(Tuple <int, int> xAxis, Tuple <float, float> yAxis) { if (xAxis != null) { ValidationUtil.RequireNonNegative(xAxis.Item1, $"Lower x-axis limit({xAxis.Item1}) cannot be negative."); ValidationUtil.RequirePositive(xAxis.Item2 - xAxis.Item1, $"Upper x-axis limit({xAxis.Item2}) must be greater than lower x-axis limit({xAxis.Item1})."); } if (yAxis != null) { ValidationUtil.RequirePositive(yAxis.Item2 - yAxis.Item1, $"Upper y-axis limit({yAxis.Item2}) must be greater than lower y-axis limit({yAxis.Item1})."); } AxisLimitX = xAxis; AxisLimitY = yAxis; return(this); }
/// <summary> /// Sets the numerical markers on the x and y axis. /// </summary> /// <param name="numAxisX">number of markers on x axis</param> /// <param name="numAxisY">number of markers on y axis</param> /// <returns>this Builder for chaining</returns> /// <exception cref="ArgumentException">if any argument is negative</exception> public Builder SetAxisMarkers(int numAxisX, int numAxisY) { NumMarkersAxisX = ValidationUtil.RequireNonNegative(numAxisX, $"numAxisX({numAxisX}) cannot be negative."); NumMarkersAxisY = ValidationUtil.RequireNonNegative(numAxisY, $"numAxisY({numAxisY}) cannot be negative."); return(SetAxisMarkers(numAxisX, numAxisY, i => { return i.ToString("0", System.Globalization.CultureInfo.CurrentCulture); }, value => { var marker = value.ToString("0.###", System.Globalization.CultureInfo.CurrentCulture); if (value > 1f) { marker = value.ToString("0.#", System.Globalization.CultureInfo.CurrentCulture); } return marker; })); }
/// <inheritdoc cref="IPlottable"/> public void Plot(ICanvas canvas) { canvas.SetFont(TextInfo.Type, TextInfo.Style, TextInfo.Size); var textHeight = canvas.GetTextHeight(Text); var textWidth = canvas.GetTextWidth(Text); ValidationUtil.RequireBetween(textWidth, 0f, 1f, $"the text is to wide with a relative width of {textWidth}."); ValidationUtil.RequireBetween(textHeight, 0f, 1f, $"the text is to high with a relative height of {textHeight}."); var textBox = PointPair.NewInstance(0.5f - textWidth / 2, 0.5f - textHeight / 2, 0.5f + textWidth / 2, 0.5f + textHeight / 2); var x = 0.5f - textWidth / 2; var y = 0.5f - textHeight / 2; if (StartAt != null) { ValidationUtil.RequireBetween(StartAt.Item1 + textWidth, 0f, 1f, $"x({StartAt.Item1}) + textWidth({textWidth}) must be less than or equal to 1."); ValidationUtil.RequireBetween(StartAt.Item2 + textHeight, 0f, 1f, $"y({StartAt.Item2}) + textHeight({textHeight}) must be less than or equal to 1."); x = StartAt.Item1; y = StartAt.Item2; textBox = PointPair.NewInstance(x, y, x + textWidth, y + textHeight); if (canvas.GetModeParam() == ModeParam.Calibration || canvas.GetModeParam() == ModeParam.BoxedCalibration) { canvas.SetColor(Colors.Red); canvas.DrawRectangle(textBox); } } else if (CenterIn != null) { ValidationUtil.RequireNonNegative(CenterIn.Width - textWidth, $"CenterIn.Width({CenterIn.Width}) cannot be less than textWidth({textWidth})."); ValidationUtil.RequireNonNegative(CenterIn.Height - textHeight, $"CenterIn.Height({CenterIn.Height}) cannot be less than textHeight({textHeight})."); x = CenterIn.FromX + CenterIn.Width / 2 - textWidth / 2; y = CenterIn.FromY + CenterIn.Height / 2 - textHeight / 2; textBox = CenterIn; if (canvas.GetModeParam() == ModeParam.Calibration || canvas.GetModeParam() == ModeParam.BoxedCalibration) { canvas.SetColor(Colors.Red); canvas.DrawRectangle(CenterIn); } } if (!IsGravity(GravityType.None)) { if (IsGravity(GravityType.Top) && !IsGravity(GravityType.Bottom, GravityType.Top)) { textBox = PointPair.NewInstance(textBox.FromX, 0, textBox.ToX, textBox.Height); y = textBox.FromY + textBox.Height / 2 - textHeight / 2; } if (IsGravity(GravityType.Bottom) && !IsGravity(GravityType.Bottom, GravityType.Top)) { textBox = PointPair.NewInstance(textBox.FromX, 1 - textBox.Height, textBox.ToX, 1f); y = textBox.FromY + textBox.Height / 2 - textHeight / 2; } if (IsGravity(GravityType.Left) && !IsGravity(GravityType.Left, GravityType.Right)) { textBox = PointPair.NewInstance(0f, textBox.FromY, textBox.Width, textBox.ToY); x = textBox.FromX + textBox.Width / 2 - textWidth / 2; } if (IsGravity(GravityType.Right) && !IsGravity(GravityType.Left, GravityType.Right)) { textBox = PointPair.NewInstance(1 - textBox.Width, textBox.FromY, 1f, textBox.ToY); x = textBox.FromX + textBox.Width / 2 - textWidth / 2; } if (canvas.GetModeParam() == ModeParam.Calibration || canvas.GetModeParam() == ModeParam.BoxedCalibration) { canvas.SetColor(Colors.Green); canvas.DrawRectangle(textBox); } } if (x.CompareTo(0f) < 0) { x = 0; } if (y.CompareTo(0f) < 0) { y = 0; } canvas.SetColor(TextInfo.Color); if (canvas.GetModeParam() == ModeParam.Calibration || canvas.GetModeParam() == ModeParam.BoxedCalibration) { canvas.SetColor(Color.Black); } canvas.SetFont(TextInfo.Type, TextInfo.Style, TextInfo.Size); canvas.WriteText(Text, x, y + textHeight * 0.85f); }
public void Test_RequireNonNegative_3() { int val = -1; ValidationUtil.RequireNonNegative(val, ""); }