Axis Labels Formatting Example for WPF

Axis Labels Formatting Example for WPF

Nevron NOV Chart for WPF ships with hundreds of examples with full source code, as part of the Nevron Open Vision Suite. All WPF Chart examples are also available as a stand-alone Visual Studio solution in C# and VB.NET, which you can download from the links below.

Axis Labels Formatting Example for WPF Description

This example demonstrates how to apply different formatting to axis labels.


Axis Labels Formatting Example

C# Axis Labels Formatting Example Code for Nevron Chart for WPF
Language: C#

using Nevron.Nov;
using Nevron.Nov.Chart;
using Nevron.Nov.Dom;
using Nevron.Nov.Graphics;
using Nevron.Nov.Layout;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System.Windows.Forms;

namespace NovWinFormsProject
{
	public partial class Form1 : Form
	{
		#region Constructors

		public Form1()
		{
			InitializeComponent();

			// Set form title
			Text = "NOV WinForms - NAxisLabelsFormattingExample";

			// Create the NOV example and its controls
			NWidget exampleContent = CreateExampleContent();
			NWidget exampleControls = CreateExampleControls();
			if (exampleControls != null)
			{
				exampleControls.PreferredWidth = 300;
			}

			// Place the example and its controls in a pair box
			NPairBox pairBox = new NPairBox(exampleContent, exampleControls);
			pairBox.FillMode = ENStackFillMode.First;
			pairBox.FitMode = ENStackFitMode.First;
			pairBox.Margins = new NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing);

			// Host the example in the form
			NNovWidgetHost host = new NNovWidgetHost(pairBox);
			host.Dock = DockStyle.Fill;
			Controls.Add(host);
		}

		#endregion

		#region Example

		public NWidget CreateExampleContent()
		{
			NChartViewWithCommandBars chartViewWithCommandBars = new NChartViewWithCommandBars();
			NChartView chartView = chartViewWithCommandBars.View;
			chartView.Surface.CreatePredefinedChart(ENPredefinedChartType.Cartesian);

			// configure title
			chartView.Surface.Titles[0].Text = "Axis Labels Formatting";

			// configure chart
			m_Chart = (NCartesianChart)chartView.Surface.Charts[0];

			m_Chart.SetPredefinedCartesianAxes(ENPredefinedCartesianAxis.XOrdinalYLinear);

			// add interlace stripe
			NLinearScale linearScale = m_Chart.Axes[ENCartesianAxis.PrimaryY].Scale as NLinearScale;
			NScaleStrip strip = new NScaleStrip(new NColorFill(ENNamedColor.Beige), null, true, 0, 0, 1, 1);
			strip.Interlaced = true;
			linearScale.Strips.Add(strip);

			// setup a bar series
			NBarSeries bar = new NBarSeries();
			bar.Name = "Bar Series";
			bar.InflateMargins = true;
			bar.UseXValues = false;
			bar.DataLabelStyle = new NDataLabelStyle(false);

			// add some data to the bar series
			bar.LegendView.Mode = ENSeriesLegendMode.DataPoints;
			bar.DataPoints.Add(new NBarDataPoint(18, "C++"));
			bar.DataPoints.Add(new NBarDataPoint(15, "Ruby"));
			bar.DataPoints.Add(new NBarDataPoint(21, "Python"));
			bar.DataPoints.Add(new NBarDataPoint(23, "Java"));
			bar.DataPoints.Add(new NBarDataPoint(27, "Javascript"));
			bar.DataPoints.Add(new NBarDataPoint(29, "C#"));
			bar.DataPoints.Add(new NBarDataPoint(26, "PHP"));

			m_Chart.Series.Add(bar);

			return chartViewWithCommandBars;
		}
		public NWidget CreateExampleControls()
		{
			NStackPanel stack = new NStackPanel();
			NUniSizeBoxGroup boxGroup = new NUniSizeBoxGroup(stack);

			NNumericUpDown yAxisDecimalPlaces = new NNumericUpDown();
			yAxisDecimalPlaces.Minimum = 0;
			yAxisDecimalPlaces.ValueChanged += OnYAxisDecimalPlacesValueChanged;
			stack.Add(NPairBox.Create("Y Axis Decimal Places:", yAxisDecimalPlaces));

			NCheckBox useCustomXAxisLabels = new NCheckBox("Use Custom X Axis Labels");
			useCustomXAxisLabels.CheckedChanged += OnUseCustomXAxisLabelsCheckedChanged;
			stack.Add(useCustomXAxisLabels);

			return boxGroup;
		}
		public string GetExampleDescription()
		{
			return @"

This example demonstrates how to apply different formatting to axis labels.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Scale Labels Formatting", "https://www.nevron.com/docs/openvision/scale-labels-formatting.html"), ("Scale Labels Generation", "https://www.nevron.com/docs/openvision/scale-labels-generation.html"), ("Scale Common Label Settings", "https://www.nevron.com/docs/openvision/scale-label-settings.html"), ("Label Formatting", "https://www.nevron.com/docs/openvision/chart-label-formatting.html"), }; } #endregion #region Implementation #endregion #region Event Handlers void OnUseCustomXAxisLabelsCheckedChanged(NValueChangeEventArgs arg) { NOrdinalScale ordinalScale = (NOrdinalScale)m_Chart.Axes[ENCartesianAxis.PrimaryX].Scale; if (((NCheckBox)arg.TargetNode).Checked) { ordinalScale.Labels.TextProvider = new NOrdinalScaleLabelTextProvider(new string[] { "C++", "Ruby", "Python", "Java", "Javascript", "C#", "PHP"}); } else { ordinalScale.Labels.TextProvider = new NFormattedScaleLabelTextProvider(new NNumericValueFormatter(ENNumericValueFormat.Arabic)); } } void OnYAxisDecimalPlacesValueChanged(NValueChangeEventArgs arg) { int decimalPlaces = (int)((NNumericUpDown)arg.TargetNode).Value; string format = "."; for (int i = 0; i < decimalPlaces; i++) { format += "0"; } NLinearScale linearScale = (NLinearScale)m_Chart.Axes[ENCartesianAxis.PrimaryY].Scale; linearScale.Labels.TextProvider = new NFormattedScaleLabelTextProvider(new NNumericValueFormatter(format)); } #endregion #region Fields NCartesianChart m_Chart; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Axis Labels Formatting Example for WPF Solution

VB.NET Axis Labels Formatting Example Code for Nevron Chart for WPF
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.Chart
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Graphics
Imports Nevron.Nov.Layout
Imports Nevron.Nov.UI
Imports Nevron.Nov.Windows.Wpf

Class MainWindow

#Region "Constructors"

	Public Sub New()
		' This call is required by the designer.
		InitializeComponent()

		' Add any initialization after the InitializeComponent() call.

		' Set form title
		Title = "NOV WPF - NAxisLabelsFormattingExample"

		' Create the NOV example and its controls
		Dim exampleContent As NWidget = CreateExampleContent()
		Dim exampleControls As NWidget = CreateExampleControls()
		If Not exampleControls Is Nothing Then
			exampleControls.PreferredWidth = 300
		End If

		' Place the example and its controls in a pair box
		Dim pairBox As NPairBox = New NPairBox(exampleContent, exampleControls)
		pairBox.FillMode = ENStackFillMode.First
		pairBox.FitMode = ENStackFitMode.First
		pairBox.Margins = New NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing)

		' Host the example in the form
		Dim host As NNovWidgetHost(Of NWidget) = New NNovWidgetHost(Of NWidget)(pairBox)
		Content = host
	End Sub


#End Region

#Region "Example"

        Public Function CreateExampleContent() As NWidget
            Dim chartViewWithCommandBars As NChartViewWithCommandBars = New NChartViewWithCommandBars()
            Dim chartView = chartViewWithCommandBars.View
            chartView.Surface.CreatePredefinedChart(ENPredefinedChartType.Cartesian)

            ' configure title
            chartView.Surface.Titles(0).Text = "Axis Labels Formatting"

            ' configure chart
            m_Chart = CType(chartView.Surface.Charts(0), NCartesianChart)

            m_Chart.SetPredefinedCartesianAxes(ENPredefinedCartesianAxis.XOrdinalYLinear)

            ' add interlace stripe
            Dim linearScale As NLinearScale = TryCast(m_Chart.Axes(ENCartesianAxis.PrimaryY).Scale, NLinearScale)
            Dim strip As NScaleStrip = New NScaleStrip(New NColorFill(ENNamedColor.Beige), Nothing, True, 0, 0, 1, 1)
            strip.Interlaced = True
            linearScale.Strips.Add(strip)

            ' setup a bar series
            Dim bar As NBarSeries = New NBarSeries()
            bar.Name = "Bar Series"
            bar.InflateMargins = True
            bar.UseXValues = False
            bar.DataLabelStyle = New NDataLabelStyle(False)

            ' add some data to the bar series
            bar.LegendView.Mode = ENSeriesLegendMode.DataPoints
            bar.DataPoints.Add(New NBarDataPoint(18, "C++"))
            bar.DataPoints.Add(New NBarDataPoint(15, "Ruby"))
            bar.DataPoints.Add(New NBarDataPoint(21, "Python"))
            bar.DataPoints.Add(New NBarDataPoint(23, "Java"))
            bar.DataPoints.Add(New NBarDataPoint(27, "Javascript"))
            bar.DataPoints.Add(New NBarDataPoint(29, "C#"))
            bar.DataPoints.Add(New NBarDataPoint(26, "PHP"))

            m_Chart.Series.Add(bar)

            Return chartViewWithCommandBars
        End Function
        Public Function CreateExampleControls() As NWidget
            Dim stack As NStackPanel = New NStackPanel()
            Dim boxGroup As NUniSizeBoxGroup = New NUniSizeBoxGroup(stack)

            Dim yAxisDecimalPlaces As NNumericUpDown = New NNumericUpDown()
            yAxisDecimalPlaces.Minimum = 0
            yAxisDecimalPlaces.ValueChanged += AddressOf OnYAxisDecimalPlacesValueChanged
            stack.Add(NPairBox.Create("Y Axis Decimal Places:", yAxisDecimalPlaces))

            Dim useCustomXAxisLabels As NCheckBox = New NCheckBox("Use Custom X Axis Labels")
            useCustomXAxisLabels.CheckedChanged += AddressOf OnUseCustomXAxisLabelsCheckedChanged
            stack.Add(useCustomXAxisLabels)

            Return boxGroup
        End Function
        Public Function GetExampleDescription() As String
            Return "

This example demonstrates how to apply different formatting to axis labels.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Scale Labels Formatting", "https://www.nevron.com/docs/openvision/scale-labels-formatting.html"), ("Scale Labels Generation", "https://www.nevron.com/docs/openvision/scale-labels-generation.html"), ("Scale Common Label Settings", "https://www.nevron.com/docs/openvision/scale-label-settings.html"), ("Label Formatting", "https://www.nevron.com/docs/openvision/chart-label-formatting.html")} End Function #End Region #Region "Implementation" #End Region #Region "Event Handlers" Private Sub OnUseCustomXAxisLabelsCheckedChanged(arg As NValueChangeEventArgs) Dim ordinalScale As NOrdinalScale = m_Chart.Axes(ENCartesianAxis.PrimaryX).Scale If CType(arg.TargetNode, NCheckBox).Checked Then ordinalScale.Labels.TextProvider = New NOrdinalScaleLabelTextProvider(New String() {"C++", "Ruby", "Python", "Java", "Javascript", "C#", "PHP"}) Else ordinalScale.Labels.TextProvider = New NFormattedScaleLabelTextProvider(New NNumericValueFormatter(ENNumericValueFormat.Arabic)) End If End Sub Private Sub OnYAxisDecimalPlacesValueChanged(arg As NValueChangeEventArgs) Dim decimalPlaces As Integer = CType(arg.TargetNode, NNumericUpDown).Value Dim format = "." For i = 0 To decimalPlaces - 1 format += "0" Next Dim linearScale As NLinearScale = m_Chart.Axes(ENCartesianAxis.PrimaryY).Scale linearScale.Labels.TextProvider = New NFormattedScaleLabelTextProvider(New NNumericValueFormatter(format)) End Sub #End Region #Region "Fields" Private m_Chart As NCartesianChart #End Region #Region "Static Methods" #End Region #Region "Constants" #End Region #Region "Nested Types" #End Region End Class

Download a fully functional VB.NET (.vb) Axis Labels Formatting Example for WPF Solution