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

How to add CartesianChartGrid in code?

3 Answers 102 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Alex
Top achievements
Rank 1
Alex asked on 20 Sep 2014, 11:11 PM
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;

3 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 23 Sep 2014, 06:16 AM
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.
 
0
Alex
Top achievements
Rank 1
answered on 23 Sep 2014, 10:20 AM
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);
0
Martin Ivanov
Telerik team
answered on 24 Sep 2014, 11:16 AM
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.
 
Tags
Chart
Asked by
Alex
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Alex
Top achievements
Rank 1
Share this question
or