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

Add ability to specify chart styles via JSON objects

Open AdrianBav opened this issue 2 years ago • 2 comments

Summary

Add ability to specify chart styles via JSON objects

Issue

Often it is nice to be able to utilize the full power of Apex Charts.
There are too many options to make a fluent helper for each one, so this solution uses a more dynamic approach.

$columnChartModel =
    (new ColumnChartModel())
        ->setTitle('Expenses by Type')
        ->addColumn('Food', 100, '#f6ad55')
        ->addColumn('Shopping', 200, '#fc8181')
        ->addColumn('Travel', 400, '#90cdf4')
        // Allow x-axis labels to be set at right angles
        ->stylesFromJson('{"xaxis": {"labels": {"rotate": -90}}}')
    ;
// Multiple lines can also be specified
->stylesFromJson('{
    "plotOptions": {"bar": {"dataLabels": {"position": "top"}}},
    "dataLabels": {"offsetY": -20, "style": {"colors": ["#304758"]}},
    "stroke": {"show": true, "width": 2, "colors": ["transparent"]}
}')

// For large amounts of customization, the styles can be loaded from a JSON file
->stylesFromFile('styles.json')

Number formatting on the chart axis requires a JavaScript callback.
The setNumberFormat fluent helper formatting numbers in three common ways:

$columnChartModel =
    (new ColumnChartModel())
        ->setTitle('Expenses by Type')
        ->addColumn('Food', 14, '#f6ad55')
        ->addColumn('Shopping', 30, '#fc8181')
        ->addColumn('Travel', 56, '#90cdf4')
        // Choose the number format for data lables and y-axis ('number', 'dollar', 'percentage')
        ->setNumberFormat('percentage')
    ;

Type of Change

  • [x] :rocket: New fluent helpers

AdrianBav avatar Jul 03 '22 19:07 AdrianBav

This is the one huge feature I am missing in this package. Thanks for your contribution it looks amazing.

engel-m avatar Jul 05 '22 07:07 engel-m

@asantibanez Any updates on when this can be merged? This is a crucial feature to allow complete customization of the charts. Thanks!

RishabSwift avatar Aug 08 '22 17:08 RishabSwift

can this be merged please? I need this so i can handle the borderRadius of the columns

laraben avatar Oct 09 '22 12:10 laraben

Why is this still not merged? :(

laraben avatar Nov 29 '22 22:11 laraben

Hello @AdrianBav

I love the idea but i would like to keep the library api simple for now. I thought once about having a setApexChartConfig method directly so you could pass in a JSON config and that would be applied instead of the defaults for each chart but it still sounds too complex.

Thanks again.

asantibanez avatar Feb 01 '23 18:02 asantibanez

@AdrianBav @engel-m @RishabSwift @laraben hello.

I am taking a different approach to achieve the same goal.

https://twitter.com/asantibanez/status/1620909755521974286

asantibanez avatar Feb 01 '23 23:02 asantibanez

hi,

imo this package does need this advanced customization option so one could pass some overrides that will get merged just before passing the data to the chart rendering, e.g. i need the title on top and not on the left, so please add an override mechanism

simonbuehler avatar Feb 10 '23 09:02 simonbuehler

@asantibanez

If the setJsonConfig() does the same thing as the PR, then that's great! About time :)

Unfortunately, I had to abandon this package because it was missing this much needed critical ability to customize.

RishabSwift avatar Feb 13 '23 17:02 RishabSwift

@asantibanez this version with the setJsonConfig method are currently available from the composer install? I need this feature, but even removing and reinstalling from the composer is not available to me.

marcioelias avatar Jun 20 '23 13:06 marcioelias