Topic
4 replies Latest Post - ‏2012-02-28T16:30:10Z by bschooly
bschooly
bschooly
30 Posts
ACCEPTED ANSWER

Pinned topic Discovering chart component clicked

‏2012-02-24T04:56:55Z |
I saw somewhere but now can't seem to locate how to determine
what chart component was clicked on (legend, scale, footer, etc).

Someone point me in the right direction.

Thanks.
Updated on 2012-02-28T16:30:10Z at 2012-02-28T16:30:10Z by bschooly
  • slowhand
    slowhand
    113 Posts
    ACCEPTED ANSWER

    Re: Discovering chart component clicked

    ‏2012-02-24T16:00:29Z  in response to bschooly
    > how to determine what chart component was clicked on (legend, scale, footer, etc).

    If you have a MouseEvent and only want to distinguish among chart area, legend,
    footer, header, you can use the AWT/Swing primitive method Container.findComponentAt.

    To distinguish the graphic elements inside the chart area (scales, data points, etc.)
    either use one of the subclasses of IlvChartDataInteractor, or the IlvChartCycleSelectInteractor.

    --slowhand
    • bschooly
      bschooly
      30 Posts
      ACCEPTED ANSWER

      Re: Discovering chart component clicked

      ‏2012-02-24T17:44:59Z  in response to slowhand
      I'm only interested in the chart contents. Are you suggesting that
      I extend IlvChartDataInteractor to do this? Not sure which method will
      tell me what point clicked corresponds to.

      What I want to do is respond to mouse clicks (probably right mouse)
      within the chart and popup the appropriate customizer. So I need to
      be able to distinguish between the components of the chart.

      This will work well for scales, legend, footer, etc but other items
      like chart (vs chartArea), and grids are problematic.

      What I originally coded was a new iterator that responded to right
      mouse click with a tabbed dialog of all the selectors. This didn't
      turn out well as it seems Jviews doesn't like multiple customizers in
      use at once. Hence my idea of breaking out the customization based on
      what was clicked.

      Looking for design ideas here that actually will work.

      Thanks for your reply..
      • DanJeffrey
        DanJeffrey
        3 Posts
        ACCEPTED ANSWER

        Re: Discovering chart component clicked

        ‏2012-02-28T16:16:40Z  in response to bschooly
        The following code demonstrates how to do hit-testing on various parts of the chart. It also shows how to convert display coordinates on the chart to data values.

        import ilog.views.chart.IlvChart;
        import ilog.views.chart.IlvChartLayout;
        import ilog.views.chart.IlvDoublePoints;
        import ilog.views.chart.IlvLegend;
        import ilog.views.chart.IlvScale;
        import ilog.views.chart.data.IlvDataSource;
        import ilog.views.chart.data.IlvDefaultDataSet;
        import ilog.views.chart.data.IlvDefaultDataSource;
        import ilog.views.util.IlvProductUtil;

        import java.awt.BorderLayout;
        import java.awt.Point;
        import java.awt.Rectangle;
        import java.awt.event.MouseAdapter;
        import java.awt.event.MouseEvent;
        import java.awt.geom.Rectangle2D;

        import javax.swing.JFrame;
        import javax.swing.JPanel;

        public class HitTestChartComponents extends JPanel {
        static {
        IlvProductUtil
        .DeploymentLicenseRequired(IlvProductUtil.IBM_ILOG_JViews_Charts_Deployment);
        }
        private final int POINT_COUNT = 50;
        private final int RANGE = 100;
        IlvDefaultDataSet data[];
        int x = 0;
        IlvChart chart;

        public HitTestChartComponents() {
        super(new BorderLayout());
        chart = new IlvChart();
        IlvLegend legend = new IlvLegend();
        chart.addLegend(legend, IlvChartLayout.NORTH_EAST);
        fillDataArray();
        IlvDataSource newData = new IlvDefaultDataSource(data);
        chart.setDataSource(newData);
        add(chart, BorderLayout.CENTER);

        chart.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
        System.out.println("You hit the chart");
        }
        });

        chart.getChartArea().addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
        System.out.println("You hit the chart area");
        Point ptData = null;
        Point ptDisplay = e.getPoint();

        IlvDoublePoints ptsConvert = new IlvDoublePoints();
        ptsConvert.add(ptDisplay.x, ptDisplay.y);
        chart.toData(ptsConvert);
        ptData = new Point(ptsConvert.getXFloor(0), ptsConvert.getYFloor(0));

        System.out.println(" display coordinates = " + ptDisplay
        + " data space coordinates = " + ptData);

        IlvScale xScale = chart.getScale(chart.getXAxis());
        Rectangle2D rectXScale = xScale.getBounds(null);
        if (rectXScale.intersects(new Rectangle(ptDisplay.x,
        ptDisplay.y, 1, 1))) {
        System.out.println(" You hit the X scale!");
        }

        IlvScale yScale = chart.getScale(chart.getYAxis(0));
        Rectangle2D rectYScale = yScale.getBounds(null);
        if (rectYScale.intersects(new Rectangle(ptDisplay.x,
        ptDisplay.y, 1, 1))) {
        System.out.println(" You hit the Y scale!");
        }

        Rectangle rectPlot = chart.getChartArea().getPlotRect();
        if (rectPlot.intersects(new Rectangle(ptDisplay.x,
        ptDisplay.y, 1, 1))) {
        System.out.println(" You hit the plot area (data or grid)!");
        }
        }
        });

        legend.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
        System.out.println("You hit the chart legend");
        }
        });
        }

        int DATA_SET_COUNT = 3;

        private void fillDataArray() {
        data = new IlvDefaultDataSetDATA_SET_COUNT;
        for (int i = 0; i < DATA_SET_COUNT; i++) {
        data[i] = new IlvDefaultDataSet(String.valueOf(i));
        }
        for (int j = 0; j < POINT_COUNT; j++) {
        for (int k = 0; k < DATA_SET_COUNT; k++) {
        data[k].addData(x, RANGE * Math.random());
        }
        x++;
        }
        }

        public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
        JFrame app = new JFrame("Skeleton JViews Charts Application");
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        app.setContentPane(new HitTestChartComponents());
        app.setBounds(new Rectangle(10, 10, 800, 600));
        app.setVisible(true);
        }
        });
        }

        private static final long serialVersionUID = 1L;
        }
  • bschooly
    bschooly
    30 Posts
    ACCEPTED ANSWER

    Re: Discovering chart component clicked

    ‏2012-02-28T16:30:10Z  in response to bschooly
    Thank you