forked from PHPOffice/PhpSpreadsheet
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Font/Effects/Theme Support for Chart Data Labels and Axis (PHPOffice#…
…3476) * Font and Effects Support for Chart Data Labels and Axis Addresses some remaining issues with 32readwriteLineChart5 (see issue PHPOffice#1797). Font size is covered. So are effects, although the results are a bit odd. For the new spreadsheet 32readwriteLineChart6, the Axis labels have a yellow-ish glow, but reading and writing the spreadsheet in PhpSpreadsheet gives them a purple-ish glow. Nevertheless, the new test shows that the output file uses schemeClr accent4, as does the input file. So the effect is handled correctly, but it seems there is likely to be a difference between theme colors (Writer/Xlsx/Theme appears to write hard-coded color schemes, and, in any case, Reader/Xlsx does not appear to handle schemeClr). Fixing that will be a great deal more difficult, with a large chance of regression, and will need to happen in a separate PR (one that I am not currently investigating, but I will open a new issue). Effects using srgbClr (and probably sysclr) should be okay. * Better Theme Support When reading Xlsx, the theme colors will now also be used for writing. This means that a file can be loaded and saved and its chart colors will now be preserved. If the spreadsheet is created new, Excel 2007-2010 colors are used. The writer is currently hard-coded to use them, so this avoids making this a breaking change. The theme colors can be explicitly changed if desired, and Excel 2013+ colors can be introduced very easily. ```php $spreadsheet->getTheme() ->setThemeColorName(Theme::COLOR_SCHEME_2013_PLUS_NAME); ``` Likewise, if the old behavior of changing to the 2007-2010 scheme rather than using the input values is desired, that is easy to achieve after the load has taken place. ```php $spreadsheet->getTheme() ->setThemeColorName(Theme::COLOR_SCHEME_2007_2010_NAME); ``` The new Theme class introduced by this change can easily be extended to include Fonts and Effects. Unlike Colors, I am unsure what the practical effects of changing those to, say, the 2013+ defaults would be. * Scrutinizer Use an alias in a use statement. * Update Change Log Due to potential behavior change.
- Loading branch information
Showing
17 changed files
with
520 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
use PhpOffice\PhpSpreadsheet\Chart\Chart; | ||
use PhpOffice\PhpSpreadsheet\Chart\DataSeries; | ||
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues; | ||
use PhpOffice\PhpSpreadsheet\Chart\Legend as ChartLegend; | ||
use PhpOffice\PhpSpreadsheet\Chart\PlotArea; | ||
use PhpOffice\PhpSpreadsheet\Chart\Title; | ||
use PhpOffice\PhpSpreadsheet\IOFactory; | ||
use PhpOffice\PhpSpreadsheet\Spreadsheet; | ||
use PhpOffice\PhpSpreadsheet\Theme as SpreadsheetTheme; | ||
|
||
require __DIR__ . '/../Header.php'; | ||
|
||
$spreadsheet = new Spreadsheet(); | ||
// same as 33_Chart_create_area, but with 2013+ schemes | ||
$spreadsheet->getTheme()->setThemeColorName(SpreadsheetTheme::COLOR_SCHEME_2013_PLUS_NAME); | ||
$worksheet = $spreadsheet->getActiveSheet(); | ||
$worksheet->fromArray( | ||
[ | ||
['', 2010, 2011, 2012], | ||
['Q1', 12, 15, 21], | ||
['Q2', 56, 73, 86], | ||
['Q3', 52, 61, 69], | ||
['Q4', 30, 32, 0], | ||
] | ||
); | ||
|
||
// 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!$B$1', null, 1), // 2010 | ||
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 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$5', null, 4), // 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!$B$2:$B$5', null, 4), | ||
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4), | ||
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', null, 4), | ||
]; | ||
|
||
// Build the dataseries | ||
$series = new DataSeries( | ||
DataSeries::TYPE_AREACHART, // plotType | ||
DataSeries::GROUPING_PERCENT_STACKED, // plotGrouping | ||
range(0, count($dataSeriesValues) - 1), // plotOrder | ||
$dataSeriesLabels, // plotLabel | ||
$xAxisTickValues, // plotCategory | ||
$dataSeriesValues // plotValues | ||
); | ||
|
||
// Set the series in the plot area | ||
$plotArea = new PlotArea(null, [$series]); | ||
// Set the chart legend | ||
$legend = new ChartLegend(ChartLegend::POSITION_TOPRIGHT, null, false); | ||
|
||
$title = new Title('Test %age-Stacked Area Chart'); | ||
$yAxisLabel = new Title('Value ($k)'); | ||
|
||
// 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 | ||
); | ||
|
||
// Set the position where the chart should appear in the worksheet | ||
$chart->setTopLeftPosition('A7'); | ||
$chart->setBottomRightPosition('H20'); | ||
|
||
// Add the chart to the worksheet | ||
$worksheet->addChart($chart); | ||
|
||
// Save Excel 2007 file | ||
$filename = $helper->getFilename(__FILE__); | ||
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); | ||
$writer->setIncludeCharts(true); | ||
$callStartTime = microtime(true); | ||
$writer->save($filename); | ||
$helper->logWrite($writer, $filename, $callStartTime); |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.