Tuesday, December 28, 2010

Dealing with Silverlight ChartPart

you need to add the following assembly references first!
xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"

xmlns:DV="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
xmlns:dataprimitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data">

XAML
//Add chart control in stack panel.
charting:Chart x:Name="SubscribeReportChart" Width="362" Height="338" >

charting:Chart.LegendStyle>
Style TargetType="Control">
Setter Property="Width" Value="0"/>
Setter Property="Height" Value="0"/>
/Style>
/charting:Chart.LegendStyle>

/charting:Chart>

XAML.cs

//Global declaration
ObservableCollection gChartDataCollection;
BackgroundWorker bwViewChart = new BackgroundWorker();
//MainPage
bwViewChart.DoWork += new DoWorkEventHandler(bwViewChart_DoWork);
bwViewChart.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwViewChart_RunWorkerCompleted);

void bwViewChart_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (gChartDataCollection.Count > 0)
{
lblNoRecordSubscription.Content = "";
List(open angular)KeyValuePair(open angular)string, int>> chartDataList = new List(open angular)KeyValuePair(open angular)string, int>>();
foreach (var month in gChartDataCollection)
{
chartDataList.Add(new KeyValuePair(month.MonthName, month.Count));
}

ColumnSeries _mySeries = new ColumnSeries();
_mySeries.Title = "Subscriptions";

Style DataPointStyle = new Style(typeof(ColumnDataPoint));
DataPointStyle.Setters.Add(new Setter(ColumnDataPoint.MaxWidthProperty, 35.0));
DataPointStyle.Setters.Add(new Setter(ColumnDataPoint.BackgroundProperty, new SolidColorBrush(Color.FromArgb(255, 179, 199, 224))));
_mySeries.DataPointStyle = DataPointStyle;

_mySeries.IndependentValueBinding = new System.Windows.Data.Binding("Key");
_mySeries.DependentValueBinding = new System.Windows.Data.Binding("Value");
_mySeries.ItemsSource = chartDataList;

SubscribeReportChart.Series.Add(_mySeries);
}

void bwViewChart_DoWork(object sender, DoWorkEventArgs e)
{
ObservableCollection _suscribedChannels = new ObservableCollection();

foreach (ListItem _item in Channels.GetSubscribedChannelsByDate(gChannelID, gFromDateChart, gToDateChart))
{
SubscribeChannel page = new SubscribeChannel();
page.Month = Convert.ToDateTime(_item["Modified"]).Month;//int
_suscribedChannels.Add(page);
}

gChartDataCollection = new ObservableCollection();

foreach (var month in _suscribedChannels.Select(i => i.Month).Distinct().OrderBy(i => i))
{
ChartData page = new ChartData();
DateTime dt=new DateTime(1,month,1);
page.MonthName = dt.ToString("MMM");
page.Count = _suscribedChannels.Where(ch => ch.Month == month).Count();
gChartDataCollection.Add(page);
}
}

public class SubscribeChannel
{
public int Month { get; set; }
}
public class ChartData
{
public string MonthName { get; set; }
public int Count { get; set; }
}

No comments:

Post a Comment