How to add CartesianChartGrid in code?

4 posts, 0 answers
  1. Alex
    Alex avatar
    3 posts
    Member since:
    Aug 2012

    Posted 20 Sep 2014 Link to this post

    Hi,

    I'm trying this code, but it doesnt draw a grid:

    ChartMain = new RadCartesianChart();

    Style style = new Style();
    style.TargetType = typeof(Line);
    style.Setters.Add(new Setter(Line.StrokeProperty, Brushes.Black));
    style.Setters.Add(new Setter(Line.StrokeThicknessProperty, new Thickness(2.0)));

    CartesianChartGrid chartGrid = new CartesianChartGrid();
    chartGrid.MajorLinesVisibility = GridLineVisibility.XY;
    chartGrid.MajorYLineStyle = style;
    chartGrid.MajorXLineStyle = style;

    ChartMain.Grid = chartGrid;
  2. Martin
    Admin
    Martin avatar
    1101 posts

    Posted 23 Sep 2014 Link to this post

    Hi Alex,

    This doesn't work because the StrokeThickness property of the Line element is not of type Thickness and setting it to such object is throwing an exception. The StrokeThickness property accepts a value of type Double. 

    style.Setters.Add(new Setter(Line.StrokeThicknessProperty, 2.0));

    The following code snippet demonstrates creating of RadCartesianChart in code:

    public partial class MainPage : Window
    {
        public MainPage()
        {
            InitializeComponent();
     
            var list = new List<DataItem>();
            Random r = new Random();
            for (int i = 0; i < 10; i++)
            {
                list.Add(new DataItem { Date = DateTime.Now.AddMonths(i), Value = r.Next(30, 300) });
            }
             
            var ChartMain = new RadCartesianChart();
     
            ChartMain.VerticalAxis = new LinearAxis();
            ChartMain.HorizontalAxis = new DateTimeContinuousAxis();
            ChartMain.Series.Add(new AreaSeries()
            {
                CategoryBinding = new PropertyNameDataPointBinding("Date"),
                ValueBinding = new PropertyNameDataPointBinding("Value"),
                ItemsSource = list
            });
     
            Style style = new Style();
            style.TargetType = typeof(Line);
            style.Setters.Add(new Setter(Line.StrokeProperty, Brushes.Black));
            style.Setters.Add(new Setter(Line.StrokeThicknessProperty, 2.3));
             
            CartesianChartGrid chartGrid = new CartesianChartGrid();
            chartGrid.MajorLinesVisibility = GridLineVisibility.XY;
            chartGrid.MajorYLineStyle = style;
            chartGrid.MajorXLineStyle = style;
     
            ChartMain.Grid = chartGrid;
     
            this.layoutGrid.Children.Add(ChartMain);
        }  
    }
    }
     
    public class DataItem
    {
        public DateTime Date { get; set; }
        public Double Value { get; set; }
    }

    Please let me know if this helps.

    Regards,
    Martin
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Alex
    Alex avatar
    3 posts
    Member since:
    Aug 2012

    Posted 23 Sep 2014 in reply to Martin Link to this post

    Hi Martin,

    Thanks for your reply, sadly it doesn't work for me.

    Might that be because how I use my chart further?
    I write it to a png, without displaying on a visual tree.
    Chart and Legend appear in a png, but ChartGrid - doesnt.

    // Create a Visual container and add Legend and Chart to it
    StackPanel stackpanel = new StackPanel() { Orientation = Orientation.Vertical };
     
    chart.Width = Convert.ToDouble(App.Current.Resources["chartwidth"]);
    chart.Height = Convert.ToDouble(App.Current.Resources["chartheight"]);
     
    legend.Width = Convert.ToDouble(App.Current.Resources["legendwidth"]);
    legend.Height = Convert.ToDouble(App.Current.Resources["legendheight"]);
     
    stackpanel.Children.Add(legend);
    stackpanel.Children.Add(chart);
     
    stackpanel.Width = chart.Width;
    stackpanel.Height = chart.Height + legend.Height;
     
    legend.Measure(new Size(legend.Width, legend.Height));
    legend.Arrange(new Rect(new Size(legend.Width, legend.Height)));
     
    chart.Measure(new Size(chart.Width, chart.Height));
    chart.Arrange(new Rect(new Size(chart.Width, chart.Height)));
     
    legend.UpdateLayout();
    chart.UpdateLayout();
    stackpanel.UpdateLayout();
     
    stackpanel.Measure(new Size(stackpanel.Width, stackpanel.Height));
    stackpanel.Arrange(new Rect(new Size(stackpanel.Width, stackpanel.Height)));
     
    returnValue = @"C:\\RegisterDeviceService\\Images\\" + date + "_" + type + ".png";
     
    SaveToPng(stackpanel, returnValue);
  5. Martin
    Admin
    Martin avatar
    1101 posts

    Posted 24 Sep 2014 Link to this post

    Hi Alex,

    Thank you for the code snippet. I tested it but I wasn't able to reproduce the issue. Can you please take a look at the attached project and let me know if I am missing something? Also, it would be helpful if you can tell me how exactly you are saving the PNG.

    Thank you for any help you can provide.

    Regards,
    Martin
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
Back to Top