I am trying to integrate a chart into a website. I tested the gd2 installment by trying to see the examples, which works, see here. Then I went ahead and integrate it in my php file, using this code:
include("grafice/class/pData.class.php");
include("grafice/class/pDraw.class.php");
include("grafice/class/pImage.class.php");
//creaza si populeaza obiectul
$MyData = new pData();
$MyData->addPoints(array(4,VOID,VOID,10,8,3),"Proba 1");
$MyData->addPoints(array(3,10,5,8,5,5),"Proba 2");
$MyData->addPoints(array(2,7,5,1,9,2),"Proba 3");
$MyData->setSerieTicks("Proba 2",4);
$MyData->setSerieWeight("Proba 3",2);
$MyData->setAxisName(0,"Note");
$MyData->addPoints(array("Ian","Feb","Mar","Apr","Mai","Iun"),"Labels");
$MyData->setSerieDescription("Labels","Luni");
$MyData->setAbscissa("Labels");
// creaza obiectul chart
$myPicture = new pImage(700,230,$MyData);
//deseneaza fundalul
$Settings = array("R"=>170, "G"=>183, "B"=>87, "Dash"=>1, "DashR"=>190, "DashG"=>203, "DashB"=>107);
$myPicture->drawFilledRectangle(0,0,700,230,$Settings);
// suprapune un gradient
$Settings = array("StartR"=>219, "StartG"=>231, "StartB"=>139, "EndR"=>1, "EndG"=>138, "EndB"=>68, "Alpha"=>50);
$myPicture->drawGradientArea(0,0,700,230,DIRECTION_VERTICAL,$Settings);
$myPicture->drawGradientArea(0,0,700,20,DIRECTION_VERTICAL,array("StartR"=>0,"StartG"=>0,"StartB"=>0,"EndR"=>50,"EndG"=>50,"EndB"=>50,"Alpha"=>80));
// bordura
$myPicture->drawRectangle(0,0,699,229,array("R"=>0,"G"=>0,"B"=>0));
// titlul pozei
$myPicture->setFontProperties(array("FontName"=>"fonts/Silkscreen.ttf","FontSize"=>6));
$myPicture->drawText(10,13,"EXEMPLU",array("R"=>255,"G"=>255,"B"=>255));
// titlul graficului
$myPicture->setFontProperties(array("FontName"=>"fonts/Forgotte.ttf","FontSize"=>11));
$myPicture->drawText(250,55,"Media Notelor",array("FontSize"=>20,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE));
//deseneaza marimea 1
$myPicture->setGraphArea(60,60,450,190);
$myPicture->drawFilledRectangle(60,60,450,190,array("R"=>255,"G"=>255,"B"=>255,"Surrounding"=>-200,"Alpha"=>10));
$AxisBoundaries = array(0=>array("Min"=>0,"Max"=>10));
$scaleSettings = array("GridR"=>200,"GridG"=>200,"GridB"=>200,"DrawSubTicks"=>TRUE,"CycleBackground"=>TRUE,"Mode"=>SCALE_MODE_MANUAL, "ManualScale"=>$AxisBoundaries);
$myPicture->drawScale($scaleSettings);
$myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10));
$myPicture->setFontProperties(array("FontName"=>"fonts/pf_arma_five.ttf","FontSize"=>6));
$myPicture->drawLineChart(array("DisplayValues"=>TRUE,"DisplayColor"=>DISPLAY_AUTO));
$myPicture->setShadow(FALSE);
//deseneaza marimea 2
$myPicture->setGraphArea(500,60,670,190);
$myPicture->drawFilledRectangle(500,60,670,190,array("R"=>255,"G"=>255,"B"=>255,"Surrounding"=>-200,"Alpha"=>10));
$myPicture->drawScale(array("Pos"=>SCALE_POS_TOPBOTTOM,"DrawSubTicks"=>TRUE));
$myPicture->setShadow(TRUE,array("X"=>-1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10));
$myPicture->drawLineChart();
$myPicture->setShadow(FALSE);
// deseneaza legenda
$myPicture->drawLegend(510,205,array("Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL));
//rendereaza poza
$myPicture->autoOutput("img/example.drawLineChart.png");
Unfortunately, this renders the whole page like so: (I am uploading a picture since you need an account to see it)
Did I did something wrong? I used this code in a function.
ALSO, in WAMP examples not work. Any way around that?
I managed to do it, and posting here to help others!
so, I made the function, but I edited the BOLD part
function grafice()
{
/* pChart library inclusions */
include("../class/pData.class.php");
include("../class/pDraw.class.php");
include("../class/pImage.class.php");
/* Create and populate the pData object */
$MyData = new pData();
$MyData->addPoints(array(10,5,7,8), "Probe 1");
$MyData->setSerieWeight("Probe 1",2);
$MyData->setAxisName(0,"Media Generala");
$MyData->addPoints(array("Ian","Feb","Mar","Apr","Mai","Iun"),"Labels");
$MyData->setSerieDescription("Labels","Luni");
$MyData->setAbscissa("Labels");
$serieSettings = array("R"=>255,"G"=>185,"B"=>11);
$MyData->setPalette("Probe 1",$serieSettings);
/* Create the pChart object */
$myPicture = new pImage(500,230,$MyData);
/* Draw the background */
$Settings = array("R"=>248, "G"=>226, "B"=>174, "Dash"=>1, "DashR"=>190, "DashG"=>203, "DashB"=>107);
$myPicture->drawFilledRectangle(0,0,700,230,$Settings);
/* Overlay with a gradient */
$Settings = array("StartR"=>238, "StartG"=>216, "StartB"=>174, "EndR"=>238, "EndG"=>216, "EndB"=>174, "Alpha"=>50);
$myPicture->drawGradientArea(0,0,700,230,DIRECTION_VERTICAL,$Settings);
$myPicture->drawGradientArea(0,0,700,20,DIRECTION_VERTICAL,array("StartR"=>0,"StartG"=>0,"StartB"=>0,"EndR"=>50,"EndG"=>50,"EndB"=>50,"Alpha"=>80));
/* Add a border to the picture */
$myPicture->drawRectangle(0,0,499,229,array("R"=>0,"G"=>0,"B"=>0));
/* Write the picture title */
$myPicture->setFontProperties(array("FontName"=>"../fonts/Silkscreen.ttf","FontSize"=>6));
$myPicture->drawText(10,13,"Evolutia Mediei Generale",array("R"=>255,"G"=>255,"B"=>255));
/* Write the chart title */
$myPicture->setFontProperties(array("FontName"=>"../fonts/Forgotte.ttf","FontSize"=>11));
$myPicture->drawText(250,55,"Media Generala",array("FontSize"=>20,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE));
/* Draw the scale and the 1st chart */
$AxisBoundaries = array(0=>array("Min"=>0,"Max"=>10));
$scaleSettings = array("DrawSubTicks"=>TRUE,"Mode"=>SCALE_MODE_MANUAL, "ManualScale"=>$AxisBoundaries);
$myPicture->setGraphArea(60,60,450,190);
$myPicture->drawFilledRectangle(60,60,450,190,array("R"=>0,"G"=>0,"B"=>0,"Surrounding"=>-200,"Alpha"=>10));
$myPicture->drawScale($scaleSettings);
$myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10));
$myPicture->setFontProperties(array("FontName"=>"../fonts/pf_arma_five.ttf","FontSize"=>10,"R"=>0,"G"=>0,"B"=>0));
$myPicture->drawLineChart(array("DisplayValues"=>TRUE,"DisplayR"=>0, "DisplayG"=>0, "DisplayB"=>0, "DisplayOffset"=>10));
$myPicture->setShadow(FALSE);
/* Render the picture (choose the best way) */
/* $myPicture->**( I DELETED ThIS)--> autoOutput <---** ("pictures/example.drawLineChart.png"); and added */
$myPicture->render("pictures/example.drawLineChart.png");
}
Then, when I want to call it, like so :
<?php grafice(); ?>
And then I can do:
<img src="pictures/example.drawLineChart.png" />
Now I just change the picture name with the session id, and so I will have a different graph for everyone ;) Hopefully this will help someone!
Related
I'm using Behat and Mink to test an application and I have this function:
/**
* #Then é gerado o PDF do cupom selecionado
*/
public function eGeradoOPdfDoCupomSelecionado()
{
$session = $this->getSession();
$windowNames = $session->getWindowNames();
if (sizeof($windowNames) < 2) {
throw new \ErrorException("2 Janelas deveriam estar abertas");
}
//You can even switch to that window
$session->switchToWindow($windowNames[1]);
}
I want to assert that the new tab have a pdf file. How can I do that?
Check the url contains .pdf and if you know the name of the file also check that, additionally you see what libraries are available for reading the file and add a check for the content of the file.
I have generated the bar graph using php.I have downloaded class library from this link. http://www.pchart.net/download.I am getting the graph.
what i want to do is after creating the image,it should not output to the browser.Instead of that I want to save the images in particular folder. what should i do for that? Thanks in advance.This is my code
include("pChart/class/pData.class.php");
include("pChart/class/pDraw.class.php");
include("pChart/class/pPie.class.php");
include("pChart/class/pImage.class.php");
$MyData = new pData();
$MyData->addPoints($val,"ScoreA");
$MyData->setSerieDescription("ScoreA","Application A");
/* Define the absissa serie */
$MyData->addPoints($keys,"Labels");
$MyData->setAbscissa("Labels");
/* Create the pChart object */
$myPicture = new pImage(300,260,$MyData);
/* Draw a solid background */
$Settings = array("R"=>170, "G"=>183, "B"=>87, "Dash"=>1, "DashR"=>190, "DashG"=>203, "DashB"=>107);
$myPicture->drawFilledRectangle(0,0,300,300,$Settings);
/* Overlay with a gradient */
$Settings = array("StartR"=>219, "StartG"=>231, "StartB"=>139, "EndR"=>1, "EndG"=>138, "EndB"=>68, "Alpha"=>50);
$myPicture->drawGradientArea(0,0,300,260,DIRECTION_VERTICAL,$Settings);
$myPicture->drawGradientArea(0,0,300,20,DIRECTION_VERTICAL,array("StartR"=>0,"StartG"=>0,"StartB"=>0,"EndR"=>50,"EndG"=>50,"EndB"=>50,"Alpha"=>100));
/* Add a border to the picture */
$myPicture->drawRectangle(0,0,299,259,array("R"=>0,"G"=>0,"B"=>0));
/* Write the picture title */
$myPicture->setFontProperties(array("FontName"=>"../fonts/Silkscreen.ttf","FontSize"=>6));
$myPicture->drawText(10,13,"pPie - Draw 2D pie charts",array("R"=>255,"G"=>255,"B"=>255));
/* Set the default font properties */
$myPicture->setFontProperties(array("FontName"=>"pChart/fonts/Forgotte.ttf","FontSize"=>10,"R"=>80,"G"=>80,"B"=>80));
/* Create the pPie object */
$PieChart = new pPie($myPicture,$MyData);
/* Draw an AA pie chart */
$PieChart->draw3DPie(160,140,array("Radius"=>70,"DrawLabels"=>TRUE,"LabelStacked"=>TRUE,"Border"=>TRUE));
/* Write the legend box */
$myPicture->setShadow(FALSE);
$PieChart->drawPieLegend(15,40,array("Alpha"=>20));
$myPicture->autoOutput("image");
Try to use:
$myPicture->render("image_name.png");
It worked for me in 1.x, don't know about 2.x - did not used it.
render() is documented in the pChart wiki on below link:
http://wiki.pchart.net/doc.faq.script.output.html
I'm having an issue with imagick running on a MAMP installation within a cakephp app.
I have followed the install instructions as indicated here and the install seems to have worked in the sense that the class 'Imagick' exists for me if i test it in a php script (I can see the module is loaded in phpinfo). However any examples that I run utilising the class hang as soon as I echo any content. My view is:
<?php
/* Create a new imagick object */
$im = new Imagick();
/* Create new image. This will be used as fill pattern */
$im->newPseudoImage(50, 50, "gradient:red-black");
/* Create imagickdraw object */
$draw = new ImagickDraw();
/* Start a new pattern called "gradient" */
$draw->pushPattern('gradient', 0, 0, 50, 50);
/* Composite the gradient on the pattern */
$draw->composite(Imagick::COMPOSITE_OVER, 0, 0, 50, 50, $im);
/* Close the pattern */
$draw->popPattern();
/* Use the pattern called "gradient" as the fill */
$draw->setFillPatternURL('#gradient');
/* Set font size to 52 */
$draw->setFontSize(52);
/* Annotate some text */
$draw->annotation(20, 50, "Hello World!");
/* Create a new canvas object and a white image */
$canvas = new Imagick();
$canvas->newImage(350, 70, "white");
/* Draw the ImagickDraw on to the canvas */
$canvas->drawImage($draw);
/* 1px black border around the image */
$canvas->borderImage('black', 1, 1);
/* Set the format to PNG */
$canvas->setImageFormat('png');
/* Output the image */
header("Content-Type: image/png");
echo $canvas;
?>
This script will just hang as soon as the echo $canvas is encountered. The script works perfectly in a plain old php file i.e outside of cake but it hangs when visited via my cakephp app action. My action code is:
public function test(){
$this->layout = false;
}
Cake error log is empty.
Okay the issue was with how the header was set in cakephp. Cake didn't allow me to set the page header in the view that way.
I added the following code to the test action:
$this->response->type("image/png");
And it works perfectly now.
I don't believe this is possible, and searching on Google didn't yield any results, but I thought it never hurts to ask.
I'm trying to implement Google Chart on my site via a PHP library. A library I found that I really like (googlechartphplib) has about 10 different class files for every type of chart. This means that in order to create a pie chart I must use $chart = new GooglePieChart(); whereas to create a QR Code I must use $chart = new GoogleQRCode();, etc.
However when I actually looked into using the API, I noticed that the type of chart is passed to the constructor (it is saved and then later passed to the API as part of the query string). For instance, the code to make a line graph isn't just $chart = new GoogleChart();, it's $chart = new GoogleChart('lc', 500, 200); (where lc defines the "line chart", 500 and 200 are dimensions)
This got me thinking: why can't I just read this first parameter to determine which type of chart to create? Have one universal constructor:
$piechart = new GoogleChart('pie');
$linechart = new GoogleChart('lc');
$qrcode = new GoogleChart('qr');
...
I can think of a way to do this using switch/case statements in all of my function calls. For example:
public function computeQuery() {
switch( $this->type ) {
case 'qr':
/* QR code function */
break;
case 'pie':
/* Pie chart function */
break;
case 'lc':
default:
/* line chart code */
break;
}
However this would involve re-writing all of the code already present (expedited slightly by my ability to copy/paste 90% of the code). Is there a way to simply choose which class the resulting object should be based on the constructor parameters? Example:
public function __construct($type, $x, $y) {
$this->type = $type;
switch( $type ) {
case 'qr':
return new GoogleQRCode($x, $y);
case 'pie':
return new GooglePieChart($x, $y);
case 'lc':
default:
$this->width = $x;
$this->height = $y;
}
}
Not in the constructor, you can't.
That's one reason that Factories exist.
I'm using Ming to create an library swf, using the first code example below. How can I access the embedded png from my Flex application? Here's the php/ming code:
<?php
// Ming to create Library.swf
//-----------------------------------
// Create background...
Ming_setScale(20.0000000);
$movie = new SWFMovie();
ming_useswfversion(7);
$movie->setDimension(550,400);
$movie->setBackground(200, 200, 200);
// Load png file...
$img_file = "src/assets/page0.png";
$png = new SWFBitmap(fopen($img_file, "rb"));
// Add png to movie...
$movie->add($png);
// Export png
$movie->addExport($png, 'png');
$movie->writeExports();
// Save movie to swf
$swfname = dirname(__FILE__);
$swfname .= "/bin-debug/Library.swf";
$movie->save($swfname, 9);
?>
And here's my flex essay:
// Loading Library.swf (works), trying to access png asset (doesn't work)
private var loader:Loader = new Loader();
private function onCreationComplete():void {
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest('Library.swf'));
}
private function onComplete(e:Event):void {
var resourceClass:Class = loader.contentLoaderInfo.applicationDomain.getDefinition("png") as Class;
}
I'm not sure that the png is exported properly. Testing the Library.swf with SwfUtils code (swfutils.riaforge.org) doesn't show any exported classes at all. Or maybe something else is wrong?
well I have the same problem
I create a library with ming and try to access it from flex and doesn't work.
I've only used this because I needed somehow for an certain url using the Loader class to retrieve the Class object that I can use it to set backgroundImage for a Canvas for example.
But I think that swf exported by ming my have a lower version than the flex compiled swf, and it can't recognize the embedded class name unfortunately.
You need to use assignSymbol function within code.
#!/usr/bin/ruby
require 'ming/ming'
include Ming
use_SWF_version(9)
set_scale(20.00000000)
#m = SWFMovie.new
#m.set_dimension(640, 480)
#bm = SWFBitmap.new("./common/MatrixFilter.jpg")
#m.add(#bm)
#text = SWFText.new
#font = SWFFont.new("./common/test.ttf")
#text.set_font(#font)
#text.set_color(0, 0, 0, 0xff)
#text.set_height(20)
#text.move_to(100, 100)
#text.add_string( "The quick brown fox jumps over the lazy dog. 1234567890")
#i1 = #m.add(#text)
#i1.set_depth(1)
#m.next_frame
#m.assign_symbol(#text, "mytext")
#m.assign_symbol(#bm,"mybitmap")
#m.save("assignSymbol.swf")
Then use something like this in Flex: (FlashDevelop project)
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.events.Event;
/**
* ...
* #author DefaultUser (Tools -> Custom Arguments...)
*/
public class Main extends Sprite
{
[Embed(source="my_clip.swf", symbol="circle")]
private static var Circle:Class;
[Embed(source="App.swf", symbol="star")]
private static var Star:Class;
[Embed(source="App.swf", symbol="square")]
private static var Square:Class;
[Embed(source = 'assignSymbol.swf', symbol = 'mytext')]
private static var Mytext:Class;
[Embed(source='assignSymbol.swf', symbol='mybitmap')]
private static var Mybitmap:Class;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
var circle:Sprite = new Circle();
addChild(circle);
circle.x = 100;
circle.y = 100;
var star:Sprite = new Star();
addChild(star);
star.x = 200;
star.y = 100;
var square:Sprite = new Square();
addChild(square);
square.x = 300;
square.y = 100;
var mybitmap:Bitmap = new Mybitmap();
addChild(mybitmap);
mybitmap.x = 300;
mybitmap.y = 300;
var mytext:Sprite = new Mytext();
addChild(mytext);
mytext.x = 0;
mytext.y = 200;
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
}
}
}