Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some Fixes for Scatter Charts #2828

Merged
merged 7 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Support Bezier Curve and Scaling of X-Axis on Scatter Plot
For Bezier, need to specify `<c:smooth>` tag in addition to already supplied `<c:scatterStyle val="smoothMarker">`

For X-Axis, scatter needs to supply both X and y axis as `<c:valAx>` rather than `<c:catAx>` for X.
  • Loading branch information
oleibman committed May 15, 2022
commit 2f623a2c7c0c2c7e1f49e9169a16e71186689721
8 changes: 4 additions & 4 deletions src/PhpSpreadsheet/Chart/DataSeriesValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DataSeriesValues
/**
* Series Data Source.
*
* @var string
* @var ?string
*/
private $dataSource;

Expand Down Expand Up @@ -95,7 +95,7 @@ class DataSeriesValues
* @param null|string|string[] $fillColor
* @param string $pointSize
*/
public function __construct($dataType = self::DATASERIES_TYPE_NUMBER, $dataSource = '', $formatCode = null, $pointCount = 0, $dataValues = [], $marker = null, $fillColor = null, $pointSize = '3')
public function __construct($dataType = self::DATASERIES_TYPE_NUMBER, $dataSource = null, $formatCode = null, $pointCount = 0, $dataValues = [], $marker = null, $fillColor = null, $pointSize = '3')
{
$this->setDataType($dataType);
$this->dataSource = $dataSource;
Expand Down Expand Up @@ -144,7 +144,7 @@ public function setDataType($dataType)
/**
* Get Series Data Source (formula).
*
* @return string
* @return ?string
*/
public function getDataSource()
{
Expand All @@ -154,7 +154,7 @@ public function getDataSource()
/**
* Set Series Data Source (formula).
*
* @param string $dataSource
* @param ?string $dataSource
*
* @return $this
*/
Expand Down
11 changes: 8 additions & 3 deletions src/PhpSpreadsheet/Writer/Xlsx/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ private function writePlotArea(XMLWriter $objWriter, PlotArea $plotArea, ?Title
if ($chartType === DataSeries::TYPE_BUBBLECHART) {
$this->writeValueAxis($objWriter, $xAxisLabel, $chartType, $id1, $id2, $catIsMultiLevelSeries, $xAxis, $majorGridlines, $minorGridlines);
} else {
$this->writeCategoryAxis($objWriter, $xAxisLabel, $id1, $id2, $catIsMultiLevelSeries, $xAxis);
$this->writeCategoryAxis($objWriter, $xAxisLabel, $id1, $id2, $catIsMultiLevelSeries, $xAxis, ($chartType === DataSeries::TYPE_SCATTERCHART) ? 'c:valAx' : 'c:catAx');
}

$this->writeValueAxis($objWriter, $yAxisLabel, $chartType, $id1, $id2, $valIsMultiLevelSeries, $yAxis, $majorGridlines, $minorGridlines);
Expand Down Expand Up @@ -367,9 +367,9 @@ private function writeDataLabels(XMLWriter $objWriter, ?Layout $chartLayout = nu
* @param string $id2
* @param bool $isMultiLevelSeries
*/
private function writeCategoryAxis(XMLWriter $objWriter, ?Title $xAxisLabel, $id1, $id2, $isMultiLevelSeries, Axis $yAxis): void
private function writeCategoryAxis(XMLWriter $objWriter, ?Title $xAxisLabel, $id1, $id2, $isMultiLevelSeries, Axis $yAxis, string $element = 'c:catAx'): void
{
$objWriter->startElement('c:catAx');
$objWriter->startElement($element);

if ($id1 > 0) {
$objWriter->startElement('c:axId');
Expand Down Expand Up @@ -1202,6 +1202,11 @@ private function writePlotGroup(?DataSeries $plotGroup, string $groupType, XMLWr

$this->writePlotSeriesValues($plotSeriesValues, $objWriter, $groupType, 'num');
$objWriter->endElement();
if ($groupType === DataSeries::TYPE_SCATTERCHART && $plotGroup->getPlotStyle() === 'smoothMarker') {
$objWriter->startElement('c:smooth');
$objWriter->writeAttribute('val', '1');
$objWriter->endElement();
}
}

if ($groupType === DataSeries::TYPE_BUBBLECHART) {
Expand Down
89 changes: 89 additions & 0 deletions tests/PhpSpreadsheetTests/Writer/Xlsx/Charts32XmlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;

use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
use PhpOffice\PhpSpreadsheet\Shared\File;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
use PHPUnit\Framework\TestCase;

class Charts32XmlTest extends TestCase
{
// These tests can only be performed by examining xml.
private const DIRECTORY = 'samples' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR;

/** @var string */
private $outputFileName = '';

protected function tearDown(): void
{
if ($this->outputFileName !== '') {
unlink($this->outputFileName);
$this->outputFileName = '';
}
}

/**
* @dataProvider providerScatterCharts
*/
public function testBezierCount(int $expectedCount, string $inputFile): void
{
$file = self::DIRECTORY . $inputFile;
$reader = new XlsxReader();
$reader->setIncludeCharts(true);
$spreadsheet = $reader->load($file);

$writer = new XlsxWriter($spreadsheet);
$writer->setIncludeCharts(true);
$this->outputFileName = File::temporaryFilename();
$writer->save($this->outputFileName);
$spreadsheet->disconnectWorksheets();

$file = 'zip://';
$file .= $this->outputFileName;
$file .= '#xl/charts/chart2.xml';
$data = file_get_contents($file);
// confirm that file contains expected tags
if ($data === false) {
self::fail('Unable to read file');
} else {
self::assertSame(1, substr_count($data, '<c:scatterStyle val='));
self::assertSame($expectedCount ? 1 : 0, substr_count($data, '<c:scatterStyle val="smoothMarker"/>'));
self::assertSame($expectedCount, substr_count($data, '<c:smooth val="1"/>'));
}
}

public function providerScatterCharts(): array
{
return [
'no line' => [0, '32readwriteScatterChart1.xlsx'],
'smooth line (Bezier)' => [3, '32readwriteScatterChart2.xlsx'],
'straight line' => [0, '32readwriteScatterChart3.xlsx'],
];
}

public function testAreaPercentageNoCat(): void
{
$file = self::DIRECTORY . '32readwriteAreaPercentageChart1.xlsx';
$reader = new XlsxReader();
$reader->setIncludeCharts(true);
$spreadsheet = $reader->load($file);

$writer = new XlsxWriter($spreadsheet);
$writer->setIncludeCharts(true);
$this->outputFileName = File::temporaryFilename();
$writer->save($this->outputFileName);
$spreadsheet->disconnectWorksheets();

$file = 'zip://';
$file .= $this->outputFileName;
$file .= '#xl/charts/chart1.xml';
$data = file_get_contents($file);
// confirm that file contains expected tags
if ($data === false) {
self::fail('Unable to read file');
} else {
self::assertSame(0, substr_count($data, '<c:cat>'));
}
}
}