Skip to content

Instantly share code, notes, and snippets.

@devfabriciobr
Created November 24, 2025 02:49
Show Gist options
  • Select an option

  • Save devfabriciobr/644c93dade79f02d99dd6f51ca93e609 to your computer and use it in GitHub Desktop.

Select an option

Save devfabriciobr/644c93dade79f02d99dd6f51ca93e609 to your computer and use it in GitHub Desktop.
TEMPLATE HTML tbarchart TBarChart (ADIANTI FRAMEWORK)
<div class="tbarchart" style="padding:10px">
<canvas style="height:{height}px;max-height:{height}px;" id="{chart_id}"></canvas>
</div>
<script>
var data = {
labels: {xlabels},
datasets: {datasets}
};
var config = {
type: 'bar',
data: data,
options: {
indexAxis: '{indexAxis}',
scales: {scales},
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: 'bottom',
},
title: {
display: true,
text: '{title}'
},
datalabels: {
anchor: 'end',
align: 'center',
formatter: (value, ctx, c, d) => {
const isStacked = ctx.chart.config.options.scales?.x?.stacked || ctx.chart.config.options.scales?.y?.stacked;
if (isStacked) {
return null;
}
return '{numberPrefix}'+number_format(value,{decimals}, '{decimalsSeparator}', '{thousandSeparator}');
},
borderRadius: 5,
backgroundColor: 'rgba(64, 64, 64, 0.7)',
color: 'white',
font: {
size: '10'
},
},
tooltip: {
callbacks: {
label: function(context) {
let value = context.raw;
let formatted = number_format(
value,
{decimals},
'{decimalsSeparator}',
'{thousandSeparator}'
);
return context.dataset.label + ': ' + '{numberPrefix}' + formatted;
}
}
}
},
},
};
Chart.register(ChartDataLabels);
var myChart = new Chart(
document.getElementById('{chart_id}'),
config
);
</script>
COMPONENTE TBarChart.php (lib\adianti\widget\chart\TBarChart.php)
<?php
namespace Adianti\Widget\Chart;
use Adianti\Widget\Base\TElement;
use Adianti\Widget\Base\TScript;
use Exception;
class TBarChart extends TChartBase
{
private $xlabels;
private $ylabel;
private $datasets;
private $horizontal;
private $stack;
public function setXLabels($labels)
{
$this->xlabels = $labels;
}
public function setYLabel($label)
{
$this->ylabel = $label;
}
public function makeHorizontal()
{
$this->horizontal = true;
}
public function makeStack()
{
$this->stack = true;
}
public function addDataset($name, $data)
{
$this->datasets[$name] = $data;
}
public function show()
{
$template = file_get_contents('lib/adianti/include/components/tbarchart/tbarchart.html');
$template = str_replace('{chart_id}', uniqid(), $template);
$template = str_replace('{title}', (string) $this->title, $template);
$template = str_replace('{data}', json_encode($this->datasets), $template);
$template = str_replace('{xlabels}', json_encode($this->xlabels), $template);
$template = str_replace('{height}', $this->height, $template);
$template = str_replace('{indexAxis}', ((bool) $this->horizontal ? 'y' : 'x'), $template);
$template = str_replace('{scales}', json_encode( $this->stack ? (object) [ 'x' => ['stacked'=>true], 'y' => ['stacked'=>true]] : (object) []), $template);
$template = str_replace('{numberPrefix}', $this->numberPrefix, $template);
$template = str_replace('{decimals}', $this->numericMask[0], $template);
$template = str_replace('{decimalsSeparator}', $this->numericMask[1], $template);
$template = str_replace('{thousandSeparator}', $this->numericMask[2], $template);
$colors = parent::getColors();
$datasets = [];
$i = 0;
if (!empty($this->datasets))
{
foreach ($this->datasets as $name => $values)
{
$color = $colors[$i];
if ($name == 'Entradas') {
$color = '#139865ff';
}
if ($name == 'Saídas') {
$color = '#e37068ff';
}
$datasets[] = ['label' => $name, 'data' => $values, 'backgroundColor' => $color];
$i++;
}
}
$template = str_replace('{datasets}', json_encode($datasets), $template);
parent::add($template);
parent::show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment