我有一个折线图,它可以创建平滑的曲线而不是锯齿状的图。 (我通过渲染器(以XYSplineRenderer为例)进行此操作)
该曲线应在数据点处具有拐点,但在该点之上或之下上升,并且看起来不佳且具有误导性(暗示某些数据存在于最大值和/或最小值之下)
产生此代码的代码:
import java.awt.Color;
import java.awt.Font;
import java.awt.Point;
import java.awt.geom.Ellipse2D;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.DateTickUnitType;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.TickUnits;
import org.jfree.chart.labels.XYItemLabelGenerator;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYSplineRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
public class LineChart {
public String saveToFile(String path,String pathColor) {
try {
JFreeChart chart = getChart(pathColor,"","","",false);
Point p = new Point(400,300);
File chartFile = new File(path);
ChartUtils.writeChartAsPNG(new FileOutputStream(chartFile), chart, Double.valueOf(p.getX()).intValue(), Double.valueOf(p.getY()).intValue(),false,0);
} catch (Throwable t) {
t.printStackTrace();
}
return null;
}
private JFreeChart getChart(
String hexColor,
String title,
String category,
String value,
boolean includeLegend) {
TimeSeriesCollection dataset = new TimeSeriesCollection();
TimeSeries series1 = new TimeSeries("Total");
series1.add(new Day(7,10,2019),0);
series1.add(new Day(8,10,2019),6);
series1.add(new Day(9,10,2019),0);
series1.add(new Day(10,10,2019),2);
series1.add(new Day(11,10,2019),0);
dataset.addSeries(series1);
XYSplineRenderer renderer = new XYSplineRenderer();
renderer.setDrawOutlines(true);
renderer.setPrecision(1000);
renderer.setDrawSeriesLineAsPath(true);
renderer.setSeriesPaint(0, Color.decode(hexColor));
renderer.setSeriesShapesVisible(0, true);
renderer.setSeriesItemLabelsVisible(0, true);
renderer.setSeriesItemLabelPaint(0, Color.decode(hexColor));
renderer.setSeriesShape(0, new Ellipse2D.Double(-3, -3, 6, 6));
renderer.setSeriesItemLabelGenerator(0, new XYItemLabelGenerator() {
@Override
public String generateLabel(XYDataset dataset, int series, int item) {
Double d = dataset.getYValue(series, item);
return "" + d.intValue();
}
});
JFreeChart chart = ChartFactory.createTimeSeriesChart(title, category, value, null,includeLegend, true, false);
chart.setAntiAlias(true);
XYPlot plot = (XYPlot)chart.getPlot();
plot.setRangeGridlinePaint(new Color(155, 155, 155) );
plot.setBackgroundPaint(new Color(0, 0, 0, 0));
final TickUnits standardUnits = new TickUnits();
standardUnits.add(
new DateTickUnit(DateTickUnitType.DAY, 1, new SimpleDateFormat("dd E"))
);
DateAxis domainAxis = (DateAxis)plot.getDomainAxis();
domainAxis.setTickLabelFont(new Font("Montserrat",Font.BOLD, 10));
domainAxis.setTickLabelPaint(Color.decode("#9b9b9b"));
domainAxis.setStandardTickUnits(standardUnits);
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
rangeAxis.setTickLabelFont(new Font("Montserrat",Font.BOLD, 10));
rangeAxis.setTickLabelPaint(Color.decode("#9b9b9b"));
plot.setDataset(0, dataset);
plot.setRenderer(0, renderer);
plot.setOutlinePaint(null);
return chart;
}
}