HubDB Google Charts

SOLVE
cives
Participant

Hi all — I need a little assistance on the google chart below; it's just about complete, though I'm getting hung up on choice.row_count. 

 

As I loop through cat in category I need it to also return the number of table rows that have that specific category; ideally rendering something like:

 

['Body Shop', 12],

['Car Dealer', 71]

 

etc. etc.. 

 

Any thoughts? Thank you.

 

	<script src="https://www.gstatic.com/charts/loader.js" type="text/javascript">
	</script>
	<script type="text/javascript">
	     google.charts.load("current", {packages:["corechart"]});
	     google.charts.setOnLoadCallback(drawChart);
	     function drawChart() {
	       var data = google.visualization.arrayToDataTable([
	         
	                           {% set table_info = hubdb_table_rows(2666166) %}
	                                     {% set category = [] %}       
	                   {% for row in table_info %}
	                    
	                     {% set option = row.member_category %}
	                     {% for choice in option %}
	                     {% do category.add(choice) %}

	                     {% endfor %}
	                   {% endfor %}
	         
	         
	         
	         
	         ['Members', 'Type'],
	         

	        {% for cat in category|unique('name') %}
	         ['{{ cat.name }}',    {{ choice.row_count }} ],
	         {% endfor %}
	       ]);

	       var options = {  
	         pieHole: 0.4,
	       };

	       var chart = new google.visualization.PieChart(document.getElementById('donutchart'));
	       chart.draw(data, options);
	     }
	</script>
	
	<div id="donutchart" style="width: 500px; height: 500px;"></div>

 

0 Upvotes
1 Accepted solution

Accepted Solutions
Indra
Solution
Top Contributor | Diamond Partner

@Hi @cives,

 

When getting table info from a column, you can use the column info by using .options.

 

So your code will look like this:

{% set categories = hubdb_table_column(module.db , 'member_category').options %}

 

The only tricky part is that it does not have a build in function to get the amount of items/rows with it. So you have to loop through the object to count the items.

 

So your code will look something like this:

{% set db = module.db %}
{% set table_info = hubdb_table_rows(db) %}
{% set cat_field = 'member_category' %}
{% set categories = hubdb_table_column(module.db , cat_field).options %}

[
{% for category in categories %}

  {# Setup count for current term #}
  {% set count = {'value': 0} %}
  {# Loop through the table #}
  {% for row in table_info %}
    {# Set counts and add up 1 #}
    {% set counts = count.value|int + 1 %}
    {% for cat in row[cat_field] %}
      {# Check if row category is equal to current category #}
      {% if category.id == cat.id %}
        {# Update counts #}
        {% do count.update({ 'value': counts }) %}
      {% endif %}
    {% endfor %}
  {% endfor %}
  '{{ category.name }}', '{{ count.value }}'{% if !loop.last %},{% endif %}
{% endfor %}
]

 

Did this answered your question? If so, please mark your question as resolved.


Bureau Vet

Did my post solve your question? Help the community by marking it as a solution

View solution in original post

2 Replies 2
Indra
Solution
Top Contributor | Diamond Partner

@Hi @cives,

 

When getting table info from a column, you can use the column info by using .options.

 

So your code will look like this:

{% set categories = hubdb_table_column(module.db , 'member_category').options %}

 

The only tricky part is that it does not have a build in function to get the amount of items/rows with it. So you have to loop through the object to count the items.

 

So your code will look something like this:

{% set db = module.db %}
{% set table_info = hubdb_table_rows(db) %}
{% set cat_field = 'member_category' %}
{% set categories = hubdb_table_column(module.db , cat_field).options %}

[
{% for category in categories %}

  {# Setup count for current term #}
  {% set count = {'value': 0} %}
  {# Loop through the table #}
  {% for row in table_info %}
    {# Set counts and add up 1 #}
    {% set counts = count.value|int + 1 %}
    {% for cat in row[cat_field] %}
      {# Check if row category is equal to current category #}
      {% if category.id == cat.id %}
        {# Update counts #}
        {% do count.update({ 'value': counts }) %}
      {% endif %}
    {% endfor %}
  {% endfor %}
  '{{ category.name }}', '{{ count.value }}'{% if !loop.last %},{% endif %}
{% endfor %}
]

 

Did this answered your question? If so, please mark your question as resolved.


Bureau Vet

Did my post solve your question? Help the community by marking it as a solution

View solution in original post

cives
Participant
Worked like a charm. Thank you!
0 Upvotes