-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Pie chart (2D) created as 3D #4425
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
Comments
Hello @oleibman, Sure, I can share the code! Let me specify that I'm using Laravel-Excel, which in case you don't know, it's a wrapper around your library that makes things easier when working in Laravel projects.
public function array(): array
{
$this->rows = [
[ '', 2010, 2011, 2012 ],
[ 'Q1', 12, 15, 21 ],
[ 'Q2', 56, 73, 86 ],
[ 'Q3', 52, 61, 69 ],
[ 'Q4', 30, 32, 0 ],
];
return $this->rows;
}
/**
* Add one or more Charts
* @return Chart|Chart[]
*/
public function charts()
{
return [ $this->makePieChart('D1', 'N25') ];
}
/**
* @return \PhpOffice\PhpSpreadsheet\Chart\Chart
*/
private function makePieChart(string $startCell, string $endCell, bool $withNameLabels = false)
{
$dataRowCount = count($this->rows)-1;
// Set the Labels for each data series we want to plot
$dataSeriesLabels = [
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1), // 2010
];
// Set the X-Axis Labels
$xAxisTickValues = [
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, $dataRowCount), // Users names
];
// Set the Data values for each data series we want to plot
$dataSeriesValues = [
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', null, $dataRowCount), // Downloads No.
];
// Build the dataseries
$series1 = new DataSeries(
DataSeries::TYPE_PIECHART, // plotType
null, // plotGrouping (Pie charts don't have any grouping)
range(0, count($dataSeriesValues) - 1),
$dataSeriesLabels,
$xAxisTickValues,
$dataSeriesValues
);
// Set up a layout object for the Pie chart
$layout1 = new Layout();
$layout1->setShowVal(true);
//$layout1->setShowPercent(true);
if($withNameLabels) {
$layout1->setShowCatName(true);
}
// Set the series in the plot area
$plotArea1 = new PlotArea($layout1, [ $series1 ]);
// Set the chart legend
$legend1 = new Legend(Legend::POSITION_RIGHT, null, false);
$title1 = new Title('Test Pie Chart');
// Create the chart
$chart = new Chart(
'pie-chart-1', // 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
);
// Set position in the worksheet
$chart->setTopLeftPosition($startCell);
$chart->setBottomRightPosition($endCell);
return $chart;
} You know, I use ONLYOFFICE to replace MS Excel, I just tried to import the exported file in Google Sheets, and it rendered correctly. What makes me think, is the fact that adding the condition mentioned in the issue description, fixes it. If you want to do a comparison: Let me know what do you think about it. EDIT: switching the chart type to |
So, the chart is displayed correctly in Excel and Google Docs, and I have just confirmed that it is displayed correctly in LibreOffice and Gnumeric as well. It sounds like you need to file a bug report with OnlyOffice. |
Nice, thanks for having checked @oleibman! I'll link this post in their issue ;-) |
Hello,
I'm trying to create a 2D Pie chart, by following this example: 33_Chart_create_pie.php
Basically the generated chart is a 3D Pie:
I've done a bit of reverse engineering and found the
<c:view3d>
tag in the generated XML, which leaded me to this line:phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xlsx/Chart.php:83
So I did a quick test by changing the if condition to the following:
And the result now looks right:

Unfortunately I can't send a PR as I don't know the codebase enough to know if this will break something else.
Cheers
The text was updated successfully, but these errors were encountered: