views:

14

answers:

0

I am trying to create a WPF Chart in c# code and saving it to file without displaying it on screen. This will be in a WCF Service where data is sent to the service, an image is created and and path to the image is returned.

So far I have got the image to save to file and the data on the X & Y axis is displayed but the columns on the graph are not drawn.

Does anyone know why the columns on the chart are not drawing... here is my code:

public void DoWork() { var newThread = new Thread(CreateImage); newThread.SetApartmentState(ApartmentState.STA); newThread.Start(); }

    public void CreateImage()
    {
        var grid = new Grid {Width = 800, Height = 600, Background = new SolidColorBrush(Colors.LightBlue)};
        var chart = new Chart();
        grid.Children.Add(chart);

        var renderTarget = new RenderTargetBitmap((int)grid.Width, (int)grid.Height, 96d, 96d, PixelFormats.Default);


        var barSeries = new BarSeries
                            {
                                Title = "Fruit in Cupboard",
                                ItemsSource = new[]
                                                  {
                                                      new KeyValuePair<string, int>("Oranges", 18),
                                                      new KeyValuePair<string, int>("Apples", 15),
                                                      new KeyValuePair<string, int>("Melons", 2),
                                                      new KeyValuePair<string, int>("Pineapples", 4),
                                                      new KeyValuePair<string, int>("Plums", 25)
                                                  },
                                IndependentValueBinding = new Binding("Key"),
                                DependentValueBinding = new Binding("Value")
                            };

        chart.Series.Add(barSeries);

        grid.Measure(new Size(grid.Width, grid.Height));
        grid.Arrange(new Rect(new Size(grid.Width, grid.Height)));
        grid.UpdateLayout();

        renderTarget.Render(grid);

        var png = new PngBitmapEncoder();
        png.Frames.Add(BitmapFrame.Create(renderTarget));
        using (Stream stm = File.Create(string.Format(@"C:/Images/{0}.png", Guid.NewGuid())))
        {
            png.Save(stm);
        }
    }