我试图根据时间尺度创建一些包含多个系列的散点图,并且我有一些问题。请注意,每个系列的时间戳可能不同,因此需要使用散点图。
我会以相反的顺序回溯,但请考虑以下代码:
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
require_once __DIR__.'/phpexcel/Classes/PHPExcel.php';
require_once __DIR__.'/phpexcel/Classes/PHPExcel/Cell/AdvancedValueBinder.php';
PHPExcel_Cell::setValueBinder( new PHPExcel_Cell_AdvancedValueBinder() );
$objPHPExcel = new PHPExcel();
$objWorksheet = $objPHPExcel->getActiveSheet();
$objWorksheet->fromArray(
array(
array('','SERIES_1','SERIES_2'),
array('2014-05-01 00:00:03',12574,''),
array('2014-05-01 04:10:01',7347,''),
array('2014-05-02 09:20:01',4175,''),
array('2014-05-03 21:30:03',1045,''),
array('2014-05-01 07:05:03','',8544),
array('2014-05-02 12:15:01','',1324),
array('2014-05-02 16:25:01','',6729),
array('2014-05-03 15:35:03','',9420),
)
);
$objWorksheet->getStyle('A2:A9')
->getNumberFormat()
->setFormatCode('yyyy-mm-dd hh:mm:ss');
// Set the Labels for each data series we want to plot
// Datatype
// Cell reference for data
// Format Code
// Number of datapoints in series
// Data values
// Data Marker
$dataseriesLabels = array(
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$B$1', NULL, 1),
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', NULL, 1),
);
// Set the X-Axis Labels
$xAxisTickValues = array(
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$9', NULL, 8),
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$9', NULL, 8),
);
// Set the Data values for each data series we want to plot
// Datatype
// Cell reference for data
// Format Code
// Number of datapoints in series
// Data values
// Data Marker
$dataSeriesValues = array(
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$2:$B$9', NULL, 8),
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$9', NULL, 8)
);
// Build the dataseries
$series = new PHPExcel_Chart_DataSeries(
PHPExcel_Chart_DataSeries::TYPE_SCATTERCHART, // plotType
NULL, // plotGrouping (Scatter charts don't have any grouping)
range(0, count($dataSeriesValues)-1), // plotOrder
$dataseriesLabels, // plotLabel
$xAxisTickValues, // plotCategory
$dataSeriesValues, // plotValues
NULL, // smooth line
PHPExcel_Chart_DataSeries::STYLE_LINEMARKER // plotStyle
);
// Set the series in the plot area
$plotarea = new PHPExcel_Chart_PlotArea(NULL, array($series));
// Set the chart legend
$legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false);
$title = new PHPExcel_Chart_Title('CHART_TITLE');
$yAxisLabel = new PHPExcel_Chart_Title('Y_AXIS_TITLE');
$xAxisLabel = new PHPExcel_Chart_Title('X_AXIS_TITLE');
// Create the chart
$chart = new PHPExcel_Chart(
'chart1', // name
$title, // title
$legend, // legend
$plotarea, // plotArea
true, // plotVisibleOnly
0, // displayBlanksAs
$xAxisLabel, // xAxisLabel
$yAxisLabel // yAxisLabel
);
// Set the position where the chart should appear in the worksheet
$chart->setTopLeftPosition('E3');
$chart->setBottomRightPosition('N18');
// Add the chart to the worksheet
$objWorksheet->addChart($chart);
// Save Excel 2007 file
echo date('H:i:s') , " Write to Excel2007 format" , EOL;
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->setIncludeCharts(TRUE);
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
真正的数据最终将来自数据库,但现在我只是想在测试期间使事情发挥作用。这似乎有效,而我现在需要进行一些手动格式化(删除标记,更改线条粗细,刻度线上的位置轴,将数字格式更改为yyyy-mm-dd)图形似乎正确生成。
但是,如果我只使用一个数据系列,则图表会正确生成,但缺少x轴标签。刻度线位于正确的位置,只缺少标签。如下面的代码所示,我所做的就是注释掉第二组数据。
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
require_once __DIR__.'/phpexcel/Classes/PHPExcel.php';
require_once __DIR__.'/phpexcel/Classes/PHPExcel/Cell/AdvancedValueBinder.php';
PHPExcel_Cell::setValueBinder( new PHPExcel_Cell_AdvancedValueBinder() );
$objPHPExcel = new PHPExcel();
$objWorksheet = $objPHPExcel->getActiveSheet();
$objWorksheet->fromArray(
array(
array('','SERIES_1','SERIES_2'),
array('2014-05-01 00:00:03',12574,''),
array('2014-05-01 04:10:01',7347,''),
array('2014-05-02 09:20:01',4175,''),
array('2014-05-03 21:30:03',1045,''),
array('2014-05-01 07:05:03','',8544),
array('2014-05-02 12:15:01','',1324),
array('2014-05-02 16:25:01','',6729),
array('2014-05-03 15:35:03','',9420),
)
);
$objWorksheet->getStyle('A2:A9')
->getNumberFormat()
->setFormatCode('yyyy-mm-dd hh:mm:ss');
// Set the Labels for each data series we want to plot
// Datatype
// Cell reference for data
// Format Code
// Number of datapoints in series
// Data values
// Data Marker
$dataseriesLabels = array(
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$B$1', NULL, 1),
// new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1' NULL, 1),
);
// Set the X-Axis Labels
$xAxisTickValues = array(
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$9', NULL, 8),
// new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$9', NULL, 8),
);
// Set the Data values for each data series we want to plot
// Datatype
// Cell reference for data
// Format Code
// Number of datapoints in series
// Data values
// Data Marker
$dataSeriesValues = array(
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$2:$B$9', NULL, 8),
// new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$9', NULL, 8)
);
// Build the dataseries
$series = new PHPExcel_Chart_DataSeries(
PHPExcel_Chart_DataSeries::TYPE_SCATTERCHART, // plotType
NULL, // plotGrouping (Scatter charts don't have any grouping)
range(0, count($dataSeriesValues)-1), // plotOrder
$dataseriesLabels, // plotLabel
$xAxisTickValues, // plotCategory
$dataSeriesValues, // plotValues
NULL, // smooth line
PHPExcel_Chart_DataSeries::STYLE_LINEMARKER // plotStyle
);
// Set the series in the plot area
$plotarea = new PHPExcel_Chart_PlotArea(NULL, array($series));
// Set the chart legend
$legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false);
$title = new PHPExcel_Chart_Title('CHART_TITLE');
$yAxisLabel = new PHPExcel_Chart_Title('Y_AXIS_TITLE');
$xAxisLabel = new PHPExcel_Chart_Title('X_AXIS_TITLE');
// Create the chart
$chart = new PHPExcel_Chart(
'chart1', // name
$title, // title
$legend, // legend
$plotarea, // plotArea
true, // plotVisibleOnly
0, // displayBlanksAs
$xAxisLabel, // xAxisLabel
$yAxisLabel // yAxisLabel
);
// Set the position where the chart should appear in the worksheet
$chart->setTopLeftPosition('E3');
$chart->setBottomRightPosition('N18');
// Add the chart to the worksheet
$objWorksheet->addChart($chart);
// Save Excel 2007 file
echo date('H:i:s') , " Write to Excel2007 format" , EOL;
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->setIncludeCharts(TRUE);
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
如上所述,刻度线位于正确的位置,只缺少标签。
有关x轴标签为何不显示的任何想法?此外,如果我手动在Excel中创建散点图(插入&gt;&gt;图表&gt;&gt;散点图),我会得到一个带有x轴选项的时间刻度图表,以选择最小值,最大值,主要单位和次要单位,而图表来自PHPExcel似乎是一个类别/值类型,因为您只能选择刻度线和间隔单位之间的间隔。有没有办法改变这个?
编辑:忘记提及我已尝试同时使用PHPEx的1.8.0版本以及git上的最新开发分支。