Charts PHPSReadSheet图表类型\条形图\ 3D自定义颜色

Charts PHPSReadSheet图表类型\条形图\ 3D自定义颜色,charts,phpspreadsheet,Charts,Phpspreadsheet,我请求帮助,以便能够自定义PHPSReadSheet图表,最好是导出到excel时的条形图类型,以便它以圆柱形显示,并且可以独立定义每个条形图的颜色 这是我的密码 // Custom colors for dataSeries (gray, blue, red, orange...........) $colors = [ '00a3f5', '00a3f5', '00a3f5', '00a3f5', '00a3f5', '00a3f5', '00a3f5', '00a3f5', '0

我请求帮助,以便能够自定义PHPSReadSheet图表,最好是导出到excel时的条形图类型,以便它以圆柱形显示,并且可以独立定义每个条形图的颜色

这是我的密码

// Custom colors for dataSeries (gray, blue, red, orange...........)
$colors = [
     '00a3f5', '00a3f5', '00a3f5', '00a3f5', '00a3f5', '00a3f5', '00a3f5', '00a3f5', '00a3f5', '00a3f5', '00a3f5',
];
 
 $dataSeriesLabels1 = [
        new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1), // 2010
    ];
  $xAxisTickValues1 = [
        new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A12', null, 11), // PI to ANI
    ];
 $dataSeriesValues1 = [
        new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2', null, 11, [], null, $colors),
        new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$3:$B$12', null, 11, [], null, $colors),
    ];


  $series1 = new DataSeries(
    DataSeries::TYPE_BARCHART_3D, // plotType
    null, // plotGrouping (Pie charts don't have any grouping)
    range(0, count($dataSeriesValues1) - 1), // plotOrder
    $dataSeriesLabels1, // plotLabel
    $xAxisTickValues1, // plotCategory
    $dataSeriesValues1          // plotValues
);
$layout1 = new Layout();
$layout1->setShowVal(true);
$layout1->setShowPercent(true);


$legend1 = new Legend(Legend::POSITION_RIGHT, null, false);

$title1 = new Title('My Bar');
$chart1 = new Chart(
    'chart1', // name
    $title1, // title
    $legend1, // legend
    $plotArea1, 
    true, 
    DataSeries::EMPTY_AS_GAP, 
    null, 
    null  
);

并不是所有的图表类型都被实现。可能这是不受支持的选项之一。

非常感谢您的帮助,我能够通过为每个想要添加颜色的条创建一种类别来解决问题,并为图形创建一个额外的表格来搜索颜色

<?php
error_reporting(0);

require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Chart\Chart;
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
use PhpOffice\PhpSpreadsheet\Chart\Layout;
use PhpOffice\PhpSpreadsheet\Chart\Legend;
use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
use PhpOffice\PhpSpreadsheet\Chart\Title;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Chart\Axis;

include 'conexao.php';
$sql = "SELECT * FROM global";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$x=$row["indice_global"]-0.369;
$ty = ($x*100)/$row["indice_global"];
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
$spreadsheet->getActiveSheet()
            ->setCellValue('A1',"Area")
            ->setCellValue('B1',"Nível de Implementação");
$media = array();
$nome_da_media = array();
$linha = 2;
$recomendacao = array("2.1","3.1","3.2","3.3","5.1","5.2","5.3","5.5","7.1","8","12","12.5","12.6","12.7","13.3","14.3","15.1","18","20.3","22.1","22.6","23.1","24","25","28.1","29.1","29.2","30.1","30.2","30.3","31.1","32","33","34","35","36","37","42");
for($i = 0; $i<count($recomendacao); $i++){
    $comando = "SELECT AVG(media_escala) as media FROM registo INNER JOIN progresso ON progresso.referencia=registo.referencia AND registo.codico_de_area = ".$recomendacao[$i];
    $comando2 = "SELECT registo.descricao_de_area FROM registo INNER JOIN progresso ON progresso.referencia=registo.referencia AND registo.codico_de_area = ".$recomendacao[$i];
    $result = $conn->query($comando);$row=$result->fetch_assoc();
    $result2 = $conn->query($comando2);$row2=$result2->fetch_assoc();
    if($row["media"] !=null && $row2["descricao_de_area"] !=null){
        $spreadsheet->getActiveSheet()
                        ->setCellValue('B'.$linha.'', number_format($row["media"],0))
                        ->setCellValue('A'.$linha.'', $row2["descricao_de_area"]);
                       
        $linha++;
    }
}


$comando2 = "SELECT grau_global FROM global";
$result = $conn->query($comando2);$row=$result->fetch_assoc();
$spreadsheet->getActiveSheet()
            ->setCellValue('A'.$linha.'',"Media")
            ->setCellValue('B'.$linha.'', number_format($row["grau_global"],0));
                    

            $spreadsheet->getActiveSheet()->getStyle('K1')
            ->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
            $spreadsheet->getActiveSheet()->getStyle('L1')
            ->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_BLUE);
            $spreadsheet->getActiveSheet()->getStyle('M1')
            ->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_YELLOW);
            $spreadsheet->getActiveSheet()->getStyle('N1')
            ->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_GREEN);
            $spreadsheet->getActiveSheet()
            ->setCellValue('K1', 'ANI')
            ->setCellValue('L1', 'EPI')
            ->setCellValue('M1', 'PI')
            ->setCellValue('N1', 'TI');
$media = array();
$nome_da_media = array();
$linha = 2;
$recomendacao = array("2.1","3.1","3.2","3.3","5.1","5.2","5.3","5.5","7.1","8","12","12.5","12.6","12.7","13.3","14.3","15.1","18","20.3","22.1","22.6","23.1","24","25","28.1","29.1","29.2","30.1","30.2","30.3","31.1","32","33","34","35","36","37","42");
for($i = 0; $i<count($recomendacao); $i++){
    $comando = "SELECT AVG(media_escala) as media FROM registo INNER JOIN progresso ON progresso.referencia=registo.referencia AND registo.codico_de_area = ".$recomendacao[$i];
    $comando2 = "SELECT registo.descricao_de_area FROM registo INNER JOIN progresso ON progresso.referencia=registo.referencia AND registo.codico_de_area = ".$recomendacao[$i];
    $result = $conn->query($comando);$row=$result->fetch_assoc();
    $result2 = $conn->query($comando2);$row2=$result2->fetch_assoc();
    if($row["media"] !=null && $row2["descricao_de_area"] !=null){
        if($row["media"]>99){
            $spreadsheet->getActiveSheet()
            ->setCellValue('N'.$linha.'', number_format($row["media"],0));
        }else if($row["media"]>50){
            $spreadsheet->getActiveSheet()
            ->setCellValue('M'.$linha.'', number_format($row["media"],0));
        }else if($row["media"]>0){
            $spreadsheet->getActiveSheet()
            ->setCellValue('L'.$linha.'', number_format($row["media"],0));
        }
        else if($row["media"]==0){
            $spreadsheet->getActiveSheet()
            ->setCellValue('K'.$linha.'', number_format($row["media"],0));
        }
        $linha++;
    }
}
$comando2 = "SELECT grau_global FROM global";
$result = $conn->query($comando2);$row=$result->fetch_assoc();
if($row["grau_global"]>99){
    $spreadsheet->getActiveSheet()
    ->setCellValue('N'.$linha.'', number_format($row["grau_global"],0));
}else if($row["grau_global"]>50){
    $spreadsheet->getActiveSheet()
    ->setCellValue('M'.$linha.'', number_format($row["grau_global"],0));
}else if($row["grau_global"]>0){
    $spreadsheet->getActiveSheet()
    ->setCellValue('L'.$linha.'', number_format($row["grau_global"],0));
}
else if($row["grau_global"]==0){
    $spreadsheet->getActiveSheet()
    ->setCellValue('K'.$linha.'', number_format($row["grau_global"],0));
}


           
// $cor=
// Set the Labels for each data series we want to plot
//     Datatype
//     Cell reference for data
//     Format Code
//     Number of datapoints in series
//     Data values
//     Data Marker
$dataSeriesLabels = [
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$K$1', null, 1, [], null, 'f20f0f'),
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$L$1', null, 1, [], null, '09b4e3'),
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$M$1', null, 1, [], null, 'fff203'),
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$N$1', null, 1, [], null, '0ff247'), // 2010
    // new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1, [], null, 'ff085a'), // 2011
    // new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // 2012
];
// // Set the X-Axis Labels
// //     Datatype
// //     Cell reference for data
// //     Format Code
// //     Number of datapoints in series
// //     Data values
// //     Data Marker
$xAxisTickValues = [
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$42', null, 39),
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$42', null, 39),
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$42', null, 39),
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$42', null, 39),
     // Q1 to Q4
];
// // Set the Data values for each data series we want to plot
// //     Datatype
// //     Cell reference for data
// //     Format Code
// //     Number of datapoints in series
// //     Data values
// //     Data Marker
$dataSeriesValues = [
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$K$2:$K$42', null, 39),
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$L$2:$L$42', null, 39),
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$M$2:$M$42', null, 39),
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$N$2:$N$42', null, 39),
    
];
// $dataSeriesLabels->getFillColor()->setFillColor('5507f0');


    $yaxis = new Axis();
    $xaxis = new Axis();
    $yaxis->setAxisOptionsProperties('low', null, null, null, null, null, 0, 100, null, null);
    $yaxis->setLineParameters('FFFFFF',100,Axis::EXCEL_COLOR_TYPE_ARGB);
    $xaxis->setAxisOptionsProperties('low', null, null, null, null, null, 0, 0, null, null);

// // Build the dataseries
$series = new DataSeries(
    DataSeries::TYPE_BARCHART, // plotType
    DataSeries::GROUPING_STANDARD, // plotGrouping
    range(0, count($dataSeriesValues) - 1), // plotOrder
    $dataSeriesLabels, // plotLabel
    $xAxisTickValues, // plotCategory
    $dataSeriesValues        // plotValues
);
// // Set additional dataseries parameters
// //     Make it a vertical column rather than a horizontal bar graph
$series->setPlotDirection(DataSeries::DIRECTION_COL);

// // Set the series in the plot area
$plotArea = new PlotArea(null, [$series]);
// // Set the chart legend
$legend = new Legend(Legend::POSITION_RIGHT, null, false);

$title = new Title('Nível De Implementação Por Area');
$yAxisLabel = new Title('');

// // Create the chart
$chart = new Chart(
    'chart1', // name
    $title, // title
    $legend, // legend
    $plotArea, // plotArea
    true, // plotVisibleOnly
    DataSeries::EMPTY_AS_GAP, // displayBlanksAs
    null, // xAxisLabel
    $yAxisLabel,  // yAxisLabel
    $yaxis,
    $xaxis
);


// // Set the position where the chart should appear in the worksheet
$chart->setTopLeftPosition('D2');
$chart->setBottomRightPosition('Z25');

// // Add the chart to the worksheet
$worksheet->addChart($chart);

header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="Graficico_Indice.xlsx"');
header("Content-Description: PHP Generated Data");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");



$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->setIncludeCharts(true);
$writer->save('php://output');
exit;
?>