今回は以下の記事で紹介したチャートに対して、各要素の色を設定する方法について説明します。 基本的にはエクセルで行うのと同じなのですが、系列ラベルの色を変更することで、チャートの各系列の領域の色設定が行えます。
PHP PhpSpreadsheet\Chart エクセルシート上に複数のチャートで異なるタイプのグラフ(Bar Chart:棒グラフ、Line Chart:線グラフ、Area Chart:面グラフ)を作成する方法について
しかし、上記の記事の中の「Line Chart:線グラフ」だけ残念ながら、線の色設定ができませんでした。
そのため、これを除いた3個のチャート「Bar Chart:棒グラフ」「Bar Chart:棒グラフ(積み上げ)」「Area Chart:面グラフ」の塗りつぶしの色設定について記します。
エクセルでは「Line Chart:線グラフ」でも線の色指定はできるのですが、このライブラリではできない様です。(私の調べ方が悪いのかもしれませんが...)
ここで、「系列ラベル」データの生成について説明します。
グラフの「系列ラベル」を PhpSpreadsheet\DataSeries クラスで生成しますが、 PhpSpreadsheet\Chart\DataSeries のクラス生成の引数の中で重要な「ラベル」の配列の要素のクラスである PhpSpreadsheet\Chart\DataSeriesValues のクラス生成の引数は以下の様に定義されています。
PhpSpreadsheet\DataSeriesValues __construct( $dataType = self::DATASERIES_TYPE_NUMBER, // データ型の定数 $dataSource = null, // シリーズ(系列)データの位置指定 $formatCode = null, // フォーマットコード $pointCount = 0, // シリーズ(系列)データの個数 $dataValues = array(), // シリーズ(系列)データを直接指定 $marker = null, // ポイントマーカーの指定(文字列) $fillColor = null // 塗りつぶし色設定 )
最後の引数の $fillColor が PhpSpreadsheet\DataSeries クラスにおける色設定となります。
ここに今まで何も設定しなかったのでデフォルトの色がチャートに表示されましたが、 色コードを文字列で設定してやれば、各系列の色が変更できます。
色コードは「FF00AA」の様に16進数の表記で、左の2桁からRGBの指定を行います。
実際のソースは以下の様になります。
<?php
// ライブラリ読込
require '../vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Chart\Chart;
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
use PhpOffice\PhpSpreadsheet\Chart\Title;
use PhpOffice\PhpSpreadsheet\Chart\Legend;
use PhpOffice\PhpSpreadsheet\IOFactory;
// Spreadsheetオブジェクト生成
$objSpreadsheet = new Spreadsheet();
// ワークシートオブジェクト取得
$objWorksheet = $objSpreadsheet->getActiveSheet();
// チャート用テストデータ生成
$objWorksheet->fromArray(
array(
array('売上', '商品1', '商品2', '商品3'),
array( 2016, 12, 18, 15),
array( 2017, 15, 19, 10),
array( 2018, 21, 23, 20),
array( 2019, 18, 14, 12),
array( 2020, 20, 21, 23),
)
);
// 系列ラベルの指定(色指定)
$arrDataSeriesLabels = array(
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING,'Worksheet!$B$1',NULL,1,[],NULL,"ff0000"), //商品1
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING,'Worksheet!$C$1',NULL,1,[],NULL,"00ff00"), //商品2
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING,'Worksheet!$D$1',NULL,1,[],NULL,"0000ff"), //商品3
);
// X軸ラベルの指定
$arrCategorysDataSeries = array(
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$6', NULL, 5), //2016 to 2020
);
// 描画データの指定
$arrDataSeriesValues = array(
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$6', NULL, 5), //商品1
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$6', NULL, 5), //商品2
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$6', NULL, 5), //商品3
);
//=====================
// Bar Chart
//=====================
// チャート・データシリーズの生成
$objSeries1 = new DataSeries(
DataSeries::TYPE_BARCHART, // plotType
DataSeries::GROUPING_STANDARD, // plotGrouping
range(0, count($arrDataSeriesValues) - 1), // plotOrder
$arrDataSeriesLabels, // plotLabel
$arrCategorysDataSeries, // plotCategory
$arrDataSeriesValues // plotValues
);
// プロットエリアにチャート・データシリーズに設定
$objPlotArea1 = new PlotArea(NULL, array($objSeries1));
// レジェンド生成(各折れ線の説明を行う)
$objLegend1 = new Legend(Legend::POSITION_TOPRIGHT, NULL, false);
// チャート・タイトル生成
$objTitle1 = new Title('売上データ:Bar Chart');
// チャート生成
$objChart1 = new Chart(
'chart1', // name
$objTitle1, // title
$objLegend1, // legend
$objPlotArea1, // plotArea
TRUE, // plotVisibleOnly
0, // displayBlanksAs
NULL, // xAxisLabel
NULL // yAxisLabel
);
// ワークシート内のチャート位置設定
$objChart1->setTopLeftPosition('A8'); // 左上
$objChart1->setBottomRightPosition('G20'); // 右下
// ワークシートにチャート追加
$objWorksheet->addChart($objChart1);
//=====================
// Bar Chart:STACKED
//=====================
// チャート・データシリーズの生成
$objSeries2 = new DataSeries(
DataSeries::TYPE_BARCHART, // plotType
DataSeries::GROUPING_PERCENT_STACKED, // plotGrouping
range(0, count($arrDataSeriesValues) - 1), // plotOrder
$arrDataSeriesLabels, // plotLabel
$arrCategorysDataSeries, // plotCategory
$arrDataSeriesValues // plotValues
);
// プロットエリアにチャート・データシリーズに設定
$objPlotArea2 = new PlotArea(NULL, array($objSeries2));
// レジェンド生成(各折れ線の説明を行う)
$objLegend2 = new Legend(Legend::POSITION_TOPRIGHT, NULL, false);
// チャート・タイトル生成
$objTitle2 = new Title('売上データ:Bar Chart:STACKED');
// チャート生成
$objChart2 = new Chart(
'chart2', // name
$objTitle2, // title
$objLegend2, // legend
$objPlotArea2, // plotArea
TRUE, // plotVisibleOnly
0, // displayBlanksAs
NULL, // xAxisLabel
NULL // yAxisLabel
);
// ワークシート内のチャート位置設定
$objChart2->setTopLeftPosition('H8'); // 左上
$objChart2->setBottomRightPosition('N20'); // 右下
// ワークシートにチャート追加
$objWorksheet->addChart($objChart2);
/*
//=====================
// Line Chart
//=====================
// チャート・データシリーズの生成
$objSeries3 = new DataSeries(
DataSeries::TYPE_LINECHART, // plotType
DataSeries::GROUPING_STANDARD, // plotGrouping
range(0, count($arrDataSeriesValues) - 1), // plotOrder
$arrDataSeriesLabels, // plotLabel
$arrCategorysDataSeries, // plotCategory
$arrDataSeriesValues // plotValues
);
// プロットエリアにチャート・データシリーズに設定
$objPlotArea3 = new PlotArea(NULL, array($objSeries3));
// レジェンド生成(各折れ線の説明を行う)
$objLegend3 = new Legend(Legend::POSITION_TOPRIGHT, NULL, false);
// チャート・タイトル生成
$objTitle3 = new Title('売上データ:Line Chart');
// チャート生成
$objChart3 = new Chart(
'chart3', // name
$objTitle3, // title
$objLegend3, // legend
$objPlotArea3, // plotArea
TRUE, // plotVisibleOnly
0, // displayBlanksAs
NULL, // xAxisLabel
NULL // yAxisLabel
);
// ワークシート内のチャート位置設定
$objChart3->setTopLeftPosition('A21'); // 左上
$objChart3->setBottomRightPosition('G33'); // 右下
// ワークシートにチャート追加
$objWorksheet->addChart($objChart3);
*/
//=====================
// Area Chart
//=====================
// チャート・データシリーズの生成
$objSeries4 = new DataSeries(
DataSeries::TYPE_AREACHART, // plotType
DataSeries::GROUPING_PERCENT_STACKED, // plotGrouping
range(0, count($arrDataSeriesValues) - 1), // plotOrder
$arrDataSeriesLabels, // plotLabel
$arrCategorysDataSeries, // plotCategory
$arrDataSeriesValues // plotValues
);
// プロットエリアにチャート・データシリーズに設定
$objPlotArea4 = new PlotArea(NULL, array($objSeries4));
// レジェンド生成(各折れ線の説明を行う)
$objLegend4 = new Legend(Legend::POSITION_TOPRIGHT, NULL, false);
// チャート・タイトル生成
$objTitle4 = new Title('売上データ:Area Chart');
// チャート生成
$objChart4 = new Chart(
'chart4', // name
$objTitle4, // title
$objLegend4, // legend
$objPlotArea4, // plotArea
TRUE, // plotVisibleOnly
0, // displayBlanksAs
NULL, // xAxisLabel
NULL // yAxisLabel
);
// ワークシート内のチャート位置設定
$objChart4->setTopLeftPosition('H21'); // 左上
$objChart4->setBottomRightPosition('N33'); // 右下
// ワークシートにチャート追加
$objWorksheet->addChart($objChart4);
// [test-g-5-1.xlsx]:Excel2007形式で保存する
$objWriter = IOFactory::createWriter($objSpreadsheet, 'Xlsx');
$objWriter->setIncludeCharts(TRUE);
$objWriter->save('test-g-5-1c.xlsx');
exit();
?>
出力されたエクセルファイルを見てみると以下の様になります。
以前の色指定が無い場合は、以下の様にデフォルトの色が設定されています。
コメント