public static void createLineChart(double[] data) { XYSeries series = new XYSeries("Data"); for (int i = 0; i < data.length; i++) { series.add(i+1, data[i]/10000); } XYSeriesCollection dataset = new XYSeriesCollection(series); JFreeChart chart = ChartFactory.createXYLineChart( "Best Time vs. Generation", "Generation", "Best Time", dataset, PlotOrientation.VERTICAL, true, true, false ); chart.setBackgroundPaint(Color.WHITE); XYPlot plot = chart.getXYPlot(); plot.setBackgroundPaint(Color.white); plot.setDomainGridlinePaint(Color.lightGray); plot.setRangeGridlinePaint(Color.lightGray); NumberAxis xAxis = (NumberAxis) plot.getDomainAxis(); xAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); xAxis.setTickUnit(new NumberTickUnit(100)); xAxis.setTickMarkInsideLength(5.0f); xAxis.setTickMarkOutsideLength(0.0f); NumberAxis yAxis = (NumberAxis) plot.getRangeAxis(); yAxis.setTickUnit(new NumberTickUnit(0.01)); yAxis.setTickMarkInsideLength(5.0f); yAxis.setTickMarkOutsideLength(0.0f); XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(); renderer.setSeriesPaint(0, Color.black); renderer.setSeriesStroke(0, new BasicStroke(1.0f)); plot.setRenderer(renderer); ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new Dimension(500, 500)); JFrame frame = new JFrame("Plot"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(chartPanel); frame.pack(); frame.setVisible(true); } 如何将纵轴变成可以自适应数据大小的
时间: 2024-01-17 13:05:43 浏览: 141
使用AChartEngine库在Android中创建图表
要将纵轴自适应数据大小,可以使用以下代码:
```
NumberAxis yAxis = (NumberAxis) plot.getRangeAxis();
yAxis.setAutoRange(true);
```
这里将 `yAxis` 强制转换为 `NumberAxis` 类型,然后调用 `setAutoRange(true)` 方法,表示让纵轴自适应数据大小。
阅读全文