Coldfusion 中的 JFreeChart 表盘图表

发布于 2024-11-26 00:14:02 字数 62 浏览 2 评论 0原文

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

谢谢

Has anyone an example of implementing a JFreechart Dial chart on Coldfusion?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扬花落满肩 2024-12-03 00:14:02

(这可能不应该是一个单独的答案,但另一个已经很长了。所以我单独发布它以保持内容井然有序且易于查找。)

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

JFreeChart MeterChart

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>     

(This probably should not be a separate answer, but the other was already pretty long. So I am posting it separately to keep things organized and easy to find.)

In case you are looking for what jFreeChart calls a MeterChart, here is a quick and dirty example of that chart type.

JFreeChart MeterChart

MeterChart Code:

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

Auxiliary 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>     
熟人话多 2024-12-03 00:14:02

org.jfree。 Chart.demo 有如何构建一些基本图表的示例;单击类名即可查看源代码。 org.jfree.chart.ChartFactory 的方法 展示如何构建更多。类 org.jfree.chart.ChartUtilities< /code>包括以多种格式传输图表的方法。相应的 response.setContentType() 可在任何 servlet 容器中工作。

如果这是未知领域,我会推荐JFreeChart 开发人员指南

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

The package org.jfree.chart.demo has examples of how to construct a few basic charts; click on the class name to see the source. The methods of org.jfree.chart.ChartFactory show how to construct still more. The class org.jfree.chart.ChartUtilities includes methods to stream charts in several formats. A corresponding response.setContentType() works from any servlet container.

If this is terra incognita, I'd recommend The JFreeChart Developer Guide.

Disclaimer: Not affiliated with Object Refinery Limited; just a satisfied customer and very minor contributor.

飘过的浮云 2024-12-03 00:14:02

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

Using trashgod's suggestions, I created a very rudimentary example for CF7. You can obviously do much more with it. Just review the api and/or purchase the developer guide.

Sample Dial Chart

Install:
Download latest jfreeChart. Copy the following jars into {cf_root}\WEB-INF\lib and restart CF. Note, jar version numbers may vary.

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

Sample:

<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#">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文