<?php
/**
 * Table to Chart example
 */

/**
 * Implements hook_menu().
 */
function tablechart_example_menu() {
  $items['examples/table_chart'] = array(
    'title' => 'Table Chart Examples',
    'page callback' => 'tablechart_example_page',
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
  );

  return $items;
}

/**
 * Page callback
 */
function tablechart_example_page() {
  $output = '';

  // Add the required libraries
  drupal_add_library('table_chart', 'tabletochart');

  $tables = tablechart_example_tables();

  foreach ($tables as $key => $variables) {
    $output .= "<div class='table-chart'>";
    $output .= theme('table', $variables);
    $output .= '</div>';
  }
  return $output;
}

/**
 * Example table configuration
 */
function tablechart_example_tables() {
  $items = array();

  $items['tablechart_example_bar'] = array(
    'header' => array(
      array('data' => t('Phone')),
      array('data' => t('Sales (Millions)')),
      array('data' => t('Storage')),
      array('data' => t('Price')),
    ),
    'rows' => array(
      array('iPhone 4s', 100, '8GB', 199),
      array('iPhone 5', 200, '16GB', 199),
      array('iPhone 5s', 300, '16GB', 199),
      array('iPhone 6', 400, '16GB', 199),
      array('iPhone 6Plus', 500, '16GB', 299),
    ),
    'attributes' => array(
      'data-morris-type' => 'bar',
      'data-morris-resize' => 'true',
      'data-morris-xkey' => 'Phone',
      'data-morris-ykeys' => 'Price,Storage,Sales (Millions)',
    ),
    'sticky' => FALSE,
  );

  $items['tablechart_example_line'] = array(
    'header' => array(
      array('data' => t('Phone')),
      array('data' => t('Sales (Millions)')),
      array('data' => t('Storage')),
      array('data' => t('Price')),
    ),
    'rows' => array(
      array('iPhone 4s', 100, '8GB', 199),
      array('iPhone 5', 200, '16GB', 199),
      array('iPhone 5s', 300, '16GB', 199),
      array('iPhone 6', 400, '16GB', 199),
      array('iPhone 6Plus', 500, '16GB', 299),
    ),
    'attributes' => array(
      'data-morris-type' => 'line',
      'data-morris-resize' => 'true',
      'data-morris-xkey' => 'Sales (Millions)',
      'data-morris-ykeys' => 'Price,Storage',
      'data-morris-labels' => 'Price (USD), Base Storage (GB)',
      'data-morris-parseTime' => 'false',
    ),
    'sticky' => FALSE,
  );

  $items['tablechart_example_area'] = array(
    'header' => array(
      array('data' => t('Phone')),
      array('data' => t('Sales (Millions)')),
      array('data' => t('Storage')),
      array('data' => t('Price')),
    ),
    'rows' => array(
      array('iPhone 4s', 100, '8GB', 0),
      array('iPhone 5', 200, '16GB', 99),
      array('iPhone 5s', 300, '16GB', 199),
      array('iPhone 6', 400, '16GB', 199),
      array('iPhone 6Plus', 500, '16GB', 299),
    ),
    'attributes' => array(
      'data-morris-type' => 'area',
      'data-morris-resize' => 'true',
      'data-morris-xkey' => 'Sales (Millions)',
      'data-morris-ykeys' => 'Price,Storage',
      'data-morris-labels' => 'Price (USD), Base Storage (GB)',
      'data-morris-parseTime' => 'false',
    ),
    'sticky' => FALSE,
  );

  $items['tablechart_example_donut'] = array(
    'header' => array(
      array('data' => t('label')),
      array('data' => t('value')),
    ),
    'rows' => array(
      array('iPhone 4s', 16),
      array('iPhone 5', 64),
      array('iPhone 5s', 64),
      array('iPhone 6', 128),
      array('iPhone 6Plus', 128),
    ),
    'caption' => t('Maximum data storage (GB)'),
    'attributes' => array(
      'data-morris-type' => 'donut',
      'data-morris-resize' => 'true',
    ),
    'sticky' => FALSE,
  );
  return $items;
}