From 4e0344c3af8cc87bef53a0c33e15c3b972710bd1 Mon Sep 17 00:00:00 2001 From: Michael Bollman Date: Tue, 9 Jan 2018 16:12:38 -0700 Subject: [PATCH] Use line width for data series when rendering Xlsx Closes #329 --- CHANGELOG.md | 1 + samples/Chart/33_Chart_create_line.php | 13 ++++---- src/PhpSpreadsheet/Chart/DataSeriesValues.php | 32 +++++++++++++++++++ src/PhpSpreadsheet/Writer/Xlsx/Chart.php | 11 +++++-- .../Chart/DataSeriesValuesTest.php | 12 +++++++ 5 files changed, 61 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 746db5d0..bc988426 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Support cell comments in HTML writer and reader - [#308](https://github.com/PHPOffice/PhpSpreadsheet/issues/308) - Option to stop at a conditional styling, if it matches (only XLSX format) - [#292](https://github.com/PHPOffice/PhpSpreadsheet/pull/292) +- Support for line width for data series when rendering Xlsx - [#329](https://github.com/PHPOffice/PhpSpreadsheet/pull/329) ### Fixed diff --git a/samples/Chart/33_Chart_create_line.php b/samples/Chart/33_Chart_create_line.php index 27931673..3961d70c 100644 --- a/samples/Chart/33_Chart_create_line.php +++ b/samples/Chart/33_Chart_create_line.php @@ -15,12 +15,12 @@ $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], - ] + ['', 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 @@ -57,6 +57,7 @@ $dataSeriesValues = [ 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), ]; +$dataSeriesValues[2]->setLineWidth(60000); // Build the dataseries $series = new DataSeries( diff --git a/src/PhpSpreadsheet/Chart/DataSeriesValues.php b/src/PhpSpreadsheet/Chart/DataSeriesValues.php index 3c0c6846..a726219c 100644 --- a/src/PhpSpreadsheet/Chart/DataSeriesValues.php +++ b/src/PhpSpreadsheet/Chart/DataSeriesValues.php @@ -66,6 +66,13 @@ class DataSeriesValues */ private $fillColor; + /** + * Line Width. + * + * @var int + */ + private $lineWidth = 12700; + /** * Create a new DataSeriesValues object. * @@ -231,6 +238,31 @@ class DataSeriesValues return $this; } + /** + * Get line width for series. + * + * @return int + */ + public function getLineWidth() + { + return $this->lineWidth; + } + + /** + * Set line width for the series. + * + * @param int $width + * + * @return DataSeriesValues + */ + public function setLineWidth($width) + { + $minWidth = 12700; + $this->lineWidth = max($minWidth, $width); + + return $this; + } + /** * Identify if the Data Series is a multi-level or a simple series. * diff --git a/src/PhpSpreadsheet/Writer/Xlsx/Chart.php b/src/PhpSpreadsheet/Writer/Xlsx/Chart.php index f250cdda..7a1a0415 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx/Chart.php +++ b/src/PhpSpreadsheet/Writer/Xlsx/Chart.php @@ -1127,11 +1127,19 @@ class Chart extends WriterPart $objWriter->endElement(); } + // Values + $plotSeriesValues = $plotGroup->getPlotValuesByIndex($plotSeriesRef); + // Formatting for the points if (($groupType == DataSeries::TYPE_LINECHART) || ($groupType == DataSeries::TYPE_STOCKCHART)) { + $plotLineWidth = 12700; + if ($plotSeriesValues) { + $plotLineWidth = $plotSeriesValues->getLineWidth(); + } + $objWriter->startElement('c:spPr'); $objWriter->startElement('a:ln'); - $objWriter->writeAttribute('w', 12700); + $objWriter->writeAttribute('w', $plotLineWidth); if ($groupType == DataSeries::TYPE_STOCKCHART) { $objWriter->startElement('a:noFill'); $objWriter->endElement(); @@ -1140,7 +1148,6 @@ class Chart extends WriterPart $objWriter->endElement(); } - $plotSeriesValues = $plotGroup->getPlotValuesByIndex($plotSeriesRef); if ($plotSeriesValues) { $plotSeriesMarker = $plotSeriesValues->getPointMarker(); if ($plotSeriesMarker) { diff --git a/tests/PhpSpreadsheetTests/Chart/DataSeriesValuesTest.php b/tests/PhpSpreadsheetTests/Chart/DataSeriesValuesTest.php index 2231bc26..ff798037 100644 --- a/tests/PhpSpreadsheetTests/Chart/DataSeriesValuesTest.php +++ b/tests/PhpSpreadsheetTests/Chart/DataSeriesValuesTest.php @@ -47,4 +47,16 @@ class DataSeriesValuesTest extends TestCase $result = $testInstance->getDataType(); self::assertEquals($dataTypeValue, $result); } + + public function testGetLineWidth() + { + $testInstance = new DataSeriesValues(); + self::assertEquals(12700, $testInstance->getLineWidth(), 'should have default'); + + $testInstance->setLineWidth(40000); + self::assertEquals(40000, $testInstance->getLineWidth()); + + $testInstance->setLineWidth(1); + self::assertEquals(12700, $testInstance->getLineWidth(), 'should enforce minimum width'); + } }