在 GRAL 中绘制多个图形

Plotting Multiple Graphs in GRAL

我目前正在使用库 GRAL 绘制线条和数据。出于某种原因,我无法在同一 XY 图上绘制多条线。这是我的代码:

import java.awt.Color;
import javax.swing.JFrame;
import de.erichseifert.gral.data.DataTable;
import de.erichseifert.gral.plots.XYPlot;
import de.erichseifert.gral.plots.lines.DefaultLineRenderer2D;
import de.erichseifert.gral.plots.lines.LineRenderer;
import de.erichseifert.gral.plots.points.PointRenderer;
import de.erichseifert.gral.ui.InteractivePanel;

public class GraphTest extends JFrame {
    public GraphTest(double x1, double y1) {

        //SETS DEFAULTS FOR JFRAME
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(600, 400);

        //CREATES A DATA TABLE AND RUNS A LOOP TO INCREMENTALLY PLOT OUT POINTS OF A SINE CURVE ON THE STEP .25
        DataTable data = new DataTable(Double.class, Double.class);
        for (double x = -5.0; x <= 5.0; x+=0.25) {
            double y = 5.0*Math.sin(x);
            data.add(x, y);
        }

        //PLOT OUT DATA AND SET XYPLOT IN JFRAME, THEN CONNECT LINES AND CHANGE COLORS
        XYPlot plot = new XYPlot(data);
        getContentPane().add(new InteractivePanel(plot));
        LineRenderer lines = new DefaultLineRenderer2D();
        plot.setLineRenderer(data, lines);
        Color color = new Color(0.0f, 0.3f, 1.0f);
        plot.getPointRenderer(data).setColor(color);
        plot.getLineRenderer(data).setColor(color);

        //PLOT OUT A POINT (4,5) ON THE SAME GRAPH AS THE SINE GRAPH
        DataTable data2 = new DataTable(Double.class, Double.class);
        data2.add(x1, y1);
        XYPlot plot2 = new XYPlot(data2);
        //getContentPane().add(new InteractivePanel(plot2));
        //plot2.setLineRenderer(data2, lines);
        //plot2.getPointRenderer(data2).setColor(color);
        //plot2.getLineRenderer(data2).setColor(color);
    }

    public static void main(double x1, double y1) {
        GraphTest frame = new GraphTest(x1, y1);
        frame.setVisible(true);
    }
}

程序 运行 就这样很好,但我取消注释以下行以使其在同一图上绘制两个图:

    //getContentPane().add(new InteractivePanel(plot2));
    //plot2.setLineRenderer(data2, lines);
    //plot2.getPointRenderer(data2).setColor(color);
    //plot2.getLineRenderer(data2).setColor(color);

并且 JFrame 变成空白,程序似乎冻结了。我是图书馆 GRAL 的初学者(我今天才开始使用它),所以请原谅我缺乏专业知识。另外,仅供参考,此代码是通过同一包中的命令 运行 而另一个 class 具有以下代码:

GraphTest.main(4,5);

问题是我真的不确定如何在同一个 XY 图上绘制两个图形(一个正弦曲线和一个点)。最后,这是当前代码在 运行:

时的样子

这是一个老问题,但这是我解决同样问题的方法:

在创建 XY 图的部分,只需添加更多数据源即可。

来自

XYPlot plot = new XYPlot(data);

XYPlot plot = new XYPlot(data, moreData);

确保分别设置两个数据点的样式。