larapex-charts icon indicating copy to clipboard operation
larapex-charts copied to clipboard

The right syntax to add options

Open romain-lgr opened this issue 1 year ago • 4 comments

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

romain-lgr avatar Mar 08 '24 02:03 romain-lgr

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

marineusde avatar Apr 24 '24 11:04 marineusde

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()

lukejames1111 avatar May 07 '24 00:05 lukejames1111

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.

smitmartijn avatar May 07 '24 09:05 smitmartijn

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.

lukejames1111 avatar May 07 '24 15:05 lukejames1111