You are here

function pSpring::drawSpring in Visitors 7

Same name and namespace in other branches
  1. 7.2 pChart/class/pSpring.class.php \pSpring::drawSpring()

File

pChart/class/pSpring.class.php, line 637

Class

pSpring

Code

function drawSpring($Object, $Settings = "") {
  $this->pChartObject = $Object;
  $Pass = isset($Settings["Pass"]) ? $Settings["Pass"] : 50;
  $Retries = isset($Settings["Retry"]) ? $Settings["Retry"] : 10;
  $this->MagneticForceA = isset($Settings["MagneticForceA"]) ? $Settings["MagneticForceA"] : 1.5;
  $this->MagneticForceR = isset($Settings["MagneticForceR"]) ? $Settings["MagneticForceR"] : 2;
  $this->RingSize = isset($Settings["RingSize"]) ? $Settings["RingSize"] : 40;
  $DrawVectors = isset($Settings["DrawVectors"]) ? $Settings["DrawVectors"] : FALSE;
  $DrawQuietZone = isset($Settings["DrawQuietZone"]) ? $Settings["DrawQuietZone"] : FALSE;
  $CenterGraph = isset($Settings["CenterGraph"]) ? $Settings["CenterGraph"] : TRUE;
  $TextPadding = isset($Settings["TextPadding"]) ? $Settings["TextPadding"] : 4;
  $Algorithm = isset($Settings["Algorithm"]) ? $Settings["Algorithm"] : ALGORITHM_WEIGHTED;
  $FontSize = $Object->FontSize;
  $this->X1 = $Object->GraphAreaX1;
  $this->Y1 = $Object->GraphAreaY1;
  $this->X2 = $Object->GraphAreaX2;
  $this->Y2 = $Object->GraphAreaY2;
  $Conflicts = 1;
  $Jobs = 0;
  $this->History["MinimumConflicts"] = -1;
  while ($Conflicts != 0 && $Jobs < $Retries) {
    $Jobs++;

    /* Compute the initial settings */
    $this
      ->firstPass($Algorithm);

    /* Apply the vectors */
    if ($Pass > 0) {
      for ($i = 0; $i <= $Pass; $i++) {
        $this
          ->doPass();
      }
    }
    $Conflicts = $this
      ->lastPass();
    if ($this->History["MinimumConflicts"] == -1 || $Conflicts < $this->History["MinimumConflicts"]) {
      $this->History["MinimumConflicts"] = $Conflicts;
      $this->History["Result"] = $this->Data;
    }
  }
  $Conflicts = $this->History["MinimumConflicts"];
  $this->Data = $this->History["Result"];
  if ($CenterGraph) {
    $this
      ->center();
  }

  /* Draw the connections */
  $Drawn = "";
  foreach ($this->Data as $Key => $Settings) {
    $X = $Settings["X"];
    $Y = $Settings["Y"];
    if (isset($Settings["Connections"])) {
      foreach ($Settings["Connections"] as $ID => $NodeID) {
        if (!isset($Drawn[$Key])) {
          $Drawn[$Key] = "";
        }
        if (!isset($Drawn[$NodeID])) {
          $Drawn[$NodeID] = "";
        }
        if (isset($this->Data[$NodeID]) && !isset($Drawn[$Key][$NodeID]) && !isset($Drawn[$NodeID][$Key])) {
          $Color = array(
            "R" => $this->Default["LinkR"],
            "G" => $this->Default["LinkG"],
            "B" => $this->Default["LinkB"],
            "Alpha" => $this->Default["Alpha"],
          );
          if ($this->Links != "") {
            if (isset($this->Links[$Key][$NodeID]["R"])) {
              $Color = array(
                "R" => $this->Links[$Key][$NodeID]["R"],
                "G" => $this->Links[$Key][$NodeID]["G"],
                "B" => $this->Links[$Key][$NodeID]["B"],
                "Alpha" => $this->Links[$Key][$NodeID]["Alpha"],
              );
            }
            if (isset($this->Links[$Key][$NodeID]["Ticks"])) {
              $Color["Ticks"] = $this->Links[$Key][$NodeID]["Ticks"];
            }
          }
          $X2 = $this->Data[$NodeID]["X"];
          $Y2 = $this->Data[$NodeID]["Y"];
          $this->pChartObject
            ->drawLine($X, $Y, $X2, $Y2, $Color);
          $Drawn[$Key][$NodeID] = TRUE;
          if (isset($this->Links) && $this->Links != "") {
            if (isset($this->Links[$Key][$NodeID]["Name"]) || isset($this->Links[$NodeID][$Key]["Name"])) {
              $Name = isset($this->Links[$Key][$NodeID]["Name"]) ? $this->Links[$Key][$NodeID]["Name"] : $this->Links[$NodeID][$Key]["Name"];
              $TxtX = ($X2 - $X) / 2 + $X;
              $TxtY = ($Y2 - $Y) / 2 + $Y;
              if ($X <= $X2) {
                $Angle = (360 - $this
                  ->getAngle($X, $Y, $X2, $Y2)) % 360;
              }
              else {
                $Angle = (360 - $this
                  ->getAngle($X2, $Y2, $X, $Y)) % 360;
              }
              $Settings = $Color;
              $Settings["Angle"] = $Angle;
              $Settings["Align"] = TEXT_ALIGN_BOTTOMMIDDLE;
              $this->pChartObject
                ->drawText($TxtX, $TxtY, $Name, $Settings);
            }
          }
        }
      }
    }
  }

  /* Draw the quiet zones */
  if ($DrawQuietZone) {
    foreach ($this->Data as $Key => $Settings) {
      $X = $Settings["X"];
      $Y = $Settings["Y"];
      $FreeZone = $Settings["FreeZone"];
      $this->pChartObject
        ->drawFilledCircle($X, $Y, $FreeZone, array(
        "R" => 0,
        "G" => 0,
        "B" => 0,
        "Alpha" => 2,
      ));
    }
  }

  /* Draw the nodes */
  foreach ($this->Data as $Key => $Settings) {
    $X = $Settings["X"];
    $Y = $Settings["Y"];
    $Name = $Settings["Name"];
    $FreeZone = $Settings["FreeZone"];
    $Shape = $Settings["Shape"];
    $Size = $Settings["Size"];
    $Color = array(
      "R" => $Settings["R"],
      "G" => $Settings["G"],
      "B" => $Settings["B"],
      "Alpha" => $Settings["Alpha"],
      "BorderR" => $Settings["BorderR"],
      "BorderG" => $Settings["BorderG"],
      "BorderB" => $Settings["BorderB"],
      "BorderApha" => $Settings["BorderAlpha"],
    );
    if ($Shape == NODE_SHAPE_CIRCLE) {
      $this->pChartObject
        ->drawFilledCircle($X, $Y, $Size, $Color);
    }
    elseif ($Shape == NODE_SHAPE_TRIANGLE) {
      $Points = "";
      $Points[] = cos(deg2rad(270)) * $Size + $X;
      $Points[] = sin(deg2rad(270)) * $Size + $Y;
      $Points[] = cos(deg2rad(45)) * $Size + $X;
      $Points[] = sin(deg2rad(45)) * $Size + $Y;
      $Points[] = cos(deg2rad(135)) * $Size + $X;
      $Points[] = sin(deg2rad(135)) * $Size + $Y;
      $this->pChartObject
        ->drawPolygon($Points, $Color);
    }
    elseif ($Shape == NODE_SHAPE_SQUARE) {
      $Offset = $Size / 2;
      $Size = $Size / 2;
      $this->pChartObject
        ->drawFilledRectangle($X - $Offset, $Y - $Offset, $X + $Offset, $Y + $Offset, $Color);
    }
    if ($Name != "") {
      $LabelOptions = array(
        "R" => $this->Labels["R"],
        "G" => $this->Labels["G"],
        "B" => $this->Labels["B"],
        "Alpha" => $this->Labels["Alpha"],
      );
      if ($this->Labels["Type"] == LABEL_LIGHT) {
        $LabelOptions["Align"] = TEXT_ALIGN_BOTTOMLEFT;
        $this->pChartObject
          ->drawText($X, $Y, $Name, $LabelOptions);
      }
      elseif ($this->Labels["Type"] == LABEL_CLASSIC) {
        $LabelOptions["Align"] = TEXT_ALIGN_TOPMIDDLE;
        $LabelOptions["DrawBox"] = TRUE;
        $LabelOptions["BoxAlpha"] = 50;
        $LabelOptions["BorderOffset"] = 4;
        $LabelOptions["RoundedRadius"] = 3;
        $LabelOptions["BoxRounded"] = TRUE;
        $LabelOptions["NoShadow"] = TRUE;
        $this->pChartObject
          ->drawText($X, $Y + $Size + $TextPadding, $Name, $LabelOptions);
      }
    }
  }

  /* Draw the vectors */
  if ($DrawVectors) {
    foreach ($this->Data as $Key => $Settings) {
      $X1 = $Settings["X"];
      $Y1 = $Settings["Y"];
      if (isset($Settings["Vectors"]) && $Settings["Type"] != NODE_TYPE_CENTRAL) {
        foreach ($Settings["Vectors"] as $ID => $Vector) {
          $Type = $Vector["Type"];
          $Force = $Vector["Force"];
          $Angle = $Vector["Angle"];
          $Factor = $Type == "A" ? $this->MagneticForceA : $this->MagneticForceR;
          $Color = $Type == "A" ? array(
            "FillR" => 255,
            "FillG" => 0,
            "FillB" => 0,
          ) : array(
            "FillR" => 0,
            "FillG" => 255,
            "FillB" => 0,
          );
          $X2 = cos(deg2rad($Angle)) * $Force * $Factor + $X1;
          $Y2 = sin(deg2rad($Angle)) * $Force * $Factor + $Y1;
          $this->pChartObject
            ->drawArrow($X1, $Y1, $X2, $Y2, $Color);
        }
      }
    }
  }
  return array(
    "Pass" => $Jobs,
    "Conflicts" => $Conflicts,
  );
}