Jump to content
JChartFX Community
  • 0

Exporting a chart to an image


jpb

Question

18 answers to this question

Recommended Posts

  • 0

We are considering it, unfortunately when doing charts in HTML5 we chose SVG over Canvas. One of the things that would be very easy to do in Canvas would be to export the chart as an image. It is not as simple (AFAIK) when using SVG so we are trying to figure out how to support this in the future.

JuanC

Link to comment
Share on other sites

  • 0

Hi Luis. after including the canvg library, this is my code:

Ā 


Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  var svgH = this.jChart.S.parentNode.innerHTML;

Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  svgH = svgH.slice(svgH.indexOf('<svg'));

Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  if ( ( p = svgH.indexOf('<div') ) > 0)

Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  svgH = svgH.slice(0,p);

Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  svgH = svgH.replace(/sfxid=/g, ' sfxid=');

Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  svgH = svgH.replace(/sfxtitle=/g, ' sfxtitle=');

Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  this.canvas = document.createElement('canvas');

Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  canvg(this.canvas, svgH);

Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  var imgData = this.canvas.toDataURL('image/png');


Note that i have a class with a jChart attribute and a canvas attribute. Ā 

After that you have in imgData Ā the "data" url with the image. You can set it to an image src o do a window.location.href = imgData. In my case after that i send it to the server an the server is sending me it back to the browser with a filename.

To include the canvg library you need this:

<script type="text/javascript" src="http://canvg.googlecode.com/svn/trunk/rgbcolor.js"></script>Ā 

<script type="text/javascript" src="http://canvg.googlecode.com/svn/trunk/StackBlur.js"></script>

<script type="text/javascript" src="http://canvg.googlecode.com/svn/trunk/canvg.js"></script>Ā 

Ā 

Greetings.

PabloĀ 

Ā 

Ā 

Link to comment
Share on other sites

  • 0

Hi. I'm posting the full code of a working example of converting a chart svg to an image. Note that you have to modify the path to the jChartFX library in order to make it work.

Ā 


<html>

Ā  Ā  <head>

Ā  Ā  Ā  Ā Ā 

Ā  Ā  Ā  Ā  <script type="text/javascript" src="/js/libs/jchartfx.7.1.4889/js/jchartfx.system.js"></script>

Ā  Ā  Ā  Ā  <script type="text/javascript" src="/js/libs/jchartfx.7.1.4889/js/jchartfx.coreBasic.js"></script>

Ā  Ā  Ā  Ā  <script type="text/javascript" src="/js/libs/jchartfx.7.1.4889/js/jchartfx.animation.js"></script>

Ā  Ā  Ā  Ā Ā 

Ā  Ā  Ā  Ā  <script type="text/javascript" src="http://canvg.googlecode.com/svn/trunk/rgbcolor.js"></script>Ā 

Ā  Ā  Ā  Ā  <script type="text/javascript" src="http://canvg.googlecode.com/svn/trunk/StackBlur.js"></script>

Ā  Ā  Ā  Ā  <script type="text/javascript" src="http://canvg.googlecode.com/svn/trunk/canvg.js"></script>Ā 


Ā  Ā  Ā  Ā  <script language="Javascript">

Ā  Ā  Ā  Ā  Ā  Ā Ā 

window.onload = function () {

Ā  Ā  Ā  Ā  onLoadDoc();

Ā  Ā  }


Ā  Ā  var chart1;


Ā  Ā  function chart2Img(chart, img ) {

Ā  Ā  Ā  Ā  var svgH = chart.S.parentNode.innerHTML;


Ā  Ā  Ā  Ā  svgH = svgH.slice(svgH.indexOf('<svg'));

Ā  Ā  Ā  Ā  if ( ( p = svgH.indexOf('<div') ) > 0)

Ā  Ā  Ā  Ā  Ā  Ā  svgH = svgH.slice(0,p);


Ā  Ā  Ā  Ā  svgH = svgH.replace(/sfxid=/g, ' sfxid=');

Ā  Ā  Ā  Ā  svgH = svgH.replace(/sfxtitle=/g, ' sfxtitle=');


Ā  Ā  Ā  Ā  var canvas = document.createElement('canvas');


Ā  Ā  Ā  Ā  canvg(canvas, svgH);


Ā  Ā  Ā  Ā  var imgData = canvas.toDataURL('image/png');


Ā  Ā  Ā  Ā Ā 

Ā  Ā  Ā  Ā  img.src=imgData;


Ā  Ā  }

Ā  Ā Ā 

Ā  Ā  function onLoadDoc() {


Ā  Ā  Ā  Ā  chart1 = new cfx.Chart();


Ā  Ā  Ā  Ā  var items = [{

Ā  Ā  Ā  Ā  Ā  Ā  "Date": "2012-01-21T23:45:10.280Z",

Ā  Ā  Ā  Ā  Ā  Ā  "Value": 7,

Ā  Ā  Ā  Ā  Ā  Ā  "UpperLimit": 8,

Ā  Ā  Ā  Ā  Ā  Ā  "LowerLimit": 6

Ā  Ā  Ā  Ā  }, {

Ā  Ā  Ā  Ā  Ā  Ā  "Date": "2012-01-26T23:45:10.280Z",

Ā  Ā  Ā  Ā  Ā  Ā  "Value": 8,

Ā  Ā  Ā  Ā  Ā  Ā  "UpperLimit": 9,

Ā  Ā  Ā  Ā  Ā  Ā  "LowerLimit": 7

Ā  Ā  Ā  Ā  }, {

Ā  Ā  Ā  Ā  Ā  Ā  "Date": "2012-02-10T23:45:10.280Z",

Ā  Ā  Ā  Ā  Ā  Ā  "Value": 9,

Ā  Ā  Ā  Ā  Ā  Ā  "UpperLimit": 10,

Ā  Ā  Ā  Ā  Ā  Ā  "LowerLimit": 7

Ā  Ā  Ā  Ā  }, {

Ā  Ā  Ā  Ā  Ā  Ā  "Date": "2012-02-19T23:45:10.280Z",

Ā  Ā  Ā  Ā  Ā  Ā  "Value": 10,

Ā  Ā  Ā  Ā  Ā  Ā  "UpperLimit": 11,

Ā  Ā  Ā  Ā  Ā  Ā  "LowerLimit": 8

Ā  Ā  Ā  Ā  }, {

Ā  Ā  Ā  Ā  Ā  Ā  "Date": "2012-03-21T23:45:10.280Z",

Ā  Ā  Ā  Ā  Ā  Ā  "Value": 12,

Ā  Ā  Ā  Ā  Ā  Ā  "UpperLimit": 13,

Ā  Ā  Ā  Ā  Ā  Ā  "LowerLimit": 9

Ā  Ā  Ā  Ā  }, {

Ā  Ā  Ā  Ā  Ā  Ā  "Date": "2012-03-29T23:45:10.280Z",

Ā  Ā  Ā  Ā  Ā  Ā  "Value": 13,

Ā  Ā  Ā  Ā  Ā  Ā  "UpperLimit": 14,

Ā  Ā  Ā  Ā  Ā  Ā  "LowerLimit": 10

Ā  Ā  Ā  Ā  }, {

Ā  Ā  Ā  Ā  Ā  Ā  "Date": "2012-04-10T23:45:10.280Z",

Ā  Ā  Ā  Ā  Ā  Ā  "Value": 14,

Ā  Ā  Ā  Ā  Ā  Ā  "UpperLimit": 15,

Ā  Ā  Ā  Ā  Ā  Ā  "LowerLimit": 10

Ā  Ā  Ā  Ā  }, {

Ā  Ā  Ā  Ā  Ā  Ā  "Date": "2012-04-19T23:45:10.280Z",

Ā  Ā  Ā  Ā  Ā  Ā  "Value": 14,

Ā  Ā  Ā  Ā  Ā  Ā  "UpperLimit": 16,

Ā  Ā  Ā  Ā  Ā  Ā  "LowerLimit": 11

Ā  Ā  Ā  Ā  }, {

Ā  Ā  Ā  Ā  Ā  Ā  "Date": "2012-05-10T23:45:10.280Z",

Ā  Ā  Ā  Ā  Ā  Ā  "Value": 15,

Ā  Ā  Ā  Ā  Ā  Ā  "UpperLimit": 17,

Ā  Ā  Ā  Ā  Ā  Ā  "LowerLimit": 12

Ā  Ā  Ā  Ā  }, {

Ā  Ā  Ā  Ā  Ā  Ā  "Date": "2012-05-19T23:45:10.280Z",

Ā  Ā  Ā  Ā  Ā  Ā  "Value": 16,

Ā  Ā  Ā  Ā  Ā  Ā  "UpperLimit": 18,

Ā  Ā  Ā  Ā  Ā  Ā  "LowerLimit": 13

Ā  Ā  Ā  Ā  }];


Ā  Ā  Ā  Ā  var axisY = chart1.getAxisY();

Ā  Ā  Ā  Ā  axisY.setMin(4);

Ā  Ā  Ā  Ā  axisY.setMax(20);

Ā  Ā  Ā  Ā  chart1.setGallery(cfx.Gallery.Area);

Ā  Ā  Ā  Ā  chart1.getData().setSeries(2);

Ā  Ā  Ā  Ā  var series1 = chart1.getSeries().getItem(0);

Ā  Ā  Ā  Ā  var series2 = chart1.getSeries().getItem(1);

Ā  Ā  Ā  Ā  series2.setColor("#801702D1");

Ā  Ā  Ā  Ā  series2.getBorder().setWidth(4);

Ā  Ā  Ā  Ā  series1.setGallery(cfx.Gallery.Lines);


Ā  Ā  Ā  Ā  var legBox = chart1.getLegendBox();

Ā  Ā  Ā  Ā  legBox.setDock(cfx.DockArea.Bottom);

Ā  Ā  Ā  Ā  legBox.setContentLayout(cfx.ContentLayout.Center);



Ā  Ā  Ā  Ā  //----Assign data fields--------


Ā  Ā  Ā  Ā  var fields = chart1.getDataSourceSettings().getFields();

Ā  Ā  Ā  Ā  var field1 = new cfx.FieldMap();

Ā  Ā  Ā  Ā  var field2 = new cfx.FieldMap();

Ā  Ā  Ā  Ā  var field3 = new cfx.FieldMap();

Ā  Ā  Ā  Ā  var field4 = new cfx.FieldMap();


Ā  Ā  Ā  Ā  field1.setName("Date");

Ā  Ā  Ā  Ā  field1.setUsage(cfx.FieldUsage.XValue);

Ā  Ā  Ā  Ā  fields.add(field1);


Ā  Ā  Ā  Ā  field2.setName("Value");

Ā  Ā  Ā  Ā  field2.setUsage(cfx.FieldUsage.Value);

Ā  Ā  Ā  Ā  fields.add(field2);


Ā  Ā  Ā  Ā  field4.setName("LowerLimit");

Ā  Ā  Ā  Ā  field4.setUsage(cfx.FieldUsage.FromValue);

Ā  Ā  Ā  Ā  fields.add(field4);


Ā  Ā  Ā  Ā  field3.setName("UpperLimit");

Ā  Ā  Ā  Ā  field3.setDisplayName("Acceptable range");

Ā  Ā  Ā  Ā  field3.setUsage(cfx.FieldUsage.Value);

Ā  Ā  Ā  Ā  fields.add(field3);


Ā  Ā  Ā  Ā  //----Set Sample Data------------

Ā  Ā  Ā  Ā  chart1.setDataSource(items);


Ā  Ā  Ā  Ā  var chartDiv = document.getElementById('ChartDiv1');

Ā  Ā  Ā  Ā  chart1.create(chartDiv);


Ā  Ā  Ā  Ā  var img = document.getElementById('dstImg');

Ā  Ā  Ā  Ā Ā 

Ā  Ā  Ā  Ā  chart2Img(chart1,img);

Ā  Ā  }

Ā  Ā  Ā  Ā  </script>

Ā  Ā  </head>

Ā  Ā  <body>

Ā  Ā  Ā  Ā  <div id="ChartDiv1" style="width:500px;height:375px;display:inline-block"></div>

Ā  Ā  Ā  Ā  <img id="dstImg" src="" style="width:500px;height:375px;display:inline-block">

Ā  Ā  </body>

</html>


Regards,

PabloĀ 

Ā 

Ā 

post-2106-13939743335491_thumb.png

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...