Some Questions about PHPExcel Charts - php

I am using PHPExcel for generating charts in Excel Files. I have some question whoes answer I couldn't find even after lot of R&D those are
How can I show data labels to chart?
How can I control the width of bar charts?
How can I customize colors of bar charts?
I tried to show labels with layout class like :
$layout = new PHPExcel_Chart_Layout();
$layout->setShowVal(TRUE);
But no success.
I have also explored DataSeries class and Chart Class but couldn't find any solution. Any body here who have already done such tasks, Please guide.
Best Regards.

With this:
$dataseriesLabels = array(
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$B$1', NULL, 1),
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', NULL, 1),
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$1', NULL, 1),
);
$series = new PHPExcel_Chart_DataSeries(
PHPExcel_Chart_DataSeries::TYPE_BARCHART, // plotType
PHPExcel_Chart_DataSeries::GROUPING_STANDARD, // plotGrouping
range(0, count($dataSeriesValues)-1), // plotOrder
$dataseriesLabels, // plotLabel
$xAxisTickValues, // plotCategory
$dataSeriesValues // plotValues
);
you can define the dataseriesLabels to use in your chart
PHPExcel set default Column Width
PhpExcel Bar Chart

Read my answer there may be it will help with charts
and yes better to create what you need and than use it with load and save(as template)
PHPExcel removes chart style when template is loaded

Related

PHPExcel Piechart legends with fix length

I have issue in setting the width of the legend lables.
I can show them but not able to show them as per expectation.
Currently it is looking like this
I want the Legend to be displayed like this
My code
//NOTE Layer for charts
$layout=new \PHPExcel_Chart_Layout();
$layout->setShowVal(true);
$layout->setShowPercent(true);
// Set series in the plot area
$pa=new \PHPExcel_Chart_PlotArea($layout, array($ds));
// Set legend
$legend=new \PHPExcel_Chart_Legend(\PHPExcel_Chart_Legend::POSITION_BOTTOM, NULL, false);
//Create Chart
$chart= new \PHPExcel_Chart('chart1',$title,$legend,$pa,true,0,NULL, NULL);
$chart->setTopLeftPosition('K'.$chart_index);
$chart->setBottomRightPosition('S'.($chart_index+20));
$ews2->addChart($chart);
I have got the solution for my problem.
I have just created array with expected label;
$pie_label = array($ea->getActiveSheet()->getCell("E".$i)->getValue(),$ea->getActiveSheet()->getCell("F".$i)->getValue(),$ea->getActiveSheet()->getCell("G".$i)->getValue(),$ea->getActiveSheet()->getCell("H".$i)->getValue());
and append it at end of the funtion of PHPExcel_Chart_DataSeriesValues,and converted my second parameter of function to null.
$spec_label = array(new \PHPExcel_Chart_DataSeriesValues('String', null, NULL, 1,$pie_label),);

How do I get the correct format x-axis for my phpspreadsheet chart?

I am using 33_Chart_create_scatter.php to make a two line xyScatter chart. Everything is going well, except, the x-axis is not formatting as I would like it to. There is no associated error involver. Only a poor x-axis. See the first image. For the correct (desired) x-axis, see the second image. Can someone help me achive the proper x-axis format?
I got the correct format x-axis by simply right clicking the chart area and selecting "Change Chart Type". Then I select (already selected) "X Y (Scatter)" and "ok". The x-axis is changed to that shown in my correct x-axis image. So, that brings about my second question, if I can not get a code correction to get the proper x-axis format, can we include macro's in our create charts? A simple three line auto_open macro included will yield the proper chart/x-axis when the new spreadsheet is opened.
I've used both horizontal and vertical selections for source values. I've also tried using specific values for the source. See J1:R1 in my image. I can not figure out how to add my images. So, incorrect x-axis runs from 1 to about 50 by 1's And it looks like there maybe two to three sets of numbers all crunched together. The correct scale is 0 to 40 by 5's evenly spaced. this is a minutes scale. The y scale looks perfect.
Here is the code involved.
$xAxisTickValues = [
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$1:$C$18', null, 18),
new
DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$F$1:$F$18', null, 18)
];
The following code should control the x and y properties. I am able to control all the properties of the Y-axis. How ever it appears the x-axis min and max values are controlled by the x Axis Tick Values. If I don't use x Axis Tick Values, It then appears x-axis min and max values are controlled by the number of y data points?
What do I have to do so this code can be used to control x-axis min and max values?
$xaxis = new Axis();
$xaxis->setAxisOptionsProperties('low', 0, 'autoZero', null, 'in', 'out', 0, 40, 5, 0);
$title = new Title('Calorimetric Calibration');
$yAxisLabel = new Title(html_entity_decode('Temp (°C)',ENT_QUOTES,'UTF-8'));
$xAxisLabel = new Title('Time (Min)');
// Create the chart
$chart = new Chart(
'chart1', // name
$title, // title
$legend, // legend
$plotArea, // plotArea
true, // plotVisibleOnly
0, // displayBlanksAs
$xAxisLabel, // xAxisLabel
$yAxisLabel, // yAxisLabel
$yaxis,
$xaxis,
null,
null
);

How can i change the style of the line chart generated with phpexcel?

I am generating a line chart using the example from Github library.
What i want to have is the option to set few custom styles for each of the lines in the chart, as we do manually in the excel sheet like:
Select Line in the chart, Format Data Series, then:
Marker Options > None
Line Style > Width > 2.5pt (1pt is default)
Line Style > Smoothed line
How can i set the above three options for the lines generated in the line chart?
So far, here is my code to set the layout and data series for the chart:
$series = new PHPExcel_Chart_DataSeries(
PHPExcel_Chart_DataSeries::TYPE_LINECHART,
PHPExcel_Chart_DataSeries::GROUPING_STANDARD,
range(0, count($dataSeriesValues)-1),
$dataseriesLabels,
$xAxisTickValues,
$dataSeriesValues
);
$layout1 = new PHPExcel_Chart_Layout();
$layout1->setShowVal(TRUE);
$plotarea = new PHPExcel_Chart_PlotArea($layout1, array($series));
Can someone provide a hint, as i can't find it in the example.
For your first question to get Marker Options > None:
In the file PHPExcel/Chart/Renderer/jpgraph.php on line 287:
$marker = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotValuesByIndex($i)->getPointMarker();
Change this into
$marker = 'none';
To get rid of the markers, note that this is a bit hacky fix, normally it loops trough all markers and there is no functionality made in the tool to set the marker yourself. (that is why you cannot find it in the examples)
You can also remove or edit the code at Excel2007/Chart.php line 792, here you can see the if ($plotSeriesMarker) code, change it to "none" or just delete this part.
For your second question: Line Style > Width > 2.5pt (1pt is default)
In the file PHPExcel/Writer/Excel2007/Chart.php
At line 781 in function _writePlotGroup you can see the line plotting code
if ($groupType == PHPExcel_Chart_DataSeries::TYPE_LINECHART) {
$objWriter->startElement('c:spPr');
$objWriter->startElement('a:ln');
$objWriter->writeAttribute('w', 12700);
$objWriter->endElement();
$objWriter->endElement();
}
Here you can change the width by using for example:
$objWriter->writeAttribute('w', '40000');
For your third question: Line Style > Smoothed line
The construct function of PHPExcel_Chart_DataSeries is
/**
* Create a new PHPExcel_Chart_DataSeries
*/
public function __construct($plotType = null, $plotGrouping = null, $plotOrder = array(), $plotLabel = array(), $plotCategory = array(), $plotValues = array(), $smoothLine = null, $plotStyle = null)
So after your $dataSeriesValues you can define a smoothline and a plotStyle value.
Passing true to the smoothline parameter gives you a smooth line style.
If for some reason that does not work, in the Chart.php you can find on line 272
$objWriter->writeAttribute('val', (integer) $plotGroup->getSmoothLine() );
Change this to
$objWriter->writeAttribute('val', 1 );
And it should work for sure.
At the time of this response the solution to your first question; Marker Options > None is simple and doesn't involve modifying base code. When creating $dataSeriesValues objects(line 86) you can define which type of marker you want or 'none'.
$dataSeriesValues = array(
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$2:$B$5', null, 4, array(), 'none'),
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$5', null, 4, array(), 'none'),
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$D$2:$D$5', null, 4, array(), 'none'),
);

PHPExcel - Excel5 - Create Line Chart - Chart Missing

Reject? I am sure I am not the first that has ran into this issue.
I am trying to create a chart using PHPExcel and the Excel5 library to produce an .xls file of the '95 format.
All goes well except for the creation/save of the chart itself. The file is saved/downloaded/opened without any errors alerted by either, PHPExcel during creation/save, or by Excel during opening the file.
The only real issue is, there is no chart visible within the Excel '95 .xls file created.
I have checked to be sure the MIME type is being set to the '95 .xls format application/vnd.ms-excel. It is.
Here is the code relating to the chart creation, its a little messy currently, but like I said, works like a charm for the 2007 version:
//ADD THE REPORT SUMMARY CHART
$labels = array(
new PHPExcel_Chart_DataSeriesValues('String', "'Report Summary'!C1", null, 1),
new PHPExcel_Chart_DataSeriesValues('String', "'Report Summary'!D1", null, 1)
);
$chrtCols = "'Report Summary'!B2:B$rowNum";
$chrtVals = "'Report Summary'!C2:C$rowNum";
$chrtVals2 = "'Report Summary'!D2:D$rowNum";
$periods = new PHPExcel_Chart_DataSeriesValues('String', $chrtCols, null, $rowNum-1);
$values = new PHPExcel_Chart_DataSeriesValues('Number', $chrtVals, null, $rowNum-1);
$values2 = new PHPExcel_Chart_DataSeriesValues('Number', $chrtVals2, null, $rowNum-1);
$series = new PHPExcel_Chart_DataSeries(
PHPExcel_Chart_DataSeries::TYPE_LINECHART,
PHPExcel_Chart_DataSeries::GROUPING_STANDARD,
array(0,1),
$labels,
array($periods,$periods),
array($values,$values2)
);
$series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_COL);
$layout = new PHPExcel_Chart_Layout();
$plotarea = new PHPExcel_Chart_PlotArea($layout, array($series));
$chart = new PHPExcel_Chart('sample', null, null, $plotarea);
$chart->setTopLeftPosition('A1', 24, 24);
$chart->setBottomRightPosition('B18', -24);
$actSheet->addChart($chart);
The issue seems to be with the '95 version only as the exact same code works to create a valid 2007 version of the file (using the Excel_2007 class rather than Excel5).
Any thoughts? Are line-charts constructed differently (or labels defined differently) in Excel_07 as compared to Excel_95? Any other declarations in the $series array I should add/modify/delete to get the chart to appear Excel_95?
Yes, I am including $objWriter->setIncludeCharts(TRUE); prior to writing the file.
$rowNum is the last row of data.
PHPExcel only supports charts for the Excel2007 Reader/Writer; that feature has not been written for the Excel5 Reader/Writer

Sample Scatter Excel Chart using OpenTBS

Can anybody give me an example of how to create an Excel Scatter chart using OpenTBS/TinyButStrong? And are there any commercial/non-commercial PHP excel writers that support excel charts?
Will really appreciate your help.
Thank you for your time!
Create a basic XY chart using the wizard in Ms Word.
Save the chart without changing it. Let the values and the design.
Select the chart, and in the contextual menu, chose "Format of the zone" or something like this. In the "Alt Text" tab, in zone "Title", enter "a nice chart".
Save the template.
At the PHP side:
$template = 'demo_ms_word.docx'; $TBS->LoadTemplate($template);
$ChartNameOrNum = 'a nice chart'; // Title of the shape that embeds the chart
$SeriesNameOrNum = 1;
$NewValues = array( array( 1.5, 2.0, 3.2, 33), array(1.77, 2.88, 2.88, 2.99) );
$NewLegend = "OpenTBS is so strong";
$TBS->PlugIn(OPENTBS_CHART, $ChartNameOrNum, $SeriesNameOrNum, $NewValues, $NewLegend);
$TBS->Show(OPENTBS_FILE+TBS_EXIT, 'result.docx');
After it works, change the chart properties and the data.

Categories