Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @file
- * Custom graphing functionality for the EconRSA website
- *
- */
- // Where we store our graph source files
- define('ECONRSA_GRAPHS_FILE_PATH', 'sites/default/files/private/graphs');
- define('ECONRSA_GRAPHS_INTERVAL_MONTHLY', 30 * 24 * 3600 * 1000);
- define('ECONRSA_GRAPHS_INTERVAL_QUARTERLY', 3 * 30 * 24 * 3600 * 1000);
- define('ECONRSA_GRAPHS_INTERVAL_YEARLY', 12 * 30 * 24 * 3600 * 1000);
- define('ECONRSA_GRAPHS_THRESHOLD_DEFAULT', 2);
- define('ECONRSA_GRAPHS_PARAMETERS_DIR', 'parameters');
- define('ECONRSA_GRAPHS_MPV_BASE_PATH', 'ersagraphs/mpv');
- /**
- * Implementation of hook_perm()
- */
- function econrsa_graphs_perm() {
- return array('view ersa graphs');
- }
- /**
- * Implementation of hook_permission().
- */
- function econrsa_graphs_permission() {
- return array(
- 'access ersa graphs' => array(
- 'title' => t('Administer ERSA Graphs'),
- 'description' => t('Access the ERSA Graphs admin section.'),
- ),
- 'view ersa graphs' => array(
- 'title' => t('View ERSA graphs'),
- 'description' => t('View any of the ERSA graphs.'),
- ),
- );
- }
- /**
- * Implementation of hook_menu()
- */
- function econrsa_graphs_menu() {
- $items[ECONRSA_GRAPHS_MPV_BASE_PATH . '/%/%/view'] = array(
- 'title' => 'Macroeconomic Volatility Project Graphs',
- 'page callback' => 'econrsa_graphs_show_graph',
- 'page arguments' => array(2, 3),
- 'access arguments' => array('view ersa graphs'),
- );
- $items[ECONRSA_GRAPHS_MPV_BASE_PATH . '/list'] = array(
- 'title' => 'Macroeconomic Volatility Project Graphs (listing)',
- 'page callback' => 'econrsa_graphs_display_menu',
- 'access arguments' => array('view ersa graphs'),
- );
- $items[ECONRSA_GRAPHS_MPV_BASE_PATH . '/about'] = array(
- 'title' => 'Macroeconomic Volatility Project',
- 'page callback' => 'econrsa_graphs_display_about',
- 'access arguments' => array('access content'),
- );
- return $items;
- }
- /**
- * Function to display a listing of graphs
- */
- function econrsa_graphs_display_menu() {
- $description = "<div class='econrsa-graphs-description'>";
- $description .= t('Please click on any of the following links to go to the respective pressure variable graphs. The graphs will allow users to select the desired threshold levels. The pressure variable graphs are further explained briefly in the page. Users will be able see component graphs (weighted) in the same page to further elaborate their effects on the pressure variable.');
- $description .= "</div>";
- $graphs = econrsa_graphs_load_graph_filenames();
- $list_items = array();
- foreach($graphs as $graph) {
- $list_items[] = l($graph['title'], 'ersagraphs/mpv/' . $graph['filename'] . '/' . $graph['type'] . '/view');
- }
- $output = array(
- 'graphs_menu' => array(
- '#type' => 'markup',
- '#markup' => $description . theme('item_list', array('items' => $list_items)),
- ),
- );
- return $output;
- }
- /**
- * Display information about the graph feature.
- */
- function econrsa_graphs_display_about() {
- global $user;
- if(user_is_logged_in()) {
- if (user_access('view ersa graphs')) {
- $link = l(t('Access the graphs here.'), ECONRSA_GRAPHS_MPV_BASE_PATH . '/list');
- }
- else {
- $link = t('Unfortunately, your user role does not permit you to access the graphs.');
- }
- }
- else {
- $message = t('Please log in to access the graphs.');
- $link = l($message, 'user/login', array('query' => array('destination' => ECONRSA_GRAPHS_MPV_BASE_PATH . '/list'), 'external' => FALSE));
- }
- $description_text = t('The pressure indicators aim at developing an early
- warning mechanism on the macroeconomic conditions of
- various types of South Africa. The current indicators
- use historical data to analyze the South African economy
- from seven perspectives. The current calculations and
- graph aim to point out years with abnormal deviations
- for the economic indicators which would theoretically
- suggest pressure scenarios. This project is a work in
- progress and aims to give users a look into historical
- macroeconomic deviations, especially the years when
- South Africa experienced abnormal shocks. For current
- use, these indexes could assist users to take a deeper
- look at the historical development of the South African
- economy, and analyze variables within each indicator
- that led to these shock moments. A key feature of the
- graphics is the facility to break down each indicator
- into its sub-components which allows users to study in
- more detail the composition of macroeconomic pressure.');
- $content = "<p>$description_text</p>";
- $content .= '<p>' . $link . '</p>';
- $contact = l('send us an email', 'mailto:Yoemna.Mosaval@econrsa.org');
- $content .= spamspan(t('If you do not have a user account and you require access to the graphs, please !contact_us.', array('!contact_us' => $contact)));
- $output = array(
- 'econrsa_graphs_about' => array(
- '#type' => 'markup',
- '#markup' => $content,
- ),
- );
- return $output;
- }
- /**
- * Return a list of all graphs we want to display
- */
- function econrsa_graphs_load_graph_filenames() {
- $graphs = array(
- array('title' => t('Bank Pressure Indicator 1 Combined MPV (excluding non-performing loans)'),
- 'filename' => 'bank-pressure-indicator-1-without-np-combined-values',
- 'type' => 'combined',
- ),
- array('title' => t('Bank Pressure Indicator 1 Combined MPV (including non-performing loans)'),
- 'filename' => 'bank-pressure-indicator-1-with-np-combined-values',
- 'type' => 'combined',
- ),
- array('title' => t('Bank Pressure Indicator 2 Combined MPV (excluding non-performing loans)'),
- 'filename' => 'bank-pressure-indicator-2-without-np-combined-values',
- 'type' => 'combined',
- ),
- array('title' => t('Bank Pressure Indicator 2 Combined MPV (including non-performing loans)'),
- 'filename' => 'bank-pressure-indicator-2-with-np-combined-values',
- 'type' => 'combined',
- ),
- array('title' => t('Capital Account Pressure Indicator Combined MPV'),
- 'filename' => 'capital-account-pressure-indicator-combined-values',
- 'type' => 'combined',
- ),
- array('title' => t('Currency Combined MPV'),
- 'filename' => 'currency-combined-values',
- 'type' => 'combined',
- ),
- array('title' => t('Current Pressure Indicator Without Gold Combined MPV'),
- 'filename' => 'current-pressure-indicator-without-gold-values',
- 'type' => 'combined',
- ),
- array('title' => t('Current Pressure Indicator With Gold Combined MPV'),
- 'filename' => 'current-pressure-indicator-with-gold-values',
- 'type' => 'combined',
- ),
- array('title' => t('Debt Pressure Indicator Combined MPV'),
- 'filename' => 'debt-pressure-indicator-combined-values',
- 'type' => 'combined',
- ),
- array('title' => t('Fiscal Policy Pressure Indicator Breakdown Model Combined MPV'),
- 'filename' => 'fiscal-policy-pressure-indicator-breakdown-values',
- 'type' => 'combined',
- ),
- array('title' => t('Fiscal Policy Pressure Indicator Basic Model Combined MPV'),
- 'filename' => 'fiscal-policy-pressure-indicator-basic-values',
- 'type' => 'combined',
- ),
- array('title' => t('Macroeconomic Pressure Combined MPV'),
- 'filename' => 'macroeconomic-pressure-combined-values',
- 'type' => 'combined',
- ),
- );
- return $graphs;
- }
- /**
- * Function to display Market Pressure Variable (MPV) graphs
- * $id string filname of the excel file without the type, e.g. mpv-graph-1
- */
- function econrsa_graphs_show_graph($id, $type) {
- $output = array();
- $library = libraries_load('PHPExcel');
- if (!empty($library['loaded'])) {
- if ($charts = econrsa_graphs_load_graphs(check_plain($id), check_plain($type))) {
- //dpm($charts);
- drupal_add_js(array('econrsaGraphs' => array('charts' => $charts)), 'setting');
- $html = l(t('Back to the list of all MPV graphs'), ECONRSA_GRAPHS_MPV_BASE_PATH . '/list');
- $html .= $charts['mpv']['html'];
- // Setup the quicktabs for the spider and component graphs
- //dpm($charts['components']);
- $html .= econrsa_graphs_setup_tabs($charts['mpv'], $charts['spider'], $charts['components']);
- $output = array(
- 'graph_container' => array(
- '#type' => 'markup',
- '#markup' => $html,
- ),
- );
- }
- else {
- drupal_set_message(t("Graph @id could not be displayed.", array('@id' => $id)), 'error');
- }
- }
- else {
- drupal_set_message(t("Could not load PHPExcel library"), 'error');
- }
- $crumbs = array();
- $crumbs[] = l(t('Home'), '<front>');
- $crumbs[] = l(t('Market Pressure Variable Graphs'), 'ersagraphs/mpv/list');
- drupal_set_breadcrumb($crumbs);
- // We are currently using the graph title, so use a generic page title
- //drupal_set_title($options['page_title']);
- return $output;
- }
- /**
- * Loads the data for a set of graphs (could also just be one graph)
- *
- * @todo I want to actually set up a class for our graphs, but don't have time
- * right now
- *
- */
- function econrsa_graphs_load_graphs($id, $type) {
- // Load the parameters for the graph based on the id. Will then be available
- // in the $parameters variable
- //if (!is_null(module_load_include('inc', 'econrsa_graphs', ECONRSA_GRAPHS_PARAMETERS_DIR . '/' . $id))) {
- if (include_once( DRUPAL_ROOT . '/' . drupal_get_path('module', 'econrsa_graphs') . '/' . ECONRSA_GRAPHS_PARAMETERS_DIR . '/' . $id . '.inc')) {
- $parameters['id'] = $id;
- $parameters['filename'] = $id . '.xlsx';
- $charts = array('page_title' => $parameters['title']);
- $parameters = econrsa_graphs_load_excel($parameters);
- // $parameters now contains all the data we require from the excel spreadsheet
- drupal_add_js(libraries_get_path('highcharts') . '/js/highcharts.js');
- drupal_add_js(libraries_get_path('highcharts') . '/js/modules/exporting.js');
- $start_time = $parameters['point_start_js_timestamp'];
- switch($parameters['type']) {
- case 'combined':
- // We setup ALL graphs - combined mpv, components spider and individual components
- $parameters['series_title'] = t('Scaled MPV');
- // Load our javascript file for this type of graph
- drupal_add_js(drupal_get_path('module', 'econrsa_graphs') . '/js/single_mpv.js');
- // Combined MPV
- $charts['mpv'] = econrsa_graphs_setup_mpv_linear($id, $parameters);
- //dpm($parameters);
- $charts['components'] = econrsa_graphs_setup_mpv_components($parameters);
- $charts['spider'] = econrsa_graphs_setup_mpv_spider($parameters);
- // For displaying the individual components in tabs
- //foreach($parameters['multiple_components'] as $key => $component) {
- // $charts['components'][$key] = econrsa_graphs_setup_mpv_component($key, $component, $parameters);
- //}
- break;
- }
- return $charts;
- }
- else {
- drupal_set_message(t('Could not load graph parameters file.'), 'error');
- }
- }
- /**
- * Read the excel file for the values we will use in graphs and or calculations
- *
- * @param array $parameters The specific graph options.
- *
- * @return array Updated parameters array containing the excel data ready for graphing
- */
- function econrsa_graphs_load_excel($parameters) {
- // When reading from sheets which include the calculations, we need to increase
- // the php script timeout limit - this means we really need to implement caching
- // otherwise the site will run out of memory
- set_time_limit(240);
- // Need to cache the data for each graph because it's only going to be updated
- // once every 3 month so no need to physically read it each time
- // check file exists
- $graph_file = ECONRSA_GRAPHS_FILE_PATH . '/' . $parameters['filename'];
- if (is_readable($graph_file)) {
- // Initialise the excel reader and fetch the sheet data
- try {
- $input_file_type = PHPExcel_IOFactory::identify($graph_file);
- $obj_reader = PHPExcel_IOFactory::createReader($input_file_type);
- $obj_reader->setReadDataOnly(TRUE);
- $obj_php_excel = $obj_reader->load($graph_file);
- } catch (Exception $e) {
- die('Error loading file "' . pathinfo($graph_file, PATHINFO_BASENAME)
- . '": ' . $e->getMessage());
- }
- // Get worksheet dimensions
- $sheet = $obj_php_excel->getSheet(0);
- // Read the Date when the workbook was last modified (as a PHP timestamp value)
- $modified_datestamp = $obj_php_excel->getProperties()->getModified();
- //Format the date and time using the standard PHP date() function
- $modified_date = date('l, d F Y',$modified_datestamp);
- $modified_time = date('g:i A', $modified_datestamp);
- $parameters['meta']['modified_date'] = $modified_date;
- $parameters['meta']['modified_time'] = $modified_time;
- $highest_row = $sheet->getHighestRow();
- $parameters['meta']['highest_row'] = $highest_row;
- $parameters['components_headers'] = $sheet->rangeToArray($parameters['component_start_column'] . $parameters['header_row_number'] . ':' .
- $parameters['component_end_column'] . $parameters['header_row_number'], NULL, TRUE, FALSE);
- // Read the calculated, combined MPV
- $parameters['calculated_mpv_values'] = $sheet->rangeToArray($parameters['scaled_mpv_column'] . $parameters['data_starting_row_number'] . ':' .
- $parameters['scaled_mpv_column'] . $highest_row, NULL, TRUE, FALSE);
- // Clean up the arrays
- $parameters['components_headers'] = array_values($parameters['components_headers'][0]);
- // Read the date column, don't use rangeToArray because we want to modify in some cases
- for ($row = $parameters['data_starting_row_number']; $row <= $highest_row; $row++) {
- $date = $sheet->getCell($parameters['date_column'] . $row)->getValue();
- if ($parameters['date_type'] == 'excel_timestamp') {
- $date = PHPExcel_Shared_Date::ExcelToPHP($date) * 1000;
- }
- else {
- $date = (string) $date;
- }
- $parameters['date_values'][] = $date;
- }
- // Set up the component checkboxes
- $parameters['html']['component_checkboxes'] = '<div id="component-checkboxes"><h3>Add or remove components</h3>';
- foreach($parameters['components_headers'] as $key=>$val) {
- $component_title = t(check_plain($val));
- $parameters['menu']['components'][] = l($component_title, ECONRSA_GRAPHS_MPV_BASE_PATH . '/' . $parameters['id'] . '/components/view');
- $parameters['html']['component_checkboxes'] .= "<input type='checkbox' class='component-checkbox' id='component-" . $key ."' name='component-" . $key ."' value='" . $key . "' checked>" . $component_title . "<br>";
- $parameters['multiple_components'][$key]['component_title'] = $component_title;
- }
- $parameters['html']['component_checkboxes'] .= '</div>';
- // Load the components used to generate the scaled MPV values, only
- // necessary if we are wanting to allow the user change the graph
- $parameters['components'] = $sheet->rangeToArray($parameters['component_start_column'] . $parameters['data_starting_row_number'] . ':' .
- $parameters['component_end_column'] . $highest_row, NULL, TRUE, FALSE);
- $highest_component_value = 0.0;
- $lowest_component_value = 0.0;
- for ($row = 0; $row < count($parameters['date_values']); $row++) {
- // Get the series data for the single calculated mpv graph
- $value = round((float) $parameters['calculated_mpv_values'][$row][0], 2);
- $parameters['named_points'][] = array('name' => $parameters['date_values'][$row], 'y' => $value);
- // Get the series data for each component cell in a row
- foreach($parameters['components'][$row] as $key => $cell_value) {
- // We need to store the lowest and highest value for setting a fixed scale on y
- $highest_component_value = ($cell_value > $highest_component_value ? $cell_value : $highest_component_value);
- $lowest_component_value = ($cell_value < $lowest_component_value ? $cell_value : $lowest_component_value);
- $component_value = round((float) $cell_value, 2);
- $parameters['multiple_components'][$key]['named_points'][] = array('name' => $parameters['date_values'][$row], 'y' => $component_value);
- }
- }
- $parameters['highest_component_value'] = $highest_component_value;
- $parameters['lowest_component_value'] = $lowest_component_value;
- //dpm($parameters);
- }
- else {
- drupal_set_message(t("Failed to load graph data."), 'error');
- }
- return($parameters);
- }
- if (!function_exists('stats_standard_deviation')) {
- /**
- * This user-land implementation follows the implementation quite strictly;
- * it does not attempt to improve the code or algorithm in any way. It will
- * raise a warning if you have fewer than 2 values in your array, just like
- * the extension does (although as an E_USER_WARNING, not E_WARNING).
- *
- * @param array $a
- * @param bool $sample [optional] Defaults to false
- * @return float|bool The standard deviation or false on error.
- */
- function stats_standard_deviation(array $a, $sample = false) {
- $n = count($a);
- if ($n === 0) {
- trigger_error("The array has zero elements", E_USER_WARNING);
- return false;
- }
- if ($sample && $n === 1) {
- trigger_error("The array has only 1 element", E_USER_WARNING);
- return false;
- }
- $mean = array_sum($a) / $n;
- $carry = 0.0;
- foreach ($a as $val) {
- $d = ((double) $val) - $mean;
- $carry += $d * $d;
- };
- if ($sample) {
- --$n;
- }
- return sqrt($carry / $n);
- }
- }
- /**
- * Given an array of MPV components, calculate the sum of each row and store
- * these in an array for reference.
- *
- * @param array $components Indexed array (indexed on row number) containing
- * each of the mpv components taken from the excel spreadsheet
- * @param array $exclude List of columns (keys) to exclude from the sum
- *
- * @return array $mpv_summed_values
- */
- function econrsa_graphs_calculate_mpv_sum($components, $exclude = array()) {
- $mpv_summed_values = array();
- foreach($components as $row) {
- // Remove the excluded column(s)
- if (!empty($exclude)) {
- foreach($exclude as $key) {
- unset($row[$key]);
- }
- }
- $mpv_summed_values[] = array_sum($row);
- }
- return $mpv_summed_values;
- }
- /**
- * Given an array of MPV values, calculate the scaled mpv values.
- *
- * @param array $values Indexed array (indexed on row number) containing
- * each of the mpv components taken from the excel spreadsheet
- *
- * @return array $mpv_summed_values
- */
- function econrsa_graphs_calculate_scaled_mpv($values, $exclude = array()) {
- $mpv_summed_values = econrsa_graphs_calculate_mpv_sum($values, $exclude);
- $mpv_ave = econrsa_graphs_calculate_mpv_average($mpv_summed_values);
- $std_dev = econrsa_graphs_calculate_std_deviation($mpv_summed_values);
- $scaled_mpvs = array();
- foreach($mpv_summed_values as $value) {
- $scaled_mpvs[] = ($value - $mpv_ave) / $std_dev;
- }
- return $scaled_mpvs;
- }
- /**
- * Given an array of summed MPV values (from components), calculate the average value.
- *
- * @param array $mpv_summed_values The array of summed MPV values
- *
- * @return float The average of all values the array
- */
- function econrsa_graphs_calculate_mpv_average($mpv_summed_values) {
- return (array_sum($mpv_summed_values) / count($mpv_summed_values));
- }
- /**
- * Given an array of summed MPV values (from components), calculate the standard deviation.
- *
- * @param array $mpv_summed_values The array of summed MPV values
- *
- * @return float The standard deviation
- */
- function econrsa_graphs_calculate_std_deviation($mpv_summed_values) {
- return stats_standard_deviation($mpv_summed_values);
- }
- /**
- * Add the javascript and html for displaying a single linear mpv component graph
- *
- */
- function econrsa_graphs_setup_mpv_component($key, $component, $parameters) {
- // Prepare the highcharts settings for this type of graph
- // Graph configuration
- $highcharts_settings = array(
- 'chart' => array(
- 'renderTo' => 'chart-component-' . $key,
- 'zoomType' => 'xy',
- ),
- 'xAxis' => array(
- 'type' => 'datetime',
- ),
- 'yAxis' => array(
- 'title' => array('text' => 'Value'),
- 'max' => $parameters['highest_component_value'],
- 'min' => $parameters['lowest_component_value'],
- ),
- 'title' => array(
- 'text' => $component['component_title'],
- 'style' => array('color' => '#000000', 'fontSize' => '14px', 'fontWeight' => 'bold'),
- ),
- 'subtitle' => array(
- 'text' => t('Data last updated: @modified_date at @modified_time.',
- array('@modified_date' => $parameters['meta']['modified_date'], '@modified_time' => $parameters['meta']['modified_time'])),
- 'align' => 'right',
- 'verticalAlign' => 'bottom',
- 'floating' => true,
- 'x' => -5,
- 'y' => -30,
- 'style' => array('color' => '#555555', 'fontSize' => '0.8em'),
- ),
- 'legend' => array(
- 'verticalAlign' => 'top',
- 'y' => 20,
- ),
- 'series' => array(
- array(
- 'name' => $component['component_title'],
- 'data' => $component['named_points'],
- 'pointStart' => $parameters['point_start_js_timestamp'],
- 'pointInterval' => $parameters['point_interval'],
- ),
- ),
- 'credits' => array(
- 'text' => 'Graph courtesy of Economic Research Southern Africa',
- 'href' => 'http://www.econrsa.org',
- ),
- );
- // Set up our html framework for the graph
- $html .= '<div id="chart-component-' . $key . '" style="width:650px; height:500px;"></div>';
- //$charts['highcharts_settings'][$key] = $options['highcharts_settings'];
- $chart['html'] = $html;
- $chart['settings'] = $highcharts_settings;
- return $chart;
- }
- /**
- * Add the javascript and html for displaying the linear mpv graph
- *
- */
- function econrsa_graphs_setup_mpv_linear($id, $parameters) {
- // Prepare the highcharts settings for this type of graph
- // Graph configuration
- $highcharts_settings = array(
- 'chart' => array(
- 'renderTo' => 'chart-' . $parameters['id'],
- 'zoomType' => 'x',
- ),
- 'xAxis' => array(
- 'type' => 'datetime',
- ),
- 'yAxis' => array(
- 'title' => array('text' => 'Value'),
- ),
- 'title' => array(
- 'text' => $parameters['title'],
- 'style' => array('color' => '#000000', 'fontSize' => '14px', 'fontWeight' => 'bold'),
- ),
- 'subtitle' => array(
- 'text' => t('Data last updated: @modified_date at @modified_time.',
- array('@modified_date' => $parameters['meta']['modified_date'], '@modified_time' => $parameters['meta']['modified_time'])),
- 'align' => 'right',
- 'verticalAlign' => 'bottom',
- 'floating' => true,
- 'x' => -5,
- 'y' => -30,
- 'style' => array('color' => '#555555', 'fontSize' => '0.8em'),
- ),
- 'legend' => array(
- 'verticalAlign' => 'top',
- 'y' => 20,
- ),
- 'series' => array(
- array(
- 'name' => $parameters['series_title'],
- 'data' => $parameters['named_points'],
- 'pointStart' => $parameters['point_start_js_timestamp'],
- 'pointInterval' => $parameters['point_interval'],
- ),
- ),
- 'credits' => array(
- 'text' => 'Graph courtesy of Economic Research Southern Africa',
- 'href' => 'http://www.econrsa.org',
- ),
- 'exporting' => array(
- 'filename' => strtolower(str_replace(' ', '-', $parameters['title'])),
- 'sourceWidth' => 1000,
- 'sourceHeight' => 750,
- ),
- );
- // Set up our html framework for the graph
- $html = '<div id="chart-' . $parameters['id'] . '" style="width:100%; height:500px;"></div>';
- $html .= '<button id="btn-threshold" class="autocompare">Show thresholds</button>';
- $html .= '<div id="threshold-container" class="ersa-graphs-threshold-container">';
- $html .= 'Threshold value: <input type="text" name="threshold" id="threshold" size="3" value=' . ECONRSA_GRAPHS_THRESHOLD_DEFAULT . '>';
- $html .= '<button id="btn-threshold-update">Update thresholds</button>';
- $html .= '</div>';
- $html .= '<div id="graph-description" class="ersa-graphs-description">';
- $html .= $parameters['description'];
- $html .= '</div>';
- $chart['html'] = $html;
- $chart['settings'] = $highcharts_settings;
- return $chart;
- }
- /**
- * Add the javascript and html for displaying the mpv spider graph
- */
- function econrsa_graphs_setup_mpv_components($parameters) {
- //// The combined mvp series
- //$multiple_series[] = array(
- // 'name' => $parameters['series_title'],
- // 'data' => $parameters['named_points'],
- // 'pointStart' => $parameters['point_start_js_timestamp'],
- // 'pointInterval' => $parameters['point_interval'],
- //);
- // Set up the components
- foreach($parameters['multiple_components'] as $component) {
- $multiple_series[] = array(
- 'name' => $component['component_title'],
- 'data' => $component['named_points'],
- 'pointStart' => $parameters['point_start_js_timestamp'],
- 'pointInterval' => $parameters['point_interval'],
- 'pointPlacement' => 'on',
- );
- }
- $highcharts_settings = array(
- 'chart' => array(
- 'renderTo' => 'chart-components-' . $parameters['id'],
- 'zoomType' => 'x',
- ),
- 'xAxis' => array(
- 'type' => 'datetime',
- ),
- 'yAxis' => array(
- 'title' => array('text' => 'Value'),
- ),
- 'title' => array(
- 'text' => $parameters['title'],
- 'style' => array('color' => '#000000', 'fontSize' => '14px', 'fontWeight' => 'bold'),
- ),
- 'subtitle' => array(
- 'text' => t('Data last updated: @modified_date at @modified_time.',
- array('@modified_date' => $parameters['meta']['modified_date'], '@modified_time' => $parameters['meta']['modified_time'])),
- 'align' => 'right',
- 'verticalAlign' => 'bottom',
- 'floating' => true,
- 'x' => -5,
- 'y' => -30,
- 'style' => array('color' => '#555555', 'fontSize' => '0.8em'),
- ),
- 'series' => $multiple_series,
- 'credits' => array(
- 'text' => 'Graph courtesy of Economic Research Southern Africa',
- 'href' => 'http://www.econrsa.org',
- ),
- 'legend' => array(
- 'verticalAlign' => 'top',
- 'y' => 20,
- 'title' => array(
- 'text' => t('Click on any of the component names in the legend below to remove them from the display.'),
- 'style' => array(
- 'fontWeight' => 'normal',
- 'color' => 'gray',
- ),
- ),
- ),
- 'exporting' => array(
- 'filename' => strtolower(str_replace(' ', '-', $parameters['title'])),
- 'sourceWidth' => 1000,
- 'sourceHeight' => 750,
- ),
- );
- // Set up our html framework for the graph
- $html = '<div id="chart-components-' . $parameters['id'] . '" style="width:100%; height:500px;"></div>';
- $chart['html'] = $html;
- $chart['settings'] = $highcharts_settings;
- return $chart;
- }
- /**
- * Add the javascript and html for displaying the mpv spider graph
- */
- function econrsa_graphs_setup_mpv_spider($parameters) {
- $render_id = 'chart-spider-' . $parameters['id'];
- $start_time = $parameters['point_start_js_timestamp'];
- // Spider graph is not part of the default highcharts js library
- drupal_add_js(libraries_get_path('highcharts') . '/js/highcharts-more.js');
- foreach($parameters['multiple_components'] as $component) {
- $multiple_series[] = array(
- 'name' => $component['component_title'],
- 'data' => $component['named_points'],
- 'pointStart' => $start_time,
- 'pointInterval' => $parameters['point_interval'],
- 'pointPlacement' => 'on',
- );
- }
- // Prepare the highcharts settings for this type of graph
- // Graph configuration
- $highcharts_settings = array(
- 'chart' => array(
- 'renderTo' => $render_id,
- 'polar' => true,
- 'type' => 'line',
- 'zoomType' => 'y',
- ),
- 'xAxis' => array(
- 'type' => 'datetime',
- 'tickmarkPlacement' => 'on',
- 'lineWidth' => 0,
- //'categories' => array('Sales', 'Marketing', 'Development', 'Customer Support', 'Information Technology', 'Administration'),
- ),
- 'yAxis' => array(
- 'title' => array('text' => 'Value'),
- 'gridLineInterpolation' => 'polygon',
- 'lineWidth' => 0,
- ),
- 'pane' => array(
- 'size' => '80%',
- ),
- 'title' => array(
- 'text' => $parameters['title'],
- 'style' => array('color' => '#000000', 'fontSize' => '14px', 'fontWeight' => 'bold'),
- ),
- 'subtitle' => array(
- 'text' => t('Data last updated: @modified_date at @modified_time.',
- array('@modified_date' => $parameters['meta']['modified_date'], '@modified_time' => $parameters['meta']['modified_time'])),
- 'align' => 'right',
- 'verticalAlign' => 'bottom',
- 'floating' => true,
- 'x' => -5,
- 'y' => -30,
- 'style' => array('color' => '#555555', 'fontSize' => '0.8em'),
- ),
- 'legend' => array(
- 'verticalAlign' => 'top',
- 'y' => 20,
- 'title' => array(
- 'text' => t('Click on any of the component names in the legend below to remove them from the display.'),
- 'style' => array(
- 'fontWeight' => 'normal',
- 'color' => 'gray',
- ),
- ),
- ),
- 'series' => $multiple_series,
- 'credits' => array(
- 'text' => 'Graph courtesy of Economic Research Southern Africa',
- 'href' => 'http://www.econrsa.org',
- ),
- );
- $html .= '<div id="' . $render_id . '" style="width:650px; height:780px;"></div>';
- $chart['html'] = $html;
- $chart['settings'] = $highcharts_settings;
- return $chart;
- }
- /**
- * Test function: to test excel reading
- *
- */
- function _text_extraction() {
- $library = libraries_load('PHPExcel');
- // check file exists
- $graph_file = ECONRSA_GRAPHS_FILE_PATH . '/_test_cpi.xlsx';
- if (is_readable($graph_file)) {
- // Initialise the excel reader and fetch the sheet data
- try {
- $input_file_type = PHPExcel_IOFactory::identify($graph_file);
- $obj_reader = PHPExcel_IOFactory::createReader($input_file_type);
- $obj_reader->setReadDataOnly(TRUE);
- $obj_php_excel = $obj_reader->load($graph_file);
- } catch (Exception $e) {
- die('Error loading file "' . pathinfo($graph_file, PATHINFO_BASENAME)
- . '": ' . $e->getMessage());
- }
- // Get worksheet dimensions
- $sheet = $obj_php_excel->getSheet(0);
- $extracted_values = $sheet->rangeToArray('AD4:AD5', NULL, TRUE, FALSE);
- //dpm($extracted_values);
- }
- }
- function econrsa_graphs_setup_tabs ($mpv, $spider, $components) {
- // Setup combined mpv tab
- $tabs[] = array(
- 'hide_title' => 0,
- 'title' => t('Linear MPV components'), // Tab title.
- 'contents' => $components['html'],
- );
- // Setup spider tab
- $tabs[] = array(
- 'hide_title' => 0,
- //'title' => $spider['settings']['title']['text'], // Tab title.
- 'title' => t('Components Spider'), // Tab title.
- 'contents' => $spider['html'],
- );
- //foreach ($components as $key => $component) {
- // $tabs[] = array(
- // 'hide_title' => 0,
- // //'title' => $component['settings']['title']['text'], // Tab title.
- // 'title' => 'X' . ($key + 1), // Tab title.
- // 'contents' => $component['html'],
- // );
- //}
- $name = 'qt_ersa_graphs_components';
- $overrides = array(
- 'ajax' => FALSE, // TRUE or FALSE.
- 'hide_empty_tabs' => FALSE, // TRUE or FALSE.
- 'renderer' => 'quicktabs', // 'accordion', 'quicktabs'
- 'style' => 'Zen',
- 'default_tab' => 0,
- );
- $html = drupal_render(quicktabs_build_quicktabs($name, $overrides, $tabs));
- return $html;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement