Skip to content

Commit

Permalink
Add Export Chart Helpers (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBernskiold authored Dec 29, 2023
1 parent 4dd5ea0 commit 23d307a
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 0 deletions.
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,66 @@ $chart = Chart::make('test-chart')
});
```

### Exporting a chart

Each chart will listen to the `export-chart` event on the `window` object. By triggering this with the appropriate
details, any chart can be exported either from JavaScript or from Livewire.

The detail object must contain the `chartId` and `type` keys. The `chartId` must match the key you used when building
the chart. The `type` must be a valid type for the Highcharts `exportChartLocal` method.

The `exportSettings` can take an object with settings for the export per the Highcharts `exportChartLocal` method.
The `options` take an object with chart options to customize the chart before exporting.

The following example shows how to export a chart from JavaScript:

```js
window.dispatchEvent(new CustomEvent('export-chart', {
detail: {
chartId: 'test-chart',
type: 'image/png',
exportSettings: {}, // Optional.
options: {}, // Optional.
}
}));
```

Using Livewire, you can either use the `dispatch` method, or use the bundled `ExportsChart` trait.

```php

// Using the trait.
use BernskioldMedia\LaravelHighcharts\Concerns\Livewire\ExportsChart;

$this->exportChart(
type: 'image/png',
chartKey: 'test-chart',
exportSettings: [], // Optional.
options: [], // Optional.
);

// Using Livewire 3's dispatch method.
$this->dispatch(
'exportChart',
chartId: 'test-chart',
type: 'image/png',
exportSettings: [], // Optional.
options: [], // Optional.
);
```

If you are using the `ExportsChart` trait, you can also customize the export settings and options by adding the
the `getChartExportSettings` and ? `getChartOptionsForExport` methods to your Livewire component. To ensure their signature,
we suggest implementing the bundled `CustomizesChartExportOptions` and `CustomizesChartExportSettings` interfaces.

If you are using the trait on the same component as the `InteractsWithChart` trait you don't have to provide the chart key, and can use the minimal syntax:

```php
use BernskioldMedia\LaravelHighcharts\Concerns\Livewire\ExportsChart;

$this->exportChart('image/png');
```

## Testing

```bash
Expand Down
61 changes: 61 additions & 0 deletions src/Concerns/Livewire/ExportsChart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace BernskioldMedia\LaravelHighcharts\Concerns\Livewire;

use BernskioldMedia\LaravelHighcharts\Exceptions\ChartExportException;
use function method_exists;

trait ExportsChart
{

/**
* @throws ChartExportException
*/
public function exportChart(string $type, ?string $chartKey = null, array $exportSettings = [], array $chartOptions = []): void
{
if ($chartKey === null && property_exists($this, 'chartKey')) {
$chartKey = $this->chartKey;
}

if ($chartKey === null) {
throw ChartExportException::missingChartKey();
}

if (method_exists($this, 'getChartExportSettings')) {
$exportSettings = array_merge(
$this->getChartExportSettings(),
$exportSettings
);
}

if (method_exists($this, 'getChartOptionsForExport')) {
$chartOptions = array_merge(
$this->getChartOptionsForExport(),
$chartOptions
);
}

// Livewire 3.
if (method_exists($this, 'dispatch')) {
$this->dispatch(
'exportChart',
chartId: $chartKey,
type: $type,
exportSettings: $exportSettings,
options: $chartOptions,
);

return;
}

// Livewire 2.
if (method_exists($this, 'emit')) {
$this->emit('exportChart', [
'chartId' => $chartKey,
'type' => $type,
'exportSettings' => $exportSettings,
'options' => $chartOptions,
]);
}
}
}
10 changes: 10 additions & 0 deletions src/Contracts/CustomizesChartExportOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace BernskioldMedia\LaravelHighcharts\Contracts;

interface CustomizesChartExportOptions
{

public function getChartOptionsForExport(): array;

}
10 changes: 10 additions & 0 deletions src/Contracts/CustomizesChartExportSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace BernskioldMedia\LaravelHighcharts\Contracts;

interface CustomizesChartExportSettings
{

public function getChartExportSettings(): array;

}
14 changes: 14 additions & 0 deletions src/Exceptions/ChartExportException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace BernskioldMedia\LaravelHighcharts\Exceptions;

use Exception;

class ChartExportException extends Exception
{

public static function missingChartKey(): self
{
return new static('The chart key is missing. Please provide a chart key to the exportChart() method or set a chartKey property on the component.');
}
}

0 comments on commit 23d307a

Please sign in to comment.