Код: Выделить всё
private void generateChart(String chartType, String xAxis, String yAxis) {
JFreeChart chart = null;
if ("Bar Chart".equals(chartType)) {
chart = createBarChart(xAxis, yAxis);
} else if ("Pie Chart".equals(chartType)) {
chart = createPieChart(xAxis, yAxis);
} else if ("Line Graph".equals(chartType)) {
chart = createLineChart(xAxis, yAxis);
}
if (chart != null) {
JFrame chartFrame = new JFrame("Chart");
chartFrame.setSize(800, 600);
chartFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// Create chart panel with scroll
ChartPanel chartPanel = createChartPanelWithScroll(chart);
chartFrame.add(chartPanel);
chartFrame.setVisible(true);
}
}
// Method to create scrollable and zoomable ChartPanel
public ChartPanel createChartPanelWithScroll(JFreeChart chart) {
// Create the ChartPanel with the chart
ChartPanel chartPanel = new ChartPanel(chart);
// Set preferred size larger than the visible area to ensure scrollbars appear
// chartPanel.setPreferredSize(new java.awt.Dimension(2000, 1200)); // Force a larger size
// Enable mouse zooming and panning
chartPanel.setMouseWheelEnabled(true); // Enable zooming with mouse wheel
chartPanel.setDomainZoomable(true); // Allow zooming on X-axis
chartPanel.setRangeZoomable(true);
// Allow zooming on Y-axis
// Wrap the ChartPanel with a JScrollPane for scrollable bars
JScrollPane scrollPane = new JScrollPane(chartPanel,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// Ensure that the JScrollPane size is set to occupy the window space
scrollPane.setPreferredSize(new java.awt.Dimension(800, 600));
// Create a panel that contains the scrollPane (with the chartPanel inside)
JPanel chartWithScrollPanel = new JPanel(new BorderLayout());
chartWithScrollPanel.add(scrollPane, BorderLayout.CENTER);
return chartPanel;
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... ng-problem