/** * Apply the dataset to the axis. * * This calculates the order of the categories, which is very important * for fx. line plots, so that the line does not "go backwards", consider * these X-sets:<p> * 1: (1, 2, 3, 4, 5, 6)<br> * 2: (0, 1, 2, 3, 4, 5, 6, 7)<p> * If they are not ordered, but simply appended, the categories on the axis * would be:<p> * X: (1, 2, 3, 4, 5, 6, 0, 7)<p> * Which would render the a line for the second plot to show incorrectly. * Instead this algorithm, uses and 'value- is- before' method to see that * the 0 is before a 1 in the second set, and that it should also be before * a 1 in the X set. Hence:<p> * X: (0, 1, 2, 3, 4, 5, 6, 7) * * @param Image_Graph_Dataset $dataset The dataset * @access private */ function _applyDataset(&$dataset) { $newLabels = array(); $allLabels = array(); $dataset->_reset(); $count = 0; $count_new = 0; while ($point = $dataset->_next()) { if ($this->_type == IMAGE_GRAPH_AXIS_X) { $data = $point['X']; } else { $data = $point['Y']; } if (!isset($this->_labels[$data])) { $newLabels[$data] = $count_new++; //$this->_labels[] = $data; } $allLabels[$data] = $count++; } if (count($this->_labels) == 0) { $this->_labels = $newLabels; } elseif (is_array($newLabels) && count($newLabels) > 0) { // get all intersecting labels $intersect = array_intersect(array_keys($allLabels), array_keys($this->_labels)); // traverse all new and find their relative position withing the // intersec, fx value X0 is before X1 in the intersection, which // means that X0 should be placed before X1 in the label array foreach ($newLabels as $newLabel => $id) { $key = $allLabels[$newLabel]; reset($intersect); $this_value = false; // intersect indexes are the same as in allLabels! $first = true; while ((list($id, $value) = each($intersect)) && $this_value === false) { if ($first && $id > $key) { $this_value = $value; } elseif ($id >= $key) { $this_value = $value; } $first = false; } if ($this_value === false) { // the new label was not found before anything in the // intersection -> append it $this->_labels[$newLabel] = count($this->_labels); } else { // the new label was found before $this_value in the // intersection, insert the label before this position in // the label array // $key = $this->_labels[$this_value]; $keys = array_keys($this->_labels); $key = array_search($this_value, $keys); $pre = array_slice($keys, 0, $key); $pre[] = $newLabel; $post = array_slice($keys, $key); $this->_labels = array_flip(array_merge($pre, $post)); } } unset($keys); } $labels = array_keys($this->_labels); $i = 0; foreach ($labels as $label) { $this->_labels[$label] = $i++; } // $this->_labels = array_values(array_unique($this->_labels)); $this->_calcLabelInterval(); }