如何在 PHPExcel 图表中发送静态工作表数据
how to send worksheet data static in PHPExcel chart
我以 PHPExcel-1.8 的 33chartcreate-pie.php 为例,并根据我的需要进行更改。现在这个例子有像这样的 x 轴值。
$xAxisTickValues1 = array(
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A:$A', NULL, 4), // Q1 to Q4
);
现在,我想将其静态添加为 PASS 和 FAIL,所以,我试一试
$xAxisTickValues1 = array(
new PHPExcel_Chart_DataSeriesValues('String', 'PASS:FAIL', NULL, 2), // Q1 to Q4
);
但是,它不起作用。如何将静态值放入该数组?
您效仿的例子:
$xAxisTickValues1 = array(
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A:$A', NULL, 4), // Q1 to Q4
);
Here Worksheet 是 sheet 的标题。因此,在绘制图形时,它将从指定的标题中读取值,然后是 A2 到 A5 的列。
如果您只是想添加静态值。创建其中包含静态内容的单元格,并将单元格索引传递给数据系列值。
我们需要在单元格中显示静态标签,以便在图形或饼图上显示它们。
在绘制图形时,我们可以使用 setShowVal、setShowCatName 等函数显示和隐藏标签或值。
$objPHPExcel = new PHPExcel();
$objWorksheet = $objPHPExcel->getActiveSheet();
$objWorksheet->fromArray(
array(
array('', 2010, 2011, 2012),
array('PASS', 12, 15, 21),
array('FAIL', 56, 73, 86)
)
);
$dataSeriesLabels1 = array(
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C', NULL, 1), // 2011
);
$xAxisTickValues1 = array(
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A:$A', NULL, 2), // Q1 to Q4
);
$dataSeriesValues1 = array(
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C:$C', NULL, 2),
);
// Build the dataseries
$series1 = new PHPExcel_Chart_DataSeries(
PHPExcel_Chart_DataSeries::TYPE_PIECHART, // plotType
NULL, // plotGrouping (Pie charts don't have any grouping)
range(0, count($dataSeriesValues1)-1), // plotOrder
$dataSeriesLabels1, // plotLabel
$xAxisTickValues1, // plotCategory
$dataSeriesValues1 // plotValues
);
// Set up a layout object for the Pie chart
$layout1 = new PHPExcel_Chart_Layout();
/*$layout1->setShowVal(TRUE);*/
$layout1->setShowCatName(TRUE);
// Set the series in the plot area
$plotArea1 = new PHPExcel_Chart_PlotArea($layout1, array($series1));
// Set the chart legend
$legend1 = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false);
$title1 = new PHPExcel_Chart_Title('Test Pie Chart');
// Create the chart
$chart1 = new PHPExcel_Chart(
'chart1', // name
$title1, // title
$legend1, // legend
$plotArea1, // plotArea
true, // plotVisibleOnly
0, // displayBlanksAs
NULL, // xAxisLabel
NULL // yAxisLabel - Pie charts don't have a Y-Axis
);
像
一样修改你的声明数组
array(
array('', 2010, 2011, 2012),
array('Q1', 12, 15, 21),
array('Pass or fail', 56, 73, 86)
)
。这是设置静态值的方法。
现在要显示它,您可以将 属性 添加到您的布局以显示带有 ->setShowCatName(TRUE);
的图表的类别名称
我以 PHPExcel-1.8 的 33chartcreate-pie.php 为例,并根据我的需要进行更改。现在这个例子有像这样的 x 轴值。
$xAxisTickValues1 = array(
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A:$A', NULL, 4), // Q1 to Q4
);
现在,我想将其静态添加为 PASS 和 FAIL,所以,我试一试
$xAxisTickValues1 = array(
new PHPExcel_Chart_DataSeriesValues('String', 'PASS:FAIL', NULL, 2), // Q1 to Q4
);
但是,它不起作用。如何将静态值放入该数组?
您效仿的例子:
$xAxisTickValues1 = array(
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A:$A', NULL, 4), // Q1 to Q4
);
Here Worksheet 是 sheet 的标题。因此,在绘制图形时,它将从指定的标题中读取值,然后是 A2 到 A5 的列。
如果您只是想添加静态值。创建其中包含静态内容的单元格,并将单元格索引传递给数据系列值。
我们需要在单元格中显示静态标签,以便在图形或饼图上显示它们。 在绘制图形时,我们可以使用 setShowVal、setShowCatName 等函数显示和隐藏标签或值。
$objPHPExcel = new PHPExcel();
$objWorksheet = $objPHPExcel->getActiveSheet();
$objWorksheet->fromArray(
array(
array('', 2010, 2011, 2012),
array('PASS', 12, 15, 21),
array('FAIL', 56, 73, 86)
)
);
$dataSeriesLabels1 = array(
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C', NULL, 1), // 2011
);
$xAxisTickValues1 = array(
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A:$A', NULL, 2), // Q1 to Q4
);
$dataSeriesValues1 = array(
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C:$C', NULL, 2),
);
// Build the dataseries
$series1 = new PHPExcel_Chart_DataSeries(
PHPExcel_Chart_DataSeries::TYPE_PIECHART, // plotType
NULL, // plotGrouping (Pie charts don't have any grouping)
range(0, count($dataSeriesValues1)-1), // plotOrder
$dataSeriesLabels1, // plotLabel
$xAxisTickValues1, // plotCategory
$dataSeriesValues1 // plotValues
);
// Set up a layout object for the Pie chart
$layout1 = new PHPExcel_Chart_Layout();
/*$layout1->setShowVal(TRUE);*/
$layout1->setShowCatName(TRUE);
// Set the series in the plot area
$plotArea1 = new PHPExcel_Chart_PlotArea($layout1, array($series1));
// Set the chart legend
$legend1 = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false);
$title1 = new PHPExcel_Chart_Title('Test Pie Chart');
// Create the chart
$chart1 = new PHPExcel_Chart(
'chart1', // name
$title1, // title
$legend1, // legend
$plotArea1, // plotArea
true, // plotVisibleOnly
0, // displayBlanksAs
NULL, // xAxisLabel
NULL // yAxisLabel - Pie charts don't have a Y-Axis
);
像
一样修改你的声明数组array(
array('', 2010, 2011, 2012),
array('Q1', 12, 15, 21),
array('Pass or fail', 56, 73, 86)
)
。这是设置静态值的方法。
现在要显示它,您可以将 属性 添加到您的布局以显示带有 ->setShowCatName(TRUE);