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 JSON Chart Specification Only #269

Closed
cbailiss opened this issue Feb 16, 2017 · 4 comments
Closed

Export JSON Chart Specification Only #269

cbailiss opened this issue Feb 16, 2017 · 4 comments
Labels

Comments

@cbailiss
Copy link

I am looking to generate charts in R, then take the chart definitions created by highcharter and use them in multiple different places - rendered on the web via javascript, also to image files (both svg and png) and to other "hardcopy" formats like PDF.

To do this, ideally I want to easily be able to retrieve the underlying High Charts JSON chart definition in R, i.e. generate a chart in R, preview it in RStudio in the normal way, then export the JSON and use that in different ways depending on the desired output format.

The export_hc function is almost what I am looking for. The one niggle is that the export_hc output actually includes javascript as well as the JSON chart definition. It would be cleaner to have an option to just get the JSON, e.g. the vegalite R package has a to_spec() function which just returns the JSON, with no javascript around it.

It looks like hc$x$hc_opts is actually the high charts JSON stored as R objects (rather than a string). So I can (at least in my quick test) get the JSON specification by using:
toJSON(hc$x$hc_opts, auto_unbox = TRUE, pretty = TRUE)
However, this feels like reading the internals of the hc object. This sort of approach might lead to trouble in the future:
In my test, the above toJSON call exactly matched the highcharts JSON, but I am not sure if this is true for all chart types or all options, etc.
Also, it may be that you change the way the hc_opts object works in the future. Ideally, a export_specification() method would be handy! This would provide a simple, clean, supported way of getting the High Charts JSON without needing to read the internals of an R object.

Hopefully this request can be reasonably easily implemented, as it looks like a subset of the existing export_hc() functionality.

@jbkunst
Copy link
Owner

jbkunst commented Feb 16, 2017

Hi @cbailiss :)!

Yes, what are you doing is exactly what you want, in fact that's what export_hc function do: use toJSON over hc$x$hc_opts (+ other details). All the data is there, there are some other exported parameters like treemaplayout, enabledButtons because this options work with plugins that non affect the usual behavior, so if you want, you can remove them from your highchart objects before use toJSON or export_hc.

The export_hc functions in the dev version (install via source("https://install-github.me/jbkunst/highcharter")) have some parameters to export the object in differents ways (#247), for example:

  • export_hc(hc, filename = "~/hc_is.js", as = "is")
// Generated by highcharter 2017-02-16 09:59:30
{
  title: {
    text: "Monthly Average Temperature"
  },
  yAxis: {
    title: {
      text: "Temperature"
    }
  },
...
  • export_hc(hc, filename = "~/hc_vr.js", as = "variable", name = "objectname")
// Generated by highcharter 2017-02-16 09:59:30
objectname = {
  title: {
    text: "Monthly Average Temperature"
  },
  yAxis: {
    title: {
      text: "Temperature"
    }
  },
...
  • export_hc(hc, filename = "~/hc_ct.js", as = "container", name = "#selectorid")
// Generated by highcharter 2017-02-16 09:59:30
$(function(){
	$('#selectorid').highcharts(
{
  title: {
    text: "Monthly Average Temperature"
  },
  yAxis: {
    title: {
      text: "Temperature"
    }
  },
...

Now, for export as svg image/pdf image/ or evend the data: are you aware about the exporting/exporintg-csv plugin http://jkunst.com/highcharter/plugins.html#exporting-csv? Obviously this is final user oriented, not sure if this helps.

Tell me if this more clearer,

@cbailiss
Copy link
Author

Thanks very much. Great to hear this enhancement is already in progress. Those additional arguments to the export_hc function (in particular as="is") should work for me. I'll install the dev version and test. When is the dev version likely to be pushed to CRAN?
One other thought... is the "// Generated by highcharter... " comment strictly allowed in JSON? (OK in javascript, but not sure about JSON). I wonder how the highcharts export server might handle the comment when it is given a JSON spec containing the comment? Hopefully it will just ignore it...

@jbkunst
Copy link
Owner

jbkunst commented Feb 16, 2017

Possibly next month in a maintenance release.

I'll modify export_hc to remove the // Generated ... part due http://stackoverflow.com/a/4183018/829971.

Thanks

@cbailiss
Copy link
Author

Thanks for that. I have nothing to further to add on this.
I'll leave the issue open so you can track the work for removing the comment from the export_hc JSON.

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

No branches or pull requests

2 participants