php line graph - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

php line graph

Homerun31Homerun31 Posts: 99Member
I need to make a line graph in php but my problem is that I need to plot many data points on an image of 600px x 400px. Does anyone have any ideas of how to do this? I tried searching around for a solution but haven't found one. The graph would need more than 600 datapoints so I would have to separate each point by less than 1px which I don't think is possible. I have seen graphs online that plot more than 20,000 data points on relatively smaller images. So I know its possible I just don't know how to do it.

Thanks,
homerun31

Comments

  • mac_doggiemac_doggie Posts: 488Member
    Maybe you can resize your image. If you need a 600x400 image then you could create a 1200x800 image. each pixel worth half of its size... a pixel is the smallest part your display can show, so I really don't think there is any other way. On your printer you can alter the DPI settings so you can add double the amount of pixels in an inch, the size of the image will reduce to the normal size it would be when you printed a 600x400 image on the normal DPI settings...

    Hope I'm talking any sense here... It's all I could think of...
    ;-)
    -mac-
    mailto:[email protected]
    the Netherlands...
  • MrUmunhumMrUmunhum Posts: 3Member
    You could use ImageMagick to resize or use GNUplot to create a smaller image?

    What OS are you running under??
    William Estrada
    [email protected]
    Mt-Umunhum-Wireless.net ( http://64.124.13.3 )
  • HolodHolod Posts: 5Member
    I have found an example of a simple line graph
    [code]
    PHP:
    1.<?php
    2.
    3.class ImagickLineGraph extends ImagickDraw
    4.{
    5. /* Maximum value in the graph */
    6. public $maxValue = 0;
    7.
    8. /* Graph data */
    9. private $values;
    10.
    11. /* how large steps to take on the y axis values */
    12. private $yValueStep;
    13. private $yStepPixels;
    14.
    15. /* The canvas to draw the graph on*/
    16. private $canvas;
    17.
    18. /* Amount of steps on the left hand side */
    19. private $amount;
    20.
    21. /* The size of the graph viewport */
    22. private $viewPortSize;
    23.
    24. public function __construct( array $values )
    25. {
    26. /* Loop trough values and find the max value */
    27. foreach ( $values as $key => $value )
    28. {
    29. if ( $value['value']> $this->maxValue )
    30. {
    31. $this->maxValue = $value['value'];
    32. }
    33. }
    34.
    35. /* Set the values into a property */
    36. $this->values = $values;
    37.
    38. /* How large steps to take on the left hand side values */
    39. $this->yValueStep = 20;
    40. $this->yStepPixels = 20;
    41.
    42. /* Create the horizontal lines */
    43. $this->createHorizontalLines();
    44.
    45. /* Create the captions for the values */
    46. $this->createCaptions();
    47.
    48. /* Create the polyline that represents the values */
    49. $this->createGraphLine();
    50. }
    51.
    52. private function createHorizontalLines()
    53. {
    54. /* The amount of vertical lines to draw */
    55. $this->amount = ceil( $this->maxValue / $this->yValueStep );
    56.
    57. /* Use zero based origo but render it a bit lower */
    58. $this->translate( 0, 20 );
    59.
    60. /* Set the viewport */
    61. $this->viewPortSize = $this->amount * $this->yStepPixels;
    62.
    63. /* Loop trough the amount of values.. */
    64. for ( $i = 0 ; $i <= $this->amount; $i++ )
    65. {
    66. /* Y position on the image */
    67. $yPos = $this->yStepPixels * $i;
    68.
    69. /* Draw a horizontal line*/
    70. $this->line( 50, $yPos, 490, $yPos );
    71.
    72. /* Create line for this value */
    73. $this->annotation( 20, $this->viewPortSize - $yPos, $yPos );
    74. }
    75. }
    76.
    77. private function createCaptions()
    78. {
    79. /* Store the line coordinates to this array */
    80. $this->polylineCoordinates = array();
    81.
    82. $xStep = 500 / count( $this->values );
    83.
    84. /* Temporary imagick object to use to get font metrics */
    85. $im = new Imagick();
    86.
    87. /* Loop trough values.. */
    88. foreach ( $this->values as $key => $value )
    89. {
    90. /* The position for the vertical lines */
    91. $pos = 50 + ( $xStep * $key ) + 30;
    92.
    93. /* Query the text properties for the caption */
    94. $properties = $im->queryFontMetrics( $this, $value['caption'] );
    95.
    96. /* Place the text in the middle of the vertical line */
    97. $tPos = $pos - ( $properties['textWidth'] / 2 );
    98.
    99. /* Write the caption, one step below the actual graph */
    100. $this->annotation( $tPos,
    101. ( $this->amount * $this->yStepPixels ) + $this->yStepPixels,
    102. $value['caption'] );
    103.
    104. /* Draw the vertical line for this value */
    105. $this->line( $pos, $this->amount * $this->yStepPixels, $pos, 0 );
    106.
    107. /* Set the polyline coordinate for this line */
    108. $this->polylineCoordinates[$key]['x'] = $pos ;
    109.
    110. /* And the Y coordinate */
    111. $this->polylineCoordinates[$key]['y'] =
    112. $this->viewPortSize - $value['value'];
    113. }
    114.
    115. }
    116.
    117. function createGraphLine()
    118. {
    119. /* Use transparent fill, otherwise polyline is filled with black */
    120. $this->setFillColor( new ImagickPixel( "transparent" ) );
    121.
    122. /* Stroke the polyline with red color*/
    123. $this->setStrokeColor( new ImagickPixel( "red" ) );
    124.
    125. /* Antialias the line */
    126. $this->setStrokeAntialias( true );
    127.
    128. /* Draw the polyline from the coordinates */
    129. $this->polyLine( $this->polylineCoordinates );
    130. }
    131.}
    132./* values to show in the graph */
    133.$values = array (
    134. array( "caption" => "Random", "value" => 202.54 ),
    135. array( "caption" => "Random again", "value" => 34.2 ),
    136. array( "caption" => "Something", "value" => 59.5 ),
    137. array( "caption" => "Test", "value" => 210.34 ),
    138. array( "caption" => "Sporks", "value" => 110.34 ),
    139. );
    140.try
    141.{
    142. /* Create a new graph */
    143. $graph = new ImagickLineGraph( $values );
    144.
    145. /* Initialize the canvas */
    146. $canvas = new Imagick();
    147.
    148. /* Create empty image */
    149. $canvas->newImage( 500, $graph->maxValue + 150,
    150. new ImagickPixel( "white" ) );
    151.
    152. /* Set the image format to png */
    153. $canvas->setImageFormat( "png" );
    154.
    155. /* Render the graph on the canvas */
    156. $canvas->drawImage( $graph );
    157.
    158.}
    159.catch ( Exception $e )
    160.{
    161. echo $e->getMessage();
    162. die();
    163.}
    164.header( "Content-Type: image/png" );
    165.echo $canvas;
    166.
    167.?>
    [/code]
    Maybe it can help you. But I'm sure you need learn many [link=http://phpforms.net/tutorial/tutorial.html]php tutorials[/link] to get what you want
  • HolodHolod Posts: 5Member
    I have found an example of a simple line graph
    [code]
    PHP:
    1.<?php
    2.
    3.class ImagickLineGraph extends ImagickDraw
    4.{
    5. /* Maximum value in the graph */
    6. public $maxValue = 0;
    7.
    8. /* Graph data */
    9. private $values;
    10.
    11. /* how large steps to take on the y axis values */
    12. private $yValueStep;
    13. private $yStepPixels;
    14.
    15. /* The canvas to draw the graph on*/
    16. private $canvas;
    17.
    18. /* Amount of steps on the left hand side */
    19. private $amount;
    20.
    21. /* The size of the graph viewport */
    22. private $viewPortSize;
    23.
    24. public function __construct( array $values )
    25. {
    26. /* Loop trough values and find the max value */
    27. foreach ( $values as $key => $value )
    28. {
    29. if ( $value['value']> $this->maxValue )
    30. {
    31. $this->maxValue = $value['value'];
    32. }
    33. }
    34.
    35. /* Set the values into a property */
    36. $this->values = $values;
    37.
    38. /* How large steps to take on the left hand side values */
    39. $this->yValueStep = 20;
    40. $this->yStepPixels = 20;
    41.
    42. /* Create the horizontal lines */
    43. $this->createHorizontalLines();
    44.
    45. /* Create the captions for the values */
    46. $this->createCaptions();
    47.
    48. /* Create the polyline that represents the values */
    49. $this->createGraphLine();
    50. }
    51.
    52. private function createHorizontalLines()
    53. {
    54. /* The amount of vertical lines to draw */
    55. $this->amount = ceil( $this->maxValue / $this->yValueStep );
    56.
    57. /* Use zero based origo but render it a bit lower */
    58. $this->translate( 0, 20 );
    59.
    60. /* Set the viewport */
    61. $this->viewPortSize = $this->amount * $this->yStepPixels;
    62.
    63. /* Loop trough the amount of values.. */
    64. for ( $i = 0 ; $i <= $this->amount; $i++ )
    65. {
    66. /* Y position on the image */
    67. $yPos = $this->yStepPixels * $i;
    68.
    69. /* Draw a horizontal line*/
    70. $this->line( 50, $yPos, 490, $yPos );
    71.
    72. /* Create line for this value */
    73. $this->annotation( 20, $this->viewPortSize - $yPos, $yPos );
    74. }
    75. }
    76.
    77. private function createCaptions()
    78. {
    79. /* Store the line coordinates to this array */
    80. $this->polylineCoordinates = array();
    81.
    82. $xStep = 500 / count( $this->values );
    83.
    84. /* Temporary imagick object to use to get font metrics */
    85. $im = new Imagick();
    86.
    87. /* Loop trough values.. */
    88. foreach ( $this->values as $key => $value )
    89. {
    90. /* The position for the vertical lines */
    91. $pos = 50 + ( $xStep * $key ) + 30;
    92.
    93. /* Query the text properties for the caption */
    94. $properties = $im->queryFontMetrics( $this, $value['caption'] );
    95.
    96. /* Place the text in the middle of the vertical line */
    97. $tPos = $pos - ( $properties['textWidth'] / 2 );
    98.
    99. /* Write the caption, one step below the actual graph */
    100. $this->annotation( $tPos,
    101. ( $this->amount * $this->yStepPixels ) + $this->yStepPixels,
    102. $value['caption'] );
    103.
    104. /* Draw the vertical line for this value */
    105. $this->line( $pos, $this->amount * $this->yStepPixels, $pos, 0 );
    106.
    107. /* Set the polyline coordinate for this line */
    108. $this->polylineCoordinates[$key]['x'] = $pos ;
    109.
    110. /* And the Y coordinate */
    111. $this->polylineCoordinates[$key]['y'] =
    112. $this->viewPortSize - $value['value'];
    113. }
    114.
    115. }
    116.
    117. function createGraphLine()
    118. {
    119. /* Use transparent fill, otherwise polyline is filled with black */
    120. $this->setFillColor( new ImagickPixel( "transparent" ) );
    121.
    122. /* Stroke the polyline with red color*/
    123. $this->setStrokeColor( new ImagickPixel( "red" ) );
    124.
    125. /* Antialias the line */
    126. $this->setStrokeAntialias( true );
    127.
    128. /* Draw the polyline from the coordinates */
    129. $this->polyLine( $this->polylineCoordinates );
    130. }
    131.}
    132./* values to show in the graph */
    133.$values = array (
    134. array( "caption" => "Random", "value" => 202.54 ),
    135. array( "caption" => "Random again", "value" => 34.2 ),
    136. array( "caption" => "Something", "value" => 59.5 ),
    137. array( "caption" => "Test", "value" => 210.34 ),
    138. array( "caption" => "Sporks", "value" => 110.34 ),
    139. );
    140.try
    141.{
    142. /* Create a new graph */
    143. $graph = new ImagickLineGraph( $values );
    144.
    145. /* Initialize the canvas */
    146. $canvas = new Imagick();
    147.
    148. /* Create empty image */
    149. $canvas->newImage( 500, $graph->maxValue + 150,
    150. new ImagickPixel( "white" ) );
    151.
    152. /* Set the image format to png */
    153. $canvas->setImageFormat( "png" );
    154.
    155. /* Render the graph on the canvas */
    156. $canvas->drawImage( $graph );
    157.
    158.}
    159.catch ( Exception $e )
    160.{
    161. echo $e->getMessage();
    162. die();
    163.}
    164.header( "Content-Type: image/png" );
    165.echo $canvas;
    166.
    167.?>
    [/code]
    Maybe it can help you. But I'm sure you need learn many [link=http://phpforms.net/tutorial/tutorial.html]php tutorials[/link] to get what you want
  • arindraarindra Posts: 7Member
    U can try solving your problem through the GD library. Check out once...Hope it works.
Sign In or Register to comment.