2012-08-30 70 views
-2

我需要繪製堆疊條形圖此陣應具有2年期限(從今年19982000查找總年跨越與差距

的一個空格隔開2個棒陣列問題: 第一條應該是這樣的,內喜歡它,採取從array020002008array 1

*1998-1997* - *2 years gap* - *2000-2008* 

的酒吧應該合併較短歲的跨度

Array 
(
    [comp_name] => C++ 
    [parent_cat_name] => Information Technology 
    [sub_cat_name] => Programming 
    [total_years] => 6 
    [last_year] => 2006 
    [start_year] => 2000 
) 

Array 
(
    [comp_name] => .NET 
    [parent_cat_name] => Information Technology 
    [sub_cat_name] => Programming 
    [total_years] => 7 
    [last_year] => 2008 
    [start_year] => 2001 
) 

Array 
(
    [comp_name] => API 
    [parent_cat_name] => Information Technology 
    [sub_cat_name] => Programming 
    [total_years] => 1 
    [last_year] => 1998 
    [start_year] => 1997 
) 
+0

那麼,中的圖表條碼的? –

+0

條形圖編碼是沒有問題的,但要尋找當年的差距是 –

+0

你可以發佈你的圖形腳本,以便我們可以看到你實際上意味着你在繪圖什麼呢?這似乎很不清楚。 – gkiar

回答

0

你要合併兩個數組項,如果他們根據一些條件是相鄰的,並有一些其他領域的一些平等的條件。

你可以這樣做:

for(;;) 
{ 
    $merge = array(); 
    $n  = count($data); 
    for ($i = 0; empty($merge) && $i < $n-1; $i++) 
    { 
     for ($j = $i+1; empty($merge) && $j < $n; $j++) 
     { 
      // Are $i and $j congruent? 
      if ($data[$i]['parent_cat_name'] != $data[$j]['parent_cat_name']) 
       continue; 
      if ($data[$i]['sub_cat_name'] != $data[$j]['sub_cat_name']) 
       continue; 

      // Are $i and $j adjacent? 
      if ($data[$i]['last_year']+1 == $data[$j]['start_year']) 
      { 
       $merge = array($i, $j); 
       break; 
      } 
      if ($data[$j]['last_year']+1 == $data[$i]['start_year']) 
       $merge = array($j, $i); 
       break; 
      } 
      // They are congruent but not adjacent, try the next 
     } 
    } 
    // If we get to the end and find nothing mergeable, exit. 
    if (empty($merge)) 
     break; 
    list($i, $j) = $merge; 
    // We add $j to $i 
    $data[$i]['last_year'] = $data[$j]['last_year'] 
    $data[$i]['total_years'] += $data[$j]['total_years'] 
    // We destroy $j 
    unset($data[$j]); 
    // Dirty renumber 
    $data = array_values($data); 
    // Now data has been modified, let's do this again. 
}