Your IP : 216.73.216.240
<?php
/*
* @package Tech Fry Library
* @license https://www.gnu.org/licenses/gpl-3.0.en.html
*/
namespace TechFry\Library\View\Others;
defined('_JEXEC') or die;
// Reference: https://quickchart.io/
class Chart
{
public $api_data = array();
public $type; // bar, line
public $data;
public $options;
public $data_sets;
public $labels;
public function __construct($type)
{
$this->type = $type;
$this->api_data['type'] = $type;
}
public function display()
{
$function = 'get_' . $this->type;
//$this->$function();
$json = json_encode($this->api_data);
$json = str_replace('"', "'", $json);
$url = 'https://quickchart.io/chart?c=' . $json;
$output = '<img class="img-fluid" src="' . $url . '">';
return $output;
}
public function get_bar()
{
}
public function add_labels($labels)
{
// $labels - string or array
if (\is_string($labels))
{
$labels = str_getcsv($labels);
$labels = array_map('trim', $labels);
}
$this->api_data['data']['labels'] = $labels;
}
public function add_data($data)
{
if (\is_string($data))
{
$data = str_getcsv($data);
$data = array_map('trim', $data);
}
$this->api_data['data']['datasets'][] = array('data' => $data);
}
// Add label to dataset
public function add_label($label)
{
$this->api_data['data']['datasets'][0]['label'] = $label;
}
// Add background color to dataset
public function add_bg($color)
{
$this->api_data['data']['datasets'][0]['backgroundColor'] = $color;
}
// Add border color to dataset
public function add_border($color)
{
$this->api_data['data']['datasets'][0]['borderColor'] = $color;
$this->api_data['data']['datasets'][0]['fill'] = false;
}
// Add width to dataset
public function add_width($width)
{
// $width - 0 to 1
$this->api_data['data']['datasets'][0]['barPercentage'] = $width;
}
public function add_dataset($dataset = array())
{
// $dataset - label, data, backgroundColor, maxBarThickness
}
public static function get_output($content, $config = array())
{
// content - chart_type, labels, dp
// config
if (empty($content))
{
return;
}
$datapoints = $content['dp'];
$labels = $content['labels'];
$chart_type = $content['chart_type'];
if (is_string($labels))
{
$labels = str_getcsv($labels);
$labels = array_map('trim', $labels);
}
if (is_string($datapoints))
{
$datapoints = str_getcsv($datapoints);
$datapoints = array_map('trim', $datapoints);
}
switch ($chart_type)
{
case 'bar' :
case 'horizontalBar' :
case 'line' :
case 'radar' :
case 'pie' :
case 'doughnut' :
case 'polarArea' :
$api_data = array(
'type' => $chart_type,
'data' => array(
'labels' => $labels,
'datasets' => array(
array(
'label' => 'Value',
'data' => $datapoints,
'backgroundColor' => 'green',
'maxBarThickness' => 10,
)
)
)
);
break;
case 'radialGauge' :
case 'progressBar' :
$api_data = array(
'type' => $chart_type,
'data' => array(
'datasets' => array(
array(
'data' => $datapoints
)
)
)
);
break;
}
$json = json_encode($api_data);
$json = str_replace('"', "'", $json);
$url = 'https://quickchart.io/chart?c=' . $json;
$output = '<img src="' . $url . '">';
return $output;
}
public static function bar_chart($x_labels, $data = array())
{
// $data - array: label, data, backgroundColor, maxBarThickness
if (is_string($x_labels))
{
$x_labels = str_getcsv($x_labels);
$x_labels = array_map('trim', $x_labels);
}
$api_data = array(
'type' => 'bar',
'data' => array(
'labels' => $x_labels,
'datasets' => $data
)
);
$json = json_encode($api_data);
$json = str_replace('"', "'", $json);
$url = 'https://quickchart.io/chart?c=' . $json;
$output = '<img src="' . $url . '">';
return $output;
}
}