2000 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 0 10 20 30 40 50 60 70 80

Amazing phpGraph

Writing svg histogram with php

Back to GitHub projectBack to ecyseo.net

Librairy updated on : December 14 2020 10:38:34.
<?php

//First we include the library

include('phpGraph.php');

//We call an instance of phpGraph() class

$G = new phpGraph();





//First sample

$flot = array(

    "0" => array(

            "0" => 2000,

            "1" => 10,

        ),



    "1" => array(

            "0" => 2002,

            "1" => 39,

        ),



    "2" => array(

            "0" => 2003,

            "1" => 47,

        ),



    "3" => array(

            "0" => 2004,

            "1" => 53,

        ),



    "4" => array(

            "0" => 2005,

            "1" => 46,

        ),



    "5" => array(

            "0" => 2006,

            "1" => 10,

        ),



    "6" => array(

            "0" => 2007,

            "1" => 98,

        ),



    "7" => array(

            "0" => 2008,

            "1" => 20,

        ),



    "8" => array(

            "0" => 2009,

            "1" => 16,

        ),



    "9" => array(

            "0" => 2010,

            "1" => 16,

        ),



    "10" => array(

            "0" => 2011,

            "1" => 12,

        ),



    "11" => array(

            "0" => 2012,

            "1" => 12,

        ),



    "12" => array(

            "0" => 2013,

            "1" => 2,

        ),



);

//We transforme multidimensionnal array to unidimensionnal array

$dataFirst  = array();

foreach ($flot as $key => $value) {

    $dataFirst[$value[0]] = $value[1];

}



//An other array

$data[0] = array(

    "2000" => 0,

    "2002" => 25,

    "2003" => 32,

    "2004" => 1,

    "2005" => 58,

    "2006" => 31,

    "2007" => 79,

    "2008" => 51,

    "2009" => 54,

    "2010" => 12,

    "2011" => 17,

    "2012" => 14,

    "2013" => 13

);

$data[1] = array(

    "2000" => 0,

    "2002" => 0,

    "2003" => 0,

    "2004" => 20,

    "2005" => 0,

    "2006" => 40,

    "2007" => 50,

    "2008" => 0,

    "2009" => 60,

    "2010" => 0,

    "2011" => 0,

    "2012" => 0,

    "2013" => 0

);

$data[2] = array(

    "2000" => 0,

    "2002" => -20,

    "2003" => -30,

    "2004" => 65,

    "2005" => 0,

    "2006" => 10,

    "2007" => 10,

    "2008" => 18,

    "2009" => 39,

    "2010" => 0,

    "2011" => 23,

    "2012" => 36,

    "2013" => 54

);

$data[3] = array(

    "2000" => 0,

    "2002" => 0,

    "2003" => 3,

    "2004" => 1,

    "2005" => 5,

    "2006" => 2,

    "2007" => 3,

    "2008" => 3,

    "2009" => 5,

    "2010" => 8,

    "2011" => 9,

    "2012" => 5,

    "2013" => 2

);

$data[4] = array(

    "2000" => 7,

    "2002" => 39,

    "2003" => 26,

    "2004" => 36,

    "2005" => 18,

    "2006" => 32,

    "2007" => 56,

    "2008" => 38,

    "2009" => 103,

    "2010" => 105,

    "2011" => 126,

    "2012" => 125,

    "2013" => 76,

);

Then we draw charts...

Simple example

 echo $G->draw($data[0]);
2000 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 0 10 20 30 40 50 60 70 80

Multi lines with histogram and pie

 echo $G->draw($data,array(

            'steps' => 50,//If we set steps it will not automatically calculated

            'filled'=>false,

            'multi'=>true,

            'tooltips'=>true,

            'diskLegends' => true,

            'diskLegendsType' => 'label',

            'type' => array(

            	'2'=>'pie',

                '3'=>'bar',

                '4'=>'ring',

            ),

            'stroke' => array(

                '0'=>'red',

                '1'=>'blue',

                '2'=>'orange',

                '3'=>'green',

                '4'=>'deeppink'

            ),

            'legends' => array(

                '0'=>'Serie 1',

                '1'=>'Serie 2',

                '2'=>'Serie 3',

                '3'=>'Serie 4',

                '4'=>'Serie 5',

            ),

            'tooltipLegend' => array(

                '0'=>'Sample of tooltip : ',

                '1'=>'Sample of tooltip : ',

                '2'=>'Sample of tooltip : ',

                '3'=>'Sample of tooltip : ',

                '4'=>'Sample of tooltip : ',

            ),

            'title' => 'Amazing phpGraph',

        )

    );
Amazing phpGraph Amazing phpGraph 2000 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2001 -30 20 70 120 Sample of tootlip : 0 Sample of tootlip : 25 Sample of tootlip : 32 Sample of tootlip : 1 Sample of tootlip : 58 Sample of tootlip : 31 Sample of tootlip : 79 Sample of tootlip : 51 Sample of tootlip : 54 Sample of tootlip : 12 Sample of tootlip : 17 Sample of tootlip : 14 Sample of tootlip : 13 Sample of tooltip : 0 Sample of tooltip : 0 Sample of tooltip : 0 Sample of tooltip : 20 Sample of tooltip : 0 Sample of tooltip : 40 Sample of tooltip : 50 Sample of tooltip : 0 Sample of tooltip : 60 Sample of tooltip : 0 Sample of tooltip : 0 Sample of tooltip : 0 Sample of tooltip : 0 Sample of tooltip : 0 Sample of tooltip : 10 Sample of tooltip : 3 Sample of tooltip : 1 Sample of tooltip : 5 Sample of tooltip : 2 Sample of tooltip : 3 Sample of tooltip : 3 Sample of tooltip : -5 Sample of tooltip : 8 Sample of tooltip : 9 Sample of tooltip : 5 Sample of tooltip : 20 Serie 1 Serie 2 Serie 4 Amazing phpGraph 2013 Sample of tooltip : 2013 : 54   2012 Sample of tooltip : 2012 : 36   2011 Sample of tooltip : 2011 : 23 Sample of tooltip : 2010 : 0   2009 Sample of tooltip : 2009 : 39   2008 Sample of tooltip : 2008 : 18   2007 Sample of tooltip : 2007 : 10   2006 Sample of tooltip : 2006 : 10 Sample of tooltip : 2005 : 0   2004 Sample of tooltip : 2004 : 65 Sample of tooltip : 2003 : 0 Sample of tooltip : 2002 : 0 Sample of tooltip : 2000 : 0 Serie 3 2000 : 0 2002 : 0 2003 : 0 2004 : 65 2005 : 0 2006 : 10 2007 : 10 2008 : 18 2009 : 39 2010 : 0 2011 : 23 2012 : 36 2013 : 54 Amazing phpGraph Sample of tooltip : 2012 : 6   2012 Sample of tooltip : 2012 : 6   2011 Sample of tooltip : 2011 : 5   2010 Sample of tooltip : 2010 : 4   2009 Sample of tooltip : 2009 : 7   2008 Sample of tooltip : 2008 : 10   2007 Sample of tooltip : 2007 : 46 Sample of tooltip : 2006 : 0 Sample of tooltip : 2005 : 0 Sample of tooltip : 2004 : 0 Sample of tooltip : 2003 : 0 Sample of tooltip : 2002 : 0 Sample of tooltip : 2000 : 0 Serie 5 2000 : 0 2002 : 0 2003 : 0 2004 : 0 2005 : 0 2006 : 0 2007 : 46 2008 : 10 2009 : 7 2010 : 4 2011 : 5 2012 : 6 2013 : 0

Multi lines filled with no legend nor tooltip. Gradient as background

echo $G->draw($data,array(

            //'steps' => 50,

            'filled' => true,

            'circles' => false,

            'gradient' => array('green', '#00FF00'),

            'diskLegendsLineColor' => '#fd4263',

        )

    );
2000 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2001 -30 -20 -10 0 10 20 30 40 50 60 70 80

Histogram

echo $G->draw($dataFirst,array(

            'filled' => true,

            'type' => 'bar',

            'tooltips' => true,

            'legends' => 'Visits by year',

            'tooltipLegend' => 'Total : ',

            'title' => '',

            'width' => 900,

            'height' => 900,

        ));
2000 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 -20 -10 0 10 20 30 40 50 60 70 80 90 100 Total : 10 Total : 39 Total : 47 Total : 53 Total : 46 Total : 10 Total : 98 Total : -20 Total : 16 Total : 16 Total : 12 Total : 12 Total : 2 Visits by year

Same data as a pie

With value in pourcent

echo $G->draw($dataFirst,array(

            'type' => 'pie',

            'title' => 'A beautifull pie with phpGraph',

            'tooltips'=> true,

            'tooltipLegend' => 'Happy users : ',

            'legends'=>array('2001'),

            'stroke' => array(

                '0'=>'red',

                '1'=>'blue',

                '2'=>'orange',

                '3'=>'green',

                '4'=>'deeppink'

            ),

            'diskLegends' => true,

            'diskLegendsType' => 'pourcent',

            'gradient' => array('grey', 'white'),

            'diskLegendsLineColor' => '#fd4263',

        )

    );
A beautifull pie with phpGraph 1% Happy users : 2013 : 2   3% Happy users : 2012 : 12   3% Happy users : 2011 : 12   4% Happy users : 2010 : 16   4% Happy users : 2009 : 16 Happy users : 2008 : 0   27% Happy users : 2007 : 98   3% Happy users : 2006 : 10   13% Happy users : 2005 : 46   15% Happy users : 2004 : 53   13% Happy users : 2003 : 47   11% Happy users : 2002 : 39   3% Happy users : 2000 : 10 2000 : 10 2002 : 39 2003 : 47 2004 : 53 2005 : 46 2006 : 10 2007 : 98 2008 : 0 2009 : 16 2010 : 16 2011 : 12 2012 : 12 2013 : 2

With absolute value

echo $G->draw($dataFirst,array(

            'type' => 'pie',

            'title' => 'A beautifull pie with phpGraph',

            'tooltips'=> true,

            'tooltipLegend' => 'Happy users : ',

            'legends'=>array('2001'),

            'stroke' => array(

                '0'=>'red',

                '1'=>'blue',

                '2'=>'orange',

                '3'=>'green',

                '4'=>'deeppink'

            ),

            'diskLegends' => true,

            'diskLegendsType' => 'value',

            'gradient' => array('grey', 'white'),

            'diskLegendsLineColor' => '#fd4263',

        )

    );
A beautifull pie with phpGraph 2 Happy users : 2013 : 2   12 Happy users : 2012 : 12   12 Happy users : 2011 : 12   16 Happy users : 2010 : 16   16 Happy users : 2009 : 16 Happy users : 2008 : 0   98 Happy users : 2007 : 98   10 Happy users : 2006 : 10   46 Happy users : 2005 : 46   53 Happy users : 2004 : 53   47 Happy users : 2003 : 47   39 Happy users : 2002 : 39   10 Happy users : 2000 : 10 2000 : 10 2002 : 39 2003 : 47 2004 : 53 2005 : 46 2006 : 10 2007 : 98 2008 : 0 2009 : 16 2010 : 16 2011 : 12 2012 : 12 2013 : 2

Graph with dimensions fixed

echo $G->draw($d,array(

            //'steps' => 20,

            'filled' => true,

            'circles' => true,

            'tooltips' => true,

            'responsive'=>false,

            'opacity'=>1,

        )

    );
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 -7 13 33 53 73 93 113 133 -7 15 39 26 36 18 32 56 38 103 105 126 125 76

Draw stock charts

 

    //Be carefull, for stock graph, array structure must be the same as the array below

$stock = array(

    'Jan' => array(

        'open' => 35,

        'close' => 20,

        'min' => 10,

        'max' => 37),

    'Feb' => array(

        'open' => 28,

        'close' => 17,

        'min' => 11,

        'max' => 32),

    'Mar' => array(

        'open' => 17,

        'close' => 25,

        'min' => 14,

        'max' => 33),

    'Apr' => array(

        'open' => 27,

        'close' => 20,

        'min' => 11,

        'max' => 29),

    'May' => array(

        'open' => 12,

        'close' => 25,

        'min' => 9,

        'max' => 29),

    'Jun' => array(

        'open' => 12,

        'close' => 23,

        'min' => 4,

        'max' => 25),

    'Jul' => array(

        'open' => 20,

        'close' => 16,

        'min' => 3,

        'max' => 22),

    'Aug' => array(

        'open' => 15,

        'close' => 29,

        'min' => 7,

        'max' => 34),

    'Sep' => array(

        'open' => 20,

        'close' => 26,

        'min' => 9,

        'max' => 29),

    'Oct' => array(

        'open' => 28,

        'close' => 17,

        'min' => 5,

        'max' => 31),

    'Nov' => array(

        'open' => 15,

        'close' => 29,

        'min' => 8,

        'max' => 37),

    'Dec' => array(

        'open' => 12,

        'close' => 60,

        'min' => 10,

        'max' => 67)

    );

$stock2 = array(

    "8h" => array(

        'open' => 34,

        'close' => 42,

        'min' => 27,

        'max' => 45),

    "10h" => array(

        'open' => 55,

        'close' => 25,

        'min' => 14,

        'max' => 59),

    "12h" => array(

        'open' => 15,

        'close' => 40,

        'min' => 12,

        'max' => 47),

    "14h" => array(

        'open' => 62,

        'close' => 38,

        'min' => 25,

        'max' => 65),

    "16h" => array(

        'open' => 38,

        'close' => 49,

        'min' => 32,

        'max' => 64),

    "18h" => array(

        'open' => 42,

        'close' => 36,

        'min' => 32,

        'max' => 48)

);

$stock3 = array(

    "Série 1" => array(

        'open' => 34,

        'close' => 42,

        'min' => 27,

        'max' => 45),

    "Série 2" => array(

        'open' => 55,

        'close' => 25,

        'min' => 14,

        'max' => 59),

    "Série 3" => array(

        'open' => 15,

        'close' => 40,

        'min' => 12,

        'max' => 47),

    "Série 4" => array(

        'open' => 62,

        'close' => 38,

        'min' => 25,

        'max' => 65),

    "Série 5" => array(

        'open' => 38,

        'close' => 49,

        'min' => 32,

        'max' => 64),

    "Série 6" => array(

        'open' => 40,

        'close' => 40,

        'min' => 32,

        'max' => 48)

    );

$stock4 = array(

    "aplasie" => array(

        'open' => 1.04,

        'close' => 1.04,

        'min' => 0.87,

        'max' => 1.24),

    "thrombopénie" => array(

        'open' => 1.09,

        'close' => 1.09,

        'min' => 0.95,

        'max' => 1.25),

    "anorexie" => array(

        'open' => 1.02,

        'close' => 1.02,

        'min' => 0.86,

        'max' => 1.21),

    "mucites" => array(

        'open' => 0.87,

        'close' => 0.87,

        'min' => 0.71,

        'max' => 1.06),

    "leucopénie" => array(

        'open' => 0.90,

        'close' => 0.90,

        'min' => 0.1,

        'max' => 1.34),

    "neutropénie" => array(

        'open' => 1,

        'close' => 1,

        'min' => 0.78,

        'max' => 1.20)

    );

    

    

Vertical stock charts

 //Vertical stock charts



    echo $G->draw($stock,array(

        'type' => 'stock',

        'tooltips' => true));

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 0 10 20 30 40 50 60 70 35 20 37 10 28 17 32 11 17 25 33 14 27 20 29 11 12 25 29 9 12 23 25 4 20 16 22 3 15 29 34 7 20 26 29 9 28 17 31 5 15 29 37 8 12 60 67 10

Vertical stock charts with legend

    //Vertical stock charts with legend

    

    echo $G->draw($stock2,array(

        'type' => 'stock',

        'tooltips' => true,

        'legends' => array(

            '0'=>'Serie 1',

            '1'=>'Serie 2',

            '2'=>'Serie 3',

            '3'=>'Serie 4',

            '4'=>'Serie 5',

            '5'=>'Serie 6',

            ),

        )

    );
Série 1 Série 2 Série 3 Série 4 Série 5 Série 6 0 10 20 30 40 50 60 70 34 42 45 27 55 25 59 14 15 40 47 12 62 38 65 25 38 49 64 32 40 40 48 32 Serie 1 Serie 2 Serie 3 Serie 4 Serie 5 Serie 6

Horizontal stock charts

Horizontal stock charts with legend (same data as previous example)

  



    //Horizontal stock charts

    //Horizontal stock charts with legend

    echo $G->draw($stock2,array(

        'type' => 'h-stock',

        'tooltips' => true,

        'legends' => array(

            '0'=>'Serie 1',

            '1'=>'Serie 2',

            '2'=>'Serie 3',

            '3'=>'Serie 4',

            '4'=>'Serie 5',

            '5'=>'Serie 6',

            ),

        )

    );
0 10 20 30 40 50 60 70 0 Sé.Série 1 Sé.Série 2 Sé.Série 3 Sé.Série 4 Sé.Série 5 Sé.Série 6 34 42 45 27 55 25 59 14 15 40 47 12 62 38 65 25 38 49 64 32 40 40 48 32 Serie 1 Serie 2 Serie 3 Serie 4 Serie 5 Serie 6

Other example

  

    echo $G->draw($stock4,array(

        'type' => 'h-stock',

        'tooltips' => true,

        'title' => 'Effets secondaires liés au traitement (IC à 95%)',

        'legends' => array(

            '0'=>'aplasie 1.04 (0.87 à 1.24)',

            '1'=>'thrombopénie 1.09 (0.95 à 1.25)',

            '2'=>'anorexie 1.02 (0.86 à 1.21)',

            '3'=>'mucites 0.87 (0.71 à 1.06)',

            '4'=>'leucopénie 0.90 (0.1 à 1.34)',

            '5'=>'neutropénie 1 (0.78 à 1.20)',

            ),

        )

    );
Effets secondaires liés au traitement (IC à 95%) Effets secondaires liés au traitement (IC à 95%) 0 1 2 0 apl.aplasie thr.thrombopénie ano.anorexie muc.mucites leu.leucopénie neu.neutropénie 1.04 1.04 1.24 0.87 1.09 1.09 1.25 0.95 1.02 1.02 1.21 0.86 0.87 0.87 1.06 0.71 0.9 0.9 1.34 0.1 1 1 1.2 0.78 aplasie 1.04 (0.87 à 1.24) thrombopénie 1.09 (0.95 à 1.25) anorexie 1.02 (0.86 à 1.21) mucites 0.87 (0.71 à 1.06) leucopénie 0.90 (0.1 à 1.34) neutropénie 1 (0.78 à 1.20)

 $disk = array('occupe'=> 40,'libre'=>60);

    echo $G->draw($disk,array(

        'type' => 'pie',

        'diskLegends'=>true,

        'stroke' => array(

            0=>'red',

            1=>'green')

    ));
libre   occupe

$p = array(

        '2000' => 7,

        '2001' => 15,

        '2002' => 39,

        '2003' => 26,

        '2004' => 36,

        '2005' => 18,

        '2006' => 32,

        '2007' => 56,

        '2008' => 38,

        '2009' => 103,

        '2010' => 105,

        '2011' => 126,

        '2012' => 125,

        '2013' => 76,

        '2014' => 10,

    );

    echo $G->draw($p,array(

        'tooltips' => true,

        'type' => 'line'

    ));

2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 0 20 40 60 80 100 120 140 7 15 39 26 36 18 32 56 38 103 105 126 125 76 10