I would like to calculate the variance and standard deviation and display this alongside the mean 'result' in a view.

Wondering if anyone has any tips on how I might achieve this.

Awesome module. Thanks in advance for any tips.

Danny.

Comments

dannybrowne created an issue. See original summary.

dannybrowne’s picture

Title: Calculating and displaying variance » Calculating and displaying variance and standard deviation
dannybrowne’s picture

So I am currently doing this by implementing a custom views handler and performing the calculations with a tiny bit of PHP in the field handler.

It is not perfect but it's working ok.


function mymodule_views_data() {

    $data = array();

    $data['node']['mymodule_handler_myname'] = array(
        'title' => t('Score'),
        'help' => t('Makes score values human readable'),
        'real field' => 'value',
        'field' => array(
          'handler' => 'mymodule_handler_myname',
          'click sortable' => TRUE,
        ),
    ); 

    return $data;  

}

and my mymodule_handler_myname.inc looks like


class mymodule_handler_myname extends views_handler_field {

    /**
    * Loads additional fields.
    */
    function query() {

    }  

    function click_sort($order) {

        $this->query->orderby[] = array(
            'field' => 'field_name', // here may be a field, or some SQL clause e.g. FIELD(str, str1, ...)
            'direction' => strtoupper($order),
        );

    }

    /**
    * Renders the field handler.
    */
    function render($values) {       

        if (isset($values->field_name)){

            switch ($values->field_name){
                case 2:
                    $vote = 'Yes';
                    break;
                case 0:
                    $vote = 'No';
                    break;
                default:
                    $vote = 'Maybe';
                    break;
            }

            return t($vote);

        }
    }
}

Danny.

dannybrowne’s picture

.