Skip to content

Instantly share code, notes, and snippets.

@dranney-sugarcrm
Created February 6, 2017 20:23
Show Gist options
  • Save dranney-sugarcrm/2bac560be4b16ede0fe61e6fb816cee8 to your computer and use it in GitHub Desktop.
Save dranney-sugarcrm/2bac560be4b16ede0fe61e6fb816cee8 to your computer and use it in GitHub Desktop.
Custom API to provide LineChart data
<?php
if (!defined('sugarEntry') || !sugarEntry) {
die('Not A Valid Entry Point');
}
require_once 'clients/base/api/ModuleApi.php';
class chartInfoAPI extends ModuleApi
{
public function registerApiRest()
{
return array(
'webhook' => array(
'reqType' => 'GET',
'path' => array('Accounts', 'get_line_chart_data'),
'pathVars' => array('module', 'action'),
'method' => 'getLineChartInfo',
'shortHelp' => 'This endpoint retrieves line chart information in the proper format',
'longHelp' => '',
),
);
}
/**
* This method generates data for a line chart example and returns it in the following LineChart format:
* array(
* array(
* 'data' => array(
* array(
* 'key' => "Data set the first", // Title of this data set
* 'values' => array(
* array(
* 'x' => {some number},
* 'y' => {some other number},
* ),
* ...
* ),
* 'color' => "#ff0000", // Any color you want, of course
* ),
* ...
* ),
* 'properties' => array(
* 'title' => "Title of this chart",
* ),
* ),
* ...
* )
*/
public function getLineChartInfo($api, $args) {
$out_data_arr = array();
$num_charts = 3; // Total number of charts to display
$num_lines = 2; // Number of lines per chart to display
$num_data_points = 5; // Number of data points per line to display
$color_map = array(
'silver',
'gray',
'black',
'red',
'maroon',
'yellow',
'olive',
'lime',
'green',
'aqua',
'teal',
'blue',
'navy',
'fuchsia',
'purple',
);
for ($i = 0; $i < $num_charts; $i++) {
$tmp_chart_arr = array(
'data' => array(),
'properties' => array(
'title' => "Chart #" . ($i + 1),
),
);
for ($j = 0; $j < $num_lines; $j++) {
// Pick a color for the line
$line_color = $color_map[rand(0, count($color_map) - 1)];
$tmp_line_arr = array(
'key' => ucfirst($line_color) . " Data",
'values' => array(),
'color' => $line_color,
);
for ($k = 1; $k <= $num_data_points; $k++) {
$tmp_data_point = array(
'x' => $k,
'y' => rand(0, 10),
);
$tmp_line_arr['values'][] = $tmp_data_point;
}
$tmp_chart_arr['data'][] = $tmp_line_arr;
}
$out_data_arr[] = $tmp_chart_arr;
}
return $out_data_arr;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment