Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export charts as png/svg/pdf #20

Open
maxklenk opened this issue Oct 8, 2014 · 19 comments
Open

Export charts as png/svg/pdf #20

maxklenk opened this issue Oct 8, 2014 · 19 comments

Comments

@maxklenk
Copy link
Member

maxklenk commented Oct 8, 2014

Discussed here: c3js/c3#313
Working solution: https://github.com/times/axisJS

@maxklenk
Copy link
Member Author

maxklenk commented Nov 1, 2014

@dilignt
Copy link

dilignt commented Feb 3, 2015

I've tried to get this working with html2canvas. It works up to a point, ,but unfortunately the labels and axes aren't rendered correctly - see example below

image

@dilignt
Copy link

dilignt commented Feb 3, 2015

Further to my previous comment, the reason that it doesn't work properly is that html2canvas can't render the css styles unless they're defined inline. So by setting the styles inline on the elements, the chart can be rendered correctly using html2canvas.

FYI - I got my download png working with the following code to be triggered on ng-click of a button. The chart needs to be inside a div with id 'exportchart', and the download name would probably be set dynamically instead of sample.png

$scope.saveImage = function() {
    d3.selectAll("svg text").style({ 'font-size':'12px'});
    d3.selectAll(".c3-axis path").style({ 'fill':'none', 'stroke': '#000' });

    html2canvas(document.getElementById('exportchart'), {
        onrendered: function(canvas) {
            var canvasdata = canvas.toDataURL("image/png");
            var a = document.createElement("a");
            a.download = "sample.png";
            a.href = canvasdata;
            a.click();
        }
    });
}

Hope this is useful

@maxklenk
Copy link
Member Author

maxklenk commented Feb 4, 2015

@dilignt Thank you, I would like to link your solution in the documentation. Can you create a sample implementation on Plunker? You can use this version to build ontop (http://plnkr.co/edit/EWC4Hr)

@dilignt
Copy link

dilignt commented Feb 4, 2015

Here it is - http://plnkr.co/edit/wdiksu

On 4 February 2015 at 07:30, Max Klenk [email protected] wrote:

@dilignt https://github.com/dilignt Thank you, I would like to link
your solution in the documentation. Can you create a sample implementation
on Plunker? You can use this version to build ontop (
http://plnkr.co/edit/EWC4Hr)


Reply to this email directly or view it on GitHub
#20 (comment)
.

@maxklenk
Copy link
Member Author

maxklenk commented Feb 4, 2015

Amazing! Thank you very much.

@btm1
Copy link

btm1 commented Jun 19, 2015

I've implemented something similar but i get something to the effect of the attached: It's not picking up the fills for the bars which is pretty strange. Any ideas?

screen shot 2015-06-19 at 11 55 44 am

@maxklenk
Copy link
Member Author

The axes are normally set using the c3.css file, for export these styles have to be inlined. But I have no idea why the bars aren't filled.

@btm1
Copy link

btm1 commented Jun 19, 2015

I actually did this using canvg.js i had tried html2canvas in the past and it didn't work... this script works with charts just made with plane old D3 but for some reason the ones made with c3 have the not filled in bars issue.

@c5236786
Copy link

could you please rewrite plunker code ? i tried but the c3 chart i download is blank.

@dilignt
Copy link

dilignt commented May 30, 2016

The angular-chart dependency is no longer available which is why it's not working. I don't where there is a different CDN of angular-chart but if you download the plunker and replace angular-chart.js with a local dependency it should work

@maxklenk
Copy link
Member Author

You can link to angular-chart resources on github using https://rawgit.com/

@c5236786
Copy link

Hi @dilignt Thank i'll try that..

https://github.com/annatomka/ng-c3-export

i also referred this to download charts. Limitation is that it doesn't work on IE and Safari browsers.

@dilignt
Copy link

dilignt commented May 30, 2016

@maxklenk where is the rawgit angular-chart v0.2 resource? This v0.3 one has a different API, and I notice that the upgrade instructions doesn't mention how to upgrade

@maxklenk
Copy link
Member Author

@dilignt you can link to files from specific versions like this:
https://rawgit.com/GraFiddle/angular-chart/0.2.9/angular-chart.js
(in the web interface of GitHub choose a specific tag and browse the files)

And yes, between v0.2 and v0.3 there was an API change, can I help you what is not clear in the upgrade guide?

@dilignt
Copy link

dilignt commented May 30, 2016

Thanks @maxklenk. Where is the angular-chart.min.css file on rawgit? I tried https://rawgit.com/GraFiddle/angular-chart/0.2.9/angular-chart.min.css but this doesn't work.

The upgrade guide doesn't mention groups within the options - you describe changing the rows options and changing them to dimensions but nothing about groups so it's unclear what to do here. How would you convert this to 0.3?

    // configure your visualization
    $scope.options = {
      rows: [{
        key: 'data1'
      }, {
        key: 'data2'
      }],
      type: 'bar',
      groups: [
        ['data1', 'data2']
      ]
    };

    // describe your data
    $scope.schema = {};

    // provide a dataset
    $scope.dataset = [
      {
        data1: 1,
        data2: 4
      },
      {
        data1: 2,
        data2: 2
      }];

@maxklenk
Copy link
Member Author

The new structure looks like this:

  • data integrated in options
  • rows translated into keys of dimensions
  • other options rewritten in the c3.js style (types and groups are nested in data), we should document this step better I think
    $scope.options = {
      data: [
      {
        data1: 1,
        data2: 4
      },
      {
        data1: 2,
        data2: 2
      }],
      dimensions: {
        'data1': {},
        'data2': {}
      },
      chart: {
        data: {
          type: 'bar',
          groups: [
            ['data1', 'data2']
          ]
        }
      }
    };

I also updated the plunker http://plnkr.co/edit/JzkdibGX1rZUBvIGhAxq?p=preview

@dilignt
Copy link

dilignt commented May 30, 2016

Thanks for the clarification and the updated plunker. There's something not quite right about the chart styling - the background of the downloaded image is now black but it used to be white. I thought this was the css file change but I'm not sure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants