highcharts-php / highcharts.php

<?php

class Highcharts
{
  private $_code;

  public function highcharts($name, $options, $engine='jquery')
  {
    $this->_code = $this->build_code($name, $options, $engine);
  }

  private function build_code($name, $options, $engine='jquery')
  {
    $code = '<script type="text/javascript">';
    if ($engine == 'mootools')
      $code .= 'window.addEvent(\'domready\', function() {';
    else
      $code .= '$(document).ready(function() {';
    $code .= 'var ' . $name . ' = new Highcharts.Chart({';
    $code .= $this->build_options($options);
    $code .= '});});</script>';
    return $code;
  }

  private function build_options($options)
  {
    $code = array();
    foreach ($options as $key => $option)
      $code []= $this->build_option($key, $option);
    return implode(',', $code);
  }

  private function build_option($key, $options)
  {
    $code = $key . ': ';
    if (!is_array($options[0])) //TODO: Fix this notice
      $code .= $this->build_properties($options);
    else
      {
        $code .= '[';
        $opts = array();
        foreach ($options as $option)
          {
            $opts []= $this->build_properties($option);
          }
        $code .= implode(',', $opts);
        $code .= ']';
      }
    return $code;
  }

  private function build_properties($options)
  {
    $code = array();
    foreach ($options as $key => $value)
      $code []= $this->build_property($key, $value);
    return '{' . implode(',', $code) . '}';
  }

  private function build_property($key, $value)
  {
    $code = $key . ': ';
    if ($value instanceof HighchartsArray)
      $code .= $value->get();
    else
      $code .= '\'' . $value . '\'';
    return $code;
  }

  public function getCode()
  {
    return $this->_code;
  }

}

class HighchartsArray
{
  private $_array;
  public function HighchartsArray($array)
  {
    $this->_array = $array;
  }
  public function get()
  {
    $js_array = '[';
    $js_array .= implode(',', $this->_array);
    $js_array .= ']';
    return $js_array;
  }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.