单击条形图或条形图时打开新框架或图表

时间:2015-03-08 09:55:25

标签: java swing charts jfreechart

我有一个从mysql数据库中获取数据集的条形图,我仍然坚持使用chartMouseClicked。目前,当我在之前的question中学习时,单击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) {
}

1 个答案:

答案 0 :(得分:2)

在处理程序中打开JOptionPane,如下面ChartMouseListener添加到BarChartDemo1中所示。该窗格显示一个标签面板,如相关的example所示,但嵌套的ChartPanel也可以使用。

image

@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);
}