我有一个条形图,它从mysql数据库中获取数据集,而我一直 坚持使用chartMouseClicked。 正如我在上一个 问题中了解到的那样,当前代码是在单击x或y轴条时打印的,但是 当我单击x或y轴上的特定条并打开新的框架或图表时,如何设置该代码。 还有一个信息框会很好。
private void lineChart() { // *************** ADDING BAR CHART FROM DATABASE ***************************** try { String sql = "select Region, Male, Female from ObeseLondon limit 14"; JDBCCategoryDataset dataset = new JDBCCategoryDataset(MySQL.Connectdb(), sql); JFreeChart chart = ChartFactory.createBarChart("", "Town", "No. Of Obese People", dataset, PlotOrientation.HORIZONTAL, true, true, true); chart.setBackgroundPaint(Color.white); BarRenderer render = null; //CategoryPlot plot = null; CategoryPlot plot = (CategoryPlot) chart.getPlot(); plot.getRenderer().setSeriesPaint(0, Color.green); plot.getRenderer().setSeriesPaint(1, Color.yellow); render = new BarRenderer(); org.jfree.chart.ChartFrame chartframe = new org.jfree.chart.ChartFrame("Query Chart", chart); //chartframe.setVisible(true); //chartframe.setSize(200,500); panelBarChart.setLayout(new java.awt.BorderLayout()); ChartPanel chartPanel = new ChartPanel(chart); panelBarChart.add(chartPanel); panelBarChart.validate(); chartPanel.addChartMouseListener(new ChartMouseListener() { @Override public void chartMouseClicked(ChartMouseEvent event) { ChartEntity entity = event.getEntity(); System.out.println(entity); } @Override public void chartMouseMoved(ChartMouseEvent event) { }
JOptionPane在您的处理程序中打开一个,如下所示ChartMouseListener添加到中BarChartDemo1。该 窗格显示如本相关标签的面板,例如,但嵌套ChartPanel将正常工作。
@Override public void chartMouseClicked(ChartMouseEvent event) { CategoryItemEntity entity = (CategoryItemEntity) event.getEntity(); JPanel panel = new JPanel(new GridLayout(0, 1)); Comparable row = entity.getRowKey(); Comparable col = entity.getColumnKey(); panel.add(new JLabel(String.valueOf(row))); panel.add(new JLabel(String.valueOf(col))); panel.add(new JLabel(String.valueOf(entity.getDataset().getValue(row, col)))); JOptionPane.showMessageDialog(rootPane, panel); }