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

Add an arrayToDataTable helper #10

Closed
sir-dunxalot opened this issue Feb 1, 2016 · 1 comment
Closed

Add an arrayToDataTable helper #10

sir-dunxalot opened this issue Feb 1, 2016 · 1 comment

Comments

@sir-dunxalot
Copy link
Owner

import Ember from 'ember';

/**
Takes an array of data points like [1,2,5,4,5,5,2,1] and
returns the count ({1:2, 2:2, 4:1, 5:3}) of each item for
Google Charts to consume.

@method arrayToDataTable
@param Array dataArray An array of items
@param String [title] An optional column header
@return Object The count of items in the array
*/

export function arrayToDataTable([dataArray = [], title]/*, hash*/) {
  const counter = dataArray.reduce((object, value) => {

    if (!object[value]) {
      object[value] = 1;
    } else {
      object[value]++;
    }

    return object;
  }, {});

  const dataTable = [[title, 'Number of occurences']];

  for (let value in counter) {
    const count = counter[value];

    dataTable.addObject([ value, count ]);
  }

  return dataTable;
}

export default Ember.Helper.helper(arrayToDataTable);
@sir-dunxalot
Copy link
Owner Author

sir-dunxalot commented Sep 10, 2018

Apps now havewindow.google.visualization.arrayToDataTable(data) available and that seems like a better option than this helper.

If you haven't rendered a chart yet, you can load the necessary packages by calling loadPackages() in the GoogleCharts service.

This might be done like:

import Component from '@ember/component';
import { getOwner } from '@ember/application';

export default Component.extend({
  formatData: async function() {
    const service = getOwner(this).lookup('service:google-charts');

    await service.loadPackages();

    /* window.google.visualization should be available now */
  },
});

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

1 participant