1. roberto.aloi
  2. highcharts-php
  3. Issues
Issue #10 new

How to format the tooltip

Anonymous created an issue

Is it possible to format the tooltip, when you hover over points?

From the example http://www.highcharts.com/demo/ {{{ tooltip: { formatter: function() { return '<b>'+ this.series.name +'</b><br/>'+this.x +': '+ this.y +'°C'; } } }}}

How should it look to work with highcharts-php?

Comments (2)

  1. Anonymous

    To deal with this, and some of the other issues raised elsewhere, I've created a simpler (I think) more generalized version of the code that does not require pre-processing of the series data. This version just targets jQuery (in my case in a Drupal site). Series data can be in the form:

    // standard line charts:
      $data = array(1,2,3,4);
    // bar charts: use array_keys($data) as the x-axis 'categories' and array_values($data) as the series data:
      $data = array('Mon' => 1, 'Tue' => 2, 'Wed' => 3);
    // scatter plot:
      $data = array(array(0.1, 2), array(10.3, 4), array(2.4, 7.1));
    
    class Highcharts {
    
      private $_code;
    
      public function highcharts($name, $options) {
    
        $code = 'jQuery(document).ready(function() {';
        $code .= 'var ' . $name . ' = new Highcharts.Chart({';
    
        $opt_code = array();
        foreach ($options as $key => $option) {
          $opt_code[] = $this->build_option($option, $key);
        }
        $code .= implode(',', $opt_code);
        $code .= '});});';
    
        $this->_code = $code;
      }
    
      public function getCode() {
        return $this->_code;
      }
    
      private function build_option($option, $key = NULL) {
    
        switch (gettype($option)) {
          case 'boolean' :
            $code = ($option) ? 'true' : 'false';
            break;
          case 'string' :
            $code = ($key == 'formatter') ? $option : '\''. $option .'\'';
            break;
          case 'integer' :
          case 'float' :
            $code = $option;
            break;
          case 'array' :
            $opt_code = array();
            if ($this->is_assoc($option)) {
              foreach ($option as $option_key => $option_value) {
                $opt_code[] = $this->build_option($option_value, $option_key);
              }
              $code = '{' . implode(',' , $opt_code) . '}';
            }
            else {
              foreach ($option as $option_value) {
                $opt_code[] = $this->build_option($option_value);
              }
              $code = '[' . implode(',' , $opt_code) . ']';
            }
            break;
          default:
            $code = '';
        }
        return ((($key) ? ($key . ': ') : '') . $code);
      }
    
      private function is_assoc($arr) {
        return (is_array($arr) && count(array_filter(array_keys($arr),'is_string')) == count($arr));
      }
    }
    
  2. Anonymous

    There is a small bug in the above code: Turns out that for historical reasons PHP returns "double" for a float so the check should be for

    case 'double' :
    

    not

    case 'float' :
    

    In the code above.

  3. Log in to comment