使用多個類別縮放JFreeChart 箱線圖
問題陳述:
正確的縮放技術:
為了確保正確的縮放,設定首選大小至關重要包含圖表的ChartPanel,不是圖表本身
。這可確保僅縮放子圖,同時保持圖例和註釋的大小和可讀性。基於頁面的方法:
在數量過多的情況下類別,考慮基於頁面的方法可能更實用。這涉及將資料劃分為較小的子集並一次僅顯示一部分資料。可以新增控制元素(例如捲軸)以在資料頁面之間導航。範例:
以下程式碼示範了基於頁面的實作:import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.util.ArrayList; import java.util.List; import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.renderer.category.BoxAndWhiskerRenderer; import org.jfree.data.statistics.DefaultBoxAndWhiskerCategoryDataset; public class BoxAndWhiskerDemo { private List<List<List<Double>>> data; private DefaultBoxAndWhiskerCategoryDataset dataset; private CategoryPlot plot; private ChartPanel chartPanel; private JPanel controlPanel; private int start = 0; private int visible = 4; public BoxAndWhiskerDemo() { createData(); createDataset(start); createChartPanel(); createControlPanel(); } // Data generation and dataset creation // ... private void createChartPanel() { CategoryAxis xAxis = new CategoryAxis("Category"); NumberAxis yAxis = new NumberAxis("Value"); BoxAndWhiskerRenderer renderer = new BoxAndWhiskerRenderer(); plot = new CategoryPlot(dataset, xAxis, yAxis, renderer); JFreeChart chart = new JFreeChart("BoxAndWhiskerDemo", plot); chartPanel = new ChartPanel(chart); } private void createControlPanel() { controlPanel = new JPanel(); controlPanel.add(new JButton(new AbstractAction("\u22b2Prev") { @Override public void actionPerformed(ActionEvent e) { start -= visible; if (start < 0) { start = 0; return; } createDataset(start); plot.setDataset(dataset); } })); controlPanel.add(new JButton(new AbstractAction("Next\u22b3") { @Override public void actionPerformed(ActionEvent e) { start += visible; if (start >= data.size() - visible) { start = data.size() - visible; return; } createDataset(start); plot.setDataset(dataset); } })); } // ... public static void main(String[] args) { BoxAndWhiskerDemo demo = new BoxAndWhiskerDemo(); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(demo.getChartPanel(), BorderLayout.CENTER); frame.add(demo.getControlPanel(), BorderLayout.SOUTH); frame.pack(); frame.setVisible(true); } }
以上是如何有效縮放具有多個類別的 JFreeChart 箱線圖?的詳細內容。更多資訊請關注PHP中文網其他相關文章!