package handler;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.DefaultXYDataset;
import javax.swing.*;
import java.util.List;
import java.util.function.Function;
import java.util.stream.IntStream;
public class GraphTest extends JPanel {
static Function<Double, Double> f = (x) -> Math.pow(x, 2);
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame();
frame.setSize(800, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
double[] xValues = IntStream.range(-50, 50).mapToDouble(i -> (double) i / 10).toArray();
double[] yValues = Arrays.stream(xValues).map(x -> f.apply(x)).toArray();
DefaultXYDataset ds = new DefaultXYDataset();
ds.addSeries("Graph 1", new double[][]{xValues, yValues});
JFreeChart chart = ChartFactory.createXYLineChart("Test-Graph",
"x", "y", ds, PlotOrientation.VERTICAL, true, true,
false);
ChartPanel cp = new ChartPanel(chart);
frame.getContentPane().add(cp);
});
}
}