是否可以找到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 for Jfreechart
請這個建議的問題。
感謝您的回覆@trashgod。但是,我擁有的數據集中並沒有相同的項目。我需要找到其中沒有共同數據點的兩個XYLineChart系列的交集。所以我想我將無法使用equals方法。 – Yoo
我已經闡述過了;請更新您的問題以澄清此要求。 – trashgod
謝謝。你說「每個清單應該包含連接相應系列的連續點的線」。您能否詳細說明一下這意味着什麼?任何幫助都會很棒。 – Yoo