Search code examples
apache-flexactionscriptchartsflexbuilderbar-chart

Converting Flex Barchart to Image without rendering


Is there a way to create a BarChart (in the background) without adding it to the current view? I basically have a chart that I need to convert to an image and add it to a PDF report (using AlivePDF).


Solution

  • No, you must add the the chart to the display list.

    A DisplayObject must be added to the display list in order to be able to render out as a Bitmap (i.e. to print it or to send it to a PDF).

    Internally AlivePDF uses the BitmapData.draw(...); method which requires the object to be on the display list and have visible=true in order to be rendered out.

    If you don't want the chart to appear on the stage whilst you are generating your PDF (or printing) then you can add the chart to a parent container and hide the parent instead.

    Here is an example of how you can do that:

    var box:VBox = new VBox();
    // Hide the parent, not the chart.
    // If you set chart.visible = false then it won't show up in the PDF.
    box.visible = false;
    box.addChild(chart);
    addChild(box);
    
    // You might need to force validation here so the chart has the correct size.
    box.validateNow();
    
    // Add chart to PDF.
    pdf.addImage(chart);
    
    // TODO: Clean up your display items here.
    box.removeChild(chart);
    removeChild(box);
    box = null;