Files
2026-04-15 15:23:57 +07:00

618 lines
22 KiB
PHP

<?php
class Chartv3 extends MY_Controller
{
var $url_renderer;
function __construct()
{
parent::__construct();
$this->url_renderer = "http://devkedungdoro.aplikasi.web.id:3000/chart";
}
function all($sdate, $edate)
{
$sql = "SELECT project_name, sum(unit_amount) as s_hour
FROM one_support.odoo_timesheet
WHERE date_trx BETWEEN '{$sdate}' AND '{$edate}'";
$qry = $this->db->query($sql);
$this->check_error($qry, "get project_name");
$rows = $qry->result_array();
if (count($rows) == 0) {
$this->chart_error("No data found");
}
$param = array(
'series' => array(
array(
'type' => 'gauge',
'axisLine' => array(
'lineStyle' => array(
'width' => 30,
'color' => array(
'0.3' => '#67e0e3',
'0.7' => '#37a2da',
'1' => '#fd666d'
)
)
)
),
'pointer' => array(
'itemStyle' => array(
'color' => 'auto'
),
'axisTick' => array(
'distance' => -30,
'length' => 8,
'lineStyle' => array(
'color' => '#fff',
'width' => 2
)
),
'splitLine' => array(
'distance' => -30,
'length' => 30,
'lineStyle' => array(
'color' => '#fff',
'width' => 4
)
),
'axisLabel' => array(
'color' => 'inherit',
'distance' => 40,
'fontSize' => 20
),
'detail' => array(
'valueAnimation' => true,
'formatter' => '{value} km/',
'color' => 'inherit'
),
'data' => array('value' => 70)
)
)
);
// 3. encapsulate in config attribute and json encode
$config = ["config" => $param];
$j_param = json_encode($config);
header("Content-Type: image/png");
// 4. post to chart renderer
echo $this->post($this->url_renderer, $j_param);
}
function project_group($sdate, $edate)
{
$sql = "SELECT project_name, sum(unit_amount) as s_hour
FROM one_support.odoo_timesheet
WHERE date_trx BETWEEN '{$sdate}' AND '{$edate}'
GROUP BY project_name";
$qry = $this->db->query($sql);
$this->check_error($qry, "get project_name");
$rows = $qry->result_array();
if (count($rows) == 0) {
$this->chart_error("No data found");
}
$param = array(
'tooltip' => array(
'trigger' => 'item',
'formatter' => '{b}: {c} ({d}%)'
),
'legend' => array(
'orient' => 'vertical',
'right' => '10%',
'top' => 'center'
),
'series' => array(
array(
'name' => 'Access From',
'type' => 'pie',
'radius' => '50%',
'labelLine' => array(
'show' => false
),
'label' => array(
'show' => true,
'position' => 'inside',
'formatter' => '{d}%',
'backgroundColor' => 'rgba(0, 0, 0, 0.5)',
'borderRadius' => 5,
'padding' => 4,
'color' => '#fff'
),
'itemStyle' => array(
'normal' => array(
'shadowBlur' => 20,
'shadowOffsetX' => 0,
'shadowColor' => 'rgba(0, 0, 0, 0.5)'
)
),
'emphasis' => array(
'itemStyle' => array(
'shadowBlur' => 30,
'shadowOffsetX' => 0,
'shadowColor' => 'rgba(0, 0, 0, 0.7)'
)
),
'data' => array()
)
)
);
for ($i = 0; $i < count($rows); $i++) {
$param['series'][0]['data'][] = array(
'value' => $rows[$i]['s_hour'],
'name' => $rows[$i]['project_name'],
'itemStyle' => array(
'color' => $color[$i] // Warna untuk potongan ini
)
);
}
// 3. encapsulate in config attribute and json encode
$config = ["config" => $param];
$j_param = json_encode($config);
header("Content-Type: image/png");
// 4. post to chart renderer
echo $this->post($this->url_renderer, $j_param);
}
function staff($sdate, $edate, $employee_id)
{
// 1. prepare the data using sql
$sql = "SELECT COUNT(*) AS total, employee_name, SUM(unit_amount) as unit_amount,project_name
FROM one_support.odoo_timesheet
WHERE date_trx BETWEEN '{$sdate}' AND '{$edate}'
AND employee_id = {$employee_id}
GROUP BY project_name";
$qry_total_data = $this->db->query($sql, [$id]);
$this->check_error($qry_total_data, "get xTotalAll mcu");
$rows = $qry_total_data->result_array();
if (count($rows) == 0) {
$this->chart_error("No data found");
}
// $rows = array(
//fungsi mengatur pembagian label / text
function wrapText($text, $maxLength)
{
return wordwrap($text, $maxLength, "\n", true); // Memecah teks menjadi beberapa baris dengan panjang maksimum dan menggunakan newline sebagai pemisah
}
// 2. generate parameter for the chart
$color = ['#36A2EB', '#FF5733', '#FFC300', '#4BC0C0', '#9966FF'];
// Mendapatkan nilai unik dari 'project_name' dan 'employee_name' serta mengatur ulang indeks array
$uniqueProjects = array_values(array_unique(array_column($rows, 'project_name')));
$uniqueStaffs = array_values(array_unique(array_column($rows, 'employee_name')));
$param = array(
'title' => array(
'text' => 'Project By Staff',
'left' => 'center', // Menempatkan legend di tengah secara horizontal,
'top' => 'top',
'orient' => 'vertical'
),
'dataset' => array(
'source' => array(
array('score', 'amount', 'product'),
)
),
//menampilkan nama bagian tiap-tiap chart
'legend' => array(
'data' => $uniqueProjects,
'left' => 'center',
'bottom' => 'bottom',
'orient' => 'horizontal'
),
'grid' => array('containLabel' => true),
'yAxis' => array('type' => 'value'),
'xAxis' => array(
'type' => 'category',
'axisTick' => array('show' => false),
'data' => $uniqueStaffs,
),
'visualMap' => array(
'orient' => 'horizontal',
'show' => false,
'left' => 'center',
'min' => 0,
'max' => 100,
'dimension' => 0,
'inRange' => array(
// 'color' => array('#0000FF', '#00eaf2', '#035bff')
)
),
'series' => array()
);
// Memetakan warna tiap bar sesuai dengan kategori project_name
$colorMapping = array();
foreach ($uniqueProjects as $index => $project_name) {
$colorMapping[$project_name] = $color[$index % count($color)]; // Menggunakan modulus untuk memastikan palet warna diulang jika jumlah kategori lebih banyak daripada warna yang tersedia
}
foreach ($uniqueProjects as $project_name) {
$data = array_fill(0, count($uniqueStaffs), null); // Inisialisasi data untuk setiap lokasi
foreach ($rows as $row) {
if ($row['project_name'] == $project_name) {
// Menemukan indeks lokasi dalam $uniqueStaffs
$employee_nameIndex = array_search($row['employee_name'], $uniqueStaffs);
if ($employee_nameIndex !== false) {
$data[$employee_nameIndex] = $row['unit_amount'] ?? 0; // Handling null values
}
}
}
print_r($data);
exit;
$param['series'][] = array(
'name' => $project_name,
'type' => 'bar',
'barGap' => 0.5,
'label' => array(
'show' => true,
'position' => 'top',
'distance' => 25,
'fontSize' => 12, // Menyesuaikan ukuran font
'formatter' => '', // Menggunakan formatter untuk memisahkan baris
'rich' => array('name' => array()),
),
'itemStyle' => array( // Menentukan gaya item untuk bar, termasuk warnanya
'color' => $colorMapping[$project_name]
),
'data' => $data
);
}
// 3. encapsulate in config attribute and json encode
$config = ["config" => $param];
$j_param = json_encode($config);
// echo $j_param;
header("Content-Type: image/png");
// 4. post to chart renderer
echo $this->post($this->url_renderer, $j_param);
}
function staff_group($sdate, $edate)
{
// 1. prepare the data using sql
$sql = "SELECT employee_id, employee_name, ROUND(sum(unit_amount),1) as s_hour
FROM one_support.odoo_timesheet
WHERE date_trx BETWEEN '{$sdate}' AND '{$edate}'
GROUP BY employee_id";
$qry_total_data = $this->db->query($sql);
$this->check_error($qry_total_data, "get xTotalAll mcu");
$rows = $qry_total_data->result_array();
if (count($rows) == 0) {
$this->chart_error("No data found");
}
// $rows = array(
//fungsi mengatur pembagian label / text
function wrapText($text, $maxLength)
{
return wordwrap($text, $maxLength, "\n", true); // Memecah teks menjadi beberapa baris dengan panjang maksimum dan menggunakan newline sebagai pemisah
}
// 2. generate parameter for the chart
$color = ['#36A2EB', '#FF5733', '#FFC300', '#4BC0C0', '#9966FF', '#17153B', '#2E236C', '#DC0083', '#C8ACD6', '#6C946F'];
// Mendapatkan nilai unik dari 'project_name' dan 'employee_name' serta mengatur ulang indeks array
$uniqueEmployees = array_values(array_unique(array_column($rows, 'employee_name')));
$uniqueEmployeeid = array_values(array_unique(array_column($rows, 'employee_id')));
$fsdate = date('d-m-Y', strtotime($sdate));
$fedate = date('d-m-Y', strtotime($edate));
$param = array(
'title' => array(
'text' => 'Staff Total Hour',
'left' => 'center',
'top' => 'top',
'orient' => 'vertical',
'textStyle' => array(
'fontSize' => 18,
'fontWeight' => 'bold',
),
'subtext' => $fsdate . ' - ' . $fedate,
'subtextStyle' => array(
'fontSize' => 12,
'color' => '#000',
),
),
'dataset' => array(
'source' => array(
array('score', 'amount', 'product'),
)
),
//menampilkan nama bagian tiap-tiap chart
'legend' => array(
'data' => $uniqueEmployees,
'left' => 'center',
'bottom' => 'bottom',
'orient' => 'horizontal',
'itemGap' => 10,
'itemWidth' => 15,
'padding' => 10,
'width' => '80%', // Batasi lebar legend agar terbagi menjadi beberapa baris
'height' => 80
),
'grid' => array(
'containLabel' => true,
'left' => '5%', // Mengurangi padding di sisi kiri untuk melebarkan chart
'right' => '5%',
'bottom' => '27%'
),
'yAxis' => array('type' => 'value'),
'xAxis' => array(
'type' => 'category',
'axisTick' => array('show' => false),
'data' => '',
"axisLabel" => array(
'show' => false,
)
),
'visualMap' => array(
'orient' => 'horizontal',
'show' => false,
'left' => 'center',
'min' => 0,
'max' => 100,
'dimension' => 0,
'inRange' => array(
// 'color' => array('#0000FF', '#00eaf2', '#035bff')
)
),
'series' => array()
);
// Memetakan warna tiap bar sesuai dengan kategori project_name
$colorMapping = array();
foreach ($uniqueEmployees as $index => $employee_name) {
$colorMapping[$employee_name] = $color[$index % count($color)]; // Menggunakan modulus untuk memastikan palet warna diulang jika jumlah kategori lebih banyak daripada warna yang tersedia
}
foreach ($uniqueEmployees as $employee_name) {
$data = array_fill(0, count($uniqueEmployeeid), null); // Inisialisasi data untuk setiap lokasi
foreach ($rows as $row) {
if ($row['employee_name'] == $employee_name) {
// Menemukan indeks lokasi dalam $uniqueEmployeeid
$hour_Index = array_search($row['employee_id'], $uniqueEmployeeid);
if ($hour_Index !== false) {
$data[$hour_Index] = $row['s_hour'] ?? 0; // Handling null values
}
}
}
$param['series'][] = array(
'name' => $employee_name,
'type' => 'bar',
'barGap' => '-86%',
'barWidth' => '50%',
'label' => array(
'show' => true,
'position' => 'top',
'fontSize' => 12, // Menyesuaikan ukuran font
'formatter' => '{@[1]}', // Menggunakan formatter untuk memisahkan baris
'rich' => array('name' => array()),
),
'itemStyle' => array( // Menentukan gaya item untuk bar, termasuk warnanya
'color' => $colorMapping[$employee_name],
),
'data' => $data
);
}
// 3. encapsulate in config attribute and json encode
$config = ["config" => $param];
$j_param = json_encode($config);
// echo $j_param;
header("Content-Type: image/png");
// 4. post to chart renderer
echo $this->post($this->url_renderer, $j_param);
}
function staff_groupv2($sdate, $edate)
{
$sql = "SELECT employee_name, sum(unit_amount) as s_hour
FROM one_support.odoo_timesheet
WHERE date_trx BETWEEN '{$sdate}' AND '{$edate}'
GROUP BY employee_id";
$qry = $this->db->query($sql);
$this->check_error($qry, "get data staff");
$rows = $qry->result_array();
if (count($rows) == 0) {
$this->chart_error("No data found");
}
$data = [
["score", "amount", "product"]
];
foreach ($rows as $index => $row) {
$angka = $row['s_hour'];
$dibulatkan = round($angka, 1);
$data[] = [
$index + 1,
$dibulatkan,
$this->splitString($row['employee_name'])
];
}
// print_r($data);
// exit;
$uniqueEmployees = array_values(array_unique(array_column($rows, 'employee_name')));
$option = [
'title' => [
'text' => 'Staff Total Jam',
'left' => 'center', // Menempatkan legend di tengah secara horizontal,
'top' => 'top',
'orient' => 'vertical'
],
"dataset" => [
"source" => $data
],
//menampilkan nama bagian tiap-tiap chart
'legend' => [
'data' => $uniqueEmployees,
'left' => 'center',
'bottom' => 'bottom',
'orient' => 'horizontal'
],
"grid" => [
"containLabel" => true
],
"xAxis" => [
"name" => "",
"type" => "category",
"axisLabel" => [
"interval" => 0,
"rotate" => 20
]
],
"yAxis" => [
"type" => "value",
"name" => "",
"nameTextStyle" => [
"align" => "right"
],
"nameGap" => 20
],
"visualMap" => [
"orient" => "horizontal",
"left" => "center",
"min" => 0,
"max" => count($data),
"dimension" => 0,
"show" => false,
"inRange" => [
"color" => ['#36A2EB', '#FF5733', '#FFC300', '#4BC0C0', '#9966FF']
]
],
"series" => [
[
"label" => [
"position" => "inside",
"show" => true,
"formatter" => "{@[1]}"
],
"type" => "bar",
"encode" => [
"x" => "product",
"y" => "amount"
]
]
],
"tooltip" => [
"trigger" => "axis",
"axisPointer" => [
"type" => "shadow"
]
]
];
$config = ["config" => $option];
$j_param = json_encode($config);
// echo $j_param;
header("Content-Type: image/png");
// 4. post to chart renderer
echo $this->post($this->url_renderer, $j_param);
}
function splitString($string, $wordsPerLine = 2)
{
$words = explode(' ', $string);
$lines = [];
for ($i = 0; $i < count($words); $i += $wordsPerLine) {
$lines[] = implode(' ', array_slice($words, $i, $wordsPerLine));
}
return implode("\n", $lines);
}
function check_error($qry, $stage)
{
if (!$qry) {
$errMsg = $stage . "<br/>" . $this->db->error()["messge"];
print_r($errMsg);
$this->chart_error($errMsg);
}
}
function chart_error($msg)
{
$param = array(
'title' => array(
'show' => true,
'textStyle' => array(
'color' => 'grey',
'fontSize' => 20
),
'text' => $msg,
'left' => 'center',
'top' => 'center'
),
'xAxis' => array(
'show' => false
),
'yAxis' => array(
'show' => false
),
'series' => array()
);
header("Content-Type: image/png");
$config = ["config" => $param];
$j_param = json_encode($config);
echo $this->post($this->url_renderer, $j_param);
exit;
}
function post($url, $data)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, j120);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Content-Type: application/json",
"Content-Length: " . strlen($data),
]);
$result = curl_exec($ch);
if (curl_errno($ch) > 0) {
return [
"status" => "ERR",
"message" => curl_error($ch),
];
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode != 200) {
return [
"status" => "ERR",
"message" => "Http Response : $httpCode",
];
}
return $result;
}
}