首页 > 解决方案 > 如何在phpspreadsheet中使用linest函数?

问题描述

我正在使用 33_Chart_create_scatter.php 生成 XY 散点图。我即将完成。我剩下的就是诱导最简单的功能。有人可以提供一些关于在phpspreadsheet中使用linest的代码吗?

我已经做了很多谷歌,没有运气。大量访问 phpspreadsheet 文档。信息量很大,看不懂。到目前为止我还没有使用过类,所以我不知道如何形成代码来与下面代码部分中显示的类进行对话?这是我使用的数据: $xAxisTickValues = [ new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$1:$C$18', null, 18), new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$F$1 :$F$18', null, 18) [ ][1];

\PhpOffice\PhpSpreadsheet\Calculation\Statistical::LINEST


    'LINEST' => [
        'category' => Category::CATEGORY_STATISTICAL,
        'functionCall' => [Statistical::class, 'LINEST'],
        'argumentCount' => '1-4',
    ],

公共静态函数 LINEST($yValues, $xValues = null, $const = true, $stats = false) { $const = ($const === null) ?true : (bool) 函数::flattenSingleValue($const); $stats = ($stats === null) ? false : (bool) 函数::flattenSingleValue($stats); if ($xValues === null) { $xValues = range(1, count(Functions::flattenArray($yValues))); }

当然,我还没有结果。我的期望是返回最简单的统计数据。

一个星期没有人添加任何东西。以下是我在那一周完成的工作:我通过对 33 Chart create scatter 中的数据运行公式 wizzard/linest 得到以下两个数据数组。我做的图表。所以,我知道我想要从 phpspreadsheet/linet 得到什么数据。

Six foot data array
0.798178535 18.35040936
0.012101577 0.241020964
0.996335545 0.53274435
4350.269442 16
1234.67843  4.541064671

Ten foot data array
0.819287926 16.98988304
0.007826008 0.15586663
0.998542214 0.344522174
10959.54502 16
1300.848983 1.899128449

setCellValue, puts the formula in cell A32 and  cell A32 displays the first number from the Six foot data array above.

$helper->log, gives the following output. The getCell gives the linest     formula in the cell.
getCalculatedValue gives the Last value in the Six foot data array above.

    19:12:47 Write Xlsx format to 33_Chart_create_scatter.xlsx in 0.0138  seconds
    19:12:47 Value of linest six foot from getCell(A32) : =LINEST(D1:D18 ,    C1:C18 , TRUE, TRUE)
    19:12:47 Value of linest six foot used getCalculatedValue get cell A32 : 4.5410646714826
    19:12:47 Value of linest ten foot from getCell(A42) : =LINEST(G1:G18 ,  F1:F18 , TRUE, TRUE)
   19:12:47 Value of linest ten foot used getCalculatedValue get cell A42 : 1.8991284485724

我试过

   $spreadsheet->getActiveSheet()->fromArray($linestSix ,null,'C32'); 

这与上面描述的 setCellValue 相同。点击此链接,在此处输入链接描述,以查看我运行代码生成图表时的浏览器输出,33 Chart create scatter。向下进入对象的大约 75 行到 [Worksheet!A32] ,有我的数据,但是,我如何将它从那里取出?我是从下面 $linet6 的 print_r 中得到的。

    $linest6 = $spreadsheet->getActiveSheet()->setCellValue('A32',     '=LINEST(D1:D18 , C1:C18 , TRUE, TRUE)');

   echo 'The six foot data array : <br>';
   echo '<pre>';
   print_r($linest6);
   echo '</pre>';

有人,也许是马克贝克,会帮我从最简单的地方获取统计数据,就像我上面从公式向导中描述的那样吗?谢谢你。

标签: phpphpspreadsheet

解决方案


我终于明白了!!多次访问谷歌。查看如何从类函数中检索数组。这是我的最终脚本。

    $stats = new Statistical();
$statArr6 = $stats->LINEST($dataArraySixY, $dataArraySixX, TRUE, TRUE);
$statArr10 = $stats->LINEST($dataArrayTenY, $dataArrayTenX, TRUE, TRUE);


$worksheet->fromArray(
    $statArr6, 
    NULL, 
    "C22");
$worksheet->fromArray(
$statArr10,
NULL,
'C32'
);

我有更多的东西可以显示我想要的信息,但我有数组。


推荐阅读