使用phpspreadsheet创建组合图表并设置次坐标轴和x轴坐标间隔
本文介绍如何使用PhpSpreadsheet库创建组合图表,并设置次坐标轴以及X轴坐标间隔。
步骤:
创建图表对象:
$chart = new PhpOfficePhpSpreadsheetChartChart();
登录后复制添加数据系列:
创建数据系列,并指定哪些系列使用次坐标轴。 以下代码示例展示了如何添加一个使用百分比值(次坐标轴)和一个使用数值(主坐标轴)的数据系列。
立即学习“PHP免费学习笔记(深入)”;
$dataSeriesValues = [ new PhpOfficePhpSpreadsheetChartDataSeriesValues('Number', 'Worksheet!$A$1:$A$5'), // 主坐标轴数据 new PhpOfficePhpSpreadsheetChartDataSeriesValues('Percentage', 'Worksheet!$B$1:$B$5') // 次坐标轴数据];$xAxisTickValues = [ new PhpOfficePhpSpreadsheetChartDataSeriesValues('String', 'Worksheet!$C$1:$C$5') // X轴分类数据];$series = new PhpOfficePhpSpreadsheetChartSeries( $dataSeriesValues, $xAxisTickValues);$series->setPlotDirection(PhpOfficePhpSpreadsheetChartSeries::PLOT_DIRECTION_HORIZONTAL); // 指定次坐标轴$chart->addSeries($series);$series = new PhpOfficePhpSpreadsheetChartSeries( new PhpOfficePhpSpreadsheetChartDataSeriesValues('Number', 'Worksheet!$A$1:$A$5') // 主坐标轴数据);$series->setPlotDirection(PhpOfficePhpSpreadsheetChartSeries::PLOT_DIRECTION_VERTICAL); // 指定主坐标轴$chart->addSeries($series);
登录后复制设置次坐标轴:
$layoutSecondaryAxes = new PhpOfficePhpSpreadsheetChartLayoutLayout();$layoutSecondaryAxes->setShowAxis(true);$yAxisSecondary = new PhpOfficePhpSpreadsheetChartAxis();$yAxisSecondary->setLayout($layoutSecondaryAxes);$chart->addAxis($yAxisSecondary, PhpOfficePhpSpreadsheetChartAxis::VERTICAL, $layoutSecondaryAxes); // 添加次坐标轴
登录后复制设置X轴坐标间隔:
$layoutXAxis = new PhpOfficePhpSpreadsheetChartLayoutLayout();$layoutXAxis->setTickValFrequency(1); // 设置X轴坐标间隔为1$xAxis = new PhpOfficePhpSpreadsheetChartAxis();$xAxis->setLayout($layoutXAxis);$chart->addAxis($xAxis, PhpOfficePhpSpreadsheetChartAxis::HORIZONTAL); // 添加X轴
登录后复制
记住替换 ‘Worksheet!’ 为你的工作表名称。 通过调整 setTickValFrequency() 的值,可以控制X轴坐标的间隔。 这个例子中设置为1,表示每个数据点都显示在X轴上。
最后,将图表添加到工作表中:
$worksheet->addChart($chart);
登录后复制
通过以上步骤,您可以成功地使用PhpSpreadsheet创建具有次坐标轴和自定义X轴坐标间隔的组合图表。 请确保您已正确安装并包含PhpSpreadsheet库。
以上就是如何用PhpSpreadsheet设置组合图表的次坐标轴和X轴坐标间隔?的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2497891.html