PHPExcel Piechart legends with fix length - php

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),);

Related

Change charts title by PhpOffice\PhpSpreadsheet in template

I try to change chart title in .xlsx template
I open template, change cels ,write to output.
Q: How to change chart title....
$reader = IOFactory::createReader( 'Xlsx' );
$reader->setIncludeCharts( true );
$spreadsheet = $reader->load( storage_path( 'app/template.xlsx' ) );
$spreadsheet->getActiveSheet()->setCellValue( 'B3', 'Blabla' );
//create new from template
$writer = new Xlsx( $spreadsheet );
$writer->setPreCalculateFormulas( true );
$writer->setIncludeCharts( true );
##how to change title in all/first charts in template?
I finally found the method. There are a few things to understand.
chartIndex - In an excel file, an id assigned to the chart based on the total number of charts starting from the first left most chart on a sheetA incrementing to the last right most chart on SheetC. This index is the entire collection of all charts on all sheets starting with chart1 and ending with chartX. If you have ten charts chart1,chart2,...chart10 and delete chart5, you will then have a new index of chart1,chart2....chart9, where the orig chart6-10 are all redindexed as chart5-9. In phpspreadsheet, this index appears to be represented with $chart->getName() ;, There is no corresponding setName() that would allow you to change/alter the index.
chartName - In an excel file, if you highlight a chart, in the upper left corner of the cells, above the A column, is a "chart name" drop down. You can name your charts for organization purposes. In excel, this field can be edited. In phpspreadsheet, I have yet to have find what object can get/set this field.
chartTitle - the in chart name of a chart, IE: "Annual Revenue Projections". In phpspreadsheet, this field is represented with $chart->getTitle->get/setCaptionText
So in order to find the chart you are looking to modify, you either must already know the chart index (ie: getName()) OR the chart visual title (ie: getCaptionText() ).
Because of adding deleting moving charts, I found it easier, in the excel template, to add visual titles to your charts then search for those titles/captionText in your code. This way you don't have to have worry about tracking which chart is which index. Use the getCaptionText to then get the getName() index.
foreach ($spreadsheet->getSheetByName("SheetName")->getChartCollection() as $chart) {
// if you know TITLE of the chart
// TITLE is the visible title in the chart
// Easy to know if you set the titles yourself in the template chart
// A template chart TITLE cannot be a cell reference, will cause an error on file import
if ($chart->getTitle()->getCaptionText() == "Chart_12") {
$curIndex = $chart->getName() ; // = "chart3"
$curTitle = $chart->getTitle()->getCaptionText() ; // "Chart_12"
$chart->getTitle()->setCaption("Quarterly Revenue Chart") ;
break ;
}
// if you know the NAME (chart id) the chart
// NAME is like the hidden chart index
if ($chart->getName() == "chart12") {
$chart->getTitle()->setCaption("New Chart Title") ;
break ;
}
}

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
);

Laravel 5.5 Console TV Bar Chart High Charts multipleDatasets and label

I have a Laravel 5.5 app that used this ConsoleTV Chart Package I used a barchart to visualize my data in a monthly report basis. I have the following query below.
$dt = Carbon::now()->year;
$anRequest = AnalysisRequest::where(DB::raw("(DATE_FORMAT(created_at,'%Y'))"),date('Y'))->orderBy('service_id')
->get();
and in my chart
$barChart = Charts::database($anRequest, 'bar', 'highcharts')
->title("Monthly service request")
->elementLabel("Total requests")
->dimensions(1000, 500)
->responsive(false)
->groupByMonth(date('Y'), true);
This code is working perfectly fine and display all the data in by month but how can I add another dataSets or multipleDataSets instead of just having one sets of data to visualize.
For example I will have another query from the AnalysisRequest but with a specific resources.
Sample code below using multi
The query
$tags_jan = DB::table('tags')->whereYear('created_at', $dt)->whereMonth('created_at', '1')->count();
$tags_feb = DB::table('tags')->whereYear('created_at', $dt)->whereMonth('created_at', '2')->count();
$tags_mar = DB::table('tags')->whereYear('created_at', $dt)->whereMonth('created_at', '3')->count();
// and the list goes on which is bad
The charts using multi
$chart = Charts::multi('bar', 'highcharts')
// Setup the chart settings
->title("Total Reports for this year")
// A dimension of 0 means it will take 100% of the space
->dimensions(0, 400) // Width x Height
// This defines a preset of colors already done:)
// ->template("material")
// ->responsive(true)
// You could always set them manually
// ->colors(['#2196F3', '#F44336', '#FFC107'])
// Setup the diferent datasets (this is a multi chart)
->colors(['green', 'aqua', 'red', 'yellow'])
->dataset('Ads', [$ads_jan,$ads_feb,$ads_mar,$ads_apr,$ads_may,$ads_june,$ads_july,$ads_aug,$ads_sept,$ads_oct,$ads_nov,$ads_dec])
->dataset('Channels', [$channels_jan,$channels_feb,$channels_mar,$channels_apr,$channels_may,$channels_june,$channels_july,$channels_aug,$channels_sept,$channels_oct,$channels_nov,$channels_dec])
->dataset('Shows', [$shows_jan,$shows_feb,$shows_mar,$shows_apr,$shows_may,$shows_june,$shows_july,$shows_aug,$shows_sept,$shows_oct,$shows_nov,$shows_dec])
->dataset('Tags', [$tags_jan,$tags_feb,$tags_mar,$tags_apr,$tags_may,$tags_june,$tags_july,$tags_aug,$tags_sept,$tags_oct,$tags_nov,$tags_dec])
This code above also works fine but as you can see in the variable I am querying for each month for this year which is isn't quite good. How can I simplified this query or another way to solve this using multiple dataSets?
Appreciate if someone could help.
Thanks in advance.
The following code can be added after you have queried your database and you would like to post different graphs to the same view, I was using the code to plot a barchart and line chart on the same graph as specified below........ look at the dataset position 2 its line and bar
$chart = new unclaimed;
$chart->labels( $days );
$chart->dataset('Daily Visitors Bar', 'bar', $totalNumber )->color('white')->backgroundColor(['#009900','#8a8a5c','#f1c40f','#e67e22','#16a085','#2980b9']);
$chart->dataset('Daily Visitors Line', 'line', $totalNumber )->color('black') ->fill(false)->backgroundColor(['##8a8a5c','009900','#f1c40f','#e67e22','#16a085','#2980b9']);

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'),
);

SetSerieDescription - pChart not working

I'm trying to assign a label to the x axis of a Bar Chart, the label is a normal text string stored in an array under $gsettings['axisXlabel']. Unfortunatly pCharts SetSerieDescription doesn't seem to be working as expected.
Below is the function producing the graphs and an attachment of the current output. The part of interest is the 3 lines under /* Bind a data series to the X axis */
/**
* function to plot bar charts
*/
function cg_graphs_plot_bar_graph($gdata, $gsettings){
$graph_data = new pData();
if(isset($gdata['bar_plots2'])){ //if this is set, its a duel bar graph
$graph_data->addPoints($gdata['bar_plots'],"surgeondata");
$graph_data->addPoints(array(0,0,0,0),"surgeondatanull");
$graph_data->addPoints($gdata['bar_plots2'],"everyonedata");
$graph_data->addPoints(array(0,0,0,0),"everyonedatanull");
$graph_data->setSerieDrawable(array("everyonedatanull"), FALSE);
$graph_data->setSerieDescription("surgeondata",$gdata['surgeonname']);
$graph_data->setSerieDescription("everyonedata","All Surgeons");
$graph_data->setAxisUnit(0,"%");
$surgeon = array("R"=>21,"G"=>0,"B"=>0); //surgeon series colour
$all = array("R"=>191,"G"=>160,"B"=>36); //everyone series colour
$graph_data->setPalette("surgeondata",$surgeon);
$graph_data->setPalette("everyonedata",$all);
$graph_data->setPalette("surgeondatanull",$surgeon);
$graph_data->setPalette("everyonedatanull",$all);
} else {
$graph_data->addPoints($gdata['bar_plots'],"percentiles");
$graph_data->addPoints($gdata['surgeon_bar'],"surgeonbar");
$graph_data->setSerieDrawable(array("surgeonbar"), FALSE);
}
$graph_data->setAxisName(0,$gsettings['axisYlabel']);
/* Bind a data serie to the X axis */
$graph_data->addPoints($gdata['xaxis_names'],"Labels");
$graph_data->setSerieDescription("Labels",$gsettings['axisXlabel']);
$graph_data->setAbscissa("Labels");
$width=540;
$height=419;
$chart = new pImage($width,$height,$graph_data);
$chart->drawFromJPG(0,0,drupal_get_path('module', 'cg_graphs')."/images/graphbg.jpg");
/* Write the picture title */
$chart->setFontProperties(array("FontName"=>drupal_get_path('module', 'cg_graphs')."/pChart/fonts/ARIAL.TTF","FontSize"=>8));
$chart->setFontProperties(array("R"=>0,"G"=>0,"B"=>0));
$chart->drawText(270,70,$gsettings['title'],array("R"=>0,"G"=>0,"B"=>0,"Align"=>TEXT_ALIGN_MIDDLEMIDDLE, "FontSize" => 12));
/* Set the graph area */
$chart->setGraphArea(70,120,490,310);
/* Draw a rectangle */
$chart->drawFilledRectangle(70,120,489,309,array("R"=>255,"G"=>255,"B"=>255,"Dash"=>FALSE, "BorderR"=>201,"BorderG"=>201,"BorderB"=>201));
/* Turn on shadow computing */
$chart->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>20));
$format = array(
"DisplayValues"=>FALSE,
"DisplayColor"=>DISPLAY_AUTO,
"Rounded"=>FALSE,
"Gradient"=>TRUE,
"GradientAlpha"=>100,
"GradientMode"=>GRADIENT_EFFECT_CAN,
"GradientStartR"=>251,
"GradientStartG"=>220,
"GradientStartB"=>96,
"GradientEndR"=>191,
"GradientEndG"=>160,
"GradientEndtB"=>36
);
if(isset($gdata['bar_plots2'])){
/* Draw the scale */
$chart->drawScale(array("XMargin"=>50, "Mode"=>SCALE_MODE_MANUAL, "ManualScale"=> $gsettings['maxmin'], "Pos" => SCALE_POS_LEFTRIGHT,'DrawXLines' => FALSE, 'GridTicks' => 500,'GridR'=>0,'GridG'=>0,'GridB'=>0, 'LabelRotation'=>0, 'AroundZero' => TRUE, 'Interleave' => 0.1));
$graph_data->setSerieDrawable(array("surgeondata"), FALSE);
$graph_data->setSerieDrawable(array("surgeondatanull"), TRUE);
} else {
/* Draw the scale*/
$chart->drawScale(array("XMargin"=>40, "Mode"=>SCALE_MODE_MANUAL, "ManualScale"=> $gsettings['maxmin'], "Pos" => SCALE_POS_LEFTRIGHT,'DrawXLines' => FALSE, 'GridTicks' => 500,'GridR'=>0,'GridG'=>0,'GridB'=>0, 'LabelRotation'=>0, 'AroundZero' => TRUE));
}
$chart->drawBarChart($format);
//draw next bar with new colour.
$format = array(
"DisplayValues"=>FALSE,
"DisplayColor"=>DISPLAY_AUTO,
"Rounded"=>FALSE,
"Gradient"=>TRUE,
"GradientAlpha"=>100,
"GradientMode"=>GRADIENT_EFFECT_CAN,
"GradientStartR"=>255,
"GradientStartG"=>230,
"GradientStartB"=>126,
"GradientEndR"=>21,
"GradientEndG"=>0,
"GradientEndtB"=>0
);
if(!isset($gdata['bar_plots2'])){ //not set? we need to draw the second one.
//set draw series to false / true here
$graph_data->setSerieDrawable(array("percentiles"), FALSE);
$graph_data->setSerieDrawable(array("surgeonbar"), TRUE);
$chart->drawBarChart($format);
} else {
$graph_data->setSerieDrawable(array("surgeondatanull", "everyonedata"), FALSE);
$graph_data->setSerieDrawable(array("surgeondata", "everyonedatanull"), TRUE);
$chart->drawBarChart($format);
$graph_data->setSerieDrawable(array("everyonedatanull"), FALSE);
$graph_data->setSerieDrawable(array("everyonedata"), TRUE);
$chart->drawLegend(190,100,array("Style"=>LEGEND_NOBORDER, "Mode" => LEGEND_HORIZONTAL)); //draw legand
}
$imagename = str_replace(' ', '-', $gdata['surgeonname']);
$chart->render(drupal_get_path('module', 'cg_graphs')."/pChart/examples/pictures/".$imagename."-".$gsettings['name'].".png");
}
And here's the output, I want to label the Xaxis, currently the label isn't showing. (sorry for removed title etc, data isn't in the public domain yet and names needed removing)
You need to use the function setAbscissaName as defined in http://wiki.pchart.net/doc.dataset.setabscissaname.html
Example:
$MyData->setAbscissaName("Months");
This will display Months in the X axis under ticks.
The pChart documentation is a mess. I've found an error or two where the documentation differs from the source code. Their naming scheme also needs work. It's unintuitive to anyone besides the creator.
I believe you are trying to add a label to the X-Axis. In order to do this, you need to use setXAxisName().
The "Temperature" that appears in the code for setSerieDescription() is a string that is used to tie that set of series data to that name. It is not actually the label that appears on the chart (which is the same name). It is the name that is used again in setAbscissa().

Categories