View unanswered posts | View active topics It is currently Mon Apr 29, 2024 5:49 am



Reply to topic  [ 2 posts ] 
Obtain the position of the added points 
Author Message
New pChart user
New pChart user

Joined: Wed Jun 06, 2012 8:52 am
Posts: 1
 Obtain the position of the added points
Hello,

I want to add some sort of label to my ploted data (see attached file ).
Is ther a way to get the X and Y of the points in order to draw text beside ?

Thanks for any tips ;)

Code:
<?php
/* pChart library inclusions */
include("./pChart2.1.3/class/pData.class.php");
include("./pChart2.1.3/class/pDraw.class.php");
include("./pChart2.1.3/class/pImage.class.php");
include("./pChart2.1.3/class/pIndicator.class.php");
/* Create and populate the pData object */
$MyData       = new pData();
$TextSettings = array(
    "R" => 0,
    "G" => 0,
    "B" => 0,
    "Angle" => 0,
    "FontSize" => 6
);
for ($i = 0; $i < 5; $i++) {
    $MyData->addPoints(rand(1, 20), "Tech Gérald");
}
for ($i = 0; $i < 5; $i++) {
    $MyData->addPoints(rand(1, 20), "Tech Robert");
}
for ($i = 0; $i < 5; $i++) {
    $MyData->addPoints(rand(1, 20), "Tech Laurent");
}
for ($i = 0; $i < 5; $i++) {
}
for ($i = 0; $i < 5; $i++) {
    $MyData->addPoints(rand(1, 20), "Tech Yorick");
}
for ($i = 0; $i < 5; $i++) {
    $MyData->addPoints(rand(1, 20), "Tech Jimmy");
}
$arrayParcours    = array();
$arrayParcours[1] = "ADSL SFR" . "\n" . "temps conseillé : 3 heures";
$arrayParcours[2] = "Fibre" . "\n" . "temps conseillé : 3 heures";
$arrayParcours[3] = "spécia. forma/assistance" . "\n" . "et ADSL Virgin" . "\n" . "temps conseillé : 3 heures";
$arrayParcours[4] = "spécia. forma/assistance" . "\n" . "et ADSL Virgin" . "\n" . "temps conseillé : 3 heures";
$arrayParcours[5] = "spécia. forma/assistance" . "\n" . "et ADSL Virgin" . "\n" . "temps conseillé : 3 heures";
$MyData->addPoints($arrayParcours, "Labels");
$MyData->setSerieDescription("Labels", "Parcours");
$MyData->setAbscissa("Labels");
$MyData->setAxisUnit(0, " heure(s)");
$MyData->setSerieShape("Tech Gérald", SERIE_SHAPE_FILLEDTRIANGLE);
$MyData->setSerieShape("Tech Robert", SERIE_SHAPE_FILLEDTRIANGLE);
$MyData->setSerieShape("Tech Kevin", SERIE_SHAPE_FILLEDTRIANGLE);
$MyData->setSerieShape("Tech Yorick", SERIE_SHAPE_FILLEDTRIANGLE);
$MyData->setSerieShape("Tech Laurent", SERIE_SHAPE_FILLEDTRIANGLE);
$MyData->setSerieShape("Tech Jimmy", SERIE_SHAPE_FILLEDTRIANGLE);
$MyData->setXAxisDisplay(AXIS_FORMAT_DEFAULT, "H:i");
$MyData->setAxisName(0, "Temps passé cumulé");
/* Create the pChart object */
$myPicture            = new pImage(900, 530, $MyData);
/* Turn of Antialiasing */
$myPicture->Antialias = True;
/* Add a border to the picture */
$myPicture->drawRectangle(0, 0, 890, 329, array(
    "R" => 0,
    "G" => 0,
    "B" => 0
));
/* Write the chart title */
$myPicture->setFontProperties(array(
    "FontName" => "./pChart2.1.3/fonts/Forgotte.ttf",
    "FontSize" => 11
));
$myPicture->drawText(250, 35, "Temps passé et état d'avancement du parcours", array(
    "FontSize" => 20,
    "Align" => TEXT_ALIGN_BOTTOMMIDDLE
));
/* Set the default font */
$myPicture->setFontProperties(array(
    "FontName" => "./pChart2.1.3/fonts/pf_arma_five.ttf",
    "FontSize" => 8
));
/* Define the chart area */
$myPicture->setGraphArea(80, 40, 750, 200);
/* Draw the scale */
$scaleSettings = array(
    "XMargin" => 10,
    "YMargin" => 10,
    "Floating" => TRUE,
    "GridR" => 200,
    "GridG" => 200,
    "GridB" => 200,
    "DrawSubTicks" => TRUE,
    "CycleBackground" => TRUE
);
$myPicture->drawScale($scaleSettings);
/* Turn on Antialiasing */
$myPicture->Antialias = TRUE;
$myPicture->setShadow(TRUE, array(
    "X" => 1,
    "Y" => 1,
    "R" => 0,
    "G" => 0,
    "B" => 0,
    "Alpha" => 10
));
/* Draw the line chart */
$myPicture->drawPlotChart(array(
    "BorderSize" => 1,
    "Surrounding" => 40,
    "BorderAlpha" => 100,
    "PlotSize" => 1,
    "PlotBorder" => TRUE,
    "DisplayValues" => false,
    "DisplayColor" => DISPLAY_AUTO
));
/* Write the chart legend */
$myPicture->drawLegend(780, 20, array(
    "Style" => LEGEND_NOBORDER,
    "Mode" => LEGEND_VERTICAL
));
//$myPicture->drawText(220,130,"Simple text",$TextSettings);

/* Render the picture (choose the best way) */
$myPicture->autoOutput("pictures/example.drawPlotChart.simple.png");




Attachments:
SNAG-4294.jpg
SNAG-4294.jpg [ 87.33 KiB | Viewed 5082 times ]
Wed Jun 06, 2012 9:02 am
Profile
Experienced pChart user
Experienced pChart user

Joined: Tue Aug 02, 2011 11:49 pm
Posts: 34
Post Re: Obtain the position of the added points
if you don't want to use writeLabel but instead drawText you will have to calculate the positions upon your data and settings for the image and graph area

your image is 900px wide and 530px high: $myPicture = new pImage(900, 530, $MyData);
within the graph area starting at 80px (X) and 40px (Y) from top left : $myPicture->setGraphArea(80, 40, 750, 200);
the area is 750px wide and 200px high and you're using margin 10px (X) and 10px (Y) in $scaleSettings

so the resulting area which displays your data is 740px wide and 190px high starting at 90,50 from top left within the image

it would be easier to get these things done, if you define your series in separate arrays instead of using the rand() function within $MyData->addPoints();
so let's guess you have extra arrays, let' call them $seriesTG, $seriesTR, $seriesTL, $seriesTY, $seriesTJ,
which will be used like $MyData->addPoints($seriesTG, "Tech Gérald"); and which you have filled with the random values before

you have 5 values per series which equals count($seriesTG), so there are 4 distances inbetween on the abscissa and the ordinate has its maximum at 25 heures

the abscissa starts at 90,240 within the image and which is the origin of this coordinate system
740px width / 4 distances = 185px between the added points
X-values for $seriesTG[i] (and for the other too) are:
[0] at 90px
[1] at 90px + 185px (= 275)
[2] at 275px + 185px (= 460)
[3] at 460px + 185px (= 645)
[4] at 645px + 185px (= 90 + 740) (= 830)

now you have to get the Y-values, which is quite simple too
190px height / 25 heures = 7.6 which gives us about 8px per hour
Y-values for $seriesTG[i] (and for the other too) are:
floor($seriesTG[0] * 190/25) = e.g. 15px (2 hours)
floor($seriesTG[1] * 190/25) = e.g. 38px (5 hours)
floor($seriesTG[2] * 190/25) = e.g. 91px (12 hours)
floor($seriesTG[3] * 190/25) = e.g. 60px (8 hours)
floor($seriesTG[4] * 190/25) = e.g. 76px (10 hours)

these Y-values must be subtracted from 240px abscissa y-position within the image (from top)
combined with the X-values from above
you get something like this for the $seriesTG
$seriesTG[0] at 90, 15
$seriesTG[1] at 275, 38
$seriesTG[2] at 460, 91
$seriesTG[3] at 645, 60
$seriesTG[4] at 830, 76

these are the coordinates for your drawText, if you calculate it with variables only, it should adapt to different series
perhaps you want a little space next to the points, so just add e.g. 8px to these X-values (and look at the Align option of drawText)


but i think the easier way is to use the built in writeLabel function and turn off all styling and only use plain text instead
thereto look at $LabelSettings options


Fri Jun 08, 2012 11:26 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 30 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron