I have a project in which data are show in pie chart. It can export the pie chart to powerpoint from php. But the pie chart is show with by exploding. I want to show it without exploding it. Is it possible?
I am using PowerPoint2007. And the code is as following -
<?php
require_once 'iconnect.php';
require_once 'QueryHandler.php';
require_once 'ResultTypes.php';
include_once 'Sample_Header.php';
use PhpOffice\PhpPowerpoint\PhpPowerpoint;
use PhpOffice\PhpPowerpoint\Shape\Chart\Type\Pie3D;
use PhpOffice\PhpPowerpoint\Style\Fill;
use PhpOffice\PhpPowerpoint\Style\Color;
use PhpOffice\PhpPowerpoint\Style\Shadow;
use PhpOffice\PhpPowerpoint\Shape\Chart\Series;
use PhpOffice\PhpPowerpoint\Style\Border;
use PhpOffice\PhpPowerpoint\Style\Alignment;
use PhpOffice\PhpPowerpoint\IOFactory;
Connect(3);
global $linkid;
$queryHandler=new QueryHandler();
$query="SELECT sku,value ".
"FROM ferrero_booker t1,booker_sku t2 ".
"WHERE t1.skuID=t2.skuID ".
"LIMIT 10";
$result = $queryHandler->runQuery($query,$linkid,ResultTypes::$TYPE_ARRAY);
$data=array();
for($i=0;$i<count($result);$i++)
{
$id=$result[$i][0];
$data[$id]=(float)$result[$i][1];
}
$objPHPPowerPoint = new PhpPowerpoint();
$objPHPPowerPoint->getProperties()->setCreator('PHPOffice')
->setLastModifiedBy('PHPPowerPoint Team')
->setTitle('Sample 07 Title')
->setSubject('Sample 07 Subject')
->setDescription('Sample 07 Description')
->setKeywords('office 2007 openxml libreoffice odt php')
->setCategory('Sample Category');
$objPHPPowerPoint->removeSlideByIndex(0);
$currentSlide = createTemplatedSlide($objPHPPowerPoint);
$pie3DChart = new Pie3D();
$series = new Series('', $data);
$series->setShowSeriesName(true);
$series->getDataPointFill(0)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF4672A8'));
$series->getDataPointFill(1)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FFAB4744'));
$series->getDataPointFill(2)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF8AA64F'));
$series->getDataPointFill(3)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF725990'));
$series->getDataPointFill(4)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF4299B0'));
$series->getDataPointFill(5)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FFDC853E'));
$series->getDataPointFill(6)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF93A9CE'));
$pie3DChart->addSeries($series);
$shape = $currentSlide->createChartShape();
$shape->setName('PHPPowerPoint Daily Downloads')
->setResizeProportional(false)
->setHeight(550)
->setWidth(700)
->setOffsetX(120)
->setOffsetY(80);
$shape->getBorder()->setLineStyle(Border::LINE_SINGLE);
$shape->getTitle()->setText('Booker Sales');
$shape->getTitle()->getFont()->setItalic(true);
$shape->getPlotArea()->setType($pie3DChart);
$shape->getView3D()->setRotationX(90);
$shape->getView3D()->setPerspective(90);
$shape->getLegend()->getBorder()->setLineStyle(Border::LINE_SINGLE);
$shape->getLegend()->getFont()->setItalic(true);
$oWriterPPTX = IOFactory::createWriter($objPHPPowerPoint, 'PowerPoint2007');
$oWriterPPTX->save("sample.pptx");
print_r($data);
?>
I haven't seen a way to do it in the code, but it's easy to modify the source. Whether you should is another matter, but I won't touch that topic here.
The pie chart explosion is controlled by an "explosion" tag written to the xml, and this happens in the writer. Specifically this file (on my installation/version):
PHPPowerPoint/src/PhpPowerpoint/Writer/PowerPoint2007/Chart.php
Within that file, the following lines add the explosion effect:
// c:explosion
$objWriter->startElement('c:explosion');
$objWriter->writeAttribute('val', '20');
$objWriter->endElement();
You can therefore remove the explosion by suppressing the above lines. If you just want this attribute unconditionally gone, comment the above lines out, or delete them. If you want to extend the API, then add the relevant methods to the Pie chart class and check for the values here, conditionally executing the above lines based on those settings.
I hope this helps.
Related
I've been trying to use Teechart to draw a couple of graphs for a project. I've managed to draw the graphs just fine. What I am struggling with is annotating specific points on the graph with some labels.
The specific problem is that when I use $chart1->CalcXPos(someIndex); it always returns 0 instead of a value of the pixels. How can I got about resolving this?
$chart1 = new TChart(640,480);
$varname = new Line($chart1->getChart());
$someYValues = array(2,3,5,7,11,13);
$theXValues = array(-3,-1,1,3,4,5);
$i=0;
foreach($someYValues as $x){
$varname->addXY($theXValues[$i],$someYValues[$i]);
$i++;
}
$varname->Setcolor(Color::BLUE());
$chart1->getAxes()->getBottom()->getTitle()->setText("X-axis label (units)");
$chart1->getAxes()->getLeft()->getTitle()->setText("Y-axis label (units)");
$tool=new Annotation($chart1->getChart());
$tool->getShape()->setCustomPosition(true);
//$chart1->paint;
$xvalue = $chart1->getAxes()->getBottom()->CalcXPosValue($theXValues[2]);
$yvalue = $chart1->getAxes()->getLeft()->CalcYPosValue($someYValues[2]);
//$xvalue = $varname->CalcXPosValue($theXValues[2]);
//$yvalue = $varname->CalcYPosValue($someYValues[2]);
echo $xvalue;
echo $yvalue;
$tool->setTop($xvalue);
$tool->setLeft($yvalue);
$tool->setText("Random Text ");
$chart1->render("ecg.png");
Try forcing a chart repaint before using Calc* functions:
$chart1->doInvalidate();
I have a set of data that spans a number of dates. Some of the data points need to be identified with a different style so they stand out. I can approximate what I need by creating 2 plot lines using different mark styles but it doesn't look cohesive and can be confusing to some people. Here is an example using that method.
All of the data points in the sample above should be connected. The questions are
Is it possible to create a single plot line with different mark styles?
If the answer to the above is no is there a way to somehow "merge" the 2 plot lines so all of the points get connected?
The code that created the sample graph:
<?php
require_once 'jpgraph/jpgraph.php';
require_once 'jpgraph/jpgraph_line.php';
require_once 'jpgraph/jpgraph_date.php';
$line1D = array(1497844800,1498449600,1505102400,1516597200);
$line1Y = array(79.00,76.00,53.00,14.00);
$line2D = array(1504584000,1507521600);
$line2Y = array(9.87,9.93);
$graph = new Graph(640,480);
$graph->clearTheme();
$graph->SetScale('datlin');
$graph->xaxis->SetLabelAngle(60);
$graph->xaxis->SetFont(FF_ARIAL,FS_NORMAL,8);
$graph->xaxis->SetLabelFormatString('m/d/Y',true);
$graph->yaxis->scale->SetGrace(5);
$line1=new LinePlot($line1Y,$line1D);
$line1->SetColor('#4A6EC6');
$line1->SetWeight(2);
$line1->mark->SetFillColor('#4A6EC6');
$line1->mark->SetType(MARK_FILLEDCIRCLE);
$graph->Add($line1);
$line2=new LinePlot($line2Y,$line2D);
$line2->SetColor('#4A6EC6');
$line2->SetWeight(2);
$line2->mark->SetType(MARK_CIRCLE);
$line2->mark->SetColor('#4A6EC6');
$graph->Add($line2);
$graph->Stroke(); // display the graph
?>
The way to have different marks on a line is to create a 3rd line that is just the line without any marks. Then change the current lines that have marks to be scatter plots instead so just the points are added with the desired marks. When the 3 things are plotted together, 2 scatter plots and the line, they come together as a single line with different marks.
The modified code that works as desired is:
<?php
require_once 'jpgraph/jpgraph.php';
require_once 'jpgraph/jpgraph_line.php';
require_once 'jpgraph/jpgraph_date.php';
require_once 'jpgraph/jpgraph_scatter.php';
$line1D = array(1497844800,1498449600,1505102400,1516597200);
$line1Y = array(79.00,76.00,53.00,14.00);
$line2D = array(1504584000,1507521600);
$line2Y = array(9.87,9.93);
$line3D = array_merge($line1D,$line2D);
$line3Y = array_merge($line1Y,$line2Y);
$graph = new Graph(640,480);
$graph->clearTheme();
$graph->SetScale('datlin');
$graph->xaxis->SetLabelAngle(60);
$graph->xaxis->SetFont(FF_ARIAL,FS_NORMAL,8);
$graph->xaxis->SetLabelFormatString('m/d/Y',true);
$graph->yaxis->scale->SetGrace(5);
$line1=new ScatterPlot($line1Y,$line1D);
$line1->SetColor('#4A6EC6');
$line1->SetWeight(2);
$line1->mark->SetFillColor('#4A6EC6');
$line1->mark->SetType(MARK_FILLEDCIRCLE);
$graph->Add($line1);
$line2=new ScatterPlot($line2Y,$line2D);
$line2->SetColor('#4A6EC6');
$line2->SetWeight(2);
$line2->mark->SetType(MARK_CIRCLE);
$line2->mark->SetColor('#4A6EC6');
$graph->Add($line2);
$line3=new LinePlot($line3Y,$line3D);
$line3->SetColor('#4A6EC6');
$line3->SetWeight(2);
$graph->Add($line3);
$graph->Stroke(); // display the graph
?>
I am using PHPPowerPoint to export a .ppt file using PHP. I am successful until now when I want to remove the legend from the Bar Graph.
The code which is generating the bar graph is:
$chart1 = new Bar();
$series = new Series('City Distribution', $timeseries);
$series->setShowSeriesName(false);
$series->setShowValue(true);
$series->getFill()->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF1dd2af'));
$chart1->addSeries($series);
$gFill = new Fill();
$gFill->setFillType(Fill::FILL_GRADIENT_PATH)->setRotation(90)->setStartColor(new Color( 'FF1dd2af' ))->setEndColor(new Color( 'FFFFFFFF' ));
$oFill = new Fill();
$oFill->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FFFFFFFF'));
$oShadow = new Shadow();
$oShadow->setVisible(true)->setDirection(45)->setDistance(10);
// Create a shape (chart)
$shape = $currentSlide->createChartShape();
//$shape->getAlignment()->setVertical( Alignment::VERTICAL_TOP );
$shape->getTitle()->setVisible(false);
$shape->setName('City Distribution')->setResizeProportional(false)->setHeight(300)->setWidth(250)->setOffsetX(25)->setOffsetY(200);
$shape->setFill($oFill);
$shape->getTitle()->setText('City Distribution');
$shape->getTitle()->getFont()->setItalic(true);
$shape->getPlotArea()->getAxisX()->setTitle('City');
$shape->getPlotArea()->getAxisY()->setTitle('Number of Users');
$shape->getPlotArea()->setType($chart1);
$shape->getLegend()->getBorder()->setLineStyle(Border::LINE_SINGLE);
$shape->getLegend()->getFont()->setItalic(true);
This is working just fine but it is putting a legend box, which I want to remove. Any Idea?
Nevermind I figured out it by myself, all you need to do is:
$shape->getLegend()->setVisible(false);
Put it below the line
$shape->getTitle()->setVisible(false);
Hi so I've been struggling a good while to make the chart I want and I'm getting very close.
First I have performance data of a stock portfolio that should be on YAXIS #1... That's good.
Then I want a benchmark on YAXIS #2... The Line plot works, but the scale is weird :
Notice how it starts at 50 and goes 10, 15, 20, etc...
The thing is the scale should be between 3400 and 3800 since that's the range of my data.
Here is my code :
<?php // content="text/plain; charset=utf-8"
require($_SERVER['DOCUMENT_ROOT'] . '/wp-blog-header.php');
require_once ('jpgraph.php');
require_once ('jpgraph_line.php');
require_once ('jpgraph_date.php');
require_once ('jpgraph_utils.inc.php');
// Get a dataset
$data = get_transient( 'daily_nav' );
$ydata = $data[1];
$xdata = $data[0];
$data2 = get_transient( 'CAC40_history' );
$ydata2 = array_reverse($data2[1]);
$xdata2 = array_reverse($data2[0]);
$dateUtils = new DateScaleUtils();
list($tickPositions, $minTickPositions) = DateScaleUtils::GetTicks($xdata);
// Setup a basic graph
$width=800; $height=500;
$graph = new Graph($width, $height);
$graph->SetScale('datlin');
$graph->SetYScale(0,'lin');
$graph->SetYScale(1,'lin');
$graph->SetMargin(60,20,40,60);
$graph->xaxis->SetPos('min');
$graph->xaxis->SetTickPositions($tickPositions,$minTickPositions);
// Setup the titles
$graph->title->SetFont(FF_GEORGIA,FS_NORMAL,16);
$graph->title->Set('Performance vs. CAC40');
$graph->subtitle->SetFont(FF_ARIAL,FS_ITALIC,10);
$graph->subtitle->Set('graphique journalier depuis la création en juin 2012');
// Setup the labels to be correctly format on the X-axis
$graph->xaxis->SetFont(FF_ARIAL,FS_NORMAL,8);
$graph->xaxis->SetLabelAngle(30);
// The second paramter set to 'true' will make the library interpret the
// format string as a date format. We use a Month + Year format m-d-Y
$graph->xaxis->SetLabelFormatString('m-Y',true);
// And then add line. We use two plots in order to get a
// more distinct border on the graph
$lp2 = new LinePlot($ydata,$xdata);
$lp2->SetColor('#71a7da');
$graph->Add($lp2);
$graph->xgrid->Show();
$graph->AddY(0,$lp2);
// second chart
$lp3 = new LinePlot($ydata2, $xdata2);
$lp3->SetColor('blue');
//$graph->Add($lp3);
//$graph->xgrid->Show();
$graph->AddY(1,$lp3);
// And send back to the client
$graph->Stroke();
?>
Would be great if anyone can help, can't figure this one out.
Thanks
I am quite a newbie but had the same issue myself a minute ago.
Missing zeros on the right? Try changing margin settings?
$graph->SetMargin(60,50,40,60);
Hi, is there a way to download the BibTeX entry for something from Google Scholar using PHP without having to download the BibTeX manually one by one? For example, setting a search value like "research" and then downloading the related BibTeX from the links automatically through code.
Any help would be appreciated. I tried to get the HTML page, but as I try to get the page contents the "Import to BibTeX" link disappears on the retrieved page contents.
My code:
<?php
$url = 'http://scholar.google.com/scholar?q=honors+college&hl=en&btnG=Search& amp;as_sdt=1%2C4&as_sdtp=on';
$needle = 'Import into bibtex';
$contents = file_get_contents($url);
echo $contents;
if(strpos($contents, $needle)!== false) {
echo 'found';
} else {
echo 'not found';
}
?>
The short answer is No you cannot do this
Google does not provide API's for search / scholar and uses firm rate-limitation. The problem is that for each BibTex entry you need 2 additional requests (1 for the query, 1 for the 'import link' and a final one to get the actual BibTex entry content)
I wrote a script that scrapes google scholar results and finds the BibTex links and saves the results. However, due to the rate limit is not viable and will get blocked almost instantly.
Code can be viewed here: https://gist.github.com/Tessmore/11099509 and is free of use, but at your own risk.
As Tessmore said - you can't. But you can make it work by using Google Scholar Organic Results API from SerpApi that bypasses quota limits and blocks from search engines so you don't have to think about how to reduce the chance of being blocked.
Example:
Install google-search-results-php package first via composer:
$ composer require serpapi/google-search-results-php:2.0
Code to integrate and full example in the online IDE:
<?php
ini_set("display_errors", 1);
ini_set("display_startup_errors", 1);
error_reporting(E_ALL);
require __DIR__ . "/vendor/autoload.php";
function getResultIds () {
$result_ids = array();
$params = [
"engine" => "google_scholar", // parsing engine
"q" => "biology" // search query
];
$search = new GoogleSearch(getenv("API_KEY"));
$response = $search->get_json($params);
foreach ($response->organic_results as $result) {
// print_r($result->result_id);
array_push($result_ids, $result->result_id);
}
return $result_ids;
}
function getBibtexData () {
$bibtex_data = array();
foreach (getResultIds() as $result_id) {
$params = [
"engine" => "google_scholar_cite", // parsing engine
"q" => $result_id
];
$search = new GoogleSearch(getenv("API_KEY"));
$response = $search->get_json($params);
foreach ($response->links as $result) {
if ($result->name === "BibTeX") {
array_push($bibtex_data, $result->link);
}
}
}
return $bibtex_data;
}
print_r(json_encode(getBibtexData(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
?>
Output:
[
"https://scholar.googleusercontent.com/scholar.bib?q=info:KNJ0p4CbwgoJ:scholar.google.com/&output=citation&scisdr=CgXjqB_WGAA:AAGBfm0AAAAAYkm8amenawYn_EBidiCQT5QBh0L1KJEX&scisig=AAGBfm0AAAAAYkm8at9X4P3eIWKUCOc6UriCEDKVsQE0&scisf=4&ct=citation&cd=-1&hl=en",
"https://scholar.googleusercontent.com/scholar.bib?q=info:6zRLFbcxtREJ:scholar.google.com/&output=citation&scisdr=CgWhqfi6GAA:AAGBfm0AAAAAYkm8bDoIhTlfTkQFCOzYGax54Bst576o&scisig=AAGBfm0AAAAAYkm8bMe_7Nq4e4pB5lg_eR9jmeGrO8ek&scisf=4&ct=citation&cd=-1&hl=en",
"https://scholar.googleusercontent.com/scholar.bib?q=info:6Yb0qOX88FMJ:scholar.google.com/&output=citation&scisdr=CgXn_4MdGAA:AAGBfm0AAAAAYkm8bi8ypCZcFDNEQZYZeoSlvx-U1OSk&scisig=AAGBfm0AAAAAYkm8bnFMnwTWGfkfJDCNEx0C4n-aQwql&scisf=4&ct=citation&cd=-1&hl=en",
"https://scholar.googleusercontent.com/scholar.bib?q=info:HFdEElNr3IgJ:scholar.google.com/&output=citation&scisdr=CgXKCFpQGAA:AAGBfm0AAAAAYkm8byukcQCl4WHQx-nSNp2pC1gUFSKG&scisig=AAGBfm0AAAAAYkm8b8EReTVkLwtxfth_pjwMyyY3dqts&scisf=4&ct=citation&cd=-1&hl=en",
"https://scholar.googleusercontent.com/scholar.bib?q=info:bs-D_MeC14YJ:scholar.google.com/&output=citation&scisdr=CgXEUXwWGAA:AAGBfm0AAAAAYkm8bwwfMNJrffe16EaGypsem9JlmGTi&scisig=AAGBfm0AAAAAYkm8b6nWlPOQL63fXg6dV2U-JQbpyQyS&scisf=4&ct=citation&cd=-1&hl=en",
"https://scholar.googleusercontent.com/scholar.bib?q=info:Rn1qFVLRfKwJ:scholar.google.com/&output=citation&scisdr=CgU-HswkGAA:AAGBfm0AAAAAYkm8cHE1YRK23eHV8nzF89Eem-Bsuz72&scisig=AAGBfm0AAAAAYkm8cDEj8ZrzZjAo2bNX-tjYYYJYQZay&scisf=4&ct=citation&cd=-1&hl=en",
"https://scholar.googleusercontent.com/scholar.bib?q=info:d8thHtTwq6YJ:scholar.google.com/&output=citation&scisdr=CgXj7oe9GAA:AAGBfm0AAAAAYkm8cTYamCKGKImjdg5MQdgbxUIIHAEY&scisig=AAGBfm0AAAAAYkm8cTcop1ceKzKYvKAKtvlSQ1EdEtSN&scisf=4&ct=citation&cd=-1&hl=en",
"https://scholar.googleusercontent.com/scholar.bib?q=info:IUmhOhGaDaEJ:scholar.google.com/&output=citation&scisdr=CgU0qZ2_GAA:AAGBfm0AAAAAYkm8ctCPwoihZkjbNcdEqSnwa0J3jwDy&scisig=AAGBfm0AAAAAYkm8cingBcYnEp8YRqFDFdN-FAEBgDT7&scisf=4&ct=citation&cd=-1&hl=en",
"https://scholar.googleusercontent.com/scholar.bib?q=info:PWsf8O5OMQEJ:scholar.google.com/&output=citation&scisdr=CgVBAJxXGAA:AAGBfm0AAAAAYkm8c3CDKQG0Wh_lWsXU_DZxEJkwZz5y&scisig=AAGBfm0AAAAAYkm8c6I-HjAxD1Gy6FLFDRdxH_qU4OBr&scisf=4&ct=citation&cd=-1&hl=en",
"https://scholar.googleusercontent.com/scholar.bib?q=info:yGvgHH8ROuIJ:scholar.google.com/&output=citation&scisdr=CgXFuhOkGAA:AAGBfm0AAAAAYkm8dD0rcSR4LQF8GgTxx865BADtXNDN&scisig=AAGBfm0AAAAAYkm8dIQhodz3rHF9IUdaCSRlhdudACNQ&scisf=4&ct=citation&cd=-1&hl=en"
]
Bibtex data from the first URL:
#article{woese2004new,
title={A new biology for a new century},
author={Woese, Carl R},
journal={Microbiology and molecular biology reviews},
volume={68},
number={2},
pages={173--186},
year={2004},
publisher={Am Soc Microbiol}
}
Disclaimer, I work for SerpApi.