FusionCharts debug mode doesn't work @ 100% x 100%

by timvasil 2/26/2008 12:21:00 AM

FusionCharts' debugMode doesn't work when you specify dimensions of 100% by 100%.

Generally speaking, 100% x 100% isn't what you want anyway, as the chart will never shrink or grow even if its container does.  You're forced to recreate the chart when you want to change its dimensions as documented here and here.  So you're better off determining the actual width and height of your container and passing those values into the FusionCharts constructor.

Another gotcha:  the value you use for a setDataXML call is different depending on whether or not the chart has already been rendered!  If the chart isn't rendered yet, you have to URL escape the XML by calling a function like encodeURIComponent.  Otherwise, you can send in XML directly.  So, when re-constructing a chart to change its dimension, make sure you're encoding the XML if you go the XML route!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

FusionCharts

Exporting FusionChart images

by timvasil 2/5/2008 11:35:00 AM

FusionCharts is a flash-based animated charting package.  One of its recent features is the ability to export the chart images so end users can save them to disk.  Unfortunately the sample code provided gives only PHP and C# examples of how to do this, which isn't so handy if you're using Java. So, in the public interest, here is a Java of that code:

@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
    // Extract parameters
    int width = Integer.parseInt(req.getParameter("width")) - 1;
    int height = Integer.parseInt(req.getParameter("height"));
    String bgColorStr = req.getParameter("bgcolor");
    int bgColor = (bgColorStr == null || bgColorStr.length() == 0) ? 0xffffff : Integer.parseInt(bgColorStr, 16);
    String data = req.getParameter("data");
   
    // Build the bitmap image
    BufferedImage image = buildBitmap(width, height, bgColor, data);
   
    // Compress the image as a JPEG
    ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
    ImageWriteParam writerParam = writer.getDefaultWriteParam();
    writerParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    writerParam.setCompressionQuality(0.95f);

    // Stream the image to the user agent
    resp.addHeader("Content-Disposition", "attachment; filename=\"FusionCharts.jpg\"");
    resp.setContentType("image/jpeg");
    ImageOutputStream imageOut = ImageIO.createImageOutputStream(resp.getOutputStream());
    writer.setOutput(imageOut);
    writer.write(null, new IIOImage(image, null, null), writerParam);
    imageOut.flush();
    imageOut.close();
}
   
private BufferedImage buildBitmap(int width, int height, int bgColor, String data)
{
    BufferedImage chart = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    String[] rows = data.split(";");
    int colIdx = 0;
    for (int rowIdx = 0; rowIdx < rows.length; rowIdx++)
    {
        // Split individual pixels
        String[] pixels = rows[rowIdx].split(",");
        colIdx = 0;
        for (int pixelIdx = 0; pixelIdx < pixels.length; pixelIdx++)
        {               
            // Split the color and repeat factor
            String[] clrs = pixels[pixelIdx].split("_");  
            int color = ("".equals(clrs[0])) ? bgColor : Integer.parseInt(clrs[0], 16);
            int repeatFactor = Integer.parseInt(clrs[1]);
           
            // Set the color the specified number of times
            for (int repeatCount = 0; repeatCount < repeatFactor; repeatCount++, colIdx++)
            {                      
                chart.setRGB(colIdx, rowIdx, color);
            }
        }
    }
   
    return chart;
}

Note:  I think there's a bug in the Flash image exporter.  It looks like it's reporting the width of the image to be 1 pixel greater than whan it actually is, hence the expression Integer.parseInt(req.getParameter("width")) - 1

Currently rated 5.0 by 5 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

FusionCharts | Java

Updating Fusion Chart Data via setDataXML in IE

by timvasil 1/3/2008 2:42:00 AM

FusionCharts is using Adobe's ExternalInterface API to communicate between JavaScript and the SWF chart when you use FusionCharts.js to do things like setDataXML. For some reason, ExternalInterface isn't working correctly on IE under some conditions; the method simply doesn't exist on the OBJECT element.  I happen to be using the ExtJS library with GWT; either framework may be doing some DOM manipulations under the covers that are freaking out Adobe--but I haven't verified this.

A couple things to try:

  1. Ensure that the ID and name attributes of the OBJECT and/or EMBED tags do not have characters such as . (period), -, +, *, /, and \.  Amusingly, this is because Adobe's ExternalInterface API is using a bunch of eval() JavaScript methods and these special characters get interpreted as JavaScript operators.  Yeah, great work, Adobe.
     
  2. Hook up the ExternalInterface functions you need manually.  For example, here's the modified setDataXML function in FusionCharts.js to do the trick (code in red has been added):

setDataXML: function(strDataXML){
    //If being set initially
    if (this.initialDataSet==false){
        //This method sets the data XML for the chart INITIALLY.
        this.addVariable('dataXML',strDataXML);
        //Update flag
        this.initialDataSet = true;
    }else{
        //Else, we update the chart data using External Interface
        //Get reference to chart object
        var chartObj = infosoftglobal.FusionChartsUtil.getChartObject(this.getAttribute('id'));
        if (!chartObj.setDataXML)
        {
            __flash__addCallback(chartObj, "setDataXML");
        }
        chartObj.setDataXML(strDataXML);
    }
},

Currently rated 4.7 by 3 people

  • Currently 4.666667/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

JavaScript | FusionCharts

 

About the author

Tim Vasil Tim Vasil
I'm a software engineer living in Cambridge, MA.

E-mail me Send mail

Search

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Recent comments