I have been working on a user control to simplify chart generation based on our needs. We adhere strictly to MVVM. I created a content control in XAML and am creating all of the Telerik charting in the view model and binding the chart to the content control. So far it has gone fairly smooth but with a hitch. I am trying to display the grid lines declared in code but the grid lines simply aren't showing up. Is it not possible to generate the grid with appropriate grid lines in code? Note that I have been focusing on the bar series in the following code:
public void Create2DCharting(ObservableCollection<
CategoricalDataItem
> data, NewChartTheme chartTheme)
{
this.ShowCartesian3DChart = false;
BarSeries barSeries = new BarSeries();
if (chartTheme.ColorBrushes.Count > 0)
{
for (int i = 0; i <
data.Count
; i++ )
{
data[i]
.Color
=
chartTheme
.ColorBrushes[i];
}
}
if (chartTheme.ChartTp == ChartType.PieChart || chartTheme.ChartTp == ChartType.DoughnutChart)
{
this.ShowPieChart
=
true
;
this.ShowCartesian2DChart
=
false
;
this.PieChart
=
new
RadPieChart();
this.PieChart.Palette
=
new
ChartPalette();
foreach (CategoricalDataItem cdi in data)
PieChart.Palette.GlobalEntries.Add(new PaletteEntry(cdi.Color, cdi.Color));
if (chartTheme.ShowLegend)
{
this.ChartLegend
=
new
RadLegend();
this.ChartLegend.Items
= this.PieChart.LegendItems;
}
}
else
{
this.ShowPieChart
=
false
;
this.ShowCartesian2DChart
=
true
;
this.CartesianChart2D
=
new
RadCartesianChart();
if (chartTheme.BackgroundColor != null)
this.CartesianChart2D.Background
=
new
SolidColorBrush((Color)ColorConverter.ConvertFromString(chartTheme.BackgroundColor));
barSeries.HorizontalAxis
=
new
CategoricalAxis();
barSeries.VerticalAxis
=
new
LinearAxis();
if (chartTheme.ShowLegend)
{
this.ChartLegend
=
new
RadLegend();
barSeries.ItemsSource
=
data
;
barSeries.ValueBinding
=
new
PropertyNameDataPointBinding("YValue");
barSeries.CategoryBinding
=
new
PropertyNameDataPointBinding("XValue");
LegendItemCollection
lic
=
new
LegendItemCollection();
foreach (var d in data)
lic.Add(new LegendItem() { MarkerFill = (Brush)d.Color, MarkerStroke = (Brush)d.Color,
Title
=
d
.XValue });
this.ChartLegend.Items
=
lic
;
}
else
{
barSeries.ItemsSource
=
data
;
barSeries.ValueBinding
=
new
PropertyNameDataPointBinding("YValue");
barSeries.CategoryBinding
=
new
PropertyNameDataPointBinding("XValue");
}
}
switch(chartTheme.ChartTp)
{
case ChartType.BarChart :
if(chartTheme.ColorBrushes.Count > 0)
barSeries.PointTemplate = GetBarColorDataTemplate();
if (chartTheme.ShowGridLines)
this.CartesianChart2D.Grid = new CartesianChartGrid() { MajorLinesVisibility = GridLineVisibility.Y };
this.CartesianChart2D.Series.Add(barSeries);
break;
case ChartType.HorizontalBarChart :
barSeries.VerticalAxis = new CategoricalAxis();
barSeries.HorizontalAxis = new LinearAxis();
if(chartTheme.ColorBrushes.Count > 0)
barSeries.PointTemplate = GetBarColorDataTemplate();
this.CartesianChart2D.Series.Add(barSeries);
break;
case ChartType.LineGraph :
LineSeries lineSeries = new LineSeries();
lineSeries.HorizontalAxis = new CategoricalAxis();
lineSeries.VerticalAxis = new LinearAxis();
lineSeries.ItemsSource = data;
lineSeries.ValueBinding = new PropertyNameDataPointBinding("YValue");
lineSeries.CategoryBinding = new PropertyNameDataPointBinding("XValue");
lineSeries.PointTemplate = GetLineDataPointDataTemplate();
this.CartesianChart2D.Series.Add(lineSeries);
break;
case ChartType.PieChart :
PieSeries pieSeries = new PieSeries();
pieSeries.ItemsSource = data;
pieSeries.ValueBinding = new PropertyNameDataPointBinding("YValue");
PieChart.Series.Add(pieSeries);
break;
case ChartType.DoughnutChart :
DoughnutSeries doughnutSeries = new DoughnutSeries();
doughnutSeries.ItemsSource = data;
doughnutSeries.ValueBinding = new PropertyNameDataPointBinding("YValue");
PieChart.Series.Add(doughnutSeries);
break;
}
}
private DataTemplate GetBarColorDataTemplate()
{
StringReader markup = new StringReader(
@"<
DataTemplate
xmlns
=
""
http://schemas.microsoft.com/winfx/2006/xaml/presentation"">" +
@"<
Rectangle
Fill
=
""
{Binding DataItem.Color}""/>" +
@"</
DataTemplate
>");
XmlReader xmlReader = XmlReader.Create(markup);
return XamlReader.Load(xmlReader) as DataTemplate;
}
private DataTemplate GetLineDataPointDataTemplate()
{
StringReader markup = new StringReader(
@"<
DataTemplate
xmlns
=
""
http://schemas.microsoft.com/winfx/2006/xaml/presentation"">" +
@"<
Ellipse
Height
=
""
10""
Width
=
""
10""
Fill
=
""
{Binding DataItem.Color}""/>" +
@"</
DataTemplate
>");
XmlReader xmlReader = XmlReader.Create(markup);
return XamlReader.Load(xmlReader) as DataTemplate;
}