coldfusion - CF : Saving a Fusioncharts graph as an image to email it

标签 coldfusion fusioncharts

有人有这方面的例子吗?今晚谷歌不是我的 friend 。我有最新版本的 FusionCharts。我正在尝试弄清楚如何将图表另存为图像文件以通过电子邮件发送。

我知道如何保存图像然后将图像插入到 HTML 电子邮件中,并且我之前已经使用其他图形产品完成过此操作。我实在找不到 1 个很好的例子来说明如何使用 Fusioncharts 做到这一点。

谢谢!

最佳答案

在模板中使用以下代码并将 imageSaveURL 属性指向该模板。

您的图表提交了重建图表所需的所有数据。在我的示例中,我将其提供给浏览器,但您可以将其保存在本地,然后在必要时附加到 cfmail

我很确定我最初是从 fusioncharts 论坛得到这个的。这是针对 fusioncharts 3.1 进行更新的。

<cfif structKeyExists(Form, "width") >
    <cfset width = int(Form.width) />
<cfelse>
    <cfset width = int(Form.Meta_Width) />
</cfif>
<cfif structKeyExists(Form, "height") >
    <cfset height = int(Form.height) />
<cfelse>
    <cfset height = int(Form.Meta_Height) />
</cfif>

<cfif structKeyExists(Form, "data") >
    <cfset Form.data = Form.data />
<cfelse>
    <cfif structKeyExists(Form, "stream") >
        <cfset Form.data = Form.stream />
    </cfif>
</cfif>

<cfset user = viewState.getValue("user", structNew()) />


<!--- Impose some limits to mitigate DOS attacks --->
<cfif Not (0 lte width and width lte 5000)>
    <cfthrow message="Width out of range." />
</cfif>
<cfif Not (0 lte height and height lte 5000)>
    <cfthrow message="Height out of range." />
</cfif>


<!--- Check if we have the chart data --->
<cfif Not StructKeyExists(Form, "data") or Not Len(Trim( Form.data ))>
    <cfthrow message="Image Data not supplied." />
</cfif>


<!--- Default background color is white --->
<cfif Not StructKeyExists(Form, "bgcolor") or Not Len(Trim( Form.bgcolor ))>
    <cfset Form.bgcolor = "FFFFFF" />
</cfif>


<cfset gColor = CreateObject("java", "java.awt.Color") />
<cfset chart = CreateObject("java", "java.awt.image.BufferedImage") />
<cfset chart.init( JavaCast("int", width), JavaCast("int", height), chart.TYPE_3BYTE_BGR) />
<cfset gr = chart.createGraphics() />
<cfset gr.setColor( gColor.decode("##" & Form.bgcolor) ) />
<cfset gr.fillRect(0, 0, JavaCast("int", width), JavaCast("int", height)) />

<!--- Get rows with pixels --->
<cfset rows = ListToArray(Form.data, ";") />

<cfloop from="1" to="#ArrayLen(rows)#" index="i">

    <cfset pixels = ListToArray(rows[i], ",") />
    <!--- Horizontal index (x scale) --->
    <cfset horizIndex = 0 />

    <cfloop from="1" to="#ArrayLen(pixels)#" index="j">

        <cfif ListLen(pixels[j], "_") eq 2>
            <!--- We have the color and the number of times it must be repeated --->
            <cfset color  = ListGetAt(pixels[j], 1, "_") />
            <cfset repeat = ListGetAt(pixels[j], 2, "_") />
        <cfelse>
            <!--- Background color; how many pixels to skip --->
            <cfset color  = "" />
            <cfset repeat = ListGetAt(pixels[j], 1, "_") />
        </cfif>

        <cfif Len(Trim(color))>

            <!---  If the hexadecimal code is less than 6 characters, prefix with 0 to get a 6 char color --->
            <cfif Len(Trim(color)) lt 6>
                <cfset color = RepeatString(0, 6 - Len(Trim(color))) & color />
            </cfif>

            <!--- Draw a horizontal line for the number of pixels we must repeat --->
            <cfset gr.setColor(gColor.decode("##" & color)) />
            <cfset gr.drawLine(JavaCast("int", horizIndex), JavaCast("int", i - 1), JavaCast("int", horizIndex + repeat -1), JavaCast("int", i - 1)) />
        </cfif>

        <cfset horizIndex = horizIndex + repeat />
    </cfloop>

</cfloop>

<!--- Get writer for Jpeg --->
<cfset writer = "" />
<cfset iter = CreateObject("java", "javax.imageio.ImageIO").getImageWritersByFormatName("jpg") />
<cfloop condition="iter.hasNext()">
    <cfset writer = iter.next() />
</cfloop>

<!--- Set Jpeg quality to maximum --->
<cfset jpgParams = CreateObject("java", "javax.imageio.plugins.jpeg.JPEGImageWriteParam").init( CreateObject("java", "java.util.Locale").init("en") ) />
<cfset jpgParams.setCompressionMode( jpgParams.MODE_EXPLICIT ) />
<cfset jpgParams.setCompressionQuality( 1 ) />

<!--- Write image to a memory stream --->
<cfset imageOutput = CreateObject("java", "java.io.ByteArrayOutputStream").init() />
<cfset writer.setOutput( CreateObject("java", "javax.imageio.stream.MemoryCacheImageOutputStream").init( imageOutput ) ) />
<cfset writer.write(JavaCast("null", 0), CreateObject("java", "javax.imageio.IIOImage").init(chart, JavaCast("null", 0), JavaCast("null", 0)), jpgParams) />

<!--- Stream the image to the browser (hint browser to display the Save dialog) --->
<cfset filename="whatever.jpg" />
<cfheader name="Content-Disposition" value="attachment; filename=""#filename#""">
<cfcontent type="image/jpeg" variable="#imageOutput.toByteArray()#">

关于coldfusion - CF : Saving a Fusioncharts graph as an image to email it,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15036180/

相关文章:

ColdFusion 解密 AES128 PKCS7Padding CBC

php - ColdFusion 和 PHP 之间有效/无效的电子邮件格式策略差异

javascript - 是否可以在 FusionCharts 中使用 JSP 导出(服务器端)javascript 图表?

javascript - fusioncharts - 在不更改图表设置的情况下更新 FusionCharts 数据

php - 如何在循环时以及在数组内部插入数据

MySQL/ColdFusion 8 : sql-mode

jquery - 如何为文件上传表单执行 jquery post ?

facebook - 在 Coldfusion 页面上聚合并显示 facebook 和 twitter 状态更新

ios - JQuery Mobile 无法在 UIWebView 中工作

php - FusionCharts 中来自 MySQL 的多系列 2d 柱形图