Nevron Forum

X-Axis labels on Bar-charts

https://www.nevron.com/Forum/Topic2469.aspx

By Johan Carlsson - Wednesday, July 8, 2009

Hello,

In your Bar-chart gallery you have an example chart with "Tomatoes", "Oranges", "Bananas" etc. etc.. The X-Axis of this chart displays 1, 2, 3.. I was wondering if it is possible to make the X-Axis display "Tomatoes", "Oranges" instead of numbers.. And if so, how do you do it?

Regards, Johan
By David Huxtable - Tuesday, July 14, 2009

Good Morning Johan,

I believe All Examples -> Axes -> General -> Axes Labels contains what you are looking for.

Ensure you have set the AutoLabels property of your scale configurator to false.

Next execute your scaleconfigurator's Clear() method to ensure the labels of the x axis are removed and 1, 2, 3...etc are not displayed.

Finally add your desired x axis label values to your scale configurator's Labels enumeration.

For example:

// Create a linear scale configurator object and apply it to the primary x axis.
NLinearScaleConfigurator scaleXConfig = (NLinearScaleConfigurator)chart.Axis(StandardAxis.PrimaryX).ScaleConfigurator;

// Ensure all your x-axis labels are displayed in the chart.
scaleConfigX.MajorTickMode = MajorTickMode.AutoMaxCount;

// Disable auto labels (this creates your 1, 2, 3..etc as far as I understand).
scaleConfigX.AutoLabels = false;

// Add your custom labels.
scaleConfigX.Labels.Add("Tomatoes");
scaleConfigX.Labels.Add("Oranges");
scaleConfigX.Labels.Add("Bananas");

I hope this post has helped Johan,

David.
By Blagovest Milanov 1 - Wednesday, July 15, 2009

Thank you David for answering this question. Jonah - sorry I did't reply earlier - must have missed the post...

By Johan Carlsson - Wednesday, July 15, 2009

It sure did, thanks a lot guys!

It possible that this is mentioned in the documentation (although I'm not able to find "All examples" neither locally on my computer nor on the Nevron page), but is it also possible to modify the color of these new X-labels?

Cheers, Johan

By Blagovest Milanov 1 - Wednesday, July 15, 2009

Hi Johan,

 

Yes - you can disable automatic labels and add custom ones, using a slightly different approach, which also allows you to have control over value, style, angle etc. The following code snippet shows how to achieve this:

 

NChart chart = nChartControl1.Charts[0];

 

NBarSeries bar = (NBarSeries)chart.Series.Add(SeriesType.Bar);

bar.Values.Add(10);

bar.Values.Add(20);

bar.Values.Add(30);

 

NOrdinalScaleConfigurator scale = chart.Axis(StandardAxis.PrimaryX).ScaleConfigurator as NOrdinalScaleConfigurator;

scale.AutoLabels = false;

 

NCustomValueLabel valLabel1 = new NCustomValueLabel();

valLabel1.Value = 0;

valLabel1.Text = "Red";

valLabel1.Style.TextStyle.FillStyle = new NColorFillStyle(Color.Red);

scale.CustomLabels.Add(valLabel1);

 

NCustomValueLabel valLabel2 = new NCustomValueLabel();

valLabel2.Value = 1;

valLabel2.Text = "Green";

valLabel2.Style.TextStyle.FillStyle = new NColorFillStyle(Color.Green);

scale.CustomLabels.Add(valLabel2);

 

NCustomValueLabel valLabel3 = new NCustomValueLabel();

valLabel3.Value = 2;

valLabel3.Text = "Blue";

valLabel3.Style.TextStyle.FillStyle = new NColorFillStyle(Color.Blue);

scale.CustomLabels.Add(valLabel3);

Hope this helps - let me know if you meet any problems.

 

Best regards,
Bob