Home > ColdFusion, Java > Creating a Meter Chart with ColdFusion and JFreeChart

Creating a Meter Chart with ColdFusion and JFreeChart

A recent question on stackoverflow.com prompted me to take a look at how to create dial charts with JFreeChart (rather than ACF’s webcharts3D engine).  Since you never know when starter code snippets will come in handy, here is the quick and dirty example I put together for creating Meter Charts.

Meter Chart Example (Compatibility MX7+)

<cfscript>
    // my chart settings
    chartTitle = "My Meter Chart";
    arrowValue  = 55;
    arrowUnits  = "widgets";
    chartWidth  = 500;
    chartHeight = 500;

    // initialize meter ranges (LOW, MEDIUM, HIGH)
    // note: quick and ugly code in dire need of improvement ...
    low  = createSolidMeterInterval("Low", 0, 40, createAwtColor(0, 255, 0, 120));
    med  = createSolidMeterInterval("Med", 40, 60, createAwtColor(255, 255, 0, 120));
    high = createSolidMeterInterval("High", 60, 100, createAwtColor(255, 0, 0, 120));

    // initialize arrow value
    DefaultValueDataset = createObject("java", "org.jfree.data.general.DefaultValueDataset");
    meterPointer = DefaultValueDataset.init(arrowValue);

    //initialize plot and apply settings
    plot = createObject("java", "org.jfree.chart.plot.MeterPlot").init();
    plot.setDataset(meterPointer);
    plot.setTickLabelsVisible(true);
    plot.addInterval(low);
    plot.addInterval(med);
    plot.addInterval(high);
    plot.setUnits(arrowUnits);

    // create chart and convert it to an image
    chart = createObject("java", "org.jfree.chart.JFreeChart").init(chartTitle, plot);
    ChartUtilities = createObject("java", "org.jfree.chart.ChartUtilities");
    ChartUtilities.applyCurrentTheme(chart);

    // applyCurrentTheme seems to overwrite some settings, so we must reapply them
    Color = createObject("java", "java.awt.Color");
    plot.setBackgroundPaint(Color.GRAY);
    plot.setNeedlePaint(Color.BLACK);

    chartImage = chart.createBufferedImage(chartWidth, chartHeight);
    ImageFormat = createObject("java", "org.jfree.chart.encoders.ImageFormat");
    EncoderUtil = createObject("java", "org.jfree.chart.encoders.EncoderUtil");
    bytes = EncoderUtil.encode( chartImage, ImageFormat.PNG);
</cfscript>
<!--- display in browser --->
<cfcontent type="image/png" variable="#bytes#">

Auxilary Functions:

<cfscript>
        // quick and ugly functions. could be improved ...
        function createSolidMeterInterval(Title, fromValue, toValue, BgColor) {
            var Range = createObject("java", "org.jfree.data.Range").init(arguments.fromValue, arguments.toValue);
            var MeterInterval = createObject("java", "org.jfree.chart.plot.MeterInterval");
            return MeterInterval.init(arguments.Title, Range   // interval from / to range
                                        , javacast("null", "") // outline color
                                        , javacast("null", "") // outline stroke
                                        , arguments.BgColor    // background color
                                    );
        }

        // using java.awt.Color is a pain due to all the javacasts ...
        function createAwtColor(r, g, b, alpha) {
            var color = createObject("java", "java.awt.Color");
            return color.init( javacast("int", arguments.r)
                                , javacast("int", arguments.g)
                                , javacast("int", arguments.b)
                                , javacast("int", arguments.alpha) // transparency
                            );
        }
</cfscript>
Advertisements
Categories: ColdFusion, Java Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: