如何用PhpSpreadsheet设置组合图表的次坐标轴和X轴坐标间隔?

霞舞
发布: 2025-02-23 23:06:01
原创
1150人浏览过

使用phpspreadsheet创建组合图表并设置次坐标轴和x轴坐标间隔

本文介绍如何使用PhpSpreadsheet库创建组合图表,并设置次坐标轴以及X轴坐标间隔。

如何用PhpSpreadsheet设置组合图表的次坐标轴和X轴坐标间隔?

步骤:

  1. 创建图表对象:
<code class="php">$chart = new \PhpOffice\PhpSpreadsheet\Chart\Chart();</code>
登录后复制
  1. 添加数据系列:

创建数据系列,并指定哪些系列使用次坐标轴。 以下代码示例展示了如何添加一个使用百分比值(次坐标轴)和一个使用数值(主坐标轴)的数据系列。

立即学习PHP免费学习笔记(深入)”;

AI Room Planner
AI Room Planner

AI 室内设计工具,免费为您的房间提供上百种设计方案

AI Room Planner 91
查看详情 AI Room Planner
<code class="php">$dataSeriesValues = [
    new \PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues('Number', 'Worksheet!$A$1:$A$5'), // 主坐标轴数据
    new \PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues('Percentage', 'Worksheet!$B$1:$B$5') // 次坐标轴数据
];

$xAxisTickValues = [
    new \PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues('String', 'Worksheet!$C$1:$C$5') // X轴分类数据
];

$series = new \PhpOffice\PhpSpreadsheet\Chart\Series(
    $dataSeriesValues,
    $xAxisTickValues
);

$series->setPlotDirection(\PhpOffice\PhpSpreadsheet\Chart\Series::PLOT_DIRECTION_HORIZONTAL); // 指定次坐标轴
$chart->addSeries($series);


$series = new \PhpOffice\PhpSpreadsheet\Chart\Series(
    new \PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues('Number', 'Worksheet!$A$1:$A$5') // 主坐标轴数据
);

$series->setPlotDirection(\PhpOffice\PhpSpreadsheet\Chart\Series::PLOT_DIRECTION_VERTICAL); // 指定主坐标轴
$chart->addSeries($series);</code>
登录后复制
  1. 设置次坐标轴:
<code class="php">$layoutSecondaryAxes = new \PhpOffice\PhpSpreadsheet\Chart\Layout\Layout();
$layoutSecondaryAxes->setShowAxis(true);

$yAxisSecondary = new \PhpOffice\PhpSpreadsheet\Chart\Axis();
$yAxisSecondary->setLayout($layoutSecondaryAxes);

$chart->addAxis($yAxisSecondary, \PhpOffice\PhpSpreadsheet\Chart\Axis::VERTICAL, $layoutSecondaryAxes); // 添加次坐标轴</code>
登录后复制
  1. 设置X轴坐标间隔:
<code class="php">$layoutXAxis = new \PhpOffice\PhpSpreadsheet\Chart\Layout\Layout();
$layoutXAxis->setTickValFrequency(1); // 设置X轴坐标间隔为1

$xAxis = new \PhpOffice\PhpSpreadsheet\Chart\Axis();
$xAxis->setLayout($layoutXAxis);

$chart->addAxis($xAxis, \PhpOffice\PhpSpreadsheet\Chart\Axis::HORIZONTAL); // 添加X轴</code>
登录后复制

记住替换 'Worksheet!' 为你的工作表名称。 通过调整 setTickValFrequency() 的值,可以控制X轴坐标的间隔。 这个例子中设置为1,表示每个数据点都显示在X轴上。

最后,将图表添加到工作表中:

<code class="php">$worksheet->addChart($chart);</code>
登录后复制

通过以上步骤,您可以成功地使用PhpSpreadsheet创建具有次坐标轴和自定义X轴坐标间隔的组合图表。 请确保您已正确安装并包含PhpSpreadsheet库。

以上就是如何用PhpSpreadsheet设置组合图表的次坐标轴和X轴坐标间隔?的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号