Creating Grid Lines in code

2 posts, 0 answers
  1. Shawn
    Shawn avatar
    7 posts
    Member since:
    Dec 2012

    Posted 07 Oct 2013 Link to this post

    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;
            }
  2. Petar Kirov
    Admin
    Petar Kirov avatar
    425 posts

    Posted 11 Oct 2013 Link to this post

    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 >>
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top