List of usage examples for org.jfree.chart ChartPanel setMouseZoomable
public void setMouseZoomable(boolean flag, boolean fillRectangle)
From source file:org.trade.ui.chart.CandlestickChart.java
/** * A demonstration application showing a candlestick chart. * /*w w w . j a va 2 s . c o m*/ * @param title * the frame title. * @param strategyData * StrategyData */ public CandlestickChart(final String title, StrategyData strategyData, Tradingday tradingday) { this.strategyData = strategyData; this.setLayout(new BorderLayout()); // Used to mark the current price stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[] { 10, 3 }, 0); valueMarker = new ValueMarker(0.00, Color.black, stroke); this.chart = createChart(this.strategyData, title, tradingday); BlockContainer container = new BlockContainer(new BorderArrangement()); container.add(titleLegend1, RectangleEdge.LEFT); container.add(titleLegend2, RectangleEdge.RIGHT); container.add(new EmptyBlock(2000, 0)); CompositeTitle legends = new CompositeTitle(container); legends.setPosition(RectangleEdge.BOTTOM); this.chart.addSubtitle(legends); final ChartPanel chartPanel = new ChartPanel(this.chart); chartPanel.setFillZoomRectangle(true); chartPanel.setMouseZoomable(true, true); chartPanel.setRefreshBuffer(true); chartPanel.setDoubleBuffered(true); chartPanel.setVerticalAxisTrace(true); chartPanel.setHorizontalAxisTrace(true); chartPanel.addChartMouseListener(new ChartMouseListener() { public void chartMouseMoved(ChartMouseEvent e) { } public void chartMouseClicked(final ChartMouseEvent e) { CombinedDomainXYPlot combinedXYplot = (CombinedDomainXYPlot) e.getChart().getPlot(); @SuppressWarnings("unchecked") List<XYPlot> subplots = combinedXYplot.getSubplots(); if (e.getTrigger().getClickCount() == 2) { double xItem = 0; double yItem = 0; if (e.getEntity() instanceof XYItemEntity) { XYItemEntity xYItemEntity = ((XYItemEntity) e.getEntity()); xItem = xYItemEntity.getDataset().getXValue(xYItemEntity.getSeriesIndex(), xYItemEntity.getItem()); yItem = xYItemEntity.getDataset().getYValue(xYItemEntity.getSeriesIndex(), xYItemEntity.getItem()); } else { PlotEntity plotEntity = ((PlotEntity) e.getEntity()); XYPlot plot = (XYPlot) plotEntity.getPlot(); xItem = plot.getDomainCrosshairValue(); yItem = plot.getRangeCrosshairValue(); } for (XYPlot xyplot : subplots) { double x = xyplot.getDomainCrosshairValue(); double y = xyplot.getRangeCrosshairValue(); /* * If the cross hair is from a right-hand y axis we need * to convert this to a left-hand y axis. */ String rightAxisName = ", Price: "; double rangeLowerLeft = 0; double rangeUpperLeft = 0; double rangeLowerRight = 0; double rangeUpperRight = 0; double yRightLocation = 0; for (int index = 0; index < xyplot.getRangeAxisCount(); index++) { AxisLocation axisLocation = xyplot.getRangeAxisLocation(index); Range range = xyplot.getRangeAxis(index).getRange(); if (axisLocation.equals(AxisLocation.BOTTOM_OR_LEFT) || axisLocation.equals(AxisLocation.TOP_OR_LEFT)) { rangeLowerLeft = range.getLowerBound(); rangeUpperLeft = range.getUpperBound(); rightAxisName = ", " + xyplot.getRangeAxis(index).getLabel() + ": "; } if (y >= range.getLowerBound() && y <= range.getUpperBound() && (axisLocation.equals(AxisLocation.BOTTOM_OR_RIGHT) || axisLocation.equals(AxisLocation.TOP_OR_RIGHT))) { rangeUpperRight = range.getUpperBound(); rangeLowerRight = range.getLowerBound(); } } if ((rangeUpperRight - rangeLowerRight) > 0) { yRightLocation = rangeLowerLeft + ((rangeUpperLeft - rangeLowerLeft) * ((y - rangeLowerRight) / (rangeUpperRight - rangeLowerRight))); } else { yRightLocation = y; } String text = " Time: " + dateFormatShort.format(new Date((long) (x))) + rightAxisName + new Money(y); if (x == xItem && y == yItem) { titleLegend1.setText(text); if (null == clickCrossHairs) { clickCrossHairs = new XYTextAnnotation(text, x, yRightLocation); clickCrossHairs.setTextAnchor(TextAnchor.BOTTOM_LEFT); xyplot.addAnnotation(clickCrossHairs); } else { clickCrossHairs.setText(text); clickCrossHairs.setX(x); clickCrossHairs.setY(yRightLocation); } } } } else if (e.getTrigger().getClickCount() == 1 && null != clickCrossHairs) { for (XYPlot xyplot : subplots) { if (xyplot.removeAnnotation(clickCrossHairs)) { clickCrossHairs = null; titleLegend1.setText(" Time: 0, Price :0.0"); break; } } } } }); this.add(chartPanel, null); this.strategyData.getCandleDataset().getSeries(0).addChangeListener(this); }
From source file:Debrief.Tools.FilterOperations.ShowTimeVariablePlot3.java
public final MWC.GUI.Tools.Action getData() { // check that some tracks are selected if (_theTracks == null) { MWC.GUI.Dialogs.DialogFactory.showMessage("Reformat Tracks", "Please select one or more tracks"); return null; }/*from w ww . ja v a 2 s . c om*/ // find out what the user wants to view final CalculationHolder theHolder = getChoice(); // check it worked if (theHolder != null) { // retrieve the necessary input data final toteCalculation myOperation = theHolder._theCalc; // declare the primary track (even though we may end up not using // it) WatchableList thePrimary = null; // is this a relative calculation? if (theHolder._isRelative) { // retrieve the necessary input data thePrimary = getPrimary(); } // //////////////////////////////////////////////// // sort out the title // //////////////////////////////////////////////// // get the title to use String theTitle = myOperation.getTitle() + " vs Time plot"; // is this a relative operation if (theHolder.isARelativeCalculation()) { if (thePrimary != null) { // if it's relative, we use the primary track name in the // title theTitle = thePrimary.getName() + " " + theTitle; } } // /////////////////////////////////////////////////////// // prepare the plot // /////////////////////////////////////////////////////// // the working variables we rely on later NewFormattedJFreeChart jChart = null; XYPlot plot = null; ValueAxis xAxis = null; XYToolTipGenerator tooltipGenerator = null; // the y axis is common to hi & lo res. Format it here final NumberAxis yAxis = new NumberAxis(myOperation.getTitle() + " " + myOperation.getUnits()); // hmm, see if we are in hi-res mode. If we are, don't use a // formatted // y-axis, just use the plain long microseconds // value if (HiResDate.inHiResProcessingMode()) { // final SimpleDateFormat _secFormat = new SimpleDateFormat("ss"); System.err .println("XY Plot of HiRes data support is incomplete. Tick formatter (below) is missing."); // ok, simple enough for us... final NumberAxis nAxis = new NumberAxis("time (secs.micros)"); // { // /** // * // */ // private static final long serialVersionUID = 1L; // // @SuppressWarnings("unused") // public String getTickLabel(final double currentTickValue) // { // final long time = (long) currentTickValue; // final Date dtg = new HiResDate(0, time).getDate(); // final String res = _secFormat.format(dtg) + "." // + DebriefFormatDateTime.formatMicros(new HiResDate(0, time)); // return res; // } // }; nAxis.setAutoRangeIncludesZero(false); xAxis = nAxis; // just show the raw data values tooltipGenerator = new StandardXYToolTipGenerator(); } else { // create a date-formatting axis final DateAxis dAxis = new RelativeDateAxis(); dAxis.setStandardTickUnits(DateAxisEditor.createStandardDateTickUnitsAsTickUnits()); xAxis = dAxis; // also create the date-knowledgable tooltip writer tooltipGenerator = new DatedToolTipGenerator(); } // create the special stepper plot final ColourStandardXYItemRenderer renderer = new ColourStandardXYItemRenderer(tooltipGenerator, null, null); plot = getPlot((RelativeDateAxis) xAxis, yAxis, _theStepper, renderer); renderer.setPlot(plot); // apply any formatting for this choice final formattingOperation fo = theHolder._theFormatter; if (fo != null) { fo.format(plot); } jChart = new NewFormattedJFreeChart(theTitle, JFreeChart.DEFAULT_TITLE_FONT, plot, true, _theStepper); // //////////////////////////////////////////////////// // get the data // //////////////////////////////////////////////////// final AbstractDataset theDataset = getDataSeries(thePrimary, theHolder, _theTracks, _start_time, _end_time, jChart.getTimeOffsetProvider()); // //////////////////////////////////////////////// // put the holder into one of our special items // //////////////////////////////////////////////// final ChartPanel chartInPanel = new StepperChartPanel(jChart, true, _theStepper); // format the chart chartInPanel.setName(theTitle); chartInPanel.setMouseZoomable(true, true); // and insert into the panel insertPanel(chartInPanel, jChart); // //////////////////////////////////////////////////// // put the time series into the plot // //////////////////////////////////////////////////// plot.setDataset((XYDataset) theDataset); } // whether we have a primary // return the new action return null; }
From source file:com.farouk.projectapp.FirstGUI.java
private void drawChart() { final XYDataset dataset = SQLConnect.getInfoToDrawChart(userID); final JFreeChart chart = createChart(dataset); final ChartPanel chartPanel = new ChartPanel(chart); //chartPanel.setPreferredSize(new java.awt.Dimension(560, 370)); chartPanel.setMouseZoomable(true, false); jTabbedPane1.add(" History ", chartPanel); }