<?php

/**
 * Chart class, allow to draw chart with multi lines data
 *
 */
class sfFlashMultiChart {
  /**
   * sfFlashChart object to retrive and render data
   *
   * @var sfFlashChart
   */
  public $flashChart;
  /**
   * List of x data (usually is labels for chart)
   *
   * @var array
   */
  protected $xs = array();
  /**
   * Count data for each line (ONE line contains an array of (x,y) values)
   * This array use to set y value for each x value all lines (if it missing)
   *
   * @var unknown_type
   */
  protected $counts = array();
  /**
   * Array of (line, y)
   *
   * @var array
   */
  protected $ys = array();
  /**
   * Min and max y (to draw chart)
   *
   * @var unknown_type
   */
  protected $min = 1e10;
  protected $max = -1e10;
  protected $colors;
  protected $lineNames;

  /**
   * Array of lines will be used in chart
   *
   * @param unknown_type $lines
   * @param unknown_type $xLegend
   * @param unknown_type $yLegend
   * @param unknown_type $legendSize
   * @param unknown_type $legendColor
   */
  public function __construct($lines, $lineNames, $xLegend = 'Ngày', $yLegend = 'Diem so',
                              $legendSize = 15, $legendColor = '0x736AFF',
                              $colors = array('#CC3399', '#003399', '#CC3300', '#CC0099')) {
    $this->flashChart = new sfFlashChart();
    foreach ($lines as $line) {
      $this->counts[$line] = 0;
    }
    $this->lineNames = $lineNames;

    $this->flashChart->set_x_legend($xLegend, $legendSize, $legendColor);
    $this->flashChart->set_y_legend($yLegend, $legendSize, $legendColor);
    $this->colors = $colors;
  }

  /**
   * Finish add y value to each x column
   *
   */
  function finishX() {
    $count = count($this->xs);
    foreach ($this->counts as $line=>$value) {
      if ($value<$count) {
        $this->counts[$line]++;
        $this->ys[$line][] = 0;    // Add zero value for missing y value
        if ($this->min > 0) $this->min = 0;
      }
    }
  }

  /**
   * Add data for each line. Notice that data must add in ascending order of x
   *
   * @param unknown_type $line
   * @param unknown_type $x
   * @param unknown_type $y
   */
   public function addData($line, $x, $y) {
    if ($this->min > $y) $this->min = $y;
    if ($this->max < $y) $this->max = $y;


    // Get last x value in current progress
    $lastX = @$this->xs[count($this->xs)-1];
    if ($lastX!=$x) {           // There a a new x value
      $this->finishX();         // Find and add missing y value for all lines
      $this->xs[] = $x;         // append new x value to xs
    }
    $this->ys[$line][] = $y;    // append new y value to ys
    $this->counts[$line]++;     // count number of y value line $line
  }

  public function render() {
    $this->finishX();           // find and fill last
    if (count($this->xs)>0) {
      $height = $this->max-$this->min;
      $this->flashChart->set_y_max(ceil($this->max+$height/10));
      $this->flashChart->set_y_min(floor(max(0, $this->min-$height/10)));
      $i = 0;
      foreach ($this->ys as $line=>$ys) {
      	$this->flashChart->set_data($ys);
      	$this->flashChart->line_dot(2, 5, $this->colors[$i % count($this->colors)],
      	   $this->lineNames[$line], 10);
      	$i++;
      }
      if ($i==1) $this->flashChart->area_hollow(2, 3, 25, $this->colors[0]);
      $this->flashChart->set_x_labels($this->xs);
    }
    return $this->flashChart->render();
  }
}