系列的交点

Intersection points of series

是否可以在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

请提出问题。

是的,您可以迭代构成 Series. As a concrete example, an 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);
    });
});

I need to find the intersection of two XYLineChart series which don't have common data points among them.

您可以在 List<Line> 的两个实例上使用相同的迭代方案;每个列表应包含连接相应系列的连续点的线。将 equals() 替换为概念性的 intersects() 方法。你可以使用line–line intersection; Line::intersection是一个典型的实现。

The code above…throws a ClassCastException with the message,

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 点等