Add a label on top of each row for floatBar chart


https://www.nevron.com/Forum/Topic4860.aspx
Print Topic | Close Window

By Clare Moore - 13 Years Ago

I have a (horizontal) float bar chart, and i have some values that i'd like to display in labels on top of the bars.

Because my data is grouped, each row is actually made up of quite a few datapoints (otherwise i'd just use the data label).

Is there a way to display one label for each row, on the exact location of each row??

I have tried writing some code that figures out how many categories i have, and tried to position labels by calculating where they should go for each category - as a percentage of the total height, but this doesnt seem to be working out.

By Nevron Support - 13 Years Ago

Hi Clare,

Can you post the code for review?

By Clare Moore - 13 Years Ago

Here is my code, ignore the label values i'm hard coding them for now

for (int i = 0; i < bar.BeginValues.Count; i++)

{

float val = (100 / bar.BeginValues.Count) * i;

NLabel title = new NLabel("7:00:00");

title.TextStyle.FontStyle = new NFontStyle("Arial", 10, FontStyle.Italic);

title.Location = new NPointL(new NLength(3, NRelativeUnit.ParentPercentage), new NLength(val, NRelativeUnit.ParentPercentage));

title.TextStyle.BorderStyle.Width = new NLength(0);

title.Margins = new NMarginsL(0, 0, 0, 0);

title.ContentAlignment = ContentAlignment.MiddleCenter;

chart.ChildPanels.Add(title);

NLabel othertitle = new NLabel("8:12:00");

othertitle.TextStyle.FontStyle = new NFontStyle("Arial", 10, FontStyle.Italic);

othertitle.Location = new NPointL(new NLength(50, NRelativeUnit.ParentPercentage), new NLength(val, NRelativeUnit.ParentPercentage));

othertitle.Margins = new NMarginsL(0, 0, 0, 0);

othertitle.TextStyle.BorderStyle.Width = new NLength(0);

othertitle.ContentAlignment = ContentAlignment.MiddleCenter;

chart.ChildPanels.Add(othertitle);

}

}

 

What i was hoping to achieve was for a label to appear over each bar but it ends up looking like the attached image

By Nevron Support - 13 Years Ago

Hi Clare,

You should populate either the axis labels or the data labels series for each bar - for example:

// 1. Modify category labels of primary x axis

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

if (ordinalScale != null)

{

ordinalScale.AutoLabels = false;


// for each category add a label here

ordinalScale.Labels.Clear();

ordinalScale.Labels.Add("Category 1");

ordinalScale.Labels.Add("Category 2");

ordinalScale.Labels.Add("Category 3");

}

// 2. Modify data labels per data point

NBarSeries barSeries = new NBarSeries();

barSeries.Labels.Clear();

barSeries.Labels[0] = "Some Label For Bar 1";

barSeries.Labels[1] = "Some Label For Bar 2";

// etc.

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