This is a migrated thread and some comments may be shown as answers.

Can't get label format to work in radcartesianchart

2 Answers 86 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
Scott
Top achievements
Rank 1
Scott asked on 24 Feb 2014, 10:48 PM
I'm dynamically binding a few stacked BarSeries objects to a RadCartesianChart as follows:

​ BarSeries seriesBar = new BarSeries
{
    CategoryBinding = new PropertyNameDataPointBinding() { PropertyName = "seriesName" },
ValueBinding = new GenericDataPointBinding<BarChartBarSeriesViewModel, decimal?>() { ValueSelector = selector => selector.segments[j].value },
ItemsSource = this.barSeries,
CombineMode = Telerik.Charting.ChartSeriesCombineMode.Stack,
DefaultVisualStyle = styleSegment,
ShowLabels = true
};

// Center labels within bar
seriesBar.LabelDefinitions.Clear();
seriesBar.LabelDefinitions.Add(new ChartSeriesLabelDefinition { VerticalAlignment = System.Windows.VerticalAlignment.Center, Format="c" });


The ValueBinding for each series is a decimal? object.  I'm trying to apply formatting to the labels within the bars by adding a new LabelDefinition object to the series (see last line of code).  The vertical centering of the label within each bar works great but the Format="c" does not work.  It causes every label to show as "c".  How do I display each bar segment's label as currency?

2 Answers, 1 is accepted

Sort by
0
Scott
Top achievements
Rank 1
answered on 25 Feb 2014, 03:03 PM
I solved this problem as shown below.  If there's a better way, I would love to hear it.

// Add handler for data binding complete
seriesBar.DataBindingComplete += seriesBar_DataBindingComplete;
...

​ // Format each label by hand
void seriesBar_DataBindingComplete(object sender, EventArgs e)
{
if (null != sender)
{
   BarSeries bar = (BarSeries)sender;
    if (null != bar.DataPoints)
    {
       foreach (CategoricalDataPoint cat in bar.DataPoints)
       {
          if (null != cat.Value)
          {
             cat.Label = ((double)cat.Value).ToString("$###,###,###,###");
          }
       }
    }
}
}
0
Martin Ivanov
Telerik team
answered on 26 Feb 2014, 05:09 PM
Hi Scott,

In order to fix this issue you will need to use a different formatting string. In your case the string should be "{0:c}" instead of only "c".
seriesBar.LabelDefinitions.Add(new ChartSeriesLabelDefinition { VerticalAlignment = System.Windows.VerticalAlignment.Center, Format = "{0:c}" });

However, I recommend you to use a SeriesProvider which helps you to dynamically create ChartSeries in an MVVM scenario. You can read more about the descriptors in the Dynamic Number of Series help article.

However I attached a sample project which demonstrates creating of dynamic number of series in a RadCartesianChart. We hope you may find it helpful.

Regards,
Martin
Telerik
Tags
ChartView
Asked by
Scott
Top achievements
Rank 1
Answers by
Scott
Top achievements
Rank 1
Martin Ivanov
Telerik team
Share this question
or