在Java 中將軸添加到.PNG 圖像
修改靜態.PNG 圖像以包含軸具有挑戰性,可能會導致註冊錯誤。相反,請考慮將軸渲染合併到影像建立過程中。以下註解的是使用 Java API 的解決方案:
利用此處概述的方法根據需要自訂渲染的形狀、顏色和軸。
要為單一項目著色,API 建議使用該方法此處演示了自訂渲染器重寫 getItemPaint()。 Color.getHSBColor() 用於建立全光譜。
用於比較的預設渲染器可以在此處觀察:
<img src="https://i.sstatic.net/aEDHy.png" alt="Response Time chart">
以下是用於建立所需圖表的程式碼片段:
<code class="java">import java.awt.Color; import java.awt.EventQueue; import java.awt.Shape; import java.awt.geom.Ellipse2D; import java.util.ArrayList; import java.util.List; import java.util.Random; import javax.swing.JFrame; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; import org.jfree.data.xy.XYDataset; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; /** @see https://stackoverflow.com/q/9843451/230513 */ public class ResponseTime { private static final int N = 600; private static final String title = "ResponseTime"; private static final Random random = new Random(); private static final Shape circle = new Ellipse2D.Double(-3, -3, 6, 6); private static final Color line = Color.gray; private ChartPanel createPanel() { JFreeChart chart = ChartFactory.createXYLineChart( title, "Elapsed Time (secs)", "Response Time (secs)", createDataset(), PlotOrientation.VERTICAL, true, true, false); XYPlot plot = chart.getXYPlot(); MyRenderer renderer = new MyRenderer(true, true, N); plot.setRenderer(renderer); renderer.setSeriesShape(0, circle); renderer.setSeriesPaint(0, line); renderer.setUseFillPaint(true); renderer.setSeriesShapesFilled(0, true); renderer.setSeriesShapesVisible(0, true); renderer.setUseOutlinePaint(true); renderer.setSeriesOutlinePaint(0, line); ValueAxis range = plot.getRangeAxis(); range.setLowerBound(0.5); return new ChartPanel(chart); } private static class MyRenderer extends XYLineAndShapeRenderer { private List<Color> clut; public MyRenderer(boolean lines, boolean shapes, int n) { super(lines, shapes); clut = new ArrayList<>(n); for (int i = 0; i < n; i++) { clut.add(Color.getHSBColor((float) i / n, 1, 1)); } } @Override public Paint getItemFillPaint(int row, int column) { return clut.get(column); } } private XYDataset createDataset() { XYSeriesCollection result = new XYSeriesCollection(); XYSeries series = new XYSeries("Series 1"); for (double x = 0; x < N - 1; x++) { series.add(x, f(x)); } series.add(25, 1.75); // outlier result.addSeries(series); return result; } private double f(double x) { double y = 0.004 * x + .75; return y + random.nextGaussian() * y / 10; } private void display() { JFrame f = new JFrame(title); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(createPanel()); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new ResponseTime().display(); } }); } }</code>
該解決方案允許動態自訂圖表並產生帶有標籤和軸資訊的高品質影像。
以上是如何在 Java 中為 .PNG 圖像添加軸?的詳細內容。更多資訊請關注PHP中文網其他相關文章!