I’m developing a service where I need to create a chart using only code behind. The chart will then be exported to a bitmap and inserted into a RadDocument.
My code is:
Grid grid = new Grid();grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });RadCartesianChart chart = new RadCartesianChart();chart.Grid = new CartesianChartGrid { MajorLinesVisibility = GridLineVisibility.Y, StripLinesVisibility = GridLineVisibility.Y };chart.HorizontalAxis = new CategoricalAxis { PlotMode = AxisPlotMode.BetweenTicks };chart.VerticalAxis = new LinearAxis { Minimum = 0, MajorTickLength = 5, Title = "Y axis title" };// create seriesfor (int i = 0; i < series.Count; i++){ LineSeries lineSeries = new LineSeries { Stroke = new SolidColorBrush(series[i].Color), LegendSettings = new SeriesLegendSettings { Title = series[i].Title } }; foreach (var value in series[i].Values) { lineSeries.DataPoints.Add(new CategoricalDataPoint { Category = value.X, Value = value.Y }); } chart.Series.Add(lineSeries);}RadLegend legend = new RadLegend { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Bottom, Items = chart.LegendItems };string panelTemplate = @"<ItemsPanelTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">" + @"<StackPanel Orientation=""Horizontal""/>" + @"</ItemsPanelTemplate>";legend.ItemsPanel = (ItemsPanelTemplate)XamlReader.Parse(panelTemplate);chart.SetValue(Grid.RowProperty, 0);grid.Children.Add(chart);legend.SetValue(Grid.RowProperty, 1);grid.Children.Add(legend);Size chartSize = new Size(width, height);grid.Measure(chartSize);grid.Arrange(new Rect(new Point(0, 0), chartSize));grid.UpdateLayout();RenderTargetBitmap bitmapRender = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);bitmapRender.Render(grid);var encoder = new PngBitmapEncoder();encoder.Frames.Add(BitmapFrame.Create(bitmapRender));encoder.Save(stream);If I set a breakpoint where the RadLegend is created, I can see that the series I’ve created have been added to chart.Series but chart.LegendItems is empty.
If I continue and export the chart to a bitmap I get an empty image.
All the code seems to be alright, what am I doing wrong?
