java曲线自动生成系统_java生成曲线图
时间: 2023-08-11 16:34:18 浏览: 118
生成曲线图可以使用Java中的图形库,比如JFreeChart或者Chart.js。这些库提供了许多API可以用来生成各种类型的图表,包括曲线图。
以下是一个使用JFreeChart生成曲线图的示例代码:
```java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Point;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.Year;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.GradientPaintTransformType;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.StandardGradientPaintTransformer;
public class LineChartDemo extends ApplicationFrame {
public LineChartDemo(String title) {
super(title);
JFreeChart chart = createChart();
ChartPanel panel = new ChartPanel(chart);
panel.setPreferredSize(new Dimension(500, 270));
setContentPane(panel);
}
private JFreeChart createChart() {
TimeSeries series = new TimeSeries("Random Data");
Date date = new Date();
for (int i = 0; i < 100; i++) {
date = new Date(date.getTime() + 1000 * 60 * 60 * 24);
series.add(new Year(date), Math.random() * 100);
}
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(series);
JFreeChart chart = ChartFactory.createXYLineChart(
"Line Chart Demo", // chart title
"Time", // x axis label
"Value", // y axis label
dataset, // data
PlotOrientation.VERTICAL,
true, // include legend
true, // tooltips
false // urls
);
chart.setBackgroundPaint(Color.white);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setSeriesLinesVisible(0, true);
renderer.setSeriesShapesVisible(0, false);
plot.setRenderer(renderer);
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
NumberAxis axis2 = (NumberAxis) plot.getRangeAxis();
axis2.setNumberFormatOverride(new DecimalFormat("#0.0"));
GradientPaint gp0 = new GradientPaint(
0.0f, 0.0f, Color.blue,
0.0f, 0.0f, new Color(0, 0, 64)
);
GradientPaint gp1 = new GradientPaint(
0.0f, 0.0f, Color.green,
0.0f, 0.0f, new Color(0, 64, 0)
);
renderer.setSeriesPaint(0, gp0);
renderer.setSeriesPaint(1, gp1);
renderer.setGradientPaintTransformer(
new StandardGradientPaintTransformer(GradientPaintTransformType.VERTICAL)
);
return chart;
}
public static void main(String[] args) {
LineChartDemo demo = new LineChartDemo("Line Chart Demo");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
```
这段代码使用JFreeChart库生成随机数据的曲线图。如果你有自己的数据,只需要修改相应的代码即可。
阅读全文