Insert a button into a JFreeChart graph

后端 未结 2 474
有刺的猬
有刺的猬 2021-01-23 01:21

I used a code in order to display a graph. I want to insert a button in this graph (Show details) that i will used in order to present some details about the graph .It is realis

相关标签:
2条回答
  • 2021-01-23 01:51

    I've never done it before, but in principle ChartPanel extends from JPanel. So you could use the setLayout() method and add components, such as a JButton to your chart.

    However, I think this is not the spirit of ChartPanel. The chart itself is not made of JComponents that would be added to ChartPanel. In facts, it is drawn by the paintComponent() method from ChartPanel (Using Java2D). So if you add buttons and components to the panel, you'll have no way of properly controlling their display with respect to the charts. In particular the graph will always be displayed in the background, covering the full space of the panel.

    What I have done a few times with JFreeCharts, and works really well, is use popup menus. You can completely control what to put in the popup menu, using getPopupMenu() and setPopupMenu(), or createPopupMenu(), and the default menu from JFreeCharts is already quite neat.

    Alternatively, you could add JButtons next to or bellow the graph in your pnl container. Here's a example.

    0 讨论(0)
  • 2021-01-23 01:55

    Add a ChartMouseListener to the ChartPanel that contains your chart. You can get details about the PieSectionEntity that was clicked as shown below. See the implementation of toString() in PieSectionEntity for more. Optionally, you can highlight the entity as shown here.

    ChartPanel panel = new ChartPanel(chart);
    panel.addChartMouseListener(new ChartMouseListener() {
    
        @Override
        public void chartMouseClicked(ChartMouseEvent e) {
            ChartEntity entity = e.getEntity();
            if (entity instanceof PieSectionEntity) {
                PieSectionEntity pse = (PieSectionEntity) entity;
                System.out.println(pse);
            }
        }
    
        @Override
        public void chartMouseMoved(ChartMouseEvent event) {
        }
    });
    
    0 讨论(0)
提交回复
热议问题