0

有没有人在 Coldfusion 上实现 JFreechart 拨号图表的示例?

谢谢

4

3 回答 3

2

(这可能不应该是一个单独的答案,但另一个已经很长了。所以我将它单独发布以使事情井井有条且易于查找。)

如果您正在寻找 jFreeChart 所称的 MeterChart,这里是该图表类型的一个快速而肮脏的示例。

JFreeChart MeterChart

仪表图代码:

<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#">

辅助功能:

<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>     
于 2011-07-25T04:14:13.850 回答
1

该软件包org.jfree.chart.demo提供了如何构建一些基本图表的示例;单击类名以查看源代码。org.jfree.chart.ChartFactory更多的展示了如何构建的方法。该类org.jfree.chart.ChartUtilities包括以多种格式流式传输图表的方法。任何 servlet 容器中的相应response.setContentType()作品。

如果这是terra incognita,我会推荐JFreeChart 开发人员指南†</sup>。

†</sup>免责声明:不隶属于 Object Refinery Limited;只是一个满意的客户和非常小的贡献者。

于 2011-07-24T17:40:24.563 回答
1

使用trashgod 的建议,我为CF7 创建了一个非常基本的示例。你显然可以用它做更多的事情。只需查看 api 和/或购买开发者指南。

示例拨号表

安装: 下载最新的 jfreeChart。将以下 jar 复制到{cf_root}\WEB-INF\lib并重新启动 CF。请注意,jar 版本号可能会有所不同。

  • jfreechart-1.0.13.jar
  • jcommon-1.0.16.jar

样本:

<cfscript>
// my chart settings
chartTitle = "My Dial Chart";
arrowValue  = 55;
dialMinimum = 0;
dialMaximum = 100;
chartWidth  = 500;
chartHeight = 500;

// initialize basic components of the chart
// see jFreeChart API on how to customize the components settings further 
DefaultValueDataset = createObject("java", "org.jfree.data.general.DefaultValueDataset");
pointerValue    = DefaultValueDataset.init(arrowValue);
dialPointer     = createObject("java", "org.jfree.chart.plot.dial.DialPointer$Pointer").init();
dialFrame       = createObject("java", "org.jfree.chart.plot.dial.StandardDialFrame").init();
dialBackground  = createObject("java", "org.jfree.chart.plot.dial.DialBackground").init();
// tweak the default range to make it more appealing.
// see angle/extent: http://java.sun.com/developer/technicalArticles/GUI/java2d/java2dpart1.html
dialScale = createObject("java", "org.jfree.chart.plot.dial.StandardDialScale").init();
dialScale.setLowerBound(dialMinimum);
dialScale.setUpperBound(dialMaximum);
dialScale.setStartAngle(-150);
dialScale.setExtent(-240);

//initialize plot and apply settings
plot = createObject("java", "org.jfree.chart.plot.dial.DialPlot").init();
plot.setDialFrame(dialFrame);
plot.setBackground(dialBackground);
plot.setDataset(pointerValue);
plot.addScale(0, dialScale);
plot.addPointer(dialPointer);

// create chart and convert it to an image
chart = createObject("java", "org.jfree.chart.JFreeChart").init(chartTitle, plot);
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#">
于 2011-07-25T01:11:29.360 回答