今回は先ず2個のチャート上にそれぞれ「Pie Chart:円グラフ」「Donut Chart:ドーナツグラフ」のグラフを作成し、 その後で「Radar Chart:レーダーチャート」作成します。
■2個のチャート上にそれぞれ「Pie Chart:円グラフ」「Donut Chart:ドーナツグラフ」のグラフを作成
「Pie Chart:円グラフ」「Donut Chart:ドーナツグラフ」は共に系列データ等の指定方法は同じで、表示される図形が円かドーナツかの違いがあるだけです。
系列データとして設定できるのは1種類のデータの系列のみになります。 以下のテストデータを例にとると、縦方向には「商品1」の「2016~2020の1列のデータ」が対象となります。 同一チャート上に「商品1」及び「商品2」のグラフを「棒グラフ」等の様に同時には表示できません。
// チャート用テストデータ生成
$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),
)
);
以前は PhpSpreadsheet\Chart\PlotArea クラス生成で先頭の引数には「NULL」を指定していましたが、 円グラフの各要素の値を表示するために PhpSpreadsheet\Chart\Layout クラスのオブジェクトを生成し、 指定します。
PhpSpreadsheet\Chart\Layout クラスの setShowVal メソッドで値表示を指定し setShowPercent メソッドでパーセント表記の指定をします。 (結果、値とパーセントの二つとも表示されます)
実際のソースは以下の様になります。
(「Pie Chart:円グラフ」は「商品1」を「Donut Chart:ドーナツグラフ」は「商品2」を対象とします。)
<?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\Layout;
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),
)
);
//=====================
// Pie Chart
//=====================
// 系列ラベルの指定
$arrDataSeriesLabels1 = array(
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', NULL, 1), //商品1
);
// X軸ラベルの指定
$arrCategorysDataSeries1 = array(
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$6', NULL, 5), //2016 to 2020
);
// 描画データの指定
$arrDataSeriesValues1 = array(
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$6', NULL, 5), //商品1
);
// チャート・データシリーズの生成
$objSeries1 = new DataSeries(
DataSeries::TYPE_PIECHART, // plotType
NULL, // plotGrouping (円グラフはグループ指定必要無し)
range(0, count($arrDataSeriesValues1) - 1), // plotOrder
$arrDataSeriesLabels1, // plotLabel
$arrCategorysDataSeries1, // plotCategory
$arrDataSeriesValues1 // plotValues
);
// 円グラフ用のレイアウト設定
$objLayout1 = new Layout();
$objLayout1->setShowVal(TRUE);
$objLayout1->setShowPercent(TRUE);
// プロットエリアにチャート・データシリーズに設定
$objPlotArea1 = new PlotArea($objLayout1, array($objSeries1));
// レジェンド生成(各折れ線の説明を行う)
$objLegend1 = new Legend(Legend::POSITION_TOPRIGHT, NULL, false);
// チャート・タイトル生成
$objTitle1 = new Title('Pie Chart:商品1');
// チャート生成
$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);
//=====================
// Donut Chart
//=====================
// 系列ラベルの指定
$arrDataSeriesLabels2 = array(
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$2', NULL, 1), //商品2
);
// X軸ラベルの指定
$arrCategorysDataSeries2 = array(
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$6', NULL, 5), //2016 to 2020
);
// 描画データの指定
$arrDataSeriesValues2 = array(
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$6', NULL, 5), //商品2
);
// チャート・データシリーズの生成
$objSeries2 = new DataSeries(
DataSeries::TYPE_DONUTCHART, // plotType
NULL, // plotGrouping (ドーナツグラフはグループ指定必要無し)
range(0, count($arrDataSeriesValues2) - 1), // plotOrder
$arrDataSeriesLabels2, // plotLabel
$arrCategorysDataSeries2, // plotCategory
$arrDataSeriesValues2 // plotValues
);
// 円グラフ用のレイアウト設定
$objLayout2 = new Layout();
$objLayout2->setShowVal(TRUE);
$objLayout2->setShowPercent(TRUE);
// プロットエリアにチャート・データシリーズに設定
$objPlotArea2 = new PlotArea($objLayout2, array($objSeries2));
// レジェンド生成(各折れ線の説明を行う)
$objLegend2 = new Legend(Legend::POSITION_TOPRIGHT, NULL, false);
// チャート・タイトル生成
$objTitle2 = new Title('Donut Chart:商品2');
// チャート生成
$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);
// [test-g-6-1.xlsx]:Excel2007形式で保存する
$objWriter = IOFactory::createWriter($objSpreadsheet, 'Xlsx');
$objWriter->setIncludeCharts(TRUE);
$objWriter->save('test-g-6-1.xlsx');
exit();
?>
出力されたエクセルファイルを見てみると以下の様になります。 ■「Radar Chart:レーダーチャート」のグラフを作成
「Radar Chart:レーダーチャート」のグラフを作成してみます。「Radar Chart:レーダーチャート」とは折れ線グラフの線を環状に繋げたイメージです。
「系列ラベル」「X軸ラベル」「系列(描画)データ」は各参照系列ごと対応するものを同じ個数設定します。
実際のソースは以下の様になります。
<?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\Layout;
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), //商品1
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', NULL, 1), //商品2
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', NULL, 1), //商品3
);
// X軸ラベルの指定
$arrCategorysDataSeries = array(
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$6', NULL, 5), //2016 to 2020
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$6', NULL, 5), //2016 to 2020
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
);
// チャート・データシリーズの生成
$objSeries = new DataSeries(
DataSeries::TYPE_RADARCHART, // plotType
NULL, // plotGrouping (レーダーチャートはグループ指定必要無し)
range(0, count($arrDataSeriesValues) - 1), // plotOrder
$arrDataSeriesLabels, // plotLabel
$arrCategorysDataSeries, // plotCategory
$arrDataSeriesValues, // plotValues
NULL, // plotDirection
NULL, // smooth line
DataSeries::STYLE_MARKER // plotStyle
);
// プロットエリアにチャート・データシリーズに設定
$objPlotArea = new PlotArea(NULL, array($objSeries));
// レジェンド生成
$objLegend = new Legend(Legend::POSITION_TOPRIGHT, NULL, false);
// チャート・タイトル生成
$objTitle = new Title('Radar Chart');
// チャート生成
$objChart = new Chart(
'chart1', // name
$objTitle, // title
$objLegend, // legend
$objPlotArea, // plotArea
TRUE, // plotVisibleOnly
0, // displayBlanksAs
NULL, // xAxisLabel
NULL // yAxisLabel
);
// ワークシート内のチャート位置設定
$objChart->setTopLeftPosition('A8'); // 左上
$objChart->setBottomRightPosition('F20'); // 右下
// ワークシートにチャート追加
$objWorksheet->addChart($objChart);
// [test-g-6-2.xlsx]:Excel2007形式で保存する
$objWriter = IOFactory::createWriter($objSpreadsheet, 'Xlsx');
$objWriter->setIncludeCharts(TRUE);
$objWriter->save('test-g-6-2.xlsx');
exit();
?>
コメント