2017-04-22 71 views
-2

是否可以找到JFreeChart中的兩個系列的交點?相交的圖表系列之間沒有任何共同點。因此,需要爲圖中發生的彼此相交的兩個系列計算交點。系列的交叉點

List<Line> lineOne = one.getItems(); 
List<Line> lineTwo = two.getItems(); 
for (Line i : lineOne) { 
    for (Line j : lineTwo) { 
     if (i.intersection(j) != null) { 
      System.out.println(i.intersection(j)); 
     } 
    } 
} 

上面的代碼是什麼,我要怎樣做,但是這將引發與此消息一ClassCastException

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: 
org.jfree.data.xy.XYDataItem cannot be cast to 
org.apache.commons.math3.geometry.euclidean.twod.Line 

Trashgod的建議後,我曾嘗試做如下:

List<XYDataItem> l1 = one.getItems(); 
List<XYDataItem> l2 = two.getItems(); 

Line itemOne = null; 
Line itemTwo = null; 
List<Line> lineOne = new ArrayList<Line>(); 
List<Line> lineTwo = new ArrayList<Line>(); 

//Add lines to the first list 
for(int i = 0; i < l1.size(); i++){ 
    if(i < l1.size()-1) { 
     itemOne = new Line(new Vector2D(l1.get(i).getXValue(), 
         l1.get(i).getYValue()), 
         new Vector2D(l1.get(i+1).getXValue(), 
         l1.get(i+1).getYValue()), 0); 
     lineOne.add(itemOne); 
    } 
} 

//Add lines to the second list 
for(int i = 0; i < l2.size(); i++){ 
    if(i < l2.size()-1) { 
     itemTwo = new Line(new Vector2D(l2.get(i).getXValue(), 
         l2.get(i).getYValue()), 
         new Vector2D(l2.get(i+1).getXValue(), 
         l2.get(i+1).getYValue()), 0); 
     lineTwo.add(itemTwo); 
    } 
} 

for(Line i: lineOne) { 
    for(Line j: lineTwo) { 
     if (i.intersection(j) != null) { 
      System.out.println(i.intersection(j)); 
     }      
    } 
} 

但是,在遍歷此列表時,即使只有很少的交點,也會產生很多結果(如下圖所示)。
交點也不正確。

Image showing results

我的表是這樣的: Image for Jfreechart

請這個建議的問題。

回答

1

是的,你可以迭代包含Series的項目。作爲具體示例,XYSeries在內部具有ListXYDataItem。因爲XYDataItem執行Comparable,所以可以使用equals()測試交集。鑑於兩個系列包含在共同的單個項目,

private final XYSeries one = new XYSeries("One"); 
private final XYSeries two = new XYSeries("Two"); 
… 
one.add(1, 1); 
one.add(1, 42); 
two.add(1, 2); 
two.add(1, 42); 

以下迭代方法找到了共同點,[1.0, 42.0]

List<XYDataItem> list1 = one.getItems(); 
List<XYDataItem> list2 = two.getItems(); 
for (XYDataItem i : list1) { 
    for (XYDataItem j : list2) { 
     if (i.equals(j)) { 
      System.out.println(i); 
     } 
    } 
} 

或者,您可以使用功能操作:

list1.stream().forEach((i) -> { 
    list2.stream().filter((j) -> (i.equals(j))).forEach((item) -> { 
     System.out.println(i); 
    }); 
}); 

我需要找到兩個不存在公共數據點的XYLineChart系列的交集在他們身上。

您可以在List<Line>的兩個實例上使用相同的迭代方案;每個列表應包含連接相應系列的連續點的線。將equals()替換爲名義上的intersects()方法。您可以使用line–line intersection; Line::intersection是一個典型的實現。

上述代碼...拋出一個ClassCastException與消息,

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: 
org.jfree.data.xy.XYDataItem cannot be cast to 
org.apache.commons.math3.geometry.euclidean.twod.Line 

即預期;您必須遍歷每個List<XYDataItem>以創建相應的List<Line>。第一個Line將受到第1點和第2點的限制;第二個Line由點2和3等。

+0

感謝您的回覆@trashgod。但是,我擁有的數據集中並沒有相同的項目。我需要找到其中沒有共同數據點的兩個XYLineChart系列的交集。所以我想我將無法使用equals方法。 – Yoo

+0

我已經闡述過了;請更新您的問題以澄清此要求。 – trashgod

+0

謝謝。你說「每個清單應該包含連接相應系列的連續點的線」。您能否詳細說明一下這意味着什麼?任何幫助都會很棒。 – Yoo