# Importing data for use with Google Charts

**URL:** <https://discuss.elastic.co/t/importing-data-for-use-with-google-charts/11440>\
**Category:** Elasticsearch\
**Created:** [April 4, 2013, 1:44am UTC](https://discuss.elastic.co/t/importing-data-for-use-with-google-charts/11440 "2013-04-04T01:44:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![OffensivelyBad\_2](https://avatars.discourse-cdn.com/v4/letter/o/51bf81/32.png) [@OffensivelyBad\_2](https://discuss.elastic.co/u/OffensivelyBad_2)\
**Post date:** [April 4, 2013, 1:44am UTC](https://discuss.elastic.co/t/importing-data-for-use-with-google-charts/11440/1 "2013-04-04T01:44:02Z")

</div>

I'm attempting to render a Google Chart based on an Elasticsearch query.  
Here is my non-working code:

```
// Load the Visualization API and the piechart package.
google.load('visualization', '1', {'packages':['corechart']});
  
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
  
function drawChart() {
  var jsonData = $.ajax({
      url: 'http://localhost:9200/inventory/_search?pretty=true'
           , type: 'POST'
           , data :

```

JSON.stringify(  
{  
"query" : { "match\_all" : {} },

```
                "facets" : {
                  "tags" : {
                    "terms" : {
                        "field" : "qty_onhand",
                        "size" : "10"
                    }
                  }
                }
              }),
      dataType:"json"
      async: false

```

,processData: false  
}).responseText;

```
  // Create our data table out of JSON data loaded from server.
  var data = new google.visualization.DataTable(jsonData);

  // Instantiate and draw our chart, passing in some options.
  var chart = new 

```

google.visualization.PieChart(document.getElementById('chart\_div'));  
chart.draw(data, {width: 400, height: 240});  
}

```
</script>

```

The issue I'm running into is that the data returned from the query is not  
the the "fields", but its the entire query summary, including:

{

- took: 10
- timed\_out: false
- \_shards: {
  - total: 5
  - successful: 5
  - failed: 0  
}

- hits: {
  - total: 11
  - max\_score: 1
  - hits: [

etc...

Is there a way to return this query while retaining only the fields? Or  
perhaps there is a way to query and format the data in a PHP file that I  
can then call in the chart?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 4, 2013, 1:51am UTC](https://discuss.elastic.co/t/importing-data-for-use-with-google-charts/11440/2 "2013-04-04T01:51:15Z")

</div>

Can you try with something like:

var data = new google.visualization.DataTable(jsonData.hits.hits);

## Does it help?

David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 4 avr. 2013 à 03:44, OffensivelyBad [shawnroller10@gmail.com](mailto:shawnroller10@gmail.com) a écrit :

> var data = new google.visualization.DataTable(jsonData);

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Karel\_Minarik\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/karel_minarik_2/32/1078_2.png) [@Karel\_Minarik\_2](https://discuss.elastic.co/u/Karel_Minarik_2)\
**Post date:** [April 4, 2013, 7:33am UTC](https://discuss.elastic.co/t/importing-data-for-use-with-google-charts/11440/3 "2013-04-04T07:33:47Z")

</div>

> var jsonData = $.ajax({

It's quite an anti-pattern to use Ajax (and JavaScript) synchronously like  
this. You should register a callbak for when your data is loaded.

Other then that, have you looked at the returned JSON response at all? I  
can see you're trying to draw a pie chart from the `terms` facet, which you  
named "tags". Do you see a corresponding part of the JSON response? Can you  
work with something like `jsonData.facets.tags.terms`?

By the way, there's an article on the Elasticsearch website about using  
facets for  
visualization: [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/2011/05/13/data-visualization-with-elasticsearch-and-protovis/)  
It's almost two years old, and uses Protovis, and antique framework, but  
the patterns and explanations still stand valid.

Karel

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![OffensivelyBad\_2](https://avatars.discourse-cdn.com/v4/letter/o/51bf81/32.png) [@OffensivelyBad\_2](https://discuss.elastic.co/u/OffensivelyBad_2)\
**Post date:** [April 5, 2013, 3:11pm UTC](https://discuss.elastic.co/t/importing-data-for-use-with-google-charts/11440/4 "2013-04-05T15:11:26Z")

</div>

I'll give that a shot Karel and David. Thanks.

If that doesn't work, what I'll end up doing is looping through a  
javascript function to format the data in a way that Google Charts  
recognizes it. Being new to most of this, I'll probably be flooding the  
javascript group here pretty soon!

On Thursday, April 4, 2013 3:33:47 AM UTC-4, Karel Minařík wrote:

> > var jsonData = $.ajax({
> 
> It's quite an anti-pattern to use Ajax (and JavaScript) synchronously like  
> this. You should register a callbak for when your data is loaded.
> 
> Other then that, have you looked at the returned JSON response at all? I  
> can see you're trying to draw a pie chart from the `terms` facet, which you  
> named "tags". Do you see a corresponding part of the JSON response? Can you  
> work with something like `jsonData.facets.tags.terms`?
> 
> By the way, there's an article on the Elasticsearch website about using  
> facets for visualization:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/2011/05/13/data-visualization-with-elasticsearch-and-protovis/It's) almost two years old, and uses Protovis, and antique framework, but  
> the patterns and explanations still stand valid.
> 
> Karel

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 2:42am UTC](https://discuss.elastic.co/t/importing-data-for-use-with-google-charts/11440/5 "2017-07-06T02:42:35Z")

</div>


