public static function pieChart($title, $labels, $data, $colors = null)
{
$id = Laralum::randomString();
if (Laralum::settings()->pie_chart_source == 'chartjs') {
$graph = "\n <canvas id='{$id}'></canvas>\n <script>\n var ctx = document.getElementById('{$id}');\n var myChart = new Chart(ctx, {\n type: 'pie',\n data: {\n labels: [";
foreach ($labels as $label) {
$graph .= "'" . $label . "',";
}
$graph .= "],\n datasets: [{\n data: [";
foreach ($data as $dta) {
$graph .= $dta . ',';
}
$graph .= "],\n backgroundColor: [";
if ($colors) {
foreach ($colors as $color) {
$graph .= "'" . $color . "',";
}
} else {
foreach ($data as $dta) {
$graph .= "'" . Laralum::randomColor() . "',";
}
}
$graph .= "],\n }]\n },\n options: {\n title: {\n display: true,\n text: '" . $title . "',\n fontSize: 20,\n }\n }\n });\n </script>\n ";
} elseif (Laralum::settings()->pie_chart_source == 'highcharts') {
$graph = "\n <script type='text/javascript'>\n \$(function () {\n \$('#{$id}').highcharts({\n chart: {\n plotBackgroundColor: null,\n plotBorderWidth: null,\n plotShadow: false,\n type: 'pie'\n },\n title: {\n text: '{$title}'\n },\n tooltip: {\n pointFormat: '{point.y} <b>({point.percentage:.1f}%)</b>'\n },\n plotOptions: {\n pie: {\n allowPointSelect: true,\n cursor: 'pointer',\n dataLabels: {\n enabled: true,\n format: '<b>{point.name}</b>: {point.y} ({point.percentage:.1f}%)',\n style: {\n color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'\n }\n }\n }\n },\n series: [{\n colorByPoint: true,\n data: [\n ";
$i = 0;
foreach ($data as $dta) {
$e = $labels[$i];
$v = $dta;
$graph .= "{name: '{$e}', y: {$v}},";
$i++;
}
$graph .= "\n ]\n }]\n });\n });\n </script>\n <div id='{$id}'></div>\n ";
} else {
$graph = "\n <script type='text/javascript'>\n google.charts.setOnLoadCallback(drawPieChart);\n function drawPieChart() {\n\n var data = google.visualization.arrayToDataTable([\n ['Element', 'Value'],\n ";
$i = 0;
foreach ($data as $dta) {
$e = $labels[$i];
$v = $dta;
$graph .= "['{$e}', {$v}],";
$i++;
}
$graph .= "\n ]);\n\n var options = {\n fontSize: 12,\n title: '{$title}',";
if ($colors) {
$graph .= "colors:[";
foreach ($colors as $color) {
$graph .= "'{$color}',";
}
$graph .= "],";
}
$graph .= "\n };\n\n var chart = new google.visualization.PieChart(document.getElementById('{$id}'));\n\n chart.draw(data, options);\n }\n </script>\n\n <div id='{$id}'></div>\n ";
}
return $graph;
}