Move Gridlines from Chart to Axis (#2923)

* Move Gridlines from Chart to Axis

This could, I hope, be my last major change to Chart for a while. When I first noticed this problem, I thought it would be a breaking change. However, although this change establishes some deprecations, I don't think it breaks anything. Major and minor gridlines had only been settable by the Chart constructor. This PR moves them where they belong, to Axis (eexisting Chart constructor code will still work). This allows them to be specified from both X and Y axis.

Chart is now entirely covered except for 2 statements, one deprecated and one that I just can't figure out. 99.71% for Charts, 88.96% overall. All references to the Chart directory in Phpstan baseline are eliminated.

* Minor Fixes, Unit Tests

Line style color type should default to null not prstClr.

Chart X-axis and Y-axis should alway be Axis, never null.

Add some unit tests.

* More Tests, Some Improvements

Make it easier to change line styles, adding an alternate method besides a setter function with at least a dozen parameters.
This commit is contained in:
oleibman 2022-07-07 21:48:12 -07:00 committed by GitHub
parent c22c6df5b5
commit 09406a6a3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 876 additions and 230 deletions

View File

@ -49,7 +49,11 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- Time interval formatting [Issue #2768](https://github.com/PHPOffice/PhpSpreadsheet/issues/2768) [PR #2772](https://github.com/PHPOffice/PhpSpreadsheet/pull/2772) - Time interval formatting [Issue #2768](https://github.com/PHPOffice/PhpSpreadsheet/issues/2768) [PR #2772](https://github.com/PHPOffice/PhpSpreadsheet/pull/2772)
- Copy from Xls(x) to Html/Pdf loses drawings [PR #2788](https://github.com/PHPOffice/PhpSpreadsheet/pull/2788) - Copy from Xls(x) to Html/Pdf loses drawings [PR #2788](https://github.com/PHPOffice/PhpSpreadsheet/pull/2788)
- Html Reader converting cell containing 0 to null string [Issue #2810](https://github.com/PHPOffice/PhpSpreadsheet/issues/2810) [PR #2813](https://github.com/PHPOffice/PhpSpreadsheet/pull/2813) - Html Reader converting cell containing 0 to null string [Issue #2810](https://github.com/PHPOffice/PhpSpreadsheet/issues/2810) [PR #2813](https://github.com/PHPOffice/PhpSpreadsheet/pull/2813)
- Many fixes for Charts, especially, but not limited to, Scatter, Bubble, and Surface charts. [Issue #2762](https://github.com/PHPOffice/PhpSpreadsheet/issues/2762) [Issue #2299](https://github.com/PHPOffice/PhpSpreadsheet/issues/2299) [Issue #2700](https://github.com/PHPOffice/PhpSpreadsheet/issues/2700) [Issue #2817](https://github.com/PHPOffice/PhpSpreadsheet/issues/2817) [Issue #2763](https://github.com/PHPOffice/PhpSpreadsheet/issues/2763) [Issue #2219](https://github.com/PHPOffice/PhpSpreadsheet/issues/2219) [Issue #2863](https://github.com/PHPOffice/PhpSpreadsheet/issues/2863) [PR #2828](https://github.com/PHPOffice/PhpSpreadsheet/pull/2828) [PR #2841](https://github.com/PHPOffice/PhpSpreadsheet/pull/2841) [PR #2846](https://github.com/PHPOffice/PhpSpreadsheet/pull/2846) [PR #2852](https://github.com/PHPOffice/PhpSpreadsheet/pull/2852) [PR #2856](https://github.com/PHPOffice/PhpSpreadsheet/pull/2856) [PR #2865](https://github.com/PHPOffice/PhpSpreadsheet/pull/2865) [PR #2872](https://github.com/PHPOffice/PhpSpreadsheet/pull/2872) [PR #2879](https://github.com/PHPOffice/PhpSpreadsheet/pull/2879) [PR #2898](https://github.com/PHPOffice/PhpSpreadsheet/pull/2898) [PR #2906](https://github.com/PHPOffice/PhpSpreadsheet/pull/2906) [PR #2922](https://github.com/PHPOffice/PhpSpreadsheet/pull/2922) - Many fixes for Charts, especially, but not limited to, Scatter, Bubble, and Surface charts. [Issue #2762](https://github.com/PHPOffice/PhpSpreadsheet/issues/2762) [Issue #2299](https://github.com/PHPOffice/PhpSpreadsheet/issues/2299) [Issue #2700](https://github.com/PHPOffice/PhpSpreadsheet/issues/2700) [Issue #2817](https://github.com/PHPOffice/PhpSpreadsheet/issues/2817) [Issue #2763](https://github.com/PHPOffice/PhpSpreadsheet/issues/2763) [Issue #2219](https://github.com/PHPOffice/PhpSpreadsheet/issues/2219) [Issue #2863](https://github.com/PHPOffice/PhpSpreadsheet/issues/2863) [PR #2828](https://github.com/PHPOffice/PhpSpreadsheet/pull/2828) [PR #2841](https://github.com/PHPOffice/PhpSpreadsheet/pull/2841) [PR #2846](https://github.com/PHPOffice/PhpSpreadsheet/pull/2846) [PR #2852](https://github.com/PHPOffice/PhpSpreadsheet/pull/2852) [PR #2856](https://github.com/PHPOffice/PhpSpreadsheet/pull/2856) [PR #2865](https://github.com/PHPOffice/PhpSpreadsheet/pull/2865) [PR #2872](https://github.com/PHPOffice/PhpSpreadsheet/pull/2872) [PR #2879](https://github.com/PHPOffice/PhpSpreadsheet/pull/2879) [PR #2898](https://github.com/PHPOffice/PhpSpreadsheet/pull/2898) [PR #2906](https://github.com/PHPOffice/PhpSpreadsheet/pull/2906) [PR #2922](https://github.com/PHPOffice/PhpSpreadsheet/pull/2922) [PR #2923](https://github.com/PHPOffice/PhpSpreadsheet/pull/2923)
- Adjust both coordinates for two-cell anchors when rows/columns are added/deleted. [Issue #2908](https://github.com/PHPOffice/PhpSpreadsheet/issues/2908) [PR #2909](https://github.com/PHPOffice/PhpSpreadsheet/pull/2909)
- Keep calculated string results below 32K. [PR #2921](https://github.com/PHPOffice/PhpSpreadsheet/pull/2921)
- Filter out illegal Unicode char values FFFE/FFFF. [Issue #2897](https://github.com/PHPOffice/PhpSpreadsheet/issues/2897) [PR #2910](https://github.com/PHPOffice/PhpSpreadsheet/pull/2910)
- Better handling of REF errors and propagation of all errors in Calculation engine. [PR #2902](https://github.com/PHPOffice/PhpSpreadsheet/pull/2902)
- Calculating Engine regexp for Column/Row references when there are multiple quoted worksheet references in the formula [Issue #2874](https://github.com/PHPOffice/PhpSpreadsheet/issues/2874) [PR #2899](https://github.com/PHPOffice/PhpSpreadsheet/pull/2899) - Calculating Engine regexp for Column/Row references when there are multiple quoted worksheet references in the formula [Issue #2874](https://github.com/PHPOffice/PhpSpreadsheet/issues/2874) [PR #2899](https://github.com/PHPOffice/PhpSpreadsheet/pull/2899)
## 1.23.0 - 2022-04-24 ## 1.23.0 - 2022-04-24

View File

@ -1110,86 +1110,6 @@ parameters:
count: 1 count: 1
path: src/PhpSpreadsheet/Cell/Coordinate.php path: src/PhpSpreadsheet/Cell/Coordinate.php
-
message: "#^Call to an undefined method object\\:\\:render\\(\\)\\.$#"
count: 1
path: src/PhpSpreadsheet/Chart/Chart.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Chart\\:\\:\\$legend \\(PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Legend\\) does not accept PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Legend\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Chart/Chart.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Chart\\:\\:\\$majorGridlines \\(PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\GridLines\\) does not accept PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\GridLines\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Chart/Chart.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Chart\\:\\:\\$minorGridlines \\(PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\GridLines\\) does not accept PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\GridLines\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Chart/Chart.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Chart\\:\\:\\$plotArea \\(PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\PlotArea\\) does not accept PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\PlotArea\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Chart/Chart.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Chart\\:\\:\\$title \\(PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Title\\) does not accept PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Title\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Chart/Chart.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Chart\\:\\:\\$worksheet \\(PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\Worksheet\\) does not accept PhpOffice\\\\PhpSpreadsheet\\\\Worksheet\\\\Worksheet\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Chart/Chart.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Chart\\:\\:\\$xAxis \\(PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Axis\\) does not accept PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Axis\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Chart/Chart.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Chart\\:\\:\\$xAxisLabel \\(PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Title\\) does not accept PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Title\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Chart/Chart.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Chart\\:\\:\\$yAxis \\(PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Axis\\) does not accept PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Axis\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Chart/Chart.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Chart\\:\\:\\$yAxisLabel \\(PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Title\\) does not accept PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Title\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Chart/Chart.php
-
message: "#^Strict comparison using \\=\\=\\= between PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\DataSeriesValues and null will always evaluate to false\\.$#"
count: 2
path: src/PhpSpreadsheet/Chart/DataSeries.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Legend\\:\\:\\$layout \\(PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Layout\\) does not accept PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Layout\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Chart/Legend.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Legend\\:\\:\\$positionXLref has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Chart/Legend.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\PlotArea\\:\\:\\$layout \\(PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Layout\\) does not accept PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Layout\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Chart/PlotArea.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Title\\:\\:\\$layout \\(PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Layout\\) does not accept PhpOffice\\\\PhpSpreadsheet\\\\Chart\\\\Layout\\|null\\.$#"
count: 1
path: src/PhpSpreadsheet/Chart/Title.php
- -
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Collection\\\\Memory\\:\\:\\$cache has no type specified\\.$#" message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Collection\\\\Memory\\:\\:\\$cache has no type specified\\.$#"
count: 1 count: 1
@ -3630,11 +3550,6 @@ parameters:
count: 1 count: 1
path: src/PhpSpreadsheet/Writer/Html.php path: src/PhpSpreadsheet/Writer/Html.php
-
message: "#^Ternary operator condition is always true\\.$#"
count: 1
path: src/PhpSpreadsheet/Writer/Html.php
- -
message: "#^Negated boolean expression is always false\\.$#" message: "#^Negated boolean expression is always false\\.$#"
count: 1 count: 1

View File

@ -0,0 +1,209 @@
<?php
use PhpOffice\PhpSpreadsheet\Chart\Chart;
use PhpOffice\PhpSpreadsheet\Chart\ChartColor;
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
use PhpOffice\PhpSpreadsheet\Chart\GridLines;
use PhpOffice\PhpSpreadsheet\Chart\Layout;
use PhpOffice\PhpSpreadsheet\Chart\Legend as ChartLegend;
use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
use PhpOffice\PhpSpreadsheet\Chart\Properties;
use PhpOffice\PhpSpreadsheet\Chart\Title;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
require __DIR__ . '/../Header.php';
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->fromArray(
[
['', 2010, 2011, 2012],
['Q1', 12, 15, 21],
['Q2', 56, 73, 86],
['Q3', 52, 61, 69],
['Q4', 30, 32, 0],
]
);
// Custom colors for dataSeries (gray, blue, red, orange)
$colors = [
'cccccc', '00abb8', 'b8292f', 'eb8500',
];
// 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
$dataSeriesLabels1 = [
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 2011
];
// Set the X-Axis Labels
// Datatype
// Cell reference for data
// Format Code
// Number of datapoints in series
// Data values
// Data Marker
$xAxisTickValues1 = [
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
// Custom colors
$dataSeriesValues1 = [
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4, [], null, $colors),
];
$labelLayout = new Layout();
$labelLayout
->setShowVal(true)
->setLabelFontColor(new ChartColor('FFFF00'))
->setLabelFillColor(new ChartColor('accent2', null, 'schemeClr'));
$dataSeriesValues1[0]->setLabelLayout($labelLayout);
// Build the dataseries
$series1 = new DataSeries(
DataSeries::TYPE_BARCHART, // plotType
null, // plotGrouping (Pie charts don't have any grouping)
range(0, count($dataSeriesValues1) - 1), // plotOrder
$dataSeriesLabels1, // plotLabel
$xAxisTickValues1, // plotCategory
$dataSeriesValues1 // plotValues
);
// Set up a layout object for the Pie chart
$layout1 = new Layout();
$layout1->setShowVal(true);
$layout1->setShowPercent(true);
// Set the series in the plot area
$plotArea1 = new PlotArea($layout1, [$series1]);
// Set the chart legend
$legend1 = new ChartLegend(ChartLegend::POSITION_RIGHT, null, false);
$title1 = new Title('Test Bar Chart');
// Create the chart
$chart1 = new Chart(
'chart1', // name
$title1, // title
$legend1, // legend
$plotArea1, // plotArea
true, // plotVisibleOnly
DataSeries::EMPTY_AS_GAP, // displayBlanksAs
null, // xAxisLabel
null // yAxisLabel - Pie charts don't have a Y-Axis
);
$majorGridlinesY = new GridLines();
$majorGridlinesY->setLineColorProperties('FF0000');
$minorGridlinesY = new GridLines();
$minorGridlinesY->setLineStyleProperty('dash', Properties::LINE_STYLE_DASH_ROUND_DOT);
$chart1
->getChartAxisY()
->setMajorGridlines($majorGridlinesY)
->setMinorGridlines($minorGridlinesY);
$majorGridlinesX = new GridLines();
$majorGridlinesX->setLineColorProperties('FF00FF');
$minorGridlinesX = new GridLines();
$minorGridlinesX->activateObject();
$chart1
->getChartAxisX()
->setMajorGridlines($majorGridlinesX)
->setMinorGridlines($minorGridlinesX);
// Set the position where the chart should appear in the worksheet
$chart1->setTopLeftPosition('A7');
$chart1->setBottomRightPosition('H20');
// Add the chart to the worksheet
$worksheet->addChart($chart1);
// 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
$dataSeriesLabels2 = [
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 2011
];
// Set the X-Axis Labels
// Datatype
// Cell reference for data
// Format Code
// Number of datapoints in series
// Data values
// Data Marker
$xAxisTickValues2 = [
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
// Custom colors
$dataSeriesValues2 = [
$dataSeriesValues2Element = new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4),
];
$dataSeriesValues2Element->setFillColor($colors);
// Build the dataseries
$series2 = new DataSeries(
DataSeries::TYPE_DONUTCHART, // plotType
null, // plotGrouping (Donut charts don't have any grouping)
range(0, count($dataSeriesValues2) - 1), // plotOrder
$dataSeriesLabels2, // plotLabel
$xAxisTickValues2, // plotCategory
$dataSeriesValues2 // plotValues
);
// Set up a layout object for the Pie chart
$layout2 = new Layout();
$layout2->setShowVal(true);
$layout2->setShowCatName(true);
$layout2->setLabelFillColor(new ChartColor('FFFF00'));
// Set the series in the plot area
$plotArea2 = new PlotArea($layout2, [$series2]);
$title2 = new Title('Test Donut Chart');
// Create the chart
$chart2 = new Chart(
'chart2', // name
$title2, // title
null, // legend
$plotArea2, // plotArea
true, // plotVisibleOnly
DataSeries::EMPTY_AS_GAP, // displayBlanksAs
null, // xAxisLabel
null // yAxisLabel - Like Pie charts, Donut charts don't have a Y-Axis
);
// Set the position where the chart should appear in the worksheet
$chart2->setTopLeftPosition('I7');
$chart2->setBottomRightPosition('P20');
// Add the chart to the worksheet
$worksheet->addChart($chart2);
// 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);

View File

@ -16,6 +16,20 @@ class Axis extends Properties
$this->fillColor = new ChartColor(); $this->fillColor = new ChartColor();
} }
/**
* Chart Major Gridlines as.
*
* @var ?GridLines
*/
private $majorGridlines;
/**
* Chart Minor Gridlines as.
*
* @var ?GridLines
*/
private $minorGridlines;
/** /**
* Axis Number. * Axis Number.
* *
@ -235,4 +249,28 @@ class Axis extends Properties
{ {
return $this->crossBetween; return $this->crossBetween;
} }
public function getMajorGridlines(): ?GridLines
{
return $this->majorGridlines;
}
public function getMinorGridlines(): ?GridLines
{
return $this->minorGridlines;
}
public function setMajorGridlines(?GridLines $gridlines): self
{
$this->majorGridlines = $gridlines;
return $this;
}
public function setMinorGridlines(?GridLines $gridlines): self
{
$this->minorGridlines = $gridlines;
return $this;
}
} }

View File

@ -17,42 +17,42 @@ class Chart
/** /**
* Worksheet. * Worksheet.
* *
* @var Worksheet * @var ?Worksheet
*/ */
private $worksheet; private $worksheet;
/** /**
* Chart Title. * Chart Title.
* *
* @var Title * @var ?Title
*/ */
private $title; private $title;
/** /**
* Chart Legend. * Chart Legend.
* *
* @var Legend * @var ?Legend
*/ */
private $legend; private $legend;
/** /**
* X-Axis Label. * X-Axis Label.
* *
* @var Title * @var ?Title
*/ */
private $xAxisLabel; private $xAxisLabel;
/** /**
* Y-Axis Label. * Y-Axis Label.
* *
* @var Title * @var ?Title
*/ */
private $yAxisLabel; private $yAxisLabel;
/** /**
* Chart Plot Area. * Chart Plot Area.
* *
* @var PlotArea * @var ?PlotArea
*/ */
private $plotArea; private $plotArea;
@ -84,20 +84,6 @@ class Chart
*/ */
private $xAxis; private $xAxis;
/**
* Chart Major Gridlines as.
*
* @var GridLines
*/
private $majorGridlines;
/**
* Chart Minor Gridlines as.
*
* @var GridLines
*/
private $minorGridlines;
/** /**
* Top-Left Cell Position. * Top-Left Cell Position.
* *
@ -157,6 +143,7 @@ class Chart
/** /**
* Create a new Chart. * Create a new Chart.
* majorGridlines and minorGridlines are deprecated, moved to Axis.
* *
* @param mixed $name * @param mixed $name
* @param mixed $plotVisibleOnly * @param mixed $plotVisibleOnly
@ -172,10 +159,14 @@ class Chart
$this->plotArea = $plotArea; $this->plotArea = $plotArea;
$this->plotVisibleOnly = $plotVisibleOnly; $this->plotVisibleOnly = $plotVisibleOnly;
$this->displayBlanksAs = $displayBlanksAs; $this->displayBlanksAs = $displayBlanksAs;
$this->xAxis = $xAxis; $this->xAxis = $xAxis ?? new Axis();
$this->yAxis = $yAxis; $this->yAxis = $yAxis ?? new Axis();
$this->majorGridlines = $majorGridlines; if ($majorGridlines !== null) {
$this->minorGridlines = $minorGridlines; $this->yAxis->setMajorGridlines($majorGridlines);
}
if ($minorGridlines !== null) {
$this->yAxis->setMinorGridlines($minorGridlines);
}
} }
/** /**
@ -190,10 +181,8 @@ class Chart
/** /**
* Get Worksheet. * Get Worksheet.
*
* @return Worksheet
*/ */
public function getWorksheet() public function getWorksheet(): ?Worksheet
{ {
return $this->worksheet; return $this->worksheet;
} }
@ -210,12 +199,7 @@ class Chart
return $this; return $this;
} }
/** public function getTitle(): ?Title
* Get Title.
*
* @return Title
*/
public function getTitle()
{ {
return $this->title; return $this->title;
} }
@ -232,12 +216,7 @@ class Chart
return $this; return $this;
} }
/** public function getLegend(): ?Legend
* Get Legend.
*
* @return Legend
*/
public function getLegend()
{ {
return $this->legend; return $this->legend;
} }
@ -254,12 +233,7 @@ class Chart
return $this; return $this;
} }
/** public function getXAxisLabel(): ?Title
* Get X-Axis Label.
*
* @return Title
*/
public function getXAxisLabel()
{ {
return $this->xAxisLabel; return $this->xAxisLabel;
} }
@ -276,12 +250,7 @@ class Chart
return $this; return $this;
} }
/** public function getYAxisLabel(): ?Title
* Get Y-Axis Label.
*
* @return Title
*/
public function getYAxisLabel()
{ {
return $this->yAxisLabel; return $this->yAxisLabel;
} }
@ -298,16 +267,21 @@ class Chart
return $this; return $this;
} }
/** public function getPlotArea(): ?PlotArea
* Get Plot Area.
*
* @return PlotArea
*/
public function getPlotArea()
{ {
return $this->plotArea; return $this->plotArea;
} }
/**
* Set Plot Area.
*/
public function setPlotArea(PlotArea $plotArea): self
{
$this->plotArea = $plotArea;
return $this;
}
/** /**
* Get Plot Visible Only. * Get Plot Visible Only.
* *
@ -356,62 +330,58 @@ class Chart
return $this; return $this;
} }
/** public function getChartAxisY(): Axis
* Get yAxis.
*
* @return Axis
*/
public function getChartAxisY()
{ {
if ($this->yAxis !== null) {
return $this->yAxis;
}
$this->yAxis = new Axis();
return $this->yAxis; return $this->yAxis;
} }
/** /**
* Get xAxis. * Set yAxis.
*
* @return Axis
*/ */
public function getChartAxisX() public function setChartAxisY(?Axis $axis): self
{
$this->yAxis = $axis ?? new Axis();
return $this;
}
public function getChartAxisX(): Axis
{ {
if ($this->xAxis !== null) {
return $this->xAxis; return $this->xAxis;
} }
$this->xAxis = new Axis();
return $this->xAxis; /**
* Set xAxis.
*/
public function setChartAxisX(?Axis $axis): self
{
$this->xAxis = $axis ?? new Axis();
return $this;
} }
/** /**
* Get Major Gridlines. * Get Major Gridlines.
* *
* @return GridLines * @Deprecated 1.24.0 Use Axis->getMajorGridlines
*
* @codeCoverageIgnore
*/ */
public function getMajorGridlines() public function getMajorGridlines(): ?GridLines
{ {
if ($this->majorGridlines !== null) { return $this->yAxis->getMajorGridLines();
return $this->majorGridlines;
}
return new GridLines();
} }
/** /**
* Get Minor Gridlines. * Get Minor Gridlines.
* *
* @return GridLines * @Deprecated 1.24.0 Use Axis->getMinorGridlines
*
* @codeCoverageIgnore
*/ */
public function getMinorGridlines() public function getMinorGridlines(): ?GridLines
{ {
if ($this->minorGridlines !== null) { return $this->yAxis->getMinorGridLines();
return $this->minorGridlines;
}
return new GridLines();
} }
/** /**
@ -668,17 +638,21 @@ class Chart
public function refresh(): void public function refresh(): void
{ {
if ($this->worksheet !== null) { if ($this->worksheet !== null && $this->plotArea !== null) {
$this->plotArea->refresh($this->worksheet); $this->plotArea->refresh($this->worksheet);
} }
} }
/** /**
* Render the chart to given file (or stream). * Render the chart to given file (or stream).
* Unable to cover code until a usable current version of JpGraph
* is made available through Composer.
* *
* @param string $outputDestination Name of the file render to * @param string $outputDestination Name of the file render to
* *
* @return bool true on success * @return bool true on success
*
* @codeCoverageIgnore
*/ */
public function render($outputDestination = null) public function render($outputDestination = null)
{ {
@ -696,7 +670,7 @@ class Chart
$renderer = new $libraryName($this); $renderer = new $libraryName($this);
return $renderer->render($outputDestination); return $renderer->render($outputDestination); // @phpstan-ignore-line
} }
public function getRotX(): ?int public function getRotX(): ?int

View File

@ -134,12 +134,12 @@ class DataSeries
$this->plotOrder = $plotOrder; $this->plotOrder = $plotOrder;
$keys = array_keys($plotValues); $keys = array_keys($plotValues);
$this->plotValues = $plotValues; $this->plotValues = $plotValues;
if ((count($plotLabel) == 0) || ($plotLabel[$keys[0]] === null)) { if (!isset($plotLabel[$keys[0]])) {
$plotLabel[$keys[0]] = new DataSeriesValues(); $plotLabel[$keys[0]] = new DataSeriesValues();
} }
$this->plotLabel = $plotLabel; $this->plotLabel = $plotLabel;
if ((count($plotCategory) == 0) || ($plotCategory[$keys[0]] === null)) { if (!isset($plotCategory[$keys[0]])) {
$plotCategory[$keys[0]] = new DataSeriesValues(); $plotCategory[$keys[0]] = new DataSeriesValues();
} }
$this->plotCategory = $plotCategory; $this->plotCategory = $plotCategory;

View File

@ -18,7 +18,7 @@ class Legend
const POSITION_TOP = 't'; const POSITION_TOP = 't';
const POSITION_TOPRIGHT = 'tr'; const POSITION_TOPRIGHT = 'tr';
private static $positionXLref = [ const POSITION_XLREF = [
self::XL_LEGEND_POSITION_BOTTOM => self::POSITION_BOTTOM, self::XL_LEGEND_POSITION_BOTTOM => self::POSITION_BOTTOM,
self::XL_LEGEND_POSITION_CORNER => self::POSITION_TOPRIGHT, self::XL_LEGEND_POSITION_CORNER => self::POSITION_TOPRIGHT,
self::XL_LEGEND_POSITION_CUSTOM => '??', self::XL_LEGEND_POSITION_CUSTOM => '??',
@ -44,7 +44,7 @@ class Legend
/** /**
* Legend Layout. * Legend Layout.
* *
* @var Layout * @var ?Layout
*/ */
private $layout; private $layout;
@ -80,7 +80,7 @@ class Legend
*/ */
public function setPosition($position) public function setPosition($position)
{ {
if (!in_array($position, self::$positionXLref)) { if (!in_array($position, self::POSITION_XLREF)) {
return false; return false;
} }
@ -92,11 +92,11 @@ class Legend
/** /**
* Get legend position as an Excel internal numeric value. * Get legend position as an Excel internal numeric value.
* *
* @return int * @return false|int
*/ */
public function getPositionXL() public function getPositionXL()
{ {
return array_search($this->position, self::$positionXLref); return array_search($this->position, self::POSITION_XLREF);
} }
/** /**
@ -108,11 +108,11 @@ class Legend
*/ */
public function setPositionXL($positionXL) public function setPositionXL($positionXL)
{ {
if (!isset(self::$positionXLref[$positionXL])) { if (!isset(self::POSITION_XLREF[$positionXL])) {
return false; return false;
} }
$this->position = self::$positionXLref[$positionXL]; $this->position = self::POSITION_XLREF[$positionXL];
return true; return true;
} }
@ -140,7 +140,7 @@ class Legend
/** /**
* Get Layout. * Get Layout.
* *
* @return Layout * @return ?Layout
*/ */
public function getLayout() public function getLayout()
{ {

View File

@ -9,7 +9,7 @@ class PlotArea
/** /**
* PlotArea Layout. * PlotArea Layout.
* *
* @var Layout * @var ?Layout
*/ */
private $layout; private $layout;
@ -31,12 +31,7 @@ class PlotArea
$this->plotSeries = $plotSeries; $this->plotSeries = $plotSeries;
} }
/** public function getLayout(): ?Layout
* Get Layout.
*
* @return Layout
*/
public function getLayout()
{ {
return $this->layout; return $this->layout;
} }

View File

@ -164,7 +164,7 @@ abstract class Properties
* *
* @return $this * @return $this
*/ */
protected function activateObject() public function activateObject()
{ {
$this->objectState = true; $this->objectState = true;
@ -782,9 +782,9 @@ abstract class Properties
* *
* @param string $value * @param string $value
* @param ?int $alpha * @param ?int $alpha
* @param string $colorType * @param ?string $colorType
*/ */
public function setLineColorProperties($value, $alpha = null, $colorType = ChartColor::EXCEL_COLOR_TYPE_STANDARD): void public function setLineColorProperties($value, $alpha = null, $colorType = null): void
{ {
$this->activateObject(); $this->activateObject();
$this->lineColor->setColorPropertiesArray( $this->lineColor->setColorPropertiesArray(
@ -873,6 +873,42 @@ abstract class Properties
} }
} }
public function getLineStyleArray(): array
{
return $this->lineStyleProperties;
}
public function setLineStyleArray(array $lineStyleProperties = []): self
{
$this->activateObject();
$this->lineStyleProperties['width'] = $lineStyleProperties['width'] ?? null;
$this->lineStyleProperties['compound'] = $lineStyleProperties['compound'] ?? '';
$this->lineStyleProperties['dash'] = $lineStyleProperties['dash'] ?? '';
$this->lineStyleProperties['cap'] = $lineStyleProperties['cap'] ?? '';
$this->lineStyleProperties['join'] = $lineStyleProperties['join'] ?? '';
$this->lineStyleProperties['arrow']['head']['type'] = $lineStyleProperties['arrow']['head']['type'] ?? '';
$this->lineStyleProperties['arrow']['head']['size'] = $lineStyleProperties['arrow']['head']['size'] ?? '';
$this->lineStyleProperties['arrow']['head']['w'] = $lineStyleProperties['arrow']['head']['w'] ?? '';
$this->lineStyleProperties['arrow']['head']['len'] = $lineStyleProperties['arrow']['head']['len'] ?? '';
$this->lineStyleProperties['arrow']['end']['type'] = $lineStyleProperties['arrow']['end']['type'] ?? '';
$this->lineStyleProperties['arrow']['end']['size'] = $lineStyleProperties['arrow']['end']['size'] ?? '';
$this->lineStyleProperties['arrow']['end']['w'] = $lineStyleProperties['arrow']['end']['w'] ?? '';
$this->lineStyleProperties['arrow']['end']['len'] = $lineStyleProperties['arrow']['end']['len'] ?? '';
return $this;
}
/**
* @param mixed $value
*/
public function setLineStyleProperty(string $propertyName, $value): self
{
$this->activateObject();
$this->lineStyleProperties[$propertyName] = $value;
return $this;
}
/** /**
* Get Line Style Property. * Get Line Style Property.
* *

View File

@ -16,7 +16,7 @@ class Title
/** /**
* Title Layout. * Title Layout.
* *
* @var Layout * @var ?Layout
*/ */
private $layout; private $layout;
@ -78,12 +78,7 @@ class Title
return $this; return $this;
} }
/** public function getLayout(): ?Layout
* Get Layout.
*
* @return Layout
*/
public function getLayout()
{ {
return $this->layout; return $this->layout;
} }

View File

@ -72,7 +72,6 @@ class Chart
$rotX = $rotY = $rAngAx = $perspective = null; $rotX = $rotY = $rAngAx = $perspective = null;
$xAxis = new Axis(); $xAxis = new Axis();
$yAxis = new Axis(); $yAxis = new Axis();
$majorGridlines = $minorGridlines = null;
foreach ($chartElementsC as $chartElementKey => $chartElement) { foreach ($chartElementsC as $chartElementKey => $chartElement) {
switch ($chartElementKey) { switch ($chartElementKey) {
case 'chart': case 'chart':
@ -110,6 +109,23 @@ class Chart
$xAxis->setFillParameters($axisColorArray['value'], $axisColorArray['alpha'], $axisColorArray['type']); $xAxis->setFillParameters($axisColorArray['value'], $axisColorArray['alpha'], $axisColorArray['type']);
} }
} }
if (isset($chartDetail->majorGridlines)) {
$majorGridlines = new GridLines();
if (isset($chartDetail->majorGridlines->spPr)) {
$this->readEffects($chartDetail->majorGridlines, $majorGridlines);
$this->readLineStyle($chartDetail->majorGridlines, $majorGridlines);
}
$xAxis->setMajorGridlines($majorGridlines);
}
if (isset($chartDetail->minorGridlines)) {
$minorGridlines = new GridLines();
$minorGridlines->activateObject();
if (isset($chartDetail->minorGridlines->spPr)) {
$this->readEffects($chartDetail->minorGridlines, $minorGridlines);
$this->readLineStyle($chartDetail->minorGridlines, $minorGridlines);
}
$xAxis->setMinorGridlines($minorGridlines);
}
$this->setAxisProperties($chartDetail, $xAxis); $this->setAxisProperties($chartDetail, $xAxis);
break; break;
@ -168,19 +184,22 @@ class Chart
$whichAxis->setFillParameters($axisColorArray['value'], $axisColorArray['alpha'], $axisColorArray['type']); $whichAxis->setFillParameters($axisColorArray['value'], $axisColorArray['alpha'], $axisColorArray['type']);
} }
} }
if (isset($chartDetail->majorGridlines)) { if ($whichAxis !== null && isset($chartDetail->majorGridlines)) {
$majorGridlines = new GridLines(); $majorGridlines = new GridLines();
if (isset($chartDetail->majorGridlines->spPr)) { if (isset($chartDetail->majorGridlines->spPr)) {
$this->readEffects($chartDetail->majorGridlines, $majorGridlines); $this->readEffects($chartDetail->majorGridlines, $majorGridlines);
$this->readLineStyle($chartDetail->majorGridlines, $majorGridlines); $this->readLineStyle($chartDetail->majorGridlines, $majorGridlines);
} }
$whichAxis->setMajorGridlines($majorGridlines);
} }
if (isset($chartDetail->minorGridlines)) { if ($whichAxis !== null && isset($chartDetail->minorGridlines)) {
$minorGridlines = new GridLines(); $minorGridlines = new GridLines();
$minorGridlines->activateObject();
if (isset($chartDetail->minorGridlines->spPr)) { if (isset($chartDetail->minorGridlines->spPr)) {
$this->readEffects($chartDetail->minorGridlines, $minorGridlines); $this->readEffects($chartDetail->minorGridlines, $minorGridlines);
$this->readLineStyle($chartDetail->minorGridlines, $minorGridlines); $this->readLineStyle($chartDetail->minorGridlines, $minorGridlines);
} }
$whichAxis->setMinorGridlines($minorGridlines);
} }
$this->setAxisProperties($chartDetail, $whichAxis); $this->setAxisProperties($chartDetail, $whichAxis);
@ -304,7 +323,7 @@ class Chart
} }
} }
} }
$chart = new \PhpOffice\PhpSpreadsheet\Chart\Chart($chartName, $title, $legend, $plotArea, $plotVisOnly, (string) $dispBlanksAs, $XaxisLabel, $YaxisLabel, $xAxis, $yAxis, $majorGridlines, $minorGridlines); $chart = new \PhpOffice\PhpSpreadsheet\Chart\Chart($chartName, $title, $legend, $plotArea, $plotVisOnly, (string) $dispBlanksAs, $XaxisLabel, $YaxisLabel, $xAxis, $yAxis);
if (is_int($rotX)) { if (is_int($rotX)) {
$chart->setRotX($rotX); $chart->setRotX($rotX);
} }

View File

@ -6,7 +6,6 @@ use PhpOffice\PhpSpreadsheet\Chart\Axis;
use PhpOffice\PhpSpreadsheet\Chart\ChartColor; use PhpOffice\PhpSpreadsheet\Chart\ChartColor;
use PhpOffice\PhpSpreadsheet\Chart\DataSeries; use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues; use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
use PhpOffice\PhpSpreadsheet\Chart\GridLines;
use PhpOffice\PhpSpreadsheet\Chart\Layout; use PhpOffice\PhpSpreadsheet\Chart\Layout;
use PhpOffice\PhpSpreadsheet\Chart\Legend; use PhpOffice\PhpSpreadsheet\Chart\Legend;
use PhpOffice\PhpSpreadsheet\Chart\PlotArea; use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
@ -99,7 +98,7 @@ class Chart extends WriterPart
} }
$objWriter->endElement(); // view3D $objWriter->endElement(); // view3D
$this->writePlotArea($objWriter, $chart->getPlotArea(), $chart->getXAxisLabel(), $chart->getYAxisLabel(), $chart->getChartAxisX(), $chart->getChartAxisY(), $chart->getMajorGridlines(), $chart->getMinorGridlines()); $this->writePlotArea($objWriter, $chart->getPlotArea(), $chart->getXAxisLabel(), $chart->getYAxisLabel(), $chart->getChartAxisX(), $chart->getChartAxisY());
$this->writeLegend($objWriter, $chart->getLegend()); $this->writeLegend($objWriter, $chart->getLegend());
@ -218,11 +217,13 @@ class Chart extends WriterPart
/** /**
* Write Chart Plot Area. * Write Chart Plot Area.
*/ */
private function writePlotArea(XMLWriter $objWriter, ?PlotArea $plotArea, ?Title $xAxisLabel = null, ?Title $yAxisLabel = null, ?Axis $xAxis = null, ?Axis $yAxis = null, ?GridLines $majorGridlines = null, ?GridLines $minorGridlines = null): void private function writePlotArea(XMLWriter $objWriter, ?PlotArea $plotArea, ?Title $xAxisLabel = null, ?Title $yAxisLabel = null, ?Axis $xAxis = null, ?Axis $yAxis = null): void
{ {
if ($plotArea === null) { if ($plotArea === null) {
return; return;
} }
$majorGridlines = ($yAxis === null) ? null : $yAxis->getMajorGridlines();
$minorGridlines = ($yAxis === null) ? null : $yAxis->getMinorGridlines();
$id1 = $id2 = $id3 = '0'; $id1 = $id2 = $id3 = '0';
$this->seriesIndex = 0; $this->seriesIndex = 0;
@ -345,12 +346,12 @@ class Chart extends WriterPart
if (($chartType !== DataSeries::TYPE_PIECHART) && ($chartType !== DataSeries::TYPE_PIECHART_3D) && ($chartType !== DataSeries::TYPE_DONUTCHART)) { if (($chartType !== DataSeries::TYPE_PIECHART) && ($chartType !== DataSeries::TYPE_PIECHART_3D) && ($chartType !== DataSeries::TYPE_DONUTCHART)) {
if ($chartType === DataSeries::TYPE_BUBBLECHART) { if ($chartType === DataSeries::TYPE_BUBBLECHART) {
$this->writeValueAxis($objWriter, $xAxisLabel, $chartType, $id2, $id1, $catIsMultiLevelSeries, $xAxis ?? new Axis(), $majorGridlines, $minorGridlines); $this->writeValueAxis($objWriter, $xAxisLabel, $chartType, $id2, $id1, $catIsMultiLevelSeries, $xAxis ?? new Axis());
} else { } else {
$this->writeCategoryAxis($objWriter, $xAxisLabel, $id1, $id2, $catIsMultiLevelSeries, $xAxis ?? new Axis()); $this->writeCategoryAxis($objWriter, $xAxisLabel, $id1, $id2, $catIsMultiLevelSeries, $xAxis ?? new Axis());
} }
$this->writeValueAxis($objWriter, $yAxisLabel, $chartType, $id1, $id2, $valIsMultiLevelSeries, $yAxis ?? new Axis(), $majorGridlines, $minorGridlines); $this->writeValueAxis($objWriter, $yAxisLabel, $chartType, $id1, $id2, $valIsMultiLevelSeries, $yAxis ?? new Axis());
if ($chartType === DataSeries::TYPE_SURFACECHART_3D || $chartType === DataSeries::TYPE_SURFACECHART) { if ($chartType === DataSeries::TYPE_SURFACECHART_3D || $chartType === DataSeries::TYPE_SURFACECHART) {
$this->writeSerAxis($objWriter, $id2, $id3); $this->writeSerAxis($objWriter, $id2, $id3);
} }
@ -448,6 +449,8 @@ class Chart extends WriterPart
} else { } else {
$objWriter->startElement('c:catAx'); $objWriter->startElement('c:catAx');
} }
$majorGridlines = $yAxis->getMajorGridlines();
$minorGridlines = $yAxis->getMinorGridlines();
if ($id1 !== '0') { if ($id1 !== '0') {
$objWriter->startElement('c:axId'); $objWriter->startElement('c:axId');
@ -481,6 +484,24 @@ class Chart extends WriterPart
$objWriter->writeAttribute('val', 'b'); $objWriter->writeAttribute('val', 'b');
$objWriter->endElement(); $objWriter->endElement();
if ($majorGridlines !== null) {
$objWriter->startElement('c:majorGridlines');
$objWriter->startElement('c:spPr');
$this->writeLineStyles($objWriter, $majorGridlines);
$this->writeEffects($objWriter, $majorGridlines);
$objWriter->endElement(); //end spPr
$objWriter->endElement(); //end majorGridLines
}
if ($minorGridlines !== null && $minorGridlines->getObjectState()) {
$objWriter->startElement('c:minorGridlines');
$objWriter->startElement('c:spPr');
$this->writeLineStyles($objWriter, $minorGridlines);
$this->writeEffects($objWriter, $minorGridlines);
$objWriter->endElement(); //end spPr
$objWriter->endElement(); //end minorGridLines
}
if ($xAxisLabel !== null) { if ($xAxisLabel !== null) {
$objWriter->startElement('c:title'); $objWriter->startElement('c:title');
$objWriter->startElement('c:tx'); $objWriter->startElement('c:tx');
@ -594,9 +615,11 @@ class Chart extends WriterPart
* @param string $id2 * @param string $id2
* @param bool $isMultiLevelSeries * @param bool $isMultiLevelSeries
*/ */
private function writeValueAxis(XMLWriter $objWriter, ?Title $yAxisLabel, $groupType, $id1, $id2, $isMultiLevelSeries, Axis $xAxis, ?GridLines $majorGridlines, ?GridLines $minorGridlines): void private function writeValueAxis(XMLWriter $objWriter, ?Title $yAxisLabel, $groupType, $id1, $id2, $isMultiLevelSeries, Axis $xAxis): void
{ {
$objWriter->startElement('c:valAx'); $objWriter->startElement('c:valAx');
$majorGridlines = $xAxis->getMajorGridlines();
$minorGridlines = $xAxis->getMinorGridlines();
if ($id2 !== '0') { if ($id2 !== '0') {
$objWriter->startElement('c:axId'); $objWriter->startElement('c:axId');

View File

@ -140,6 +140,7 @@ class BarChartCustomColorsTest extends AbstractFunctional
$chart2 = $charts2[0]; $chart2 = $charts2[0];
self::assertNotNull($chart2); self::assertNotNull($chart2);
$plotArea2 = $chart2->getPlotArea(); $plotArea2 = $chart2->getPlotArea();
self::assertNotNull($plotArea2);
$dataSeries2 = $plotArea2->getPlotGroup(); $dataSeries2 = $plotArea2->getPlotGroup();
self::assertCount(1, $dataSeries2); self::assertCount(1, $dataSeries2);
$plotValues = $dataSeries2[0]->getPlotValues(); $plotValues = $dataSeries2[0]->getPlotValues();

View File

@ -0,0 +1,142 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Chart;
use PhpOffice\PhpSpreadsheet\Chart\Axis;
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\Spreadsheet;
use PHPUnit\Framework\TestCase;
class ChartMethodTest extends TestCase
{
public function testMethodVsConstructor(): void
{
$spreadsheet = new Spreadsheet();
$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_LINECHART, // 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]);
$title = new Title('Method vs Constructor test');
$legend = new ChartLegend(ChartLegend::POSITION_TOPRIGHT, null, false);
$xAxis = new Axis();
$yAxis = new Axis();
$xAxisLabel = new Title('X-Axis label');
$yAxisLabel = new Title('Y-Axis label');
$chart1 = new Chart(
'chart1', // name
$title, // title
$legend, // legend
$plotArea, // plotArea
true, // plotVisibleOnly
DataSeries::EMPTY_AS_GAP, // displayBlanksAs
$xAxisLabel, // xAxisLabel
$yAxisLabel, // yAxisLabel
$xAxis, // xAxis
$yAxis // yAxis
);
$chart2 = new Chart('chart1');
$chart2
->setLegend($legend)
->setPlotArea($plotArea)
->setPlotVisibleOnly(true)
->setDisplayBlanksAs(DataSeries::EMPTY_AS_GAP)
->setChartAxisX($xAxis)
->setChartAxisY($yAxis)
->setXAxisLabel($xAxisLabel)
->setYAxisLabel($yAxisLabel)
->setTitle($title);
self::assertEquals($chart1, $chart2);
$spreadsheet->disconnectWorksheets();
}
public function testPositions(): void
{
$chart = new Chart('chart1');
$chart->setTopLeftPosition('B3', 2, 4);
self::assertSame('B3', $chart->getTopLeftCell());
self::assertEquals(['X' => 2, 'Y' => 4], $chart->getTopLeftOffset());
self::assertEquals(2, $chart->getTopLeftXOffset());
self::assertEquals(4, $chart->getTopLeftYOffset());
$chart->setTopLeftCell('B5');
self::assertSame('B5', $chart->getTopLeftCell());
self::assertEquals(2, $chart->getTopLeftXOffset());
self::assertEquals(4, $chart->getTopLeftYOffset());
$chart->setTopLeftOffset(6, 8);
self::assertSame('B5', $chart->getTopLeftCell());
self::assertEquals(6, $chart->getTopLeftXOffset());
self::assertEquals(8, $chart->getTopLeftYOffset());
$chart->setbottomRightPosition('H9', 3, 5);
self::assertSame('H9', $chart->getBottomRightCell());
self::assertEquals(['X' => 3, 'Y' => 5], $chart->getBottomRightOffset());
self::assertEquals(3, $chart->getBottomRightXOffset());
self::assertEquals(5, $chart->getBottomRightYOffset());
$chart->setbottomRightCell('H11');
self::assertSame('H11', $chart->getBottomRightCell());
self::assertEquals(3, $chart->getBottomRightXOffset());
self::assertEquals(5, $chart->getBottomRightYOffset());
$chart->setbottomRightOffset(7, 9);
self::assertSame('H11', $chart->getBottomRightCell());
self::assertEquals(7, $chart->getBottomRightXOffset());
self::assertEquals(9, $chart->getBottomRightYOffset());
}
}

View File

@ -46,6 +46,7 @@ class Charts32ColoredAxisLabelTest extends AbstractFunctional
self::assertNotNull($chart); self::assertNotNull($chart);
$xAxisLabel = $chart->getXAxisLabel(); $xAxisLabel = $chart->getXAxisLabel();
self::assertNotNull($xAxisLabel);
$captionArray = $xAxisLabel->getCaption(); $captionArray = $xAxisLabel->getCaption();
self::assertIsArray($captionArray); self::assertIsArray($captionArray);
self::assertCount(1, $captionArray); self::assertCount(1, $captionArray);
@ -64,6 +65,7 @@ class Charts32ColoredAxisLabelTest extends AbstractFunctional
self::assertSame('srgbClr', $chartColor->getType()); self::assertSame('srgbClr', $chartColor->getType());
$yAxisLabel = $chart->getYAxisLabel(); $yAxisLabel = $chart->getYAxisLabel();
self::assertNotNull($yAxisLabel);
$captionArray = $yAxisLabel->getCaption(); $captionArray = $yAxisLabel->getCaption();
self::assertIsArray($captionArray); self::assertIsArray($captionArray);
self::assertCount(1, $captionArray); self::assertCount(1, $captionArray);

View File

@ -42,6 +42,7 @@ class Charts32DsvGlowTest extends AbstractFunctional
self::assertNotNull($chart); self::assertNotNull($chart);
$plotArea = $chart->getPlotArea(); $plotArea = $chart->getPlotArea();
self::assertNotNull($plotArea);
$dataSeriesArray = $plotArea->getPlotGroup(); $dataSeriesArray = $plotArea->getPlotGroup();
self::assertCount(1, $dataSeriesArray); self::assertCount(1, $dataSeriesArray);
$dataSeries = $dataSeriesArray[0]; $dataSeries = $dataSeriesArray[0];
@ -53,6 +54,24 @@ class Charts32DsvGlowTest extends AbstractFunctional
self::assertSame('accent2', $dataSeriesValues->getGlowProperty(['color', 'value'])); self::assertSame('accent2', $dataSeriesValues->getGlowProperty(['color', 'value']));
self::assertSame(60, $dataSeriesValues->getGlowProperty(['color', 'alpha'])); self::assertSame(60, $dataSeriesValues->getGlowProperty(['color', 'alpha']));
$yAxis = $chart->getChartAxisY();
$majorGridlines = $yAxis->getMajorGridlines();
self::assertNotNull($majorGridlines);
self::assertSame('triangle', $majorGridlines->getLineStyleProperty(['arrow', 'head', 'type']));
self::assertSame('triangle', $majorGridlines->getLineStyleProperty(['arrow', 'end', 'type']));
$minorGridlines = $yAxis->getMinorGridlines();
self::assertNotNull($minorGridlines);
self::assertSame('sysDot', $minorGridlines->getLineStyleProperty('dash'));
self::assertSame('FFC000', $minorGridlines->getLineColor()->getValue());
$xAxis = $chart->getChartAxisX();
$majorGridlines = $xAxis->getMajorGridlines();
$minorGridlines = $xAxis->getMinorGridlines();
self::assertNotNull($majorGridlines);
self::assertSame('7030A0', $majorGridlines->getLineColor()->getValue());
self::assertNotNull($minorGridlines);
self::assertFalse($minorGridlines->getLineColor()->isUsable());
$reloadedSpreadsheet->disconnectWorksheets(); $reloadedSpreadsheet->disconnectWorksheets();
} }
} }

View File

@ -42,6 +42,7 @@ class Charts32DsvLabelsTest extends AbstractFunctional
self::assertNotNull($chart); self::assertNotNull($chart);
$plotArea = $chart->getPlotArea(); $plotArea = $chart->getPlotArea();
self::assertNotNull($plotArea);
$dataSeriesArray = $plotArea->getPlotGroup(); $dataSeriesArray = $plotArea->getPlotGroup();
self::assertCount(1, $dataSeriesArray); self::assertCount(1, $dataSeriesArray);
$dataSeries = $dataSeriesArray[0]; $dataSeries = $dataSeriesArray[0];

View File

@ -46,6 +46,7 @@ class Charts32ScatterTest extends AbstractFunctional
$chart = $charts[0]; $chart = $charts[0];
self::assertNotNull($chart); self::assertNotNull($chart);
$title = $chart->getTitle(); $title = $chart->getTitle();
self::assertNotNull($title);
$captionArray = $title->getCaption(); $captionArray = $title->getCaption();
self::assertIsArray($captionArray); self::assertIsArray($captionArray);
self::assertCount(1, $captionArray); self::assertCount(1, $captionArray);
@ -72,6 +73,7 @@ class Charts32ScatterTest extends AbstractFunctional
self::assertSame('srgbClr', $chartColor->getType()); self::assertSame('srgbClr', $chartColor->getType());
$plotArea = $chart->getPlotArea(); $plotArea = $chart->getPlotArea();
self::assertNotNull($plotArea);
$plotSeries = $plotArea->getPlotGroup(); $plotSeries = $plotArea->getPlotGroup();
self::assertCount(1, $plotSeries); self::assertCount(1, $plotSeries);
$dataSeries = $plotSeries[0]; $dataSeries = $plotSeries[0];
@ -121,6 +123,7 @@ class Charts32ScatterTest extends AbstractFunctional
$chart = $charts[0]; $chart = $charts[0];
self::assertNotNull($chart); self::assertNotNull($chart);
$title = $chart->getTitle(); $title = $chart->getTitle();
self::assertNotNull($title);
$captionArray = $title->getCaption(); $captionArray = $title->getCaption();
self::assertIsArray($captionArray); self::assertIsArray($captionArray);
self::assertCount(1, $captionArray); self::assertCount(1, $captionArray);
@ -182,6 +185,7 @@ class Charts32ScatterTest extends AbstractFunctional
self::assertSame('srgbClr', $chartColor->getType()); self::assertSame('srgbClr', $chartColor->getType());
$plotArea = $chart->getPlotArea(); $plotArea = $chart->getPlotArea();
self::assertNotNull($plotArea);
$plotSeries = $plotArea->getPlotGroup(); $plotSeries = $plotArea->getPlotGroup();
self::assertCount(1, $plotSeries); self::assertCount(1, $plotSeries);
$dataSeries = $plotSeries[0]; $dataSeries = $plotSeries[0];
@ -231,6 +235,7 @@ class Charts32ScatterTest extends AbstractFunctional
$chart = $charts[0]; $chart = $charts[0];
self::assertNotNull($chart); self::assertNotNull($chart);
$title = $chart->getTitle(); $title = $chart->getTitle();
self::assertNotNull($title);
$captionArray = $title->getCaption(); $captionArray = $title->getCaption();
self::assertIsArray($captionArray); self::assertIsArray($captionArray);
self::assertCount(1, $captionArray); self::assertCount(1, $captionArray);
@ -257,6 +262,7 @@ class Charts32ScatterTest extends AbstractFunctional
self::assertSame('srgbClr', $chartColor->getType()); self::assertSame('srgbClr', $chartColor->getType());
$plotArea = $chart->getPlotArea(); $plotArea = $chart->getPlotArea();
self::assertNotNull($plotArea);
$plotSeries = $plotArea->getPlotGroup(); $plotSeries = $plotArea->getPlotGroup();
self::assertCount(1, $plotSeries); self::assertCount(1, $plotSeries);
$dataSeries = $plotSeries[0]; $dataSeries = $plotSeries[0];
@ -305,6 +311,7 @@ class Charts32ScatterTest extends AbstractFunctional
$chart = $charts[0]; $chart = $charts[0];
self::assertNotNull($chart); self::assertNotNull($chart);
$title = $chart->getTitle(); $title = $chart->getTitle();
self::assertNotNull($title);
$captionArray = $title->getCaption(); $captionArray = $title->getCaption();
self::assertIsArray($captionArray); self::assertIsArray($captionArray);
self::assertCount(1, $captionArray); self::assertCount(1, $captionArray);
@ -334,6 +341,7 @@ class Charts32ScatterTest extends AbstractFunctional
} }
$plotArea = $chart->getPlotArea(); $plotArea = $chart->getPlotArea();
self::assertNotNull($plotArea);
$plotSeries = $plotArea->getPlotGroup(); $plotSeries = $plotArea->getPlotGroup();
self::assertCount(1, $plotSeries); self::assertCount(1, $plotSeries);
$dataSeries = $plotSeries[0]; $dataSeries = $plotSeries[0];
@ -384,6 +392,7 @@ class Charts32ScatterTest extends AbstractFunctional
self::assertNotNull($chart); self::assertNotNull($chart);
$plotArea = $chart->getPlotArea(); $plotArea = $chart->getPlotArea();
self::assertNotNull($plotArea);
$plotSeries = $plotArea->getPlotGroup(); $plotSeries = $plotArea->getPlotGroup();
self::assertCount(1, $plotSeries); self::assertCount(1, $plotSeries);
$dataSeries = $plotSeries[0]; $dataSeries = $plotSeries[0];

View File

@ -27,6 +27,7 @@ class ChartsOpenpyxlTest extends TestCase
self::assertTrue($chart->getOneCellAnchor()); self::assertTrue($chart->getOneCellAnchor());
$plotArea = $chart->getPlotArea(); $plotArea = $chart->getPlotArea();
self::assertNotNull($plotArea);
$plotSeries = $plotArea->getPlotGroup(); $plotSeries = $plotArea->getPlotGroup();
self::assertCount(1, $plotSeries); self::assertCount(1, $plotSeries);
$dataSeries = $plotSeries[0]; $dataSeries = $plotSeries[0];

View File

@ -120,8 +120,10 @@ class DataSeriesValues2Test extends AbstractFunctional
// Add the chart to the worksheet // Add the chart to the worksheet
$worksheet->addChart($chart); $worksheet->addChart($chart);
self::assertSame(1, $chart->getPlotArea()->getPlotGroupCount()); $plotArea = $chart->getPlotArea();
$plotValues = $chart->getPlotArea()->getPlotGroup()[0]->getPlotValues(); self::assertNotNull($plotArea);
self::assertSame(1, $plotArea->getPlotGroupCount());
$plotValues = $plotArea->getPlotGroup()[0]->getPlotValues();
self::assertCount(3, $plotValues); self::assertCount(3, $plotValues);
self::assertSame([], $plotValues[1]->getDataValues()); self::assertSame([], $plotValues[1]->getDataValues());
self::assertNull($plotValues[1]->getDataValue()); self::assertNull($plotValues[1]->getDataValue());
@ -138,20 +140,27 @@ class DataSeriesValues2Test extends AbstractFunctional
self::assertCount(1, $charts2); self::assertCount(1, $charts2);
$chart2 = $charts2[0]; $chart2 = $charts2[0];
self::assertNotNull($chart2); self::assertNotNull($chart2);
$plotValues2 = $chart2->getPlotArea()->getPlotGroup()[0]->getPlotValues(); $plotArea2 = $chart2->getPlotArea();
self::assertNotNull($plotArea2);
$plotGroup2 = $plotArea2->getPlotGroup()[0];
self::assertNotNull($plotGroup2);
$plotValues2 = $plotGroup2->getPlotValues();
self::assertCount(3, $plotValues2); self::assertCount(3, $plotValues2);
self::assertSame([15.0, 73.0, 61.0, 32.0], $plotValues2[1]->getDataValues()); self::assertSame([15.0, 73.0, 61.0, 32.0], $plotValues2[1]->getDataValues());
self::assertSame([15.0, 73.0, 61.0, 32.0], $plotValues2[1]->getDataValue()); self::assertSame([15.0, 73.0, 61.0, 32.0], $plotValues2[1]->getDataValue());
$labels2 = $chart->getPlotArea()->getPlotGroup()[0]->getPlotLabels(); $labels2 = $plotGroup2->getPlotLabels();
self::assertCount(3, $labels2); self::assertCount(3, $labels2);
self::assertSame(2010, $labels2[0]->getDataValue()); self::assertEquals(2010, $labels2[0]->getDataValue());
$dataSeries = $chart->getPlotArea()->getPlotGroup()[0]; $dataSeries = $plotArea2->getPlotGroup()[0];
self::assertFalse($dataSeries->getPlotValuesByIndex(99)); self::assertFalse($dataSeries->getPlotValuesByIndex(99));
self::assertNotFalse($dataSeries->getPlotValuesByIndex(0)); self::assertNotFalse($dataSeries->getPlotValuesByIndex(0));
self::assertSame([12, 56, 52, 30], $dataSeries->getPlotValuesByIndex(0)->getDataValues()); self::assertEquals([12, 56, 52, 30], $dataSeries->getPlotValuesByIndex(0)->getDataValues());
self::assertSame(DataSeries::TYPE_AREACHART, $dataSeries->getPlotType()); self::assertSame(DataSeries::TYPE_AREACHART, $dataSeries->getPlotType());
self::assertSame(DataSeries::GROUPING_PERCENT_STACKED, $dataSeries->getPlotGrouping()); self::assertSame(DataSeries::GROUPING_PERCENT_STACKED, $dataSeries->getPlotGrouping());
self::assertTrue($dataSeries->getSmoothLine()); // SmoothLine written out for DataSeries only for LineChart.
// Original test was wrong - used $chart rather than $chart2
// to retrieve data which was read in.
//self::assertTrue($dataSeries->getSmoothLine());
$reloadedSpreadsheet->disconnectWorksheets(); $reloadedSpreadsheet->disconnectWorksheets();
} }

View File

@ -2,6 +2,7 @@
namespace PhpOffice\PhpSpreadsheetTests\Chart; namespace PhpOffice\PhpSpreadsheetTests\Chart;
use PhpOffice\PhpSpreadsheet\Chart\Axis;
use PhpOffice\PhpSpreadsheet\Chart\Chart; use PhpOffice\PhpSpreadsheet\Chart\Chart;
use PhpOffice\PhpSpreadsheet\Chart\DataSeries; use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues; use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
@ -138,6 +139,202 @@ class GridlinesLineStyleTest extends AbstractFunctional
self::assertSame(30, $minorGridlines->getLineColorProperty('alpha')); self::assertSame(30, $minorGridlines->getLineColorProperty('alpha'));
self::assertSame('srgbClr', $minorGridlines->getLineColorProperty('type')); self::assertSame('srgbClr', $minorGridlines->getLineColorProperty('type'));
// Create the chart
$yAxis = new Axis();
$yAxis->setMajorGridlines($majorGridlines);
$yAxis->setMinorGridlines($minorGridlines);
$chart = new Chart(
'chart1', // name
$title, // title
$legend, // legend
$plotArea, // plotArea
true, // plotVisibleOnly
DataSeries::EMPTY_AS_GAP, // displayBlanksAs
null, // xAxisLabel
$yAxisLabel, // yAxisLabel
null, // xAxis
$yAxis // yAxis
);
$yAxis2 = $chart->getChartAxisY();
$majorGridlines2 = $yAxis2->getMajorGridlines();
self::assertNotNull($majorGridlines2);
self::assertEquals($width, $majorGridlines2->getLineStyleProperty('width'));
self::assertEquals($compound, $majorGridlines2->getLineStyleProperty('compound'));
self::assertEquals($dash, $majorGridlines2->getLineStyleProperty('dash'));
self::assertEquals($cap, $majorGridlines2->getLineStyleProperty('cap'));
self::assertEquals($join, $majorGridlines2->getLineStyleProperty('join'));
self::assertEquals($headArrowType, $majorGridlines2->getLineStyleProperty(['arrow', 'head', 'type']));
self::assertEquals($headArrowSize, $majorGridlines2->getLineStyleProperty(['arrow', 'head', 'size']));
self::assertEquals($endArrowType, $majorGridlines2->getLineStyleProperty(['arrow', 'end', 'type']));
self::assertEquals($endArrowSize, $majorGridlines2->getLineStyleProperty(['arrow', 'end', 'size']));
self::assertEquals('sm', $majorGridlines2->getLineStyleProperty(['arrow', 'head', 'w']));
self::assertEquals('med', $majorGridlines2->getLineStyleProperty(['arrow', 'head', 'len']));
self::assertEquals('sm', $majorGridlines2->getLineStyleProperty(['arrow', 'end', 'w']));
self::assertEquals('lg', $majorGridlines2->getLineStyleProperty(['arrow', 'end', 'len']));
$minorGridlines2 = $yAxis2->getMinorGridlines();
self::assertNotNull($minorGridlines2);
self::assertSame('00FF00', $minorGridlines2->getLineColorProperty('value'));
self::assertSame(30, $minorGridlines2->getLineColorProperty('alpha'));
self::assertSame('srgbClr', $minorGridlines2->getLineColorProperty('type'));
// 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);
/** @var callable */
$callableReader = [$this, 'readCharts'];
/** @var callable */
$callableWriter = [$this, 'writeCharts'];
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx', $callableReader, $callableWriter);
$spreadsheet->disconnectWorksheets();
$sheet = $reloadedSpreadsheet->getActiveSheet();
$charts2 = $sheet->getChartCollection();
self::assertCount(1, $charts2);
$chart2 = $charts2[0];
self::assertNotNull($chart2);
self::assertSame('A7', $chart2->getTopLeftCell());
self::assertSame('H20', $chart2->getBottomRightCell());
self::assertSame($sheet, $chart2->getWorksheet());
$yAxis3 = $chart2->getChartAxisY();
$majorGridlines3 = $yAxis3->getMajorGridlines();
self::assertNotNull($majorGridlines3);
self::assertEquals($width, $majorGridlines3->getLineStyleProperty('width'));
self::assertEquals($compound, $majorGridlines3->getLineStyleProperty('compound'));
self::assertEquals($dash, $majorGridlines3->getLineStyleProperty('dash'));
self::assertEquals($cap, $majorGridlines3->getLineStyleProperty('cap'));
self::assertEquals($join, $majorGridlines3->getLineStyleProperty('join'));
self::assertEquals($headArrowType, $majorGridlines3->getLineStyleProperty(['arrow', 'head', 'type']));
self::assertEquals($endArrowType, $majorGridlines3->getLineStyleProperty(['arrow', 'end', 'type']));
self::assertEquals('sm', $majorGridlines3->getLineStyleProperty(['arrow', 'head', 'w']));
self::assertEquals('med', $majorGridlines3->getLineStyleProperty(['arrow', 'head', 'len']));
self::assertEquals('sm', $majorGridlines3->getLineStyleProperty(['arrow', 'end', 'w']));
self::assertEquals('lg', $majorGridlines3->getLineStyleProperty(['arrow', 'end', 'len']));
$minorGridlines3 = $yAxis3->getMinorGridlines();
self::assertNotNull($minorGridlines3);
self::assertSame('00FF00', $minorGridlines3->getLineColorProperty('value'));
self::assertSame(30, $minorGridlines3->getLineColorProperty('alpha'));
self::assertSame('srgbClr', $minorGridlines3->getLineColorProperty('type'));
$reloadedSpreadsheet->disconnectWorksheets();
}
public function testLineStylesDeprecated(): void
{
$spreadsheet = new Spreadsheet();
$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_LINECHART, // 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)');
$majorGridlines = new GridLines();
$width = 2;
$compound = Properties::LINE_STYLE_COMPOUND_THICKTHIN;
$dash = Properties::LINE_STYLE_DASH_ROUND_DOT;
$cap = Properties::LINE_STYLE_CAP_ROUND;
$join = Properties::LINE_STYLE_JOIN_MITER;
$headArrowType = Properties::LINE_STYLE_ARROW_TYPE_DIAMOND;
$headArrowSize = (string) Properties::LINE_STYLE_ARROW_SIZE_2;
$endArrowType = Properties::LINE_STYLE_ARROW_TYPE_OVAL;
$endArrowSize = (string) Properties::LINE_STYLE_ARROW_SIZE_3;
$majorGridlines->setLineStyleProperties(
$width,
$compound,
$dash,
$cap,
$join,
$headArrowType,
$headArrowSize,
$endArrowType,
$endArrowSize
);
$minorGridlines = new GridLines();
$minorGridlines->setLineColorProperties('00FF00', 30, 'srgbClr');
self::assertEquals($width, $majorGridlines->getLineStyleProperty('width'));
self::assertEquals($compound, $majorGridlines->getLineStyleProperty('compound'));
self::assertEquals($dash, $majorGridlines->getLineStyleProperty('dash'));
self::assertEquals($cap, $majorGridlines->getLineStyleProperty('cap'));
self::assertEquals($join, $majorGridlines->getLineStyleProperty('join'));
self::assertEquals($headArrowType, $majorGridlines->getLineStyleProperty(['arrow', 'head', 'type']));
self::assertEquals($headArrowSize, $majorGridlines->getLineStyleProperty(['arrow', 'head', 'size']));
self::assertEquals($endArrowType, $majorGridlines->getLineStyleProperty(['arrow', 'end', 'type']));
self::assertEquals($endArrowSize, $majorGridlines->getLineStyleProperty(['arrow', 'end', 'size']));
self::assertEquals('sm', $majorGridlines->getLineStyleProperty(['arrow', 'head', 'w']));
self::assertEquals('med', $majorGridlines->getLineStyleProperty(['arrow', 'head', 'len']));
self::assertEquals('sm', $majorGridlines->getLineStyleProperty(['arrow', 'end', 'w']));
self::assertEquals('lg', $majorGridlines->getLineStyleProperty(['arrow', 'end', 'len']));
self::assertEquals('sm', $majorGridlines->getLineStyleArrowWidth('end'));
self::assertEquals('lg', $majorGridlines->getLineStyleArrowLength('end'));
self::assertEquals('lg', $majorGridlines->getLineStyleArrowParameters('end', 'len'));
self::assertSame('00FF00', $minorGridlines->getLineColorProperty('value'));
self::assertSame(30, $minorGridlines->getLineColorProperty('alpha'));
self::assertSame('srgbClr', $minorGridlines->getLineColorProperty('type'));
// Create the chart // Create the chart
$chart = new Chart( $chart = new Chart(
'chart1', // name 'chart1', // name
@ -154,6 +351,7 @@ class GridlinesLineStyleTest extends AbstractFunctional
$minorGridlines // minorGridlines $minorGridlines // minorGridlines
); );
$majorGridlines2 = $chart->getMajorGridlines(); $majorGridlines2 = $chart->getMajorGridlines();
self::assertNotNull($majorGridlines2);
self::assertEquals($width, $majorGridlines2->getLineStyleProperty('width')); self::assertEquals($width, $majorGridlines2->getLineStyleProperty('width'));
self::assertEquals($compound, $majorGridlines2->getLineStyleProperty('compound')); self::assertEquals($compound, $majorGridlines2->getLineStyleProperty('compound'));
self::assertEquals($dash, $majorGridlines2->getLineStyleProperty('dash')); self::assertEquals($dash, $majorGridlines2->getLineStyleProperty('dash'));
@ -169,6 +367,7 @@ class GridlinesLineStyleTest extends AbstractFunctional
self::assertEquals('lg', $majorGridlines2->getLineStyleProperty(['arrow', 'end', 'len'])); self::assertEquals('lg', $majorGridlines2->getLineStyleProperty(['arrow', 'end', 'len']));
$minorGridlines2 = $chart->getMinorGridlines(); $minorGridlines2 = $chart->getMinorGridlines();
self::assertNotNull($minorGridlines2);
self::assertSame('00FF00', $minorGridlines2->getLineColorProperty('value')); self::assertSame('00FF00', $minorGridlines2->getLineColorProperty('value'));
self::assertSame(30, $minorGridlines2->getLineColorProperty('alpha')); self::assertSame(30, $minorGridlines2->getLineColorProperty('alpha'));
self::assertSame('srgbClr', $minorGridlines2->getLineColorProperty('type')); self::assertSame('srgbClr', $minorGridlines2->getLineColorProperty('type'));
@ -193,6 +392,7 @@ class GridlinesLineStyleTest extends AbstractFunctional
$chart2 = $charts2[0]; $chart2 = $charts2[0];
self::assertNotNull($chart2); self::assertNotNull($chart2);
$majorGridlines3 = $chart2->getMajorGridlines(); $majorGridlines3 = $chart2->getMajorGridlines();
self::assertNotNull($majorGridlines3);
self::assertEquals($width, $majorGridlines3->getLineStyleProperty('width')); self::assertEquals($width, $majorGridlines3->getLineStyleProperty('width'));
self::assertEquals($compound, $majorGridlines3->getLineStyleProperty('compound')); self::assertEquals($compound, $majorGridlines3->getLineStyleProperty('compound'));
self::assertEquals($dash, $majorGridlines3->getLineStyleProperty('dash')); self::assertEquals($dash, $majorGridlines3->getLineStyleProperty('dash'));
@ -206,6 +406,7 @@ class GridlinesLineStyleTest extends AbstractFunctional
self::assertEquals('lg', $majorGridlines3->getLineStyleProperty(['arrow', 'end', 'len'])); self::assertEquals('lg', $majorGridlines3->getLineStyleProperty(['arrow', 'end', 'len']));
$minorGridlines3 = $chart2->getMinorGridlines(); $minorGridlines3 = $chart2->getMinorGridlines();
self::assertNotNull($minorGridlines3);
self::assertSame('00FF00', $minorGridlines3->getLineColorProperty('value')); self::assertSame('00FF00', $minorGridlines3->getLineColorProperty('value'));
self::assertSame(30, $minorGridlines3->getLineColorProperty('alpha')); self::assertSame(30, $minorGridlines3->getLineColorProperty('alpha'));
self::assertSame('srgbClr', $minorGridlines3->getLineColorProperty('type')); self::assertSame('srgbClr', $minorGridlines3->getLineColorProperty('type'));

View File

@ -2,6 +2,7 @@
namespace PhpOffice\PhpSpreadsheetTests\Chart; namespace PhpOffice\PhpSpreadsheetTests\Chart;
use PhpOffice\PhpSpreadsheet\Chart\Axis;
use PhpOffice\PhpSpreadsheet\Chart\Chart; use PhpOffice\PhpSpreadsheet\Chart\Chart;
use PhpOffice\PhpSpreadsheet\Chart\DataSeries; use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues; use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
@ -93,7 +94,9 @@ class GridlinesShadowGlowTest extends AbstractFunctional
$title = new Title('Test %age-Stacked Area Chart'); $title = new Title('Test %age-Stacked Area Chart');
$yAxisLabel = new Title('Value ($k)'); $yAxisLabel = new Title('Value ($k)');
$yAxis = new Axis();
$majorGridlines = new GridLines(); $majorGridlines = new GridLines();
$yAxis->setMajorGridlines($majorGridlines);
$majorGlowSize = 10.0; $majorGlowSize = 10.0;
$majorGridlines->setGlowProperties($majorGlowSize, 'FFFF00', 30, Properties::EXCEL_COLOR_TYPE_ARGB); $majorGridlines->setGlowProperties($majorGlowSize, 'FFFF00', 30, Properties::EXCEL_COLOR_TYPE_ARGB);
$softEdgeSize = 2.5; $softEdgeSize = 2.5;
@ -110,6 +113,7 @@ class GridlinesShadowGlowTest extends AbstractFunctional
self::assertEquals($softEdgeSize, $majorGridlines->getSoftEdgesSize()); self::assertEquals($softEdgeSize, $majorGridlines->getSoftEdgesSize());
$minorGridlines = new GridLines(); $minorGridlines = new GridLines();
$yAxis->setMinorGridlines($minorGridlines);
$expectedShadow = [ $expectedShadow = [
'effect' => 'outerShdw', 'effect' => 'outerShdw',
'algn' => 'tl', 'algn' => 'tl',
@ -146,15 +150,16 @@ class GridlinesShadowGlowTest extends AbstractFunctional
null, // xAxisLabel null, // xAxisLabel
$yAxisLabel, // yAxisLabel $yAxisLabel, // yAxisLabel
null, // xAxis null, // xAxis
null, // yAxis $yAxis // yAxis
$majorGridlines,
$minorGridlines
); );
$majorGridlines2 = $chart->getMajorGridlines(); $yAxis2 = $chart->getChartAxisY();
$majorGridlines2 = $yAxis2->getMajorGridlines();
self::assertNotNull($majorGridlines2);
self::assertEquals($majorGlowSize, $majorGridlines2->getGlowProperty('size')); self::assertEquals($majorGlowSize, $majorGridlines2->getGlowProperty('size'));
self::assertEquals($expectedGlowColor, $majorGridlines2->getGlowProperty('color')); self::assertEquals($expectedGlowColor, $majorGridlines2->getGlowProperty('color'));
self::assertEquals($softEdgeSize, $majorGridlines2->getSoftEdgesSize()); self::assertEquals($softEdgeSize, $majorGridlines2->getSoftEdgesSize());
$minorGridlines2 = $chart->getMinorGridlines(); $minorGridlines2 = $yAxis2->getMinorGridlines();
self::assertNotNull($minorGridlines2);
foreach ($expectedShadow as $key => $value) { foreach ($expectedShadow as $key => $value) {
self::assertEquals($value, $minorGridlines2->getShadowProperty($key), $key); self::assertEquals($value, $minorGridlines2->getShadowProperty($key), $key);
} }
@ -178,11 +183,14 @@ class GridlinesShadowGlowTest extends AbstractFunctional
self::assertCount(1, $charts2); self::assertCount(1, $charts2);
$chart2 = $charts2[0]; $chart2 = $charts2[0];
self::assertNotNull($chart2); self::assertNotNull($chart2);
$majorGridlines3 = $chart2->getMajorGridlines(); $yAxis3 = $chart2->getChartAxisY();
$majorGridlines3 = $yAxis3->getMajorGridlines();
self::assertNotNull($majorGridlines3);
self::assertEquals($majorGlowSize, $majorGridlines3->getGlowProperty('size')); self::assertEquals($majorGlowSize, $majorGridlines3->getGlowProperty('size'));
self::assertEquals($expectedGlowColor, $majorGridlines3->getGlowProperty('color')); self::assertEquals($expectedGlowColor, $majorGridlines3->getGlowProperty('color'));
self::assertEquals($softEdgeSize, $majorGridlines3->getSoftEdgesSize()); self::assertEquals($softEdgeSize, $majorGridlines3->getSoftEdgesSize());
$minorGridlines3 = $chart->getMinorGridlines(); $minorGridlines3 = $yAxis3->getMinorGridlines();
self::assertNotNull($minorGridlines3);
foreach ($expectedShadow as $key => $value) { foreach ($expectedShadow as $key => $value) {
self::assertEquals($value, $minorGridlines3->getShadowProperty($key), $key); self::assertEquals($value, $minorGridlines3->getShadowProperty($key), $key);
} }

View File

@ -0,0 +1,42 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Chart;
use PhpOffice\PhpSpreadsheet\Chart\GridLines;
use PhpOffice\PhpSpreadsheet\Chart\Properties;
use PHPUnit\Framework\TestCase;
class LineStylesTest extends TestCase
{
public function testLineStyles(): void
{
$gridlines1 = new GridLines();
$originalLineStyle = $gridlines1->getLineStyleArray();
$gridlines1->setLineStyleProperties(
3, // lineWidth
Properties::LINE_STYLE_COMPOUND_DOUBLE, // compoundType
'', // dashType
Properties::LINE_STYLE_CAP_SQUARE, // capType
'', // jointType
'', // headArrowType
'', // headArrowSize
'', // endArrowType
'', // endArrowSize
'lg', // headArrowWidth
'med', // headArrowLength
'', // endArrowWidth
'' // endArrowLength
);
$gridlines2 = new GridLines();
$lineStyleProperties = [
'width' => 3,
'compound' => Properties::LINE_STYLE_COMPOUND_DOUBLE,
'cap' => Properties::LINE_STYLE_CAP_SQUARE,
'arrow' => ['head' => ['w' => 'lg', 'len' => 'med']],
];
$gridlines2->setLineStyleArray($lineStyleProperties);
self::assertSame($gridlines1->getLineStyleArray(), $gridlines2->getLineStyleArray());
$gridlines2->setLineStyleArray(); // resets line styles
self::assertSame($originalLineStyle, $gridlines2->getLineStyleArray());
}
}

View File

@ -138,6 +138,7 @@ class PieFillTest extends AbstractFunctional
$chart2 = $charts2[0]; $chart2 = $charts2[0];
self::assertNotNull($chart2); self::assertNotNull($chart2);
$plotArea2 = $chart2->getPlotArea(); $plotArea2 = $chart2->getPlotArea();
self::assertNotNull($plotArea2);
$dataSeries2 = $plotArea2->getPlotGroup(); $dataSeries2 = $plotArea2->getPlotGroup();
self::assertCount(1, $dataSeries2); self::assertCount(1, $dataSeries2);
$plotValues = $dataSeries2[0]->getPlotValues(); $plotValues = $dataSeries2[0]->getPlotValues();

View File

@ -23,6 +23,7 @@ class SheetsXlsxChartTest extends TestCase
$chart1 = $charts[0]; $chart1 = $charts[0];
self::assertNotNull($chart1); self::assertNotNull($chart1);
$pa1 = $chart1->getPlotArea(); $pa1 = $chart1->getPlotArea();
self::assertNotNull($pa1);
self::assertEquals(2, $pa1->getPlotSeriesCount()); self::assertEquals(2, $pa1->getPlotSeriesCount());
$pg1 = $pa1->getPlotGroup()[0]; $pg1 = $pa1->getPlotGroup()[0];
@ -35,6 +36,7 @@ class SheetsXlsxChartTest extends TestCase
$chart2 = $charts[1]; $chart2 = $charts[1];
self::assertNotNull($chart2); self::assertNotNull($chart2);
$pa1 = $chart2->getPlotArea(); $pa1 = $chart2->getPlotArea();
self::assertNotNull($pa1);
self::assertEquals(2, $pa1->getPlotSeriesCount()); self::assertEquals(2, $pa1->getPlotSeriesCount());
$pg1 = $pa1->getPlotGroupByIndex(0); $pg1 = $pa1->getPlotGroupByIndex(0);