【问题标题】:CF: Saving a Fusioncharts graph as an image to email itCF:将 Fusioncharts 图表保存为图像以通过电子邮件发送
【发布时间】:2013-02-08 18:42:06
【问题描述】:

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

我知道如何保存图像并将其插入 HTML 电子邮件中,我之前使用其他图形产品也这样做过。我只是不能很好地举例说明如何使用 Fusioncharts 做到这一点。

谢谢!

【问题讨论】:

    标签: coldfusion fusioncharts


    【解决方案1】:

    在模板中使用以下代码并将 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()#">
    

    【讨论】:

    • 谢谢。你有什么叫这个的例子吗?我不确定“imageSaveURL”是什么,除非我认为它是 FusionCharts 的属性?我在他们的文档或知识库(或谷歌)中获得的唯一 FusionCharts 搜索结果是对将 FusionMaps 导出为图像的引用——但没有关于该属性是什么的示例。谢谢!
    • PS 一个表单是如何参与其中的(这里仍然大量丢失)?
    • 是的 - imageSaveURL 是图表的属性。这应该指向我发布的代码的网络可访问版本 - 例如您图表中的imagesaveurl = 'fusionchartssave.cfm'。闪图posts通过这个url的所有数据。我刚刚注意到您说您使用的是最新版本,也许这种方法已被弃用。我将它与 v3 图表一起使用
    • 有点分心于其他事情。我现在已经尝试过了,没有错误,但什么也没有发生。看起来该文件应该打开浏览器提示符(而不是保存文件)?我的意思是我需要将图形图像保存在 CF 临时目录中,以便我可以将其插入电子邮件中(不涉及浏览器,并且该“页面”仅由服务器按计划调用)。
    • 我一直在尝试阅读我能阅读的所有内容,看起来(仅使用 CF)如果不涉及浏览器窗口是不可能做到这一点的,所以我想绝对没有办法调度程序调用带有 fusioncharts 图表的页面,并将这些图表保存为临时文件,以便可以通过电子邮件发送它们。呃。
    【解决方案2】:

    查看最新的博客文章 Export Chart Images at Server Side,其中描述了两个选项(wkhtmltoimage 和 PhantomJS.),并提供了两个选项的分步说明。

    对于 ColdFusion,您可以尝试以下代码:

    例如:

    <cfexecute name="C:\Program Files\wkhtmltopdf\wkhtmltoimage.exe" arguments="--javascript-delay 10000 http://docs.fusioncharts.com/charts/Code/MyFirstChart/ms-weekly-sales-no-animation.html savedimage.png" />
    

    在执行上述脚本时,我们提供以下参数: 调用 wkhtmltoimage 将您网页的 URL 传递给它 传递将保存图像的图像文件(带扩展名)的路径和名称 如果需要,任何额外的延迟以确保图表完全呈现。

    【讨论】:

      猜你喜欢
      • 2018-09-20
      • 2015-10-26
      • 2012-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-14
      • 2018-04-08
      • 1970-01-01
      相关资源
      最近更新 更多