首页 > 解决方案 > PhpSpreadSheet 图表 TYPE_BARCHART_3D 自定义颜色

问题描述

我寻求帮助以便能够自定义 PhpSpreadSheet 图表,最好是导出到 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  
);

标签: chartsphpspreadsheet

解决方案


并非所有图表类型都已实现。也许这是不支持的之一。


推荐阅读