{"id":1029,"date":"2015-09-02T16:51:38","date_gmt":"2015-09-02T16:51:38","guid":{"rendered":"https:\/\/www.anychart.com\/blog\/?p=1029"},"modified":"2015-09-02T17:03:09","modified_gmt":"2015-09-02T17:03:09","slug":"save-as-image-oracle-apex","status":"publish","type":"post","link":"https:\/\/www.anychart.com\/blog\/2015\/09\/02\/save-as-image-oracle-apex\/","title":{"rendered":"Saving AnyChart 6 Charts as Images in Oracle APEX"},"content":{"rendered":"<p><b>NOTE: This article tells how to set up the script in Oracle APEX and doesn&#8217;t affect AnyChart customers who use AnyChart outside of Oracle APEX.<\/b><\/p>\n<p>Oracle Application Express is a web-based application development tool integrated with all editions of Oracle Database. It enables users with limited programming skills to easily develop secure web applications that run inside their Oracle Database, and scale to support tens, hundreds, or thousands of users.<\/p>\n<p>Since Oracle Application Express Release 3.0, AnyChart is used as a charting engine (you can read about this in a success story: <a href=\"https:\/\/www.anychart.com\/company\/success_stories\/stories\/anychart-case-study-oracle-apex.pdf\" target=\"_blank\">Oracle Chooses AnyChart Solution for Latest Release of Oracle\u00ae Application Express<\/a>). <\/p>\n<p>One of the nice features available in <a href=\"https:\/\/www.anychart.com\/products\/anychart\/history\/\" target=\"_blank\">AnyChart since version 4.1.0<\/a> is the ability to save a Flash chart as an image &#8211; in order to make it possible, we have to use a server-side script, which is hosted on the AnyChart.Com site by default (read more about <a href=\"http:\/\/6.anychart.com\/products\/anychart\/docs\/users-guide\/index.html?SaveAsImage.html\" target=\"_blank\" rel=\"nofollow\">saving charts as images in AnyChart 6<\/a>).<\/p>\n<p>Federico Bonifazi from Geko Sistemi Informativi s.r.l. shared a solution that allows to save charts as images on the server side. We are very thankful for that and present it here for anyone who wants to do the same. This tutorial is very basic and is not a ready-to-go solution, but it gives the general idea, so you can use it to break ground on your project.<\/p>\n<p>This configuration in based on Oracle Standard Edition on a Linux server, Oracle Rest and Tomcat are used.<\/p>\n<p><!--more--><\/p>\n<p><b>1. Things to do in an Oracle APEX page<\/b><\/p>\n<p><b>1.1. Add the following to the html header of the page:<\/b><br \/>\n<code><br \/>\n&lt;script type=\"text\/javascript\" src=\"\/i\/flashchart\/anychart_6\/js\/AnyChart.js\"&gt;&lt;\/script&gt;<br \/>\n&lt;script type=\"text\/javascript\" language=\"JavaScript\"&gt;<br \/>\n\/\/&lt;![CDATA[<\/p>\n<p>  AnyChart.swfFile = \"\/i\/flashchart\/anychart_6\/swf\/OracleAnyChart.swf\";<\/p>\n<p>  function makeMultipartFormDataPostRequest(path, params) {<br \/>\n    var xmlhttp = false;<br \/>\n    var formData = new FormData();<\/p>\n<p>    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {<br \/>\n      try {<br \/>\n        xmlhttp = new XMLHttpRequest();<br \/>\n      } catch (e) {<br \/>\n        xmlhttp=false;<br \/>\n      }<br \/>\n    }<\/p>\n<p>    if (!xmlhttp && window.createRequest) {<br \/>\n      try {<br \/>\n        xmlhttp = window.createRequest();<br \/>\n      }catch (e) {<br \/>\n        xmlhttp=false;<br \/>\n      }<br \/>\n    }<br \/>\n    xmlhttp.open('POST', path, false);<br \/>\n    for (var propName in params) {<br \/>\n      formData.append(propName, params[propName]);<br \/>\n    }<br \/>\n    return xmlhttp.send(formData);<br \/>\n  }<\/p>\n<p>  function saveChartAsImage(chartName, chart) {<br \/>\n    var requestData = {};<br \/>\n    requestData['sessione'] = '&SESSION.';<br \/>\n    requestData['imgName']  = chartName;<br \/>\n    requestData['imgType']  = 'png';<br \/>\n    requestData['imgDesc']  = 'nn';<br \/>\n    requestData['imgData']  = chart.getPNG();<\/p>\n<p>    var path = makeMultipartFormDataPostRequest('http:\/\/localhost:8080\/apex\/report\/saveImage\/saveChart', requestData);<br \/>\n  }<br \/>\n  \/\/]]&gt;<br \/>\n&lt;\/script&gt;<br \/>\n<\/code><\/p>\n<p><b>1.2. Create a Flash chart:<\/b><br \/>\n<code><br \/>\n&lt;div id=\"report\"><br \/>\n  &lt;div id=\"chart_container1\"&gt;&lt;\/div&gt;<br \/>\n  &lt;script type=\"text\/javascript\" language=\"javascript\"&gt;<\/p>\n<p>    var chart = new AnyChart();<br \/>\n    chart.setXMLData($v('P205_CHARTO1_XML'));<\/p>\n<p>    chart.addEventListener(\"draw\", function() {<br \/>\n      saveChartAsImage(\"chart1\", chart);<br \/>\n    });<\/p>\n<p>    chart.write(\"chart_container1\");<br \/>\n  &lt;\/script&gt;<br \/>\n&lt;\/div&gt;<br \/>\n<\/code><\/p>\n<p>P205_CHART1_XML is hidden and filled with the dynamic content based on a query.<\/p>\n<p><b>2. RestFull Service<\/b><\/p>\n<p><b>2. Create a RestFull Service:<\/b><br \/>\nOn Oracle APEX main page -> SQL Workshop -> RESTful services<\/p>\n<p>RESTful Service Module: report<br \/>\nURI Prefix:    saveImage\/<\/p>\n<p><b>2.1. then create<\/b><br \/>\nRESTful Service Module:    report<br \/>\nURI Template:    saveChart<\/p>\n<p><b>2.2. Create POST Handler with PL\/SQL code:<\/b><br \/>\n<code><br \/>\nbegin<br \/>\n  :status := saveBase64toBlob<br \/>\n             (<br \/>\n              pSession => :session,<br \/>\n              pImgName =>  :imgName,<br \/>\n              pImgType =>  :imgType,<br \/>\n              pImgDesc =>  :imgDesc,<br \/>\n              pImgData =>  :imgData<br \/>\n             );<br \/>\nend;<br \/>\n<\/code><\/p>\n<p><b>3.Saving the image to a table<\/b><\/p>\n<p>saveBase64toBlob converts the base64 png to a blob and into the tbl_image:<br \/>\n<code><br \/>\n  function saveBase64toBlob<br \/>\n  (<br \/>\n    pSession varchar2,<br \/>\n    pImgName varchar2,<br \/>\n    pImgType varchar2,<br \/>\n    pImgDesc varchar2,<br \/>\n    pImgData clob<br \/>\n  ) return integer<br \/>\n  is<br \/>\n    retVal            integer;<br \/>\n    l_clob            clob;<br \/>\n    l_blob            blob;<br \/>\n    l_offset          number;<br \/>\n    v_buffer_size     binary_integer := 48;<br \/>\n    v_buffer_varchar  varchar2(48);<br \/>\n    v_buffer_raw      raw(48);<\/p>\n<p>  begin<br \/>\n    -- initialize<br \/>\n    dbms_lob.createtemporary(l_blob, true);<br \/>\n    l_offset := 1;<br \/>\n    retVal := 0;<\/p>\n<p>    -- remove CRLF<br \/>\n    l_clob := REPLACE(pImgData, chr(10), '');<br \/>\n    l_clob := REPLACE(l_clob, chr(13), '');<\/p>\n<p>    for i in 1 .. ceil(dbms_lob.getlength(l_clob) \/ v_buffer_size)<br \/>\n    loop<br \/>\n      dbms_lob.read(l_clob, v_buffer_size, l_offset, v_buffer_varchar);<br \/>\n      v_buffer_raw := utl_raw.cast_to_raw(v_buffer_varchar);<br \/>\n      v_buffer_raw := utl_encode.base64_decode(v_buffer_raw);<br \/>\n      dbms_lob.writeappend(l_blob, utl_raw.length(v_buffer_raw), v_buffer_raw);<br \/>\n      l_offset := l_offset + v_buffer_size;<br \/>\n    end loop;<\/p>\n<p>    insert into tbl_image<br \/>\n    (<br \/>\n      session,<br \/>\n      img_name,<br \/>\n      img_type,<br \/>\n      img_desc,<br \/>\n      img_data,<br \/>\n      data_ora<br \/>\n    )<br \/>\n    values<br \/>\n    (<br \/>\n      pSession,<br \/>\n      pImgName,<br \/>\n      pImgType,<br \/>\n      pImgDesc,<br \/>\n      l_blob,<br \/>\n      sysdate<br \/>\n    );<br \/>\n    commit;<br \/>\n    return retVal;<br \/>\n  end saveBase64toBlob;<br \/>\n<\/code><\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>NOTE: This article tells how to set up the script in Oracle APEX and doesn&#8217;t affect AnyChart customers who use AnyChart outside of Oracle APEX. Oracle Application Express is a web-based application development tool integrated with all editions of Oracle Database. It enables users with limited programming skills to easily develop secure web applications that [&hellip;]<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,14,4],"tags":[],"class_list":["post-1029","post","type-post","status-publish","format-standard","hentry","category-anychart-charting-component","category-oracle-apex","category-tips-and-tricks","wpautop"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Saving AnyChart 6 Charts as Images in Oracle APEX | AnyChart News<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.anychart.com\/blog\/2015\/09\/02\/save-as-image-oracle-apex\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Saving AnyChart 6 Charts as Images in Oracle APEX | AnyChart News\" \/>\n<meta property=\"og:description\" content=\"NOTE: This article tells how to set up the script in Oracle APEX and doesn&#8217;t affect AnyChart customers who use AnyChart outside of Oracle APEX. Oracle Application Express is a web-based application development tool integrated with all editions of Oracle Database. It enables users with limited programming skills to easily develop secure web applications that [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.anychart.com\/blog\/2015\/09\/02\/save-as-image-oracle-apex\/\" \/>\n<meta property=\"og:site_name\" content=\"AnyChart News\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/AnyCharts\" \/>\n<meta property=\"article:published_time\" content=\"2015-09-02T16:51:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-09-02T17:03:09+00:00\" \/>\n<meta name=\"author\" content=\"Timothy Loginov\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@AnyChart\" \/>\n<meta name=\"twitter:site\" content=\"@AnyChart\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Timothy Loginov\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.anychart.com\/blog\/2015\/09\/02\/save-as-image-oracle-apex\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2015\/09\/02\/save-as-image-oracle-apex\/\"},\"author\":{\"name\":\"Timothy Loginov\",\"@id\":\"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/a944a69f34509440ab4a9b5ead73caa6\"},\"headline\":\"Saving AnyChart 6 Charts as Images in Oracle APEX\",\"datePublished\":\"2015-09-02T16:51:38+00:00\",\"dateModified\":\"2015-09-02T17:03:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2015\/09\/02\/save-as-image-oracle-apex\/\"},\"wordCount\":349,\"commentCount\":1,\"articleSection\":[\"AnyChart Charting Component\",\"Oracle Apex\",\"Tips and Tricks\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.anychart.com\/blog\/2015\/09\/02\/save-as-image-oracle-apex\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.anychart.com\/blog\/2015\/09\/02\/save-as-image-oracle-apex\/\",\"url\":\"https:\/\/www.anychart.com\/blog\/2015\/09\/02\/save-as-image-oracle-apex\/\",\"name\":\"Saving AnyChart 6 Charts as Images in Oracle APEX | AnyChart News\",\"isPartOf\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/#website\"},\"datePublished\":\"2015-09-02T16:51:38+00:00\",\"dateModified\":\"2015-09-02T17:03:09+00:00\",\"author\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/a944a69f34509440ab4a9b5ead73caa6\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2015\/09\/02\/save-as-image-oracle-apex\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.anychart.com\/blog\/2015\/09\/02\/save-as-image-oracle-apex\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.anychart.com\/blog\/2015\/09\/02\/save-as-image-oracle-apex\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.anychart.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Saving AnyChart 6 Charts as Images in Oracle APEX\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.anychart.com\/blog\/#website\",\"url\":\"https:\/\/www.anychart.com\/blog\/\",\"name\":\"AnyChart News\",\"description\":\"AnyChart JS Charts\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.anychart.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/a944a69f34509440ab4a9b5ead73caa6\",\"name\":\"Timothy Loginov\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/00860a412d134244a5234231874065a30044d4d59f78aef4cca746e1e2dd3691?s=96&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/00860a412d134244a5234231874065a30044d4d59f78aef4cca746e1e2dd3691?s=96&r=g\",\"caption\":\"Timothy Loginov\"},\"url\":\"https:\/\/www.anychart.com\/blog\/author\/timothy-loginov\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Saving AnyChart 6 Charts as Images in Oracle APEX | AnyChart News","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.anychart.com\/blog\/2015\/09\/02\/save-as-image-oracle-apex\/","og_locale":"en_US","og_type":"article","og_title":"Saving AnyChart 6 Charts as Images in Oracle APEX | AnyChart News","og_description":"NOTE: This article tells how to set up the script in Oracle APEX and doesn&#8217;t affect AnyChart customers who use AnyChart outside of Oracle APEX. Oracle Application Express is a web-based application development tool integrated with all editions of Oracle Database. It enables users with limited programming skills to easily develop secure web applications that [&hellip;]","og_url":"https:\/\/www.anychart.com\/blog\/2015\/09\/02\/save-as-image-oracle-apex\/","og_site_name":"AnyChart News","article_publisher":"https:\/\/www.facebook.com\/AnyCharts","article_published_time":"2015-09-02T16:51:38+00:00","article_modified_time":"2015-09-02T17:03:09+00:00","author":"Timothy Loginov","twitter_card":"summary_large_image","twitter_creator":"@AnyChart","twitter_site":"@AnyChart","twitter_misc":{"Written by":"Timothy Loginov","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.anychart.com\/blog\/2015\/09\/02\/save-as-image-oracle-apex\/#article","isPartOf":{"@id":"https:\/\/www.anychart.com\/blog\/2015\/09\/02\/save-as-image-oracle-apex\/"},"author":{"name":"Timothy Loginov","@id":"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/a944a69f34509440ab4a9b5ead73caa6"},"headline":"Saving AnyChart 6 Charts as Images in Oracle APEX","datePublished":"2015-09-02T16:51:38+00:00","dateModified":"2015-09-02T17:03:09+00:00","mainEntityOfPage":{"@id":"https:\/\/www.anychart.com\/blog\/2015\/09\/02\/save-as-image-oracle-apex\/"},"wordCount":349,"commentCount":1,"articleSection":["AnyChart Charting Component","Oracle Apex","Tips and Tricks"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.anychart.com\/blog\/2015\/09\/02\/save-as-image-oracle-apex\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.anychart.com\/blog\/2015\/09\/02\/save-as-image-oracle-apex\/","url":"https:\/\/www.anychart.com\/blog\/2015\/09\/02\/save-as-image-oracle-apex\/","name":"Saving AnyChart 6 Charts as Images in Oracle APEX | AnyChart News","isPartOf":{"@id":"https:\/\/www.anychart.com\/blog\/#website"},"datePublished":"2015-09-02T16:51:38+00:00","dateModified":"2015-09-02T17:03:09+00:00","author":{"@id":"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/a944a69f34509440ab4a9b5ead73caa6"},"breadcrumb":{"@id":"https:\/\/www.anychart.com\/blog\/2015\/09\/02\/save-as-image-oracle-apex\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.anychart.com\/blog\/2015\/09\/02\/save-as-image-oracle-apex\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.anychart.com\/blog\/2015\/09\/02\/save-as-image-oracle-apex\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.anychart.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Saving AnyChart 6 Charts as Images in Oracle APEX"}]},{"@type":"WebSite","@id":"https:\/\/www.anychart.com\/blog\/#website","url":"https:\/\/www.anychart.com\/blog\/","name":"AnyChart News","description":"AnyChart JS Charts","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.anychart.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/a944a69f34509440ab4a9b5ead73caa6","name":"Timothy Loginov","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/00860a412d134244a5234231874065a30044d4d59f78aef4cca746e1e2dd3691?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/00860a412d134244a5234231874065a30044d4d59f78aef4cca746e1e2dd3691?s=96&r=g","caption":"Timothy Loginov"},"url":"https:\/\/www.anychart.com\/blog\/author\/timothy-loginov\/"}]}},"_links":{"self":[{"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/posts\/1029","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/comments?post=1029"}],"version-history":[{"count":30,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/posts\/1029\/revisions"}],"predecessor-version":[{"id":1061,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/posts\/1029\/revisions\/1061"}],"wp:attachment":[{"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/media?parent=1029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/categories?post=1029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/tags?post=1029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}