List of usage examples for org.jfree.chart.axis SegmentedTimeline addBaseTimelineExclusions
public void addBaseTimelineExclusions(long fromBaseDomainValue, long toBaseDomainValue)
From source file:com.charts.FiveDayChart.java
public FiveDayChart(YStockQuote currentStock) { TimeSeries series = new TimeSeries(currentStock.get_name()); ArrayList<String> fiveDayData = currentStock.get_five_day_data(); int length = fiveDayData.size(); for (int i = 22; i < length; i += 5) { String[] data = fiveDayData.get(i).split(","); Date time = new Date((long) Integer.parseInt(data[0]) * 1000); DateFormat df = new SimpleDateFormat("MM-dd-yyyy-h-m"); series.addOrUpdate(new Minute(time), Double.parseDouble(data[1])); }// w w w.jav a 2 s. c om String[] data = fiveDayData.get(length - 1).split(","); Date time = new Date((long) Integer.parseInt(data[0]) * 1000); DateFormat df = new SimpleDateFormat("MM-dd-yyyy-h-m"); series.addOrUpdate(new Minute(time), Double.parseDouble(data[1])); TimeSeriesCollection dataset = new TimeSeriesCollection(); dataset.addSeries(series); JFreeChart chart = ChartFactory.createTimeSeriesChart( currentStock.get_name() + "(" + currentStock.get_symbol() + ")" + " Five Day", "Date", "Price", dataset, true, true, false); XYPlot plot = (XYPlot) chart.getPlot(); plot.setDomainCrosshairVisible(true); plot.setRangeCrosshairVisible(true); ValueAxis yAxis = (ValueAxis) plot.getRangeAxis(); DateAxis xAxis = (DateAxis) plot.getDomainAxis(); Date now = new Date(); SegmentedTimeline segmentedTimeline = SegmentedTimeline.newFifteenMinuteTimeline(); segmentedTimeline.addBaseTimelineExclusions(segmentedTimeline.getStartTime(), now.getTime()); Calendar[][] holidays = DayRange.getHolidayDates(); for (int i = 0; i < holidays[0].length; i++) { Calendar day = Calendar.getInstance(); day.set(Calendar.YEAR, holidays[0][i].get(Calendar.YEAR)); day.set(Calendar.MONTH, holidays[0][i].get(Calendar.MONTH)); day.set(Calendar.DAY_OF_MONTH, holidays[0][i].get(Calendar.DAY_OF_MONTH)); day.set(Calendar.HOUR_OF_DAY, 9); segmentedTimeline.addException(day.getTimeInMillis(), day.getTimeInMillis() + 21600000); } xAxis.setTimeline(segmentedTimeline); xAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE); //xAxis.setVerticalTickLabels(true); xAxis.setDateFormatOverride(new SimpleDateFormat("MM-dd")); xAxis.setAutoTickUnitSelection(false); xAxis.setAutoRange(false); StandardXYItemRenderer renderer1 = new StandardXYItemRenderer(); renderer1.setSeriesPaint(0, Color.BLUE); TimeSeries movingAverage5 = MovingAverage.createMovingAverage(series, "MA(5)", 30, 0); Double currMA5 = (Double) movingAverage5.getDataItem(movingAverage5.getItemCount() - 1).getValue(); currMA5 = Math.round(currMA5 * 100.0) / 100.0; movingAverage5.setKey("MA(5): " + currMA5); TimeSeriesCollection collection = new TimeSeriesCollection(); collection.addSeries(movingAverage5); plot.setDataset(1, collection); plot.setRenderer(1, renderer1); plot.setBackgroundPaint(Color.WHITE); chartPanel = new ChartPanel(chart); chart.setBackgroundPaint(chartPanel.getBackground()); plot.setDomainGridlinePaint(Color.lightGray); plot.setRangeGridlinePaint(Color.lightGray); chartPanel.setVisible(true); chartPanel.revalidate(); chartPanel.repaint(); }
From source file:org.jfree.chart.demo.SegmentedHighLowChartDemo.java
/** * A demonstration application showing a high-low-open-close chart using a * segmented or non-segmented axis.//from w ww .j a va 2s .co m * * @param title the frame title. * @param useSegmentedAxis use a segmented axis for this demo? * @param timelineType Type of timeline to use: 1=Monday through Friday, 2=Intraday */ public SegmentedHighLowChartDemo(final String title, final boolean useSegmentedAxis, final int timelineType) { super(title); System.out.println("\nMaking SegmentedHighLowChartDemo(" + title + ")"); // create a Calendar object with today's date at midnight final Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); // create a timeline for the demo SegmentedTimeline timeline = null; switch (timelineType) { case 1: timeline = SegmentedTimeline.newMondayThroughFridayTimeline(); break; case 2: timeline = SegmentedTimeline.newFifteenMinuteTimeline(); final Calendar cal2 = (Calendar) cal.clone(); cal2.add(Calendar.YEAR, 1); // add 1 year of baseTimeline's excluded segments (Saturdays and Sundays) as // exceptions of the intraday timeline timeline.addBaseTimelineExclusions(cal.getTime().getTime(), cal2.getTime().getTime()); break; default: System.out.println("Invalid timelineType."); System.exit(1); } // create a data set that has data for trading days (Monday through Friday). final DefaultHighLowDataset dataset = DemoDatasetFactory.createSegmentedHighLowDataset(timeline, cal.getTime()); final JFreeChart chart; if (useSegmentedAxis) { chart = ChartFactory.createHighLowChart(title, "Time", "Value", dataset, timeline, true); } else { chart = ChartFactory.createHighLowChart(title, "Time", "Value", dataset, true); } final DateAxis axis = (DateAxis) chart.getXYPlot().getDomainAxis(); axis.setAutoRange(true); final TickUnits units = new TickUnits(); units.add(new DateTickUnit(DateTickUnit.DAY, 1, DateTickUnit.HOUR, 1, new SimpleDateFormat("d-MMM"))); units.add(new DateTickUnit(DateTickUnit.DAY, 2, DateTickUnit.HOUR, 1, new SimpleDateFormat("d-MMM"))); units.add(new DateTickUnit(DateTickUnit.DAY, 7, DateTickUnit.DAY, 1, new SimpleDateFormat("d-MMM"))); units.add(new DateTickUnit(DateTickUnit.DAY, 15, DateTickUnit.DAY, 1, new SimpleDateFormat("d-MMM"))); units.add(new DateTickUnit(DateTickUnit.DAY, 30, DateTickUnit.DAY, 1, new SimpleDateFormat("d-MMM"))); axis.setStandardTickUnits(units); final NumberAxis vaxis = (NumberAxis) chart.getXYPlot().getRangeAxis(); vaxis.setAutoRangeIncludesZero(false); final ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new java.awt.Dimension(500, 270)); setContentPane(chartPanel); }
From source file:ch.algotrader.client.chart.ChartTab.java
private void initAxis() { DateAxis domainAxis = (DateAxis) getPlot().getDomainAxis(); // configure the Date Axis (if startTime & endTime is set) if (this.chartDefinition.getStartTime() != null && this.chartDefinition.getEndTime() != null && !this.chartDefinition.getStartTime().equals(this.chartDefinition.getEndTime())) { // creat the SegmentedTimeline long startTime = this.chartDefinition.getStartTime().getTime(); long endTime = this.chartDefinition.getEndTime().getTime(); if (endTime == -3600000) { // adjust 00:00 endTime += 86400000;/*from www .j a va 2 s . c om*/ } long segmentSize = 60 * 1000; // minute int segmentsIncluded = (int) (endTime - startTime) / (60 * 1000); int segmentsExcluded = 24 * 60 - segmentsIncluded; SegmentedTimeline timeline = new SegmentedTimeline(segmentSize, segmentsIncluded, segmentsExcluded); Date fromDate = domainAxis.getMinimumDate(); Date toDate = domainAxis.getMaximumDate(); long fromTime = fromDate.getTime(); long toTime = toDate.getTime(); // get year/month/day from fromTime and hour/minute from diagrm.startTime Date truncatedDate = DateUtils.truncate(fromDate, Calendar.DAY_OF_MONTH); Calendar truncatedCalendar = DateUtils.toCalendar(truncatedDate); Calendar startCalendar = DateUtils.toCalendar(this.chartDefinition.getStartTime()); truncatedCalendar.set(Calendar.HOUR_OF_DAY, startCalendar.get(Calendar.HOUR_OF_DAY)); truncatedCalendar.set(Calendar.MINUTE, startCalendar.get(Calendar.MINUTE)); timeline.setStartTime(truncatedCalendar.getTimeInMillis()); timeline.setBaseTimeline(SegmentedTimeline.newMondayThroughFridayTimeline()); timeline.addBaseTimelineExclusions(fromTime, toTime); timeline.setAdjustForDaylightSaving(true); domainAxis.setTimeline(timeline); } // make sure the markers are within the rangeAxis ValueAxis rangeAxis = getPlot().getRangeAxis(); for (Marker marker : this.markers.values()) { if (marker instanceof ValueMarker) { ValueMarker valueMarker = (ValueMarker) marker; if (marker.getAlpha() > 0 && valueMarker.getValue() != 0.0) { if (valueMarker.getValue() < rangeAxis.getLowerBound()) { rangeAxis.setLowerBound(valueMarker.getValue()); marker.setLabelAnchor(RectangleAnchor.TOP_RIGHT); marker.setLabelTextAnchor(TextAnchor.BOTTOM_RIGHT); } if (valueMarker.getValue() > rangeAxis.getUpperBound()) { rangeAxis.setUpperBound(valueMarker.getValue()); marker.setLabelAnchor(RectangleAnchor.BOTTOM_RIGHT); marker.setLabelTextAnchor(TextAnchor.TOP_RIGHT); } } } else { IntervalMarker intervalMarker = (IntervalMarker) marker; if (marker.getAlpha() > 0 && intervalMarker.getStartValue() != 0.0) { if (intervalMarker.getStartValue() < rangeAxis.getLowerBound()) { rangeAxis.setLowerBound(intervalMarker.getStartValue()); marker.setLabelAnchor(RectangleAnchor.TOP_RIGHT); marker.setLabelTextAnchor(TextAnchor.TOP_RIGHT); } if (intervalMarker.getEndValue() > rangeAxis.getUpperBound()) { rangeAxis.setUpperBound(intervalMarker.getEndValue()); marker.setLabelAnchor(RectangleAnchor.BOTTOM_RIGHT); marker.setLabelTextAnchor(TextAnchor.BOTTOM_RIGHT); } } } } }