Scales / Labels / Scale Labels Generation
In This Topic
    Scale Labels Generation
    In This Topic

    Standard scales (numeric, ordinal, date/time etc.) by default generate an automatic label per each major tick in a single scale level. You can control the process of automatic label generation using the TextProvider, NumberOfTicksPerLabel and LabelGenerationMode properties of the scale's Labels child (NScaleLabels).

     Customize the Text of the Labels

    To customize the text of the labels, use the TextProvider property of the scale's Labels child. This is typically used with ordinal scales:

    Customize the text of the labels
    Copy Code
    string[] labelTexts = new string[] { "Label 1", "Label 2", "Label 3" };
    NOrdinalScale scale = (NOrdinalScale)chart.Axes[ENCartesianAxis.PrimaryX].Scale;
    scale.Labels.TextProvider = new NOrdinalScaleLabelTextProvider(labelTexts);
    

    The following is a complete example that creates a bar chart showing the programming languages popularity:

    Complete example - bar chart with custom texts on the X axis
    Copy Code
    NChartViewSurface chartSurface = chartView.Content;
    chartSurface.CreatePredefinedChart(ENPredefinedChartType.Cartesian);
    
    // Set the title
    chartSurface.Titles[0].Text = "Standard Bar";
    
    // Configure the chart
    NCartesianChart chart = (NCartesianChart)chartSurface.Charts[0];
    chart.SetPredefinedCartesianAxes(ENPredefinedCartesianAxis.XOrdinalYLinear);
    
    // Add an interlace stripe
    NLinearScale linearScale = 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.Shadow = new NShadow(NColor.LightGray, 2, 2);
    
    // 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"));
    chart.Series.Add(bar);
    
    // Configure the X axis to show the language names
    string[] labels = new string[bar.DataPoints.Count];
    for (int i = 0; i < bar.DataPoints.Count; i++)
    {
        labels[i] = bar.DataPoints[i].Label;
    }
    
    NOrdinalScale xAxisScale = (NOrdinalScale)chart.Axes[ENCartesianAxis.PrimaryX].Scale;
    xAxisScale.Labels.TextProvider = new NOrdinalScaleLabelTextProvider(labels);
    

    The code above creates the following chart:

    For more information on stripes see the Scale Strip Lines topic.

     Ticks per Label

    By default the scale will create a label for each major tick on the scale. In some cases however you may want labels to be less dense than ticks - in such cases you can change the NumberOfTicksPerLabel property allowing you to create scales where labels skip major ticks. The following code snippet will configure the axis to display one label for each two ticks:

    Set the number of ticks per label
    Copy Code
    scale.Labels.NumberOfTicksPerLabel = 2;
    
     Label Generation Mode

    Often when you have densely populated scales the labels overlap. By default the scale is configured to resolve label overlapping by staggering and auto scaling texts. However this can often be unpractical, because the scale will change its size when you have overlapping and when not. To avoid this you can consider to force the scale to always generate staggered labels regardless of whether the labels overlap or not. This is done with the help of the LabelGenerationMode property:

    Set the label generation mode
    Copy Code
    scale.Labels.LabelGenerationMode = ENLabelGenerationMode.Stagger2;
    

    The following table lists the available options:

    ENLabelGenerationMode Description
    SingleLevel All labels are generated in one scale level
    Stagger2 Labels are generated in two scale levels, thus producing a two level staggering effect
    Stagger3 Labels are generated in three scale levels, thus producing a three level staggering effect