JFreeChart是一个强大的库,但其使用较为复杂,本文定义了一套方便的接口,可类似于XChart一样,方便使用。使用JFreeChart时,需导入JFreeChart 1.5.4版本的包。效果如下:
代码如下:
import com.formdev.flatlaf.FlatLightLaf;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.*;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.ui.*;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
public class Plot {
private JFreeChart chart;
private final ArrayList<Line> lines = new ArrayList<>();
private Font axisLabelFont = new Font("Microsoft YaHei", Font.PLAIN, 11);
// new Font("Microsoft YaHei", Font.PLAIN, 11);
private String titleText = "";
private String xAxisTitle = "x";
private String yAxisTitle = "y";
private boolean verticalGridLineVisible = true;
private boolean horizontalGridLineVisible = true;
private double xmin = 0;
private double xmax = 0;
private double ymin = 0;
private double ymax = 0;
class Line {
double[] x;
double[] y;
Color color = Color.BLACK;
BasicStroke stroke = new BasicStroke(1.5f);
boolean showMarker = false;
int markerSize = 4;
boolean lineVisible = true;
boolean itemLabelsVisible = false;
char markerType = 'c';
boolean markerFilled = false;
boolean isVerticalLine = false;
String verticalLineLabel = "";
boolean isLabel = false;
String label = "";
float labelAngle = 0;
}
public Plot verticalGridLineVisible(boolean bool) {
this.verticalGridLineVisible = bool;
System.out.println("执行");
return this;
}
public Plot horizontalGridLineVisible(boolean bool) {
this.horizontalGridLineVisible = bool;
return this;
}
public Plot line(double[] x, double[] y,
Color color, float lineWidth, boolean lineVisible,
boolean dashed, Character markerType, boolean showMarker,
int markerSize, boolean itemLabelVisible) {
Line line = new Line();
line.x = x;
line.y = y;
line.color = color;
line.showMarker = showMarker;
line.markerSize = markerSize;
line.lineVi