GoogleWebComponents / google-chart

Google Charts API web components
https://www.webcomponents.org/element/@google-web-components/google-chart/elements/google-chart
Apache License 2.0
355 stars 129 forks source link

access to google.visualization.DateFormat with 1.1.0? #142

Open raphaelsavina opened 8 years ago

raphaelsavina commented 8 years ago

Since I've updated to 1.1.0 I can't access DateFormat. I might need to use the new google-chart-loader but the docs, readme and release notes didn't give any indication of that.

My dates are as EPOC (1470240006293) (as var d in this example), I was using:

var dateFormatter = new google.visualization.DateFormat({
   pattern: "d/MM H:mm"
});
var theDate = [dateFormatter.formatValue(new Date(parseInt(d)))];

It throws: my-charts.html:102 Uncaught TypeError: Cannot read property 'DateFormat' of undefined

...maybe I've removed something else while I was updating.

wesalvaro commented 8 years ago

You are getting that error because the library has not been loaded yet. I'm kind of glad you're getting that error, now as it reveals a race condition that was very hard to debug with the previous version. My team suffered a huge setback because of it.

Currently, a few objects (DataTable, DataView, Query) are exposed, but there are others that may be desired.

Do you have an idea for a nice way to expose the other types?

Currently, you can do this:

document.createElement('google-chart-loader').dataTable().then(dataTable => {
  let formatter = new google.visualization.DateFormat({
    pattern: "d/MM H:mm"
  });
  let now = new Date();
  console.log(formatter.formatValue(now));
});

You could also access the (currently private) promise for the corechart package:

document.createElement('google-chart-loader')._corePackage.then(viz => {
  let formatter = new viz.DateFormat({
    pattern: "d/MM H:mm"
  });
  let now = new Date();
  console.log(formatter.formatValue(now));
});

Neither of these situations are ideal, but I'm not sure what a good way to do it is.

raphaelsavina commented 8 years ago

That worked (1st one), thanks!

Maybe having flag(s) like googleChart.drawn for the useful objects... would be a nicer solution.

wesalvaro commented 8 years ago

The problem with using drawn is that you probably need the formatter to create the data set. drawn isn't true until the chart data is loaded and the data is actually drawn.