Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setLabels doesn't work with donut chart #83

Open
rohan-nerdster opened this issue Nov 17, 2023 · 17 comments
Open

setLabels doesn't work with donut chart #83

rohan-nerdster opened this issue Nov 17, 2023 · 17 comments
Assignees

Comments

@rohan-nerdster
Copy link

rohan-nerdster commented Nov 17, 2023

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

@ArielMejiaDev ArielMejiaDev self-assigned this Jan 3, 2024
@ArielMejiaDev
Copy link
Owner

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

@chimit
Copy link
Contributor

chimit commented Jan 24, 2024

The same with PieChart.

@chimit
Copy link
Contributor

chimit commented Jan 31, 2024

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

@lukejames1111
Copy link

lukejames1111 commented Feb 2, 2024

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

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

@chimit
Copy link
Contributor

chimit commented Feb 2, 2024

Yes, exactly like this:

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

@lukejames1111
Copy link

lukejames1111 commented Feb 2, 2024

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
Copy link
Contributor

chimit commented Feb 2, 2024

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
Copy link

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
Copy link
Contributor

chimit commented Feb 6, 2024

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
Copy link

Im having the same issue

@blisstechllc
Copy link

Yes I am having the same issue with pie charts.

@ArielMejiaDev
Copy link
Owner

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

@blisstechllc
Copy link

blisstechllc commented Feb 22, 2024

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
Copy link

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
Copy link

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

@marineusde
Copy link
Contributor

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
Copy link

cdterry87 commented May 22, 2024

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants