We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to change the ScaleLabel numerical value into string representation in WinForms LinearGauge?

Platform: WinForms |
Control: LinearGauge |
Published Date: September 4, 2018 |
Last Revised Date: January 5, 2020 |
.NET Framework: 4

Customization of LinearGauge

In LinearGauge, there is no direct option to format the ScaleLabel input into string representation and this requirement can be achieved by using its CustomRenderer support, which helps to customize each element of LinearGauge control.

 

For Example,

Consider ScaleLabel numerical value “1” need to be formatted into string representation like “One”. Following steps will guide in this process.

 

  1. Define new Custom Renderer class inherited from ILinearGaugeRenderer Interface.
  2. Implement all necessary functions for Custom Rendering purpose in LinearGauge.
  3. Perform the required Number to string formatting in DrawLines function.
  4. Apply Custom Renderer class to LinearGauge.

 

The following code demonstrates the same.

C#

//Codes in CustomRenderer Class
//Custom Renderer class inherited from ILinearGaugeRenderer
public class CustomRenderer : ILinearGaugeRenderer
{
public void DrawLines(Graphics Graphics)
{
if (LinearGauge.LinearFrameType == LinearFrameType.Vertical)
{
if (LinearGauge.ShowScaleLabel)
{
Graphics.DrawString(this.NumberToStringRepresentation((int)Math.Round(majortickValue, 
2)), LinearGauge.Font, brush, new PointF(x - LinearGauge.MajorTicksHeight - 25, 
this.LinearGauge.Height - tickPosition), sf);
}
}
else
{
if (LinearGauge.ShowScaleLabel)
Graphics.DrawString(Math.Round(majortickValue, 2).ToString(), LinearGauge.Font, brush, new PointF(x - LinearGauge.MajorTicksHeight - 25, this.LinearGauge.Height - tickPosition), sf);
}
}
}
//To convert number as String
public string NumberToStringRepresentation(int n)
{
if (n < 0)
return "Minus " + NumberToStringRepresentation(-n);
else if (n == 0)
return "";
else if (n <= 19)
return new string[] {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",
"Seventeen", "Eighteen", "Nineteen"}[n - 1] + " ";
else if (n <= 99)
return new string[] {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy",
"Eighty", "Ninety"}[n / 10 - 2] + " " + NumberToStringRepresentation(n % 10);
else if (n <= 199)
return "One Hundred " + NumberToStringRepresentation(n % 100);
else if (n <= 999)
return NumberToStringRepresentation(n / 100) + "Hundreds " + NumberToStringRepresentation(n % 
100);
else if (n <= 1999)
return "One Thousand " + NumberToStringRepresentation(n % 1000);
else if (n <= 999999)
return NumberToStringRepresentation(n / 1000) + "Thousands " + NumberToStringRepresentation(n % 1000);
else if (n <= 1999999)
return "One Million " + NumberToStringRepresentation(n % 1000000);
else if (n <= 999999999)
return NumberToStringRepresentation(n / 1000000) + "Millions " + NumberToStringRepresentation(n % 
1000000);
else if (n <= 1999999999)
return "One Billion " + NumberToStringRepresentation(n % 1000000000);
else
return NumberToStringRepresentation(n / 1000000000) + "Billions " + NumberToStringRepresentation(n % 1000000000);
}
//Updates the Render
public void UpdateRenderer(PaintEventArgs PaintEventArgs)
{
DrawLines(PaintEventArgs.Graphics);
}
//Codes in Form1.cs 
//LinearGauge customization properties
this.linearGauge1.MinimumValue = 10;
this.linearGauge1.MaximumValue = 50;
this.linearGauge1.MajorDifference = 10;
this.linearGauge1.OuterFrameGradientEndColor = Color.Gray;
this.linearGauge1.OuterFrameGradientStartColor = Color.Gray;
this.linearGauge1.InnerFrameGradientStartColor = Color.Gray;
this.linearGauge1.InnerFrameGradientEndColor = Color.Gray;
//Apply LinearGauge into custom renderer
this.linearGauge1.Renderer = new CustomRenderer(this.linearGauge1);

VB

‘Codes in CustomRenderer Class
‘Custom Renderer class inherited from ILinearGaugeRenderer
Public Class CustomRenderer Implements ILinearGaugeRenderer
Public Sub DrawLines(ByVal Graphics As Graphics) Implements ILinearGaugeRenderer.DrawLines
If LinearGauge.LinearFrameType = LinearFrameType.Vertical Then
If LinearGauge.ShowScaleLabel Then
Graphics.DrawString(Me.NumberToStringRepresentation(CInt(Math.Truncate(Math.Round(majortickValue, 2)))), LinearGauge.Font, brush, New PointF(x - LinearGauge.MajorTicksHeight - 25, Me.LinearGauge.Height - tickPosition), sf)
End If
Else
If LinearGauge.ShowScaleLabel Then
Graphics.DrawString(Math.Round(majortickValue, 2).ToString(), LinearGauge.Font, brush, New PointF(tickPosition, y - 10), sf)
End If
End Sub
‘To convert number as String
Public Function NumberToStringRepresentation(ByVal n As Integer) As String
If n < 0 Then
Return "Minus " & NumberToStringRepresentation(-n)
ElseIf n = 0 Then
Return ""
ElseIf n <= 19 Then
Return New String() {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}(n - 1) & " "
ElseIf n <= 99 Then
Return New String() {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}(n \ 10 - 2) & " " & NumberToStringRepresentation(n Mod 10)
ElseIf n <= 199 Then
Return "One Hundred " & NumberToStringRepresentation(n Mod 100)
ElseIf n <= 999 Then
Return NumberToStringRepresentation(n \ 100) & "Hundreds " & NumberToStringRepresentation(n Mod 100)
ElseIf n <= 1999 Then
Return "One Thousand " & NumberToStringRepresentation(n Mod 1000)
ElseIf n <= 999999 Then
Return NumberToStringRepresentation(n \ 1000) & "Thousands " & NumberToStringRepresentation(n Mod 1000)
ElseIf n <= 1999999 Then
Return "One Million " & NumberToStringRepresentation(n Mod 1000000)
ElseIf n <= 999999999 Then
Return NumberToStringRepresentation(n \ 1000000) & "Millions " & NumberToStringRepresentation(n Mod 1000000)
ElseIf n <= 1999999999 Then
Return "One Billion " & NumberToStringRepresentation(n Mod 1000000000)
Else
Return NumberToStringRepresentation(n \ 1000000000) & "Billions " & NumberToStringRepresentation(n Mod 1000000000)
End If
End Function
‘Updates the Render
Public Sub UpdateRenderer(ByVal PaintEventArgs As PaintEventArgs) Implements ILinearGaugeRenderer.UpdateRenderer
DrawLines(PaintEventArgs.Graphics)
End Sub
‘Codes in Form1.cs 
‘LinearGauge customization properties 
Me.linearGauge1.MinimumValue = 10
Me.linearGauge1.MaximumValue = 50
Me.linearGauge1.MajorDifference = 10
Me.linearGauge1.MinorTickMarkColor = Color.DarkBlue
Me.linearGauge1.MajorTickMarkColor = Color.Maroon
Me.linearGauge1.ScaleColor = Color.FromArgb(255, 128, 0)
‘Apply LinearGauge into custom renderer
Me.linearGauge1.Renderer = New CustomRenderer(Me.linearGauge1)

 

Without Formatting

Show the scale without format

 

With Formatting

Show the scale with formatting

Samples:

C#:  LinearGaugeSample

VB:  LinearGaugeSample

Reference link: https://help.syncfusion.com/windowsforms/radial-gauge/linear-gauge

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile