Jump to content

Achievo/Howto/Developers/Add graphs to your node

From NusaATK

Achievo Howto: Achievo/Howto/Developers/Add graphs to your node

Complexity: Easy
Author: Sandy Pleyte <sandy@achievo.org>

List of other Howto's

Achievo has a graph module which includes jpgraph. To add graphs to your node you can use the dataGraphAttribute which is located in the graph module.


dataGraphAttribute($name, $callback, $params=array(), $graphtype="auto", $flags=0)

  • name String

The name of the graph (must be unique within the node).

  • callback String

The name of the method to call on the node to collect the data. Example: if you specify "totals" as $callback, the system invokes the method "graph_totals()" in the node. Callback prototype:

function graph_<callbackname>($params)
{
}

The callback must return an array of data in the following format:

 array('plotname1'=>array('x-axis/legend title'=>
                          'y-axis/pie value',
                          ...),
       'plotname2'=>...);

In other words, it must return one or more plots.

  • callback Array

Specify an array of parameters to be passed to the callback method. It is possible to specify 'templates' as values. For example, specifying array("selected"=>"[id]" will result in a callback invokation with a parameter 'selected' which has the id of the current record as parameter. A special template is [pk], which passes the entire primary key of the current record.

  • graphtype String

The type of graph to plot. Currently supported are: "pie" - Create a pie chart "bar" - Create a bar chart "line" - Create a line chart "auto" - Creates a pie chart if only one plot is passed and a bar chart if more than one are passed

  • graphtype Integer

Any standard attribute (AF_*) flag can be passed. As of yet, there are no specific flags for this attribute.

Example node

<?php
useattrib('graph.dataGraphAttribute'); 
class graphtest extends atkNode 
{
  function __construct()
  {
    $this->atkNode('graphtest',NF_ADD_LINK);
    $this->add(new atkAttribute("id" ,AF_AUTOKEY));
    $this->add(new atkAttribute("title"));
    
    $this->add(new dataGraphAttribute('pie_graph','pie',array("title"=>"[title]"),'pie'));
    $this->add(new dataGraphAttribute('bar_graph','bar',array("title"=>"[title]"),'bar'));
    $this->add(new dataGraphAttribute('line_graph','line',array("title"=>"[title]"),'line'));
    
    $this->setTable("crm_lead");
  }
  
  function graph_pie($params)
  {
    return array('pie graph'=>array(
                 'Label 1'=>10,
                 'Label 2'=>11,
                 'label 3'=>13));
  }
  
  function graph_bar($params)
  {
    return array('bar graph'=>array(
                 '1'=>1,'2'=>2,'3'=>1,'4'=>4,
                 '5'=>7,'6'=>4));
  } 

  function graph_line($params)
  {
    return array('Line 1'=>array(
                 '1'=>1,'2'=>2,
                 '3'=>1,'4'=>4,
                 '5'=>7,'6'=>4),
                 'Line 2'=>array(
                 '1'=>7,'2'=>4,
                 '3'=>6,'4'=>4,
                 '5'=>2,'6'=>2));
  } 
 
}