ArielMejiaDev / larapex-charts

A Laravel wrapper for apex charts
https://larapex-charts.netlify.app/
MIT License
284 stars 84 forks source link

The right syntax to add options #102

Open romain-lgr opened 6 months ago

romain-lgr commented 6 months ago

Hello!,

I am a bit struggling with the options features. I am trying to add options to my area chart like this but I can not make it work. Is it the good way to add options?

$salesData = $this->calculateSalesData();
        $totalSales = array_sum($salesData['sales']);

        $options = [
            'yaxis' => [
                'labels' => [
                    'formatter' => function ($value) {
                        return Money::JPY($value);
                    },
                ],
            ],
        ];

        return $this->chart->areaChart()
            ->setTitle('Ticket Sales - ' . Money::JPY($totalSales))
            ->setHeight(350)
            ->addData('Sales', $salesData['sales'])
            ->setXAxis($salesData['days'])
            ->setColors(['#cddaea'])
            ->setOptions($options)
            ->toVue();

Thank you

marineusde commented 4 months ago

In the documentation of apexcharts "formatter" is a js callback function:

https://apexcharts.com/docs/options/yaxis/

It worked this example:

        $options = [
            'yaxis' => [
                'labels' => [
                    'formatter: function(val, index) {
                        return val.toFixed(2);
                    }'
                ],
            ],
        ];

In my own code I

lukejames1111 commented 4 months ago

I too am struggling with the ->setOptions($options) feature. I'm trying to do something similar:

$options = [
    'yaxis' => [
        'decimalsInFloat' => 0,
    ]
];
$chart = (new OriginalLineChart)
    ->addData('Total Viewings', $weeklyViewings)
    ->addData('Trend Line', $trendLine)
    ->setXAxis($labels)
    ->setGrid()
    ->setStroke(2, [], 'smooth')
    ->setHeight(300)
    ->setOptions($options)
;

But every time I try to do that I get Call to undefined method ArielMejiaDev\LarapexCharts\Charts\LineChart::setOptions()

smitmartijn commented 4 months ago

If you're seeing Call to undefined method - maybe you've got an older version installed. But, the setOptions doesn't seem to do anything, except set options within the class - but then those options aren't called on when rendering the chart.

@pandigresik made this commit: https://github.com/ArielMejiaDev/larapex-charts/commit/480d8f7df6ddb2d0086cf4f16ad638a0e2218992 to add options override support, but that didn't change the rendering of the script in https://github.com/ArielMejiaDev/larapex-charts/blob/master/stubs/resources/views/chart/script.blade.php

I'm going to try and make setOptions effective, then submit a PR.

lukejames1111 commented 4 months ago

I saw the commit, but I only installed this a couple of weeks ago. The only way that I could figure out how to change the options was to edit them directly unfortunately.