Search in sources :

Example 1 with ChartMouseListener

use of org.jfree.chart.ChartMouseListener in project jgnash by ccavanaugh.

the class IncomeExpensePieChart method createPanel.

private JPanel createPanel() {
    EnumSet<AccountType> set = EnumSet.of(AccountType.INCOME, AccountType.EXPENSE);
    JButton refreshButton = new JButton(rb.getString("Button.Refresh"));
    startField = new DatePanel();
    endField = new DatePanel();
    showEmptyCheck = new JCheckBox(rb.getString("Label.ShowEmptyAccounts"));
    showPercentCheck = new JCheckBox(rb.getString("Button.ShowPercentValues"));
    final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
    Objects.requireNonNull(engine);
    combo = AccountListComboBox.getParentTypeInstance(engine.getRootAccount(), set);
    final LocalDate dStart = DateUtils.getFirstDayOfTheMonth(LocalDate.now()).minusYears(1);
    long start = pref.getLong(START_DATE, DateUtils.asEpochMilli(dStart));
    startField.setDate(DateUtils.asLocalDate(start));
    currentAccount = combo.getSelectedAccount();
    JFreeChart chart = createPieChart(currentAccount);
    chartPanel = new ChartPanel(chart, true, true, true, false, true);
    //                         (chart, properties, save, print, zoom, tooltips)
    FormLayout layout = new FormLayout("p, 4dlu, 70dlu, 8dlu, p, 4dlu, 70dlu, 8dlu, p, 4dlu:g, left:p", "f:d, 3dlu, f:d, 6dlu, f:p:g");
    DefaultFormBuilder builder = new DefaultFormBuilder(layout);
    layout.setRowGroups(new int[][] { { 1, 3 } });
    builder.append(combo, 9);
    builder.append(showEmptyCheck);
    builder.nextLine();
    builder.nextLine();
    builder.append(rb.getString("Label.StartDate"), startField);
    builder.append(rb.getString("Label.EndDate"), endField);
    builder.append(refreshButton);
    builder.append(showPercentCheck);
    builder.nextLine();
    builder.nextLine();
    builder.append(chartPanel, 11);
    JPanel panel = builder.getPanel();
    combo.addActionListener(e -> {
        setCurrentAccount(combo.getSelectedAccount());
        pref.putLong(START_DATE, DateUtils.asEpochMilli(startField.getLocalDate()));
    });
    refreshButton.addActionListener(e -> {
        setCurrentAccount(currentAccount);
        pref.putLong(START_DATE, DateUtils.asEpochMilli(startField.getLocalDate()));
    });
    showEmptyCheck.addActionListener(e -> setCurrentAccount(currentAccount));
    showPercentCheck.addActionListener(e -> ((PiePlot) chartPanel.getChart().getPlot()).setLabelGenerator(showPercentCheck.isSelected() ? percentLabels : defaultLabels));
    ChartMouseListener mouseListener = new ChartMouseListener() {

        @Override
        public void chartMouseClicked(final ChartMouseEvent event) {
            MouseEvent me = event.getTrigger();
            if (me.getID() == MouseEvent.MOUSE_CLICKED && me.getClickCount() == 1) {
                try {
                    ChartEntity entity = event.getEntity();
                    // expand sections if interesting, back out if in nothing
                    if (entity instanceof PieSectionEntity) {
                        Account a = (Account) ((PieSectionEntity) entity).getSectionKey();
                        if (a.getChildCount() > 0) {
                            setCurrentAccount(a);
                        }
                    } else if (entity == null) {
                        Account parent = currentAccount;
                        if (parent == null) {
                            return;
                        }
                        parent = parent.getParent();
                        if (parent == null || parent instanceof RootAccount) {
                            return;
                        }
                        setCurrentAccount(parent);
                    }
                } catch (final Exception e) {
                    Logger.getLogger(IncomeExpensePieChart.class.getName()).log(Level.SEVERE, e.getLocalizedMessage(), e);
                }
            }
        }

        @Override
        public void chartMouseMoved(ChartMouseEvent event) {
            setChartCursor(chartPanel, event.getEntity(), event.getTrigger().getPoint());
        }
    };
    chartPanel.addChartMouseListener(mouseListener);
    return panel;
}
Also used : FormLayout(com.jgoodies.forms.layout.FormLayout) JPanel(javax.swing.JPanel) RootAccount(jgnash.engine.RootAccount) Account(jgnash.engine.Account) ChartPanel(org.jfree.chart.ChartPanel) ChartMouseEvent(org.jfree.chart.ChartMouseEvent) MouseEvent(java.awt.event.MouseEvent) JButton(javax.swing.JButton) ChartMouseEvent(org.jfree.chart.ChartMouseEvent) AccountType(jgnash.engine.AccountType) LocalDate(java.time.LocalDate) JFreeChart(org.jfree.chart.JFreeChart) ChartMouseListener(org.jfree.chart.ChartMouseListener) JCheckBox(javax.swing.JCheckBox) RootAccount(jgnash.engine.RootAccount) DatePanel(jgnash.ui.components.DatePanel) DefaultFormBuilder(com.jgoodies.forms.builder.DefaultFormBuilder) ChartEntity(org.jfree.chart.entity.ChartEntity) Engine(jgnash.engine.Engine) PieSectionEntity(org.jfree.chart.entity.PieSectionEntity)

Example 2 with ChartMouseListener

use of org.jfree.chart.ChartMouseListener in project cubrid-manager by CUBRID.

the class MonitorStatisticChart method loadChart.

public void loadChart() {
    recordMillisecs = System.currentTimeMillis();
    if (groupName != null) {
        final Group chartGroup = addGeneralGroup(composite);
        frame = new ChartComposite(chartGroup, SWT.NONE, createChart(), false, true, false, true, true);
    } else {
        frame = new ChartComposite(composite, SWT.NONE, createChart(), false, true, false, true, true);
    }
    frame.setDisplayToolTips(true);
    GridData gdFrame = new GridData(SWT.FILL, SWT.FILL, true, true);
    frame.setLayoutData(gdFrame);
    frame.setLayout(new FillLayout());
    if (!isDetailView) {
        frame.addChartMouseListener(new ChartMouseListener() {

            public void chartMouseMoved(ChartMouseEvent event) {
            }

            public void chartMouseClicked(ChartMouseEvent event) {
                if (!isHasValidData) {
                    return;
                }
                if (event.getTrigger().getButton() == 1) {
                    long interval = System.currentTimeMillis() - recordMillisecs;
                    if (interval < 300) {
                        //trigger double click action
                        if (editor == null) {
                            return;
                        }
                        editor.openDetailView(statisticChartItem, statisticDataList);
                    } else {
                        recordMillisecs = System.currentTimeMillis();
                    }
                }
            }
        });
    }
}
Also used : Group(org.eclipse.swt.widgets.Group) ChartComposite(org.jfree.experimental.chart.swt.ChartComposite) GridData(org.eclipse.swt.layout.GridData) FillLayout(org.eclipse.swt.layout.FillLayout) ChartMouseEvent(org.jfree.chart.ChartMouseEvent) ChartMouseListener(org.jfree.chart.ChartMouseListener)

Example 3 with ChartMouseListener

use of org.jfree.chart.ChartMouseListener in project eclipse-cs by checkstyle.

the class GraphStatsView method createMasterView.

/**
 * Creates the master view containing the chart.
 *
 * @param parent
 *          the parent composite
 * @return the chart composite
 */
public Composite createMasterView(Composite parent) {
    // create the date set for the chart
    mPieDataset = new GraphPieDataset();
    mPieDataset.setShowAllCategories(mShowAllCategoriesAction.isChecked());
    mGraph = createChart(mPieDataset);
    // creates the chart component
    // Composite embeddedComposite = new Composite(parent, SWT.EMBEDDED |
    // SWT.NO_BACKGROUND);
    // embeddedComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
    // experimental usage of JFreeChart SWT
    // the composite to harbor the Swing chart control
    ChartComposite embeddedComposite = new ChartComposite(parent, SWT.NONE, mGraph, true);
    embeddedComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
    embeddedComposite.addChartMouseListener(new ChartMouseListener() {

        @Override
        public void chartMouseClicked(final ChartMouseEvent event) {
            MouseEvent trigger = event.getTrigger();
            if (trigger.getButton() == MouseEvent.BUTTON1 && event.getEntity() instanceof PieSectionEntity) {
                // && trigger.getClickCount() == 2 //doubleclick not correctly
                // detected with the SWT
                // composite
                mMasterComposite.getDisplay().syncExec(new Runnable() {

                    @Override
                    public void run() {
                        mIsDrilledDown = true;
                        mCurrentDetailCategory = (String) ((PieSectionEntity) event.getEntity()).getSectionKey();
                        mStackLayout.topControl = mDetailViewer.getTable();
                        mMainSection.layout();
                        mDetailViewer.setInput(mDetailViewer.getInput());
                        updateActions();
                        updateLabel();
                    }
                });
            } else {
                event.getTrigger().consume();
            }
        }

        @Override
        public void chartMouseMoved(ChartMouseEvent event) {
        // NOOP
        }
    });
    return embeddedComposite;
}
Also used : ChartComposite(org.jfree.experimental.chart.swt.ChartComposite) ChartMouseEvent(org.jfree.chart.ChartMouseEvent) MouseEvent(java.awt.event.MouseEvent) GridData(org.eclipse.swt.layout.GridData) ChartMouseEvent(org.jfree.chart.ChartMouseEvent) PieSectionEntity(org.jfree.chart.entity.PieSectionEntity) ChartMouseListener(org.jfree.chart.ChartMouseListener)

Example 4 with ChartMouseListener

use of org.jfree.chart.ChartMouseListener in project tdq-studio-se by Talend.

the class TOPChartService method addMouseListenerForChart.

@Override
public void addMouseListenerForChart(Object chartComposite, final Map<String, Object> menuMap, final boolean useRowFirst) {
    final ChartComposite chartComp = (ChartComposite) chartComposite;
    final ChartMouseListener listener = new ChartMouseListener() {

        @Override
        public void chartMouseClicked(ChartMouseEvent event) {
            boolean flag = event.getTrigger().getButton() != MouseEvent.BUTTON3;
            chartComp.setDomainZoomable(flag);
            chartComp.setRangeZoomable(flag);
            if (flag) {
                return;
            }
            ChartEntity chartEntity = event.getEntity();
            if (chartEntity != null && chartEntity instanceof CategoryItemEntity) {
                CategoryItemEntity cateEntity = (CategoryItemEntity) chartEntity;
                Menu menu = getCurrentMenu(cateEntity);
                if (menu != null) {
                    chartComp.setMenu(menu);
                    menu.setVisible(true);
                }
            }
        }

        private Menu getCurrentMenu(CategoryItemEntity cateEntity) {
            if (useRowFirst) {
                return findCurrentMenu(cateEntity.getRowKey(), cateEntity.getColumnKey());
            } else {
                return findCurrentMenu(cateEntity.getColumnKey(), cateEntity.getRowKey());
            }
        }

        /**
         * DOC yyin Comment method "findCurrentMenu".
         *
         * @param firstKey
         * @param secondKey
         * @return
         */
        private Menu findCurrentMenu(final Object firstKey, Object secondKey) {
            Object menu = menuMap.get(firstKey);
            if (menu != null) {
                return (Menu) menu;
            }
            menu = menuMap.get(secondKey);
            if (menu != null) {
                return (Menu) menu;
            }
            return null;
        }

        @Override
        public void chartMouseMoved(ChartMouseEvent event) {
        // no action here
        }
    };
    chartComp.addChartMouseListener(listener);
    chartComp.addDisposeListener(new DisposeListener() {

        @Override
        public void widgetDisposed(DisposeEvent e) {
            chartComp.removeChartMouseListener(listener);
            chartComp.dispose();
        }
    });
}
Also used : TalendChartComposite(org.talend.dataprofiler.chart.util.TalendChartComposite) ChartComposite(org.jfree.experimental.chart.swt.ChartComposite) DisposeListener(org.eclipse.swt.events.DisposeListener) ChartEntity(org.jfree.chart.entity.ChartEntity) Menu(org.eclipse.swt.widgets.Menu) ChartMouseEvent(org.jfree.chart.ChartMouseEvent) DisposeEvent(org.eclipse.swt.events.DisposeEvent) CategoryItemEntity(org.jfree.chart.entity.CategoryItemEntity) ChartMouseListener(org.jfree.chart.ChartMouseListener)

Example 5 with ChartMouseListener

use of org.jfree.chart.ChartMouseListener in project mafscaling by vimsh.

the class LogView method createGraghPanel.

private void createGraghPanel() {
    JFreeChart chart = ChartFactory.createXYLineChart(null, null, null, null, PlotOrientation.VERTICAL, false, true, false);
    chartPanel = new ChartPanel(chart, true, true, true, true, true);
    chartPanel.setFocusable(true);
    chartPanel.setAutoscrolls(true);
    chartPanel.setPopupMenu(null);
    chart.setBackgroundPaint(chartColor);
    rpmDataset = new XYSeriesCollection();
    rpmPlotRenderer = new XYLineAndShapeRenderer();
    dataset = new XYSeriesCollection();
    plotRenderer = new XYLineAndShapeRenderer();
    NumberAxis xAxis = new NumberAxis();
    xAxis.setTickLabelsVisible(false);
    xAxis.setTickLabelPaint(Color.WHITE);
    xAxis.setAutoRangeIncludesZero(false);
    NumberAxis yAxis = new NumberAxis();
    yAxis.setTickLabelsVisible(false);
    yAxis.setTickLabelPaint(Color.WHITE);
    yAxis.setAutoRangeIncludesZero(false);
    NumberAxis y2Axis = new NumberAxis();
    y2Axis.setTickLabelsVisible(false);
    y2Axis.setTickLabelPaint(Color.WHITE);
    y2Axis.setAutoRangeIncludesZero(false);
    plot = chartPanel.getChart().getXYPlot();
    plot.setRangePannable(true);
    plot.setDomainPannable(true);
    plot.setDomainGridlinePaint(Color.LIGHT_GRAY);
    plot.setRangeGridlinePaint(Color.LIGHT_GRAY);
    plot.setBackgroundPaint(chartBgColor);
    plot.setDataset(0, rpmDataset);
    plot.setRenderer(0, rpmPlotRenderer);
    plot.setDomainAxis(0, xAxis);
    plot.setRangeAxis(0, yAxis);
    plot.mapDatasetToDomainAxis(0, 0);
    plot.mapDatasetToRangeAxis(0, 0);
    plot.setDataset(1, dataset);
    plot.setRenderer(1, plotRenderer);
    plot.setRangeAxis(1, y2Axis);
    plot.mapDatasetToDomainAxis(1, 0);
    plot.mapDatasetToRangeAxis(1, 1);
    LegendTitle legend = new LegendTitle(plot);
    legend.setItemFont(new Font("Arial", 0, 10));
    legend.setPosition(RectangleEdge.TOP);
    legend.setItemPaint(Color.WHITE);
    chart.addLegend(legend);
    xMarker = new XYDomainMutilineAnnotation();
    xMarker.setDefaultPaint(Color.WHITE);
    plot.addAnnotation(xMarker);
    chartMouseListener = new ChartMouseListener() {

        @Override
        public void chartMouseMoved(ChartMouseEvent event) {
            try {
                Rectangle2D dataArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea();
                Point2D p = chartPanel.translateScreenToJava2D(event.getTrigger().getPoint());
                double x = plot.getDomainAxis().java2DToValue(p.getX(), dataArea, plot.getDomainAxisEdge());
                boolean isLeft = (p.getX() < (dataArea.getMaxX() - dataArea.getMinX()) / 2) ? true : false;
                if (setMarkers(x, isLeft)) {
                    try {
                        int selectedCol = logDataTable.getTable().getSelectedColumn();
                        if (selectedCol < 0)
                            selectedCol = 0;
                        if (logPlayWindow == null || startMarker != null || endMarker != null) {
                            logDataTable.getTable().setRowSelectionInterval((int) x, (int) x);
                            logDataTable.getTable().changeSelection((int) x, selectedCol, false, false);
                        } else {
                            logPlayWindow.setProgressBar((int) x);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void chartMouseClicked(ChartMouseEvent event) {
            chartPanel.requestFocusInWindow();
            if (logPlayWindow == null)
                return;
            if (xMarker.count() == 0)
                return;
            Rectangle2D dataArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea();
            Point2D p = chartPanel.translateScreenToJava2D(event.getTrigger().getPoint());
            double x = plot.getDomainAxis().java2DToValue(p.getX(), dataArea, plot.getDomainAxisEdge());
            if (x < 0 || (int) x >= logDataTable.getRowCount())
                return;
            if (SwingUtilities.isLeftMouseButton(event.getTrigger())) {
                if (startMarker == null) {
                    startMarker = new ValueMarker(x);
                    startMarker.setPaint(Color.GREEN);
                    startMarker.setStroke(new BasicStroke(1.5f));
                    plot.addDomainMarker(startMarker);
                } else {
                    plot.removeDomainMarker(startMarker);
                    startMarker = null;
                }
            } else if (SwingUtilities.isRightMouseButton(event.getTrigger())) {
                if (endMarker == null) {
                    endMarker = new ValueMarker(x);
                    endMarker.setPaint(Color.GREEN);
                    endMarker.setStroke(new BasicStroke(1.5f));
                    plot.addDomainMarker(endMarker);
                } else {
                    plot.removeDomainMarker(endMarker);
                    endMarker = null;
                }
            }
            chartPanel.repaint();
            logPlayWindow.setStartEndArea(startMarker, endMarker);
        }
    };
    chartPanel.addChartMouseListener(chartMouseListener);
    chartPanel.addKeyListener(new KeyListener() {

        public void keyPressed(KeyEvent e) {
            if (!chartPanel.hasFocus())
                return;
            int keyCode = e.getKeyCode();
            if (keyCode < KeyEvent.VK_LEFT || keyCode > KeyEvent.VK_DOWN)
                return;
            ValueAxis axis = null;
            if (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_RIGHT)
                axis = ((XYPlot) chartPanel.getChart().getXYPlot()).getDomainAxis();
            else
                axis = ((XYPlot) chartPanel.getChart().getXYPlot()).getRangeAxis();
            if (axis != null) {
                double delta = (axis.getUpperBound() - axis.getLowerBound()) / 100.0;
                if (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_DOWN)
                    axis.setRange(axis.getLowerBound() - delta, axis.getUpperBound() - delta);
                else if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_RIGHT)
                    axis.setRange(axis.getLowerBound() + delta, axis.getUpperBound() + delta);
            }
        }

        public void keyReleased(KeyEvent arg0) {
        }

        public void keyTyped(KeyEvent arg0) {
        }
    });
}
Also used : BasicStroke(java.awt.BasicStroke) ChartPanel(org.jfree.chart.ChartPanel) NumberAxis(org.jfree.chart.axis.NumberAxis) XYLineAndShapeRenderer(org.jfree.chart.renderer.xy.XYLineAndShapeRenderer) Rectangle2D(java.awt.geom.Rectangle2D) LegendTitle(org.jfree.chart.title.LegendTitle) ChartMouseEvent(org.jfree.chart.ChartMouseEvent) JFreeChart(org.jfree.chart.JFreeChart) Font(java.awt.Font) IOException(java.io.IOException) ChartMouseListener(org.jfree.chart.ChartMouseListener) KeyEvent(java.awt.event.KeyEvent) Point2D(java.awt.geom.Point2D) ValueAxis(org.jfree.chart.axis.ValueAxis) KeyListener(java.awt.event.KeyListener) ValueMarker(org.jfree.chart.plot.ValueMarker) XYSeriesCollection(org.jfree.data.xy.XYSeriesCollection)

Aggregations

ChartMouseEvent (org.jfree.chart.ChartMouseEvent)15 ChartMouseListener (org.jfree.chart.ChartMouseListener)15 ChartEntity (org.jfree.chart.entity.ChartEntity)10 ChartPanel (org.jfree.chart.ChartPanel)8 ChartComposite (org.jfree.experimental.chart.swt.ChartComposite)7 BasicStroke (java.awt.BasicStroke)6 Color (java.awt.Color)6 XYItemEntity (org.jfree.chart.entity.XYItemEntity)6 GridBagLayout (java.awt.GridBagLayout)5 JLabel (javax.swing.JLabel)5 JPanel (javax.swing.JPanel)5 CrosshairOverlay (org.jfree.chart.panel.CrosshairOverlay)5 Crosshair (org.jfree.chart.plot.Crosshair)5 XYDataset (org.jfree.data.xy.XYDataset)5 TalendChartComposite (org.talend.dataprofiler.chart.util.TalendChartComposite)4 DisposeEvent (org.eclipse.swt.events.DisposeEvent)3 DisposeListener (org.eclipse.swt.events.DisposeListener)3 Menu (org.eclipse.swt.widgets.Menu)3 CategoryItemEntity (org.jfree.chart.entity.CategoryItemEntity)3 Font (java.awt.Font)2