Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

symphony28's avatar

ChartJs with Laravel Livewire

Hello, i am a beginner and i work on a task with Laravel, Livewire and ChartJS. I want to be able to show in the chart the Dividend Yield DAILY form a company, from the last 20 years. The formula is like that : The dividend / The price * 100. The problem i am facing is that the dividend from that company is given once in 3 months, but the price value is daily, for an example Let's say on 1st Jan 1990 the dividend is 5 and the price on that day is 100, the dividend yield from 1st Jan 1990 will be 5%. And now the problem : on 2nd Jan 1990 the dividend will be the same, but the price will change and that dividend will remain the same till 1st Apr 1990. I am working with endpoitns API which you can find below. The arrays are not the same length, how can i put the condition for that first dividend to be divided with daily prices until the company offer the next dividend, and that one also to divide every daily prices and so on. I left the code (I do not believe that it's good also), so any advice will be very helpful. Thank you for your time!

public array $labels = [];

public array $datasets = [];

public function mount()
{           
    $this->labels = $this->getDate();
    $this->datasets = [
        [
            'label' => 'Dividend yiled',
            'backgroundColor' => 'rgb(255, 99, 132)',
            'borderColor' => 'rgb(255, 99, 132)',
            'data' => $this->getDividendYield(),
        ],
    ];
}

public function render()
{
    return view('livewire.charts');
}

private  function getDate()
{
    $labels = [];
    $response1 = Http::get('https://financialmodelingprep.com/api/v3/historical-price-full/AAPL?serietype=line&apikey=9d356e0853e8c35e63893849ce5c2de5');
    $response1->throw();
    foreach($response1->json()['historical'] as $date){
        $labels [] =  $date['date'];
    } 
    return $labels;

}

private function getDividend()
{
    $dividend = [];
    $response = Http::get('https://financialmodelingprep.com/api/v3/historical-price-full/stock_dividend/AAPL?apikey=9d356e0853e8c35e63893849ce5c2de5');
    $response->throw();
    foreach($response->json()('historical') as $stock){
        $dividend [] = $stock['dividend'];
    }
    return $dividend;
}

private function getPrice()
{
    $price = [];
    $response = Http::get('https://financialmodelingprep.com/api/v3/historical-price-full/AAPL?serietype=line&apikey=9d356e0853e8c35e63893849ce5c2de5');
    $response->throw();
    foreach($response->json()['historical'] as $stockPrice){
        $price [] = $stockPrice['close'];
    }   
    return $price;
}

private function getDividendYield()
{
    // find out the answer
}
0 likes
0 replies

Please or to participate in this conversation.