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 format the ScaleLabel text in WinForms RadialGauge?

Platform: WinForms |
Control: RadialGauge |
Published Date: August 10, 2018 |
Last Revised Date: January 7, 2020 |
.NET Framework: 4

RadialGauge formatting text

In RadialGauge ScaleLabel value will be calculated and rendered based on MajorDifference with respect to its MinimumValue and MaximumValue. But when loading multiple digit values like thousands in RadialGauge control, it will be better to format ScaleLabel value as like in Microsoft Excel.

For example

Representing scale value as 150K is nicer than 150000.

 

You can format the ScaleLabel value in RadialGauge by using its Custom Renderer support. Below steps helps to configure the Custom Renderer class to customize the RadialGauge appearance.

 

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

 

The following code demonstrates the same. 

C#

// Custom Renderer class inherited from IRadialGaugeRenderer
public class CustomRenderer : IRadialGaugeRenderer
{
  public void DrawTickMarks(Graphics Graphics, GraphicsPath GraphicsPath, Int32 
GaugeArcRadius, Int32 GaugeArcStart, Int32 GaugeArcEnd, Int32 MajorTickMarkWidth, 
Point Center, int ScaleNumbersRadius)
  {
    String valueText = "";
    if (RadialGauge.ShowScaleLabel)
    {
      if (this.RadialGauge.TextOrientation != TextOrientation.Horizontal)
      {
        Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
        Graphics.RotateTransform(90.0F + GaugeArcStart + countValue * GaugeArcEnd / (RadialGauge.MaximumValue - RadialGauge.MinimumValue));
      }
    Graphics.TranslateTransform((Single)(Center.X + ScaleNumbersRadius * Math.Cos((GaugeArcStart + countValue * GaugeArcEnd / (RadialGauge.MaximumValue - RadialGauge.MinimumValue)) * Math.PI / 180.0f)),
(Single)(Center.Y + ScaleNumbersRadius * Math.Sin((GaugeArcStart + countValue * GaugeArcEnd / (RadialGauge.MaximumValue - RadialGauge.MinimumValue)) * Math.PI / 180.0f)),
System.Drawing.Drawing2D.MatrixOrder.Append);
    if(valueText != "0")
    {
      switch(valueText)
      {
        case "1000":
             valueText = "1K";
             break;
        case "2000":
             valueText = "2K";
             break;
        case "3000":
             valueText = "3K";
             break;
        case "4000":
             valueText = "4K";
             break;
        case "5000":
             valueText = "5K";
             break;
        case "6000":
             valueText = "6K";
             break;
        case "7000":
             valueText = "7K";
             break;
        case "8000":
             valueText = "8K";
             break;
        case "9000":
             valueText = "9K";
             break;
        case "10000":
             valueText = "10K";
             break;
        case "11000":
             valueText = "11K";
             break;
        case "12000":
             valueText = "12K";
             break;
        case "13000":
             valueText = "13K";
             break;
        case "14000":
             valueText = "14K";
             break;
        case "15000":
             valueText = "15K";
             break;
      }
    }
    Graphics.DrawString(valueText, RadialGauge.Font, new SolidBrush(this.RadialGauge.ScaleLabelColor), -boundingBox.Width/4, -fontBoundY1, StringFormat.GenericTypographic);
  }
 }
}
CustomRenderer custom;custom = new CustomRenderer(radialGauge1);radialGauge1.MajorDifference = 1000;radialGauge1.MajorTickMarkHeight = 15;// Applying Custom Renderer in RadialGaugethis.radialGauge1.Renderer = custom;

VB

'Custom Renderer class inherited from IRadialGaugeRendererPublic Class CustomRenderer Implements IRadialGaugeRenderer  Public Sub DrawTickMarks(ByVal Graphics As Graphics, ByVal GraphicsPath As GraphicsPath, ByVal GaugeArcRadius As Int32, ByVal GaugeArcStart As Int32, ByVal GaugeArcEnd As Int32, ByVal MajorTickMarkWidth As Int32, ByVal Center As Point, ByVal ScaleNumbersRadius As Integer) Implements IRadialGaugeRenderer.DrawTickMarks
  Dim valueText As String = ""  If RadialGauge.ShowScaleLabel Then
    If Me.RadialGauge.TextOrientation <> TextOrientation.Horizontal Then
      Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias
      Graphics.RotateTransform(90.0F + GaugeArcStart + countValue * GaugeArcEnd / (RadialGauge.MaximumValue - RadialGauge.MinimumValue))
    End If
    Graphics.TranslateTransform(CSng(Center.X + ScaleNumbersRadius * Math.Cos((GaugeArcStart + countValue * GaugeArcEnd / (RadialGauge.MaximumValue - RadialGauge.MinimumValue)) * Math.PI / 180.0F)), CSng(Center.Y + ScaleNumbersRadius * Math.Sin((GaugeArcStart + countValue * GaugeArcEnd / (RadialGauge.MaximumValue - RadialGauge.MinimumValue)) * Math.PI / 180.0F)), System.Drawing.Drawing2D.MatrixOrder.Append)
    If valueText <> "0" Then
      Select Case valueText
         Case "1000"
              valueText = "1K"
         Case "2000"
              valueText = "2K"
         Case "3000"
              valueText = "3K"
         Case "4000"
              valueText = "4K"
         Case "5000"
              valueText = "5K"
         Case "6000"
              valueText = "6K"
         Case "7000"
              valueText = "7K"
         Case "8000"
              valueText = "8K"
         Case "9000"
              valueText = "9K"
         Case "10000"
              valueText = "10K"
         Case "11000"
              valueText = "11K"
         Case "12000"
              valueText = "12K"
         Case "13000"
              valueText = "13K"
         Case "14000"
              valueText = "14K"
         Case "15000"
              valueText = "15K"
      End Select
    End If
    Graphics.DrawString(valueText, RadialGauge.Font, New SolidBrush(Me.RadialGauge.ScaleLabelColor), -boundingBox.Width \ 4, -fontBoundY1, StringFormat.GenericTypographic)
  End If End SubEnd Class
Dim custom As CustomRenderercustom = New CustomRenderer(radialGauge1)radialGauge1.MajorDifference = 1000radialGauge1.MajorTickMarkHeight = 15'Applying Custom Renderer in RadialGaugeMe.radialGauge1.Renderer = custom

 

Screenshot
Without Formatting

Format the scalelabel text with formatting text

 

With Formatting

Format the scalelabel text without formatting text

 

Samples:

C#:  RadialGaugeFormatStringSample

VB:  RadialGaugeFormatStringSample

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