-
Notifications
You must be signed in to change notification settings - Fork 0
/
barChart.php
86 lines (77 loc) · 2.88 KB
/
barChart.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
// 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 = [];
foreach ($_chart->seriesLabels as $s) {
$dataSeriesLabels[] = new PHPExcel_Chart_DataSeriesValues(
$_chart->seriesLabelsDataType, $sheet->name . '!' . $s, NULL, $_chart->seriesLabelsRowNumber
);
}
// Set the X-Axis Labels
// Datatype
// Cell reference for data
// Format Code
// Number of datapoints in series
// Data values
// Data Marker
$xAxisTickValues = [];
foreach ($_chart->xAxisTickValues as $s) {
$xAxisTickValues[] = new PHPExcel_Chart_DataSeriesValues(
$_chart->xAxisTickValuesDataType, $sheet->name . '!' . $s, NULL, $_chart->xAxisTickValuesRowNumber
);
}
// 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$5', NULL, 4),
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$5', NULL, 4),
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$D$2:$D$5', NULL, 4),
); */
$dataSeriesValues = [];
foreach ($_chart->seriesValues as $s) {
$dataSeriesValues[] = new PHPExcel_Chart_DataSeriesValues($_chart->seriesValuesDataType, $sheet->name . '!' . $s, NULL, $_chart->seriesValuesRowNumber);
}
// Build the dataseries
$series = new PHPExcel_Chart_DataSeries(
PHPExcel_Chart_DataSeries::TYPE_BARCHART, // plotType
PHPExcel_Chart_DataSeries::GROUPING_CLUSTERED, // plotGrouping
range(0, count($dataSeriesValues) - 1), // plotOrder
$dataSeriesLabels, // plotLabel
$xAxisTickValues, // plotCategory
$dataSeriesValues // plotValues
);
// Set additional dataseries parameters
// Make it a horizontal bar rather than a vertical column graph
$series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_VERTICAL);
// 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($_chart->valueTitle);
// Create the chart
$chart = new PHPExcel_Chart(
'chart1', // name
$title, // title
$legend, // legend
$plotArea, // plotArea
true, // plotVisibleOnly
0, // displayBlanksAs
NULL, // xAxisLabel
$yAxisLabel // yAxisLabel
);
// Set the position where the chart should appear in the worksheet
$chart->setTopLeftPosition($_chart->topLeftPosition);
$chart->setBottomRightPosition($_chart->bottomRightPosition);
// Add the chart to the worksheet
$activeSheet->addChart($chart);