How to export pie chart as gif image

39 views
Skip to first unread message

saurabh jain

unread,
Sep 24, 2009, 4:51:29 AM9/24/09
to Flex India Community
I am stuck with this issue ..

I have a pie chart .now on button click i need to export it as a gif
image..

heard of using GIFencoder..can anyone throw light on this issue..

my code is as follows :

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
xmlns:components="components.*"
>
<components:PieChart id="pie1"/>

<mx:Script>
<![CDATA[
import mx.controls.Alert;
//import flash.trace.Trace;
import mx.containers.VBox;
import flash.display.*;
import flash.events.*;
import flash.utils.ByteArray;
import mx.events.*;
import mx.utils.Base64Encoder;
import mx.rpc.events.ResultEvent;

[Bindable]
public var encodedstring:String;



private function eventHandlerr(): void{
Alert.show("hi1: " );
var bitdata:BitmapData = createBitmap(pie1);
Alert.show("bytes1: ");
var bytearray:ByteArray=ImgToByteArray(bitdata);
Alert.show("bytes3: " );
var lengthh:int=bytearray.length;
Alert.show("bytes: " +lengthh);

// encodeBytes(bytearray,0,lengthh);
//var encodedstring:String=encodeit(bytearray,lengthh);
encodedstring=encodeit(bytearray,lengthh);
Alert.show("string: ");





}

private function createBitmap(source:PieChart):BitmapData {
var bmd:BitmapData = new BitmapData(source.width,
source.height);
bmd.draw(source);

return bmd;

}



private function ImgToByteArray( bmpImg : BitmapData ) : ByteArray
{
var binaryImage : ByteArray = new ByteArray();
var bmpWidth:Number;
var bmpHeight:Number;
bmpWidth = bmpImg.width;
bmpHeight = bmpImg.height;

for( var i:uint=0; i< bmpWidth; i++ )
{
for( var j:uint=0; j<bmpHeight; j++ )
{
binaryImage.writeUnsignedInt( bmpImg.getPixel( i,
j ) );
}
}

return binaryImage;
}


/* public function encodeBytes(data:ByteArray,
offset:uint , length:int):void{


var benq:Base64Encoder = new Base64Encoder();
benq.encode("force",0,5);
var tmp:String = benq.toString();
return tmp;

} */

private function encodeit
(bytearray:ByteArray,lengthh:int): String {
var benq:Base64Encoder = new Base64Encoder();
benq.encodeBytes(bytearray,0,lengthh);
// benq.encode(bytearray,0,lengthh);
var tmp:String = benq.toString();
return tmp;
}

private function resultHandler(ev:ResultEvent):void{

}

]]>
</mx:Script>

<mx:Button id="btn1" label="Export Chart to gif" click="eventHandlerr
()" />
<mx:TextArea text="{encodedstring}"/>
</mx:Application>




right now am able to generate the byte array ..
But i have nio where done or added any header for GIF image...p
can anyone tell me what editing is reqd here...
Thx

Yuvaraj Mohite

unread,
Sep 24, 2009, 11:30:47 PM9/24/09
to flex_...@googlegroups.com
Hi Saurabh,
As you are using flex you will have to pass that byte array to server side, from where you can save as a gif, in AIR you have direct option of saving to local disk. Currently what you are doing is converting it into a base64 that is string representation, trying using jpeg encoder or png encoder. Hope that will solve your problem
~Yuvaraj

Reply all
Reply to author
Forward
0 new messages