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

Creating Grid Lines in code

1 Answer 89 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
Shawn
Top achievements
Rank 1
Shawn asked on 07 Oct 2013, 07:22 PM
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;
        }

1 Answer, 1 is accepted

Sort by
0
Petar Kirov
Telerik team
answered on 11 Oct 2013, 06:17 AM
Hi Shawn,

The CartesianChartGrid works only with the default chart axes (RadCartesianChart.HorizontalAxis and RadCartesianChart.VerticalAxis) and not with the axes defined per series (eg. BarSeries.HorizontalAxis). This means that:
  • If you have only a single series per chart you need to use only default axes, or
  • If you have multiple series per chart you need at least one of the series to not have any axes defined and to work with the default axes.

Regards,
Petar Kirov
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
ChartView
Asked by
Shawn
Top achievements
Rank 1
Answers by
Petar Kirov
Telerik team
Share this question
or