Last updated February 2, 2011. Created by xqbzzr on February 2, 2011.
Log in to edit this page.

This snippet displays the percentage of nodes with a certain term in relation to the total node count of that type. It will output a simple bar diagram.

Besides the Views2 module you will need http://drupal.org/project/views_customfield. This allows to create custom php fields..

  1. Create a new view of type "term".
  2. Add the field "taxonomy: term". Check "Exclude from display"
  3. Add the field "taxonomy: term ID". Check "Exclude from display"
  4. Add a filter "taxonomy: vocabulary" to filter results of a certain vocabulary.
  5. If you are working with hierarchical vocabularies, the calculation of percentages may feel a bit awkward. You might want to add additional filters to exclude parent terms.
  6. Add a field "taxonomy: vocabulary-name".(this will be used later for grouping)
  7. Up to now the view is completely empty. We only just loaded the data we will use in the next step.

  8. Add a new field "custumfield". Label: empty. Check "output this field as a link" and in link-path insert taxonomy/term/[tid] in alt-text insert [name]. (This will only be possible if the field for the term-name is above this field!(check step 2))
  9. Check "Count the number 0 as empty" and "hide if empty".
  10. Enter this code in the value-region:
    <?php $nodecount_bytype=db_result(db_query("SELECT COUNT(nid) FROM {node} WHERE type= 'XYZ'")); //count all nodes of a certain type(replace XYZ with your nodetype name)?>
    <?php $nodecount_byterm=taxonomy_term_count_nodes($data->tid, $type = 0); //count all nodes on this term?>
    <?php $nodecount_percent=($nodecount_byterm/$nodecount_bytype)*100; //calculate percentage?>
    <?php if ($nodecount_byterm!='0' ) { //check if any nodes for this term exist ?>
    <div class="percent-bg" style="width: 100%; background: silver; margin-bottom: 5px; ">
    <div class="percent-bar" style="width: <?php echo($nodecount_percent);?>%; background: orange; overflow: visible; white-space: nowrap;">
    <span class="percent-text" style="padding-left: 5px;">
    <?php echo ($data->term_data_name.': '.number_format($nodecount_percent,2).' Percent');?>
    </span>
    </div>
    </div>
    <?php  } else echo '0'; //output zero for terms with no nodes and hide that term?>

    The first row defines the nodetype. Replace "XYZ" with the machine-name of your type.
  11. Choose "View Style" -> "Unformatted" wählen.
  12. In the style options (the little gear-icon) choose the grouping-field "taxonomy:vokabularyname".(check step 6)

The percentage-bars have css classes to access them from the theme. If you are happy with this simple display, you can replace the colors "silver" and "orange" with your own colors(in hexadecimal-value).
Probably this query is not very database-friendly with regard to performance, you might want to use http://drupal.org/project/term_node_count instead.
The advantage is, you will not need any additional module.
I hope this snippet is of any help.
Good luck!

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.