<?php
$array=array('pam1' =>array('A'=>1, 'B'=>2, 'C'=>3), 'pam2' =>array('A'=>3, 'B'=>2, 'C'=>2), 'pam3' =>array('A'=>2, 'B'=>4, 'C'=>3), 'pam4' =>array('A'=>3, 'B'=>2, 'C'=>5), 'pam5' =>array('A'=>4, 'B'=>4, 'C'=>1), 'pam6' =>array('A'=>4, 'B'=>4, 'C'=>4), 'pam7' =>array('A'=>4, 'B'=>5, 'C'=>4), 'pam8' =>array('A'=>6, 'B'=>2, 'C'=>7), 'pam9' =>array('A'=>1, 'B'=>5, 'C'=>3));
$keys = array_keys($array);
$firstKey = $keys[0];
foreach ($array[$firstKey] as $key => $value1) {
foreach ($array as $key0 => $value) {
$mass=array("label"=> $key0, "y"=>$array[$key0][$key]);
$dataPoints1[] = array($mass);
}
}
$chess=count($array[$firstKey]);
$chery=count($dataPoints1);
$delenie=$chery/$chess;
$ramb=array_chunk($dataPoints1,$delenie);
for($i = 0; $i <= $chess-1; $i++) {
${'result'.$i}=array_merge(...$ramb[$i]);
}
foreach ($array[$firstKey] as $key6 => $value1) {
$key7[]=$key6;
}
for($s = 0; $s <= $chess-1; $s++) {
${'data'.$s} = array(
array(
'type' => 'stackedColumn',
'showInLegend' => true,
'type' => 'stackedColumn',
'yValueFormatString' => '#,##0 шт.'
) );
foreach(${'data'.$s}as $key5 => $item){
${'data'.$s}[$key5]['name']=$key7[$s];
${'data'.$s}[$key5]['dataPoints'] = ${'result'.$s};
} }
for($s = 0; $s <= $chess-1; $s++) {
$main[]= json_encode(${'data'.$s}).',';
}
$vat=str_replace('}]', '}', $main);
$vat=str_replace('[{', '{', $vat);
$vat=str_replace(':{', ':[{', $vat);
$vat=str_replace('}},', '}]},', $vat);
?>
<!DOCTYPE HTML>
<html>
<head>
<script>
window.onload = function () {
var data = [<?php
for($s = 0; $s <= $chess-1; $s++) {
echo $vat[$s];
} ?>]
var chart = new CanvasJS.Chart("chartContainer", {
title: {
text: "Spending of Money Based on Household Composition"
},
theme: "light2",
animationEnabled: true,
toolTip:{
shared: true,
reversed: true
},
axisY: {
suffix: "шт."
},
data: data
});
chart.render();
}
</script>
</head>
<body>
<div id="chartContainer" style="height: 370px; width: 100%;"></div>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
</body>
</html>
сделал