The C1Chart LightSwitch Extension provides various options to customize the various charts that that can be rendered with this extension. While most customization can be done easily at design time, but for some we need to write a little bit of code to get what we want. Displaying Labels on a Pie Chart perfectly fits this scenario , so lets have a look at how we can get this done in 2 easy steps.

  1. Displaying a Label on a Pie Chart only requires rendering the Y value for a DataPoint. However, in order to display the Y value we need to create a Label Template. The label template can also be customized to set the Color, Size, Formatting for the Label.
  2. Assign the Label Template to the PointLabelTemplate for each DataSeries in the Chart.

Here is small code snippet that implements the above mentioned steps.


using System;  
using Microsoft.LightSwitch;  
using Microsoft.LightSwitch.Framework.Client;  
using Microsoft.LightSwitch.Presentation;  
using Microsoft.LightSwitch.Presentation.Extensions;  
using System.Windows;  
using C1.Silverlight.Chart;  
using System.Windows.Markup;  

namespace LightSwitchApplication  
{  
  public partial class Chart  
  {  

    string template = @"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:c1=""http://schemas.componentone.com/winfx/2006/xaml""><Border c1:PlotElement.LabelAlignment=""MiddleCenter"" Background=""LightGray"" BorderThickness=""1"" BorderBrush=""Black""><TextBlock> <Run Text=""{Binding Name}"" /><Run Text=""{Binding Path=Value,StringFormat=p1}"" /></TextBlock></Border></DataTemplate>";  
    partial void ChartMultipleSeries_Created()  
    {  
      // Provide programmatic access to the underlying C1.Silverlight.Chart.C1Chart control:  
      var chart = this.FindControl("C1Chart");  
      chart.ControlAvailable += new EventHandler<ControlAvailableEventArgs>(chart_ControlAvailable);  
    }  

    void chart_ControlAvailable(object sender, ControlAvailableEventArgs e)  
    {  
      var c1chart = (C1.Silverlight.Chart.C1Chart)e.Control;  
      // TODO: write code against ComponentOne Chart for Silverlight here  

      if (c1chart == null)  
       return;  

      DataTemplate lbl = null;  

      lbl = XamlReader.Load(template) as DataTemplate;  

      c1chart.BeginUpdate();  
      foreach (DataSeries ds in c1chart.Data.Children)  
      {  
        ds.PointLabelTemplate = lbl;  
      }  
      c1chart.EndUpdate();  
    }  
  }  
}