Can't get label format to work in radcartesianchart

3 posts, 0 answers
  1. Scott
    Scott avatar
    11 posts
    Member since:
    Oct 2012

    Posted 24 Feb 2014 Link to this post

    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. Scott
    Scott avatar
    11 posts
    Member since:
    Oct 2012

    Posted 25 Feb 2014 in reply to Scott Link to this post

    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("$###,###,###,###");
              }
           }
        }
    }
    }
  3. DevCraft banner
  4. Martin
    Admin
    Martin avatar
    1099 posts

    Posted 26 Feb 2014 Link to this post

    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
Back to Top