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

Passing an object of type DataTable as a data attribute to google-chart does not work #329

Open
maxevilmind opened this issue Nov 3, 2022 · 2 comments

Comments

@maxevilmind
Copy link

maxevilmind commented Nov 3, 2022

Passing an object of type DataTable as a data attribute to google-chart does not work.
In the network tab I see a request to localhost that looks like this:
http://localhost:9001/[object%20Object]

  /* istanbul ignore file */
  /* eslint-disable class-methods-use-this */
  import { LitElement } from 'lit-element';
  import { html } from 'lit-html';

  import '@google-web-components/google-chart';
  import { dataTable } from '@google-web-components/google-chart/loader.js';

  export class WbWebGeoChart extends LitElement {
    static get properties() {
      return {
        myDataTable: Object,
      };
    }

    async connectedCallback() {
      super.connectedCallback();
      this.myDataTable = await dataTable();
      this.myDataTable.addColumn('string','Country');
      this.myDataTable.addColumn('number','Popularity');
      this.myDataTable.addRow(['Germany', 1]);
    }

    render() {
      return html`
        <google-chart type="geo" data="${this.myDataTable}"></google-chart>
      `;
    }
  }

Documentation in google-chart.js says The data format can be a two-dimensional array or the DataTable format * expected by Google Charts.

What happens actually:

    try {
        // Try to deserialize the value of the `data` property which might be a
        // serialized array.
        data = JSON.parse(data);
    }
    catch (e) {
        isString = typeof data === 'string' || data instanceof String;
    }
    if (isString) {
        // Load data asynchronously, from external URL.
        dataPromise = fetch(data).then(response => response.json());
    }

In reality you just check if data is string and if not you fetch it (???)
This logic is not documented and looks a bit broken.

Was it done intentionally that you don't support passing of DataTable here?

@rslawik
Copy link
Contributor

rslawik commented Nov 5, 2022

Hi @maxevilmind

A DataTable object cannot be passed as an attribute, it must be passed as a property: .data=${this.myDataTable}, see https://lit.dev/docs/v1/components/templates/#bind-properties-to-templated-elements.

There is documentation about the string at the class level: https://github.com/GoogleWebComponents/google-chart/blob/main/google-chart.ts#L100. It would be useful to explain that on the data property as well. Would you like to contribute that? And maybe add a demo that assigns a data table using a template?

@maxevilmind
Copy link
Author

@rslawik ehh, I probably was too tired that day passing object as an attribute...
Good to know contributions are welcome,
I will try to find some time to work out a PR

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

No branches or pull requests

2 participants