chart icon indicating copy to clipboard operation
chart copied to clipboard

Support writing the HTML to a file

Open jerrypnz opened this issue 7 years ago • 11 comments

Maybe introduce an command line option -o or something like that, which is followed by a file path for writing the chart to. It can be super useful for generating a lot of charts using scripts.

jerrypnz avatar Jul 03 '17 10:07 jerrypnz

I've been keen to do this myself as well. A few considerations:

  • Do we not need some additional modifier to just return the current supported source code of Chart.js? The chart code alone will not work without the library, and while the user is free to include it himself it'd be better to have a supported version output by chart itself.
  • What exactly should be returned by chart? I'd go for <div></div><script>...</script> tags, the div containing the canvas and the script containing the data.

marianogappa avatar Jul 03 '17 22:07 marianogappa

I think what we're looking at is a way to save it as an image directly.

jprobichaud avatar Nov 03 '17 18:11 jprobichaud

@jprobichaud that's an interesting use case. We've had a one-on-one chat with @moonranger a few months ago and his use case was a throw-away dashboard. Think of it as a poor man's Grafana. Btw, I've convinced @kuraio to write the code for this feature soon.

marianogappa avatar Nov 05 '17 04:11 marianogappa

Thanks a lot, this is very appreciated!

jprobichaud avatar Nov 07 '17 14:11 jprobichaud

@jprobichaud, I would argue that saving directly as an image in some ways puts requirements at odds with chart, because the library has only minimal support, as far as I know, in its API for binary encoding to a PNG format. It is not that it is wrong tool for the job, just that perhaps as currently implemented it is not very suitable. Certainly even less suitable if you wanted to export to say PDF.

szaydel avatar Nov 07 '17 16:11 szaydel

I see. Currently, I'm using chart from the command line to generate simple charts to add to wiki pages. I simply right-click on the canvas in the web browser and do "save image" and get the png out (then go through imagemagick to change the transparent background into a solid white background). It's all good, but it would be nice of we could do that in shot.

The simplicity of the interface compared to more powerful tools like gnuplot is appealing but the lack of automation to get the non-dynamic "picture" of the graph is a limiting factor.

jprobichaud avatar Nov 07 '17 18:11 jprobichaud

@jprobichaud: I'm afraid @szaydel is right about the complexity of adding this image export feature: chart outputs HTML/JS and relies on ChartJS for actually drawing the chart; adding image export would require an entirely separate output framework that would need to be feature-by-feature compatible with ChartJS. It's probably not happening.

You could try this instead, though: https://medium.com/@dschnr/using-headless-chrome-as-an-automated-screenshot-tool-4b07dffba79a I'm sure you can automate your screenshots. You can also batch resize if the screenshot doesn't provide this feature.

marianogappa avatar Nov 07 '17 20:11 marianogappa

I think realistically unless folks maintaining Chart.js add more robust support to export image data, adding that to chart is probably not a good idea. You are likely going to have to substantially add to complexity while also dealing with new cross-compatibility issues and such.

szaydel avatar Nov 07 '17 21:11 szaydel

Agreed, I understand the concerns. I guess then the original intent of this ticket to have the ability to specify the output filename (instead of a random tmp filename) would still be of actuality.

jprobichaud avatar Nov 07 '17 22:11 jprobichaud

@jprobichaud: If you are still looking, I pushed a small changeset, which admittedly can use some refinement, with support for writing files to a given path. There is a -filename option, which, given a valid path, i.e. directory needs to exist, and if file exists it will be overwritten, chart data will be placed into the file identified by the path.

$ chart -filename '/some/known/existing/location/mychart.html'

You should be able to grab the code here and build it identically to how you build code form this repository. Branch with changeset

Let me know if this works for you. I hope it is at least in the ballpark.

szaydel avatar Nov 15 '17 01:11 szaydel

@szaydel Thanks

TonyStark avatar May 10 '22 03:05 TonyStark