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);
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 a popular geoplugin to send a amount to and then currency convert it based on the user IP. What comes back in this case is £7.62. I want to round it up and drop the decimals. So in this case £8.
I have tried a few things but all i get is a long number output, like: 165439.
<?php
require_once('/resources/geoplugin/geoplugin.class.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();
$minDeposit = $output[$fname]['mindeposit'];
$convertMinDeposit = $geoplugin->convert($minDeposit);
echo $convertMinDeposit;
?>
Definition of convert is
function convert($amount, $float=2, $symbol=true) { ... }
so second argument can be used to define wanted accuracy, i.e. use
$convertMinDeposit = $geoplugin->convert($minDeposit, 0);
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.
We have displayed the student statistics for each departments in our application. Everything is working fine for us. But the designs are getting overlap for each student stats.
FYI, See the below screenshot
Here the stats % 0.3 and 1.0 are getting overlapped.
Code:
$color=array("#5c61fb","#bf7a7c","#f46163","#f4fafb","#f4fa63",
"#f4c463","#5cfa63","#bc74f2","#66e1e3","#f4a0ce");
$graph = new PieGraph(750,400,"auto");
$graph->SetShadow();
$graph->SetBackgroundImage('logo.jpg',BGIMG_COPY);
$graph->legend->SetFillColor('lightblue#0.7');
$graph->title->Set("Résultat $_GET[annee] $_GET[bourse]");
//$p1 = new PiePlot3D($arrayx_val);
$p1 = new PiePlot3D($arrayy);
$p1->SetSize(0.5);
$p1->SetCenter(0.4);
$p1->SetSliceColors($color);
$p1->SetLegends($arrayx);
$p1->value->SetFormat('%.1f%%');
//$p1->value->SetFormat("%.5d%%"); //$p1->SetLabelType(PIE_VALUE_ABS);
$p1->SetLabelPos(1);
$p1->value->HideZero();
//$p1->value->SetFont( FF_FONT1, FS_BOLD);
//$p1->value->SetMargin(50);
$p1->SetLabelMargin(20);
//$p1->SetShadow();
$graph->footer->left->SetFont( FF_FONT2, FS_BOLD);
$graph->footer->left->Set("Exclusion : ".$array_footer[0]." Abandon : ".$array_footer[1]." Démission : ".$array_footer[2]."");
$graph->footer-> center-> SetColor("red");
$graph->Add($p1);
$graph->Stroke();
So kindly assist our request and how to avoid this overlapping design as per the screenshot.
Thanks.