Skip to content

Commit

Permalink
add chats generation
Browse files Browse the repository at this point in the history
  • Loading branch information
aVadim483 committed Mar 3, 2024
1 parent 2ed7c93 commit f64fc48
Show file tree
Hide file tree
Showing 33 changed files with 349 additions and 38 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v.5.0

Chart support!

## v.4.7

The order of writing entries to the file has been changed so that the mimetype is determined correctly
Expand Down
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ This library is designed to be lightweight, super-fast and requires minimal memo
* You can add formulas, notes and images in you XLSX-files
* Supports workbook and sheet protection with/without passwords
* Supports page settings - page margins, page size
* Inserting multiple charts

Jump To:
* [Changes in version 4](#changes-in-version-4)
* [Changes in version 5](#changes-in-version-5)
* [Simple Example](#simple-example)
* [Advanced Example](#advanced-example)
* [Row's settings](#rows-settings)
Expand Down Expand Up @@ -65,10 +67,15 @@ Jump To:
* [Apply Fonts](/docs/04-styles.md#apply-fonts)
* [Apply Colors](/docs/04-styles.md#apply-colors)
* [Apply Text Styles](/docs/04-styles.md#apply-text-styles)
* [Protection of workbook and sheets](/docs/05-protection.md)
* [Workbook protection](/docs/05-protection.md#workbook-protection)
* [Sheet protection](/docs/05-protection.md#sheet-protection)
* [Cells locking/unlocking](/docs/05-protection.md#cells-lockingunlocking)
* [Charts](/docs/05-charts.md)
* [Simple usage](/docs/05-charts.md#simple-usage-of-chart)
* [Multiple charts](/docs/05-charts.md#multiple-charts)
* [Chart types](/docs/05-charts.md#chart-types)
* [Useful Chart Methods](/docs/05-charts.md#useful-chart-methods)
* [Protection of workbook and sheets](/docs/06-protection.md)
* [Workbook protection](/docs/06-protection.md#workbook-protection)
* [Sheet protection](/docs/06-protection.md#sheet-protection)
* [Cells locking/unlocking](/docs/06-protection.md#cells-lockingunlocking)
* [FastExcelWriter vs PhpSpreadsheet](#fastexcelwriter-vs-phpspreadsheet)
* [Do you want to support FastExcelWriter?](#do-you-want-to-support-fastexcelwriter)

Expand All @@ -87,6 +94,10 @@ composer require avadim/fast-excel-writer
* Added a fluent interface for applying styles.
* New methods and code refactoring

## Changes In Version 5

* General is Chart support

## Usage

You can find usage examples below or in */demo* folder
Expand Down
4 changes: 2 additions & 2 deletions demo/demo-v5-21-chart-area-3d-stacked.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
$timer = microtime(true);

// Create Excel workbook
$excel = Excel::create(['Without Psw', 'With Psw']);
$excel = Excel::create(['Chart Demo']);

$sheet = $excel->sheet();

Expand All @@ -29,7 +29,7 @@
$sheet->writeRow($row);
}

$chart1 = Chart::make(Chart::TYPE_AREA_3D_STACKED, 'Area Chart', ['b1' => 'B2:B5', 'c1' => 'c2:c5', 'd1' => 'd2:d5'])
$chart1 = Chart::make(Chart::TYPE_AREA_3D_STACKED, 'Stacked Area 3D Chart', ['b1' => 'B2:B5', 'c1' => 'c2:c5', 'd1' => 'd2:d5'])
->setDataSeriesTickLabels('A2:A5')
->setLegendPosition(Legend::POSITION_TOPRIGHT)
;
Expand Down
2 changes: 1 addition & 1 deletion demo/demo-v5-21-chart-area-3d.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
$timer = microtime(true);

// Create Excel workbook
$excel = Excel::create(['Without Psw', 'With Psw']);
$excel = Excel::create(['Chart Demo']);

$sheet = $excel->sheet();

Expand Down
2 changes: 1 addition & 1 deletion demo/demo-v5-21-chart-area-stacked.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
$timer = microtime(true);

// Create Excel workbook
$excel = Excel::create(['Without Psw', 'With Psw']);
$excel = Excel::create(['Chart Demo']);

$sheet = $excel->sheet();

Expand Down
2 changes: 1 addition & 1 deletion demo/demo-v5-21-chart-area.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
$timer = microtime(true);

// Create Excel workbook
$excel = Excel::create(['Without Psw', 'With Psw']);
$excel = Excel::create(['Chart Demo']);

$sheet = $excel->sheet();

Expand Down
2 changes: 1 addition & 1 deletion demo/demo-v5-21-chart-bar-stacked.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
$timer = microtime(true);

// Create Excel workbook
$excel = Excel::create(['Without Psw', 'With Psw']);
$excel = Excel::create(['Chart Demo']);

$sheet = $excel->sheet();

Expand Down
2 changes: 1 addition & 1 deletion demo/demo-v5-21-chart-bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
$timer = microtime(true);

// Create Excel workbook
$excel = Excel::create(['Without Psw', 'With Psw']);
$excel = Excel::create(['Chart Demo']);

$sheet = $excel->sheet();

Expand Down
2 changes: 1 addition & 1 deletion demo/demo-v5-21-chart-column-grouped.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
$timer = microtime(true);

// Create Excel workbook
$excel = Excel::create(['Without Psw', 'With Psw']);
$excel = Excel::create(['Chart Demo']);

$sheet = $excel->sheet();

Expand Down
2 changes: 1 addition & 1 deletion demo/demo-v5-21-chart-column-stacked.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
$timer = microtime(true);

// Create Excel workbook
$excel = Excel::create(['Without Psw', 'With Psw']);
$excel = Excel::create(['Chart Demo']);

$sheet = $excel->sheet();

Expand Down
2 changes: 1 addition & 1 deletion demo/demo-v5-21-chart-column.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
$timer = microtime(true);

// Create Excel workbook
$excel = Excel::create(['Without Psw', 'With Psw']);
$excel = Excel::create(['Chart Demo']);

$sheet = $excel->sheet();

Expand Down
2 changes: 1 addition & 1 deletion demo/demo-v5-21-chart-donut.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
$timer = microtime(true);

// Create Excel workbook
$excel = Excel::create(['Without Psw', 'With Psw']);
$excel = Excel::create(['Chart Demo']);

$sheet = $excel->sheet();

Expand Down
2 changes: 1 addition & 1 deletion demo/demo-v5-21-chart-line-3d-stacked.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
$timer = microtime(true);

// Create Excel workbook
$excel = Excel::create(['Without Psw', 'With Psw']);
$excel = Excel::create(['Chart Demo']);

$sheet = $excel->sheet();

Expand Down
2 changes: 1 addition & 1 deletion demo/demo-v5-21-chart-line-3d.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
$timer = microtime(true);

// Create Excel workbook
$excel = Excel::create(['Without Psw', 'With Psw']);
$excel = Excel::create(['Chart Demo']);

$sheet = $excel->sheet();

Expand Down
2 changes: 1 addition & 1 deletion demo/demo-v5-21-chart-line-stacked.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
$timer = microtime(true);

// Create Excel workbook
$excel = Excel::create(['Without Psw', 'With Psw']);
$excel = Excel::create(['Chart Demo']);

$sheet = $excel->sheet();

Expand Down
2 changes: 1 addition & 1 deletion demo/demo-v5-21-chart-line.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
$timer = microtime(true);

// Create Excel workbook
$excel = Excel::create(['Without Psw', 'With Psw']);
$excel = Excel::create(['Chart Demo']);

$sheet = $excel->sheet();

Expand Down
2 changes: 1 addition & 1 deletion demo/demo-v5-21-chart-pie-3d.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
$timer = microtime(true);

// Create Excel workbook
$excel = Excel::create(['Without Psw', 'With Psw']);
$excel = Excel::create(['Chart Demo']);

$sheet = $excel->sheet();

Expand Down
2 changes: 1 addition & 1 deletion demo/demo-v5-21-chart-pie.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
$timer = microtime(true);

// Create Excel workbook
$excel = Excel::create(['Without Psw', 'With Psw']);
$excel = Excel::create(['Chart Demo']);

$sheet = $excel->sheet();

Expand Down
58 changes: 58 additions & 0 deletions demo/demo-v5-22-chart-multiple-charts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../src/autoload.php';

$outFileName = __DIR__ . '/output/' . basename(__FILE__, '.php') . '.xlsx';

use avadim\FastExcelWriter\Charts\Chart;
use avadim\FastExcelWriter\Charts\Legend;
use \avadim\FastExcelWriter\Excel;
use avadim\FastExcelWriter\Style;

$timer = microtime(true);

// Create Excel workbook
$excel = Excel::create(['Chart Demo']);

$sheet = $excel->sheet();

$data = [
['', 2010, 2011, 2012],
['Q1', 12, 15, 21],
['Q2', 56, 73, 86],
['Q3', 52, 61, 69],
['Q4', 30, 32, 0],
['Total', '=SUM(B2:B5)', '=SUM(C2:C5)', '=SUM(D2:D5)'],
];

foreach ($data as $row) {
$sheet->writeRow($row);
}

$chart1 = Chart::make(Chart::TYPE_COLUMN, 'Column Chart', ['b1' => 'B2:B5', 'c1' => 'c2:c5', 'd1' => 'd2:d5'])
->setDataSeriesTickLabels('A2:A5')
->setLegendPosition(Legend::POSITION_TOPRIGHT)
;

// Add the chart to the worksheet
$sheet->addChart('a9:h22', $chart1);

$chart2 = Chart::make(Chart::TYPE_PIE, 'Pie Chart', ['b6:d6'])
->setDataSeriesTickLabels('b1:d1')
->setLegendPosition(Legend::POSITION_TOPRIGHT)
->setPlotShowPercent(true)
;

// Add the chart to the worksheet
$sheet->addChart('i9:m22', $chart2);

// Save to XLSX-file
$excel->save($outFileName);

echo '<b>', basename(__FILE__, '.php'), "</b><br>\n<br>\n";
echo 'out filename: ', $outFileName, "<br>\n";
echo 'elapsed time: ', round(microtime(true) - $timer, 3), ' sec', "<br>\n";
echo 'memory peak usage: ', memory_get_peak_usage(true), "<br>\n";

// EOF
100 changes: 100 additions & 0 deletions docs/05-charts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
## FastExcelWriter - Charts

You can insert charts to generated spreadsheets (You can find usage examples below or in */demo* folder)

### Simple usage of chart

```php
// Create Excel workbook
$excel = Excel::create(['Chart Demo']);

$sheet = $excel->sheet();

$data = [
['', 2010, 2011, 2012],
['Q1', 12, 15, 21],
['Q2', 56, 73, 86],
['Q3', 52, 61, 69],
['Q4', 30, 32, 0],
];

foreach ($data as $row) {
$sheet->writeRow($row);
}

// Define data series for chart
$dataSeries = [
// key - cell with name of data series
// value - range with data series
'B1' => 'B2:B5',
'C1' => 'c2:c5',
'D1' => 'd2:d5',
];

$chartTitle = 'Bar Chart';

// Create chart
$chart = Chart::make(Chart::TYPE_COLUMN, $chartTitle, $dataSeries)
// X axis tick values
->setDataSeriesTickLabels('A2:A5')
// Position of legend
->setLegendPosition(Legend::POSITION_TOPRIGHT)
;

// Add the chart to the worksheet
$sheet->addChart('A7:H20', $chart);

// Save to XLSX-file
$excel->save($outFileName);

```

### Multiple charts

```php
// Make chart 1
$chart1 = Chart::make(Chart::TYPE_COLUMN, 'Column Chart', ['b1' => 'B2:B5', 'c1' => 'c2:c5', 'd1' => 'd2:d5'])
->setDataSeriesTickLabels('A2:A5')
->setLegendPosition(Legend::POSITION_TOPRIGHT)
;

// Add the chart to the worksheet
$sheet->addChart('a9:h22', $chart1);

// Make chart 2
$chart2 = Chart::make(Chart::TYPE_PIE, 'Pie Chart', ['b6:d6'])
->setDataSeriesTickLabels('b1:d1')
->setLegendPosition(Legend::POSITION_TOPRIGHT)
->setPlotShowPercent(true)
;

// Add the chart to the worksheet
$sheet->addChart('i9:m22', $chart2);
```

### Chart types

| const name of class<br/>Chart | chart type | |
|-------------------------------------|-----------------------|-------------------------------------------------------------------------|
| TYPE_BAR | bar chart | ![img/chart-bar_240.jpg](img/chart-bar_240.jpg) |
| TYPE_BAR_STACKED | stacked bar chart | ![img/chart-bar_240.jpg](img/chart-bar_240.jpg) |
| TYPE_COLUMN | column chart | ![img/chart-column_240.jpg](img/chart-column_240.jpg) |
| TYPE_COLUMN_STACKED | stacked column chart | ![img/chart-column-stacked_240.jpg](img/chart-column-stacked_240.jpg) |
| TYPE_LINE | line chart | ![img/chart-line_240.jpg](img/chart-line_240.jpg) |
| TYPE_LINE_STACKED | stacked line chart | ![img/chart-line-stacked_240.jpg](img/chart-line-stacked_240.jpg) |
| TYPE_LINE_3D | line 3D chart | ![img/chart-line-3d_240.jpg](img/chart-line-3d_240.jpg) |
| TYPE_LINE_3D_STACKED | stacked line 3D chart | ![img/chart-line-3d-stacked_240.jpg](img/chart-line-3d-stacked_240.jpg) |
| TYPE_AREA | area chart | ![img/chart-area_240.jpg](img/chart-area_240.jpg) |
| TYPE_AREA_STACKED | stacked area chart | ![img/chart-area-stacked_240.jpg](img/chart-area-stacked_240.jpg) |
| TYPE_AREA_3D | area 3D chart | ![img/chart-area-3d_240.jpg](img/chart-area-3d_240.jpg) |
| TYPE_AREA_3D_STACKED | stacked area 3D chart | ![img/chart-area-stacked_240.jpg](img/chart-area-stacked_240.jpg) |
| TYPE_PIE | pie chart | ![img/chart-pie_240.jpg](img/chart-pie_240.jpg) |
| TYPE_PIE_3D | pie 3D chart | ![img/chart-pie-3d_240.jpg](img/chart-pie-3d_240.jpg) |
| TYPE_DONUT | doughnut chart | ![img/chart-donut_240.jpg](img/chart-donut_240.jpg) |

### Useful Chart Methods

* setDataSeriesTickLabels(\<range>) - X axis tick values
* setLegendPosition(\<position>) - position of legend (use constants Legend::POSITION_XXX)
* setPlotShowValues(true) - show values on the chart
* setPlotShowPercent(true) - show values is percents (for pie and sonut)
File renamed without changes.
5 changes: 5 additions & 0 deletions src/FastExcelWriter/Charts/Axis.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace avadim\FastExcelWriter\Charts;

/**
* This class uses source code of PHPExcel
*
* @license LGPL http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
*/
class Axis extends Properties
{
/**
Expand Down
Loading

0 comments on commit f64fc48

Please sign in to comment.