ArielMejiaDev / larapex-charts

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

setLabels doesn't work with donut chart #83

Open rohan-nerdster opened 9 months ago

rohan-nerdster commented 9 months ago

The chart still shows the default legend instead of using the given array of labels

image
ArielMejiaDev commented 7 months ago

Thanks for adding the issue, I am going to check this as soon as possible.

chimit commented 7 months ago

The same with PieChart.

chimit commented 7 months ago

@ArielMejiaDev can you, please, have a look? Pie and Donut charts are completely useless without working labels.

lukejames1111 commented 6 months ago

How are you populating the labels? It works fine for me using a donut chart:

->setLabels(['label one', 'label two']);

chimit commented 6 months ago

Yes, exactly like this:

app(LarapexChart::class)->donutChart()
            ->addData([1, 2])
            ->setLabels(['asd', 'dsa']);
image
lukejames1111 commented 6 months ago

Hmm, only difference I have is that I'm calling it slightly differently, but this works for me:

    public function build(array $data, array $labels): \ArielMejiaDev\LarapexCharts\DonutChart
    {
        return $this->chart->donutChart()
            ->setTitle('Valuations Outcome')
            ->addData($data)
            ->setLabels($labels);
    }
chimit commented 6 months ago

No, nothing helps.

public function build(): \ArielMejiaDev\LarapexCharts\DonutChart
{
    return (new LarapexChart)->donutChart()
        ->setTitle('Valuations Outcome')
        ->addData([1, 2, 3, 4])
        ->setLabels(['Label 1', 'Label 2', 'Label 3', 'Label 4']);
}

Are you on the latest version of Larapex charts?

lukejames1111 commented 6 months ago

Yes, installed it today. Did you create the chart using php artisan make:chart ChartName? Can you share your app/Charts and your method which you use to call it?

chimit commented 6 months ago

I'm using Livewire:

// app/Livewire/MyComponent.php

<?php

namespace App\Livewire;

use ArielMejiaDev\LarapexCharts\DonutChart;
use ArielMejiaDev\LarapexCharts\LarapexChart;
use Livewire\Component;

class MyComponent extends Component
{
    public function build(): DonutChart
    {
        return app(LarapexChart::class)->donutChart()
            ->setTitle('Some title')
            ->addData([1, 2, 3])
            ->setLabels(['Label 1', 'Label 2', 'Label 3']);
    }

    public function render()
    {
        return view('livewire.my-component', ['chart' => $this->build()]);
    }
}
<!-- resources/views/livewire/my-component.blade.php -->

<div>
    {!! $chart->container() !!}

    {{ $chart->script() }}
</div>

The same approach works well with other types of charts.

HubertAlva commented 6 months ago

Im having the same issue

blisstechllc commented 6 months ago

Yes I am having the same issue with pie charts.

ArielMejiaDev commented 6 months ago

I would check this issue, sorry for the delay, thanks

blisstechllc commented 6 months ago

Sorry, even after updating to the newest version I am still not seeing the labels update for pie or donut charts.

public function build(): \ArielMejiaDev\LarapexCharts\PieChart
    {
        $reminder_sent = Reminder::where('sent', 'y')->count();
        $reminder_not_sent = Reminder::where('sent', 'n')->count();

        return $this->chart->pieChart()
            ->setTitle('Notifications sent.')
            ->setSubtitle('09/01/23 - '.Date("m/d/Y"))
            ->addData([$reminder_sent, $reminder_not_sent])
            ->setLabels(['Sent', 'Not Sent']);
    }
blisstechllc commented 6 months ago

I did notice if you remove the conditional on lines 23 and 25 in larapex-charts/stubs/resources/views/chart/script.blade.php the labels start to work.

nicosalvadore commented 5 months ago

Hello, I have the same issue, the labels are not set and the default values are displayed.

marineusde commented 4 months ago

I published a fork of the project some days ago and have done some codestyle, bugfixed ect. I tested your code in a laravel 10 project in my version 1.2.3, maybe you can use it too:

https://github.com/marineusde/larapex-charts

working code from @blisstechllc :

(new DonutChart())
            ->setTitle('Notifications sent.')
            ->setSubtitle('09/01/23 - '.Date("m/d/Y"))
            ->addData([5, 10])
            ->setLabels(['Sent', 'Not Sent']);

Bildschirmfoto vom 2024-04-24 12-25-40

cdterry87 commented 3 months ago

@ArielMejiaDev

From what I can tell, in stubs/resources/views/chart/script.blade.php the following code is what is causing this issue:

@if ($chart->labels())
    labels: {!! json_encode($chart->labels(), true) !!},
@endif

When it renders in the blade view by calling $myTable->script(), it generates the following code for the label:

...
<!--[if BLOCK]><![endif]-->            labels: ["My First Label","My Second Label", "My Third Label"],
        <!--[if ENDBLOCK]><![endif]-->
...

The simplest fix is just adding an extra line after the initial @if like this:

@if ($chart->labels())

    labels: {!! json_encode($chart->labels(), true) !!},
@endif

Removing the @if check also fixes the issue for my tables, but I'm not sure if that causes any other issues if labels aren't present. Also in this file are the stacked and stroke properties that may be having similar issues since they also have @if checks around them and can probably be resolved the same way I mentioned above.