ChartJS 如何将两个长度不同的数组中的值相除?

5anewei6  于 2022-11-06  发布在  Chart.js
关注(0)|答案(2)|浏览(158)

我试图在laravel项目中使用ChartJs从一家公司获取过去20年的DividendYield。数组来自HTTP客户端API。公式如下:$dividendPayed / $dailyPrice * 100。我面临的问题是,$dividendPayed是三个月一次,因此它使数组比包含每日价格的数组短。

private function getDividend()
{
    $dividend = [];
    $response = Http::get('https://some-endpoint-for-once-in-3-months-dividend');
    $response->throw();
    foreach($response->json('historical') as $stock){
        $dividend [] = $stock['dividend'];
    }
    return $dividend;

   //THIS ARRAY RETURNS LET'S SAY FOR EXAMPLE 50 RESULTS
   // $dividend = [
    0 => 0.23
    1 => 0.23
    2 => 0.22
    3 => 0.22
    ..........
    50 => .43
                  ]

}

private function getPrice()
{
    $price = [];
    $response = Http::get('https://some-endpoint-for-daily-prices');
    $response->throw();
    foreach($response->json('historical') as $stockPrice){
        $price [] = $stockPrice['close'];
    }   
    return $price;
}

 //THIS ARRAY RETURNS LET'S SAY FOR EXAMPLE 240 RESULTS
   // $price = [
    0 => 147.27
    1 => 143.39
    2 => 143.86
    3 => 143.75
    4 => 142.41
    5 => 138.38
    ..........
    240 => 300.43
                  ]

我还必须提到,图表中标签的“日期”(过去20年的逐日)是从与$dailyPrice相同的端点获取的。

szqfcxe2

szqfcxe21#

由于您没有提到来自https://some-endpoint-for-once-in-3-months-dividend和https://some-endpoint-for-daily-prices的完整响应,我假设响应中存在一个日期键,例如

['historical'=>[['date'=>'2022-03-03','price'=>0.2]];

用于获取API响应

private function fetchApiData(string $api,string $dateKey,string $priceKey,string $mainRespKey='historical'){
   $data = [];
   $response = Http::get($api);
   $response->throw();
   return $response->json($mainRespKey);}

用于计算股息收益率

public function DividendYield(){
   $dividend   = $this->fetchApiData('https://some-endpoint-for-once-in-3-months-dividend','date','dividend');

   $dailyPrice = $this->fetchApiData('https://some-endpoint-for-daily-prices','date','price');

   if(!empty($dividend)){
       $dividendColl  = collect($dividend);
       return collect($dailyPrice)->whereIn('date',$dividendColl->pluck('date'))
                                         ->map(fn($price)=>$dividendColl->where('date',$price['date'])
                                                                        ->map(fn($dividend)=> ['date'=>$price['date'],
                                                                                               'DividendYield'=>(($dividend['dividend']/$price['price'])*100)]))->collapse();     
   }}
oo7oh9g9

oo7oh9g92#

因此,首先您需要按月份对您的$price进行分组,假设您的评论中的dd()就是price,方法是:

$price = collect($price)->groupBy(function($val) {
      return Carbon::parse($val->date)->format('m');
});

则需要循环$dividend

$result = array();
foreach ($dividend as $key => $value) {
    $mounthResult = array();
    foreach ($price[key] as $p) {
// assuming dividend is the price you mean
        array_push($mounthResult, $value/$p->dividend*100);
    }
    array_push($result, $temp);
}

$price[key]表示我们想要的月份组,因此如果key = 0,则意味着我们正在循环第一个月份。
我希望我得到你的权利,随时评论这一点,以帮助我了解你更多,如果我错了。

相关问题