public Chart(String filename) { try { // Get Stock Symbol this.stockSymbol = filename.substring(0, filename.indexOf('.')); // Create time series TimeSeries open = new TimeSeries("Open Price", Day.class); TimeSeries close = new TimeSeries("Close Price", Day.class); TimeSeries high = new TimeSeries("High", Day.class); TimeSeries low = new TimeSeries("Low", Day.class); TimeSeries volume = new TimeSeries("Volume", Day.class); BufferedReader br = new BufferedReader(new FileReader(filename)); String key = br.readLine(); String line = br.readLine(); while (line != null && !line.startsWith("<!--")) { StringTokenizer st = new StringTokenizer(line, ",", false); Day day = getDay(st.nextToken()); double openValue = Double.parseDouble(st.nextToken()); double highValue = Double.parseDouble(st.nextToken()); double lowValue = Double.parseDouble(st.nextToken()); double closeValue = Double.parseDouble(st.nextToken()); long volumeValue = Long.parseLong(st.nextToken()); // Add this value to our series' open.add(day, openValue); close.add(day, closeValue); high.add(day, highValue); low.add(day, lowValue); // Read the next day line = br.readLine(); } // Build the datasets dataset.addSeries(open); dataset.addSeries(close); dataset.addSeries(low); dataset.addSeries(high); datasetOpenClose.addSeries(open); datasetOpenClose.addSeries(close); datasetHighLow.addSeries(high); datasetHighLow.addSeries(low); JFreeChart summaryChart = buildChart(dataset, "Summary", true); JFreeChart openCloseChart = buildChart(datasetOpenClose, "Open/Close Data", false); JFreeChart highLowChart = buildChart(datasetHighLow, "High/Low Data", true); JFreeChart highLowDifChart = buildDifferenceChart(datasetHighLow, "High/Low Difference Chart"); // Create this panel this.setLayout(new GridLayout(2, 2)); this.add(new ChartPanel(summaryChart)); this.add(new ChartPanel(openCloseChart)); this.add(new ChartPanel(highLowChart)); this.add(new ChartPanel(highLowDifChart)); } catch (Exception e) { e.printStackTrace(); } }
public Chart(String title, String timeAxis, String valueAxis, TimeSeries data) { try { // Build the datasets dataset.addSeries(data); // Create the chart JFreeChart chart = ChartFactory.createTimeSeriesChart( title, timeAxis, valueAxis, dataset, true, true, false); // Setup the appearance of the chart chart.setBackgroundPaint(Color.white); XYPlot plot = chart.getXYPlot(); plot.setBackgroundPaint(Color.lightGray); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); plot.setAxisOffset(new RectangleInsets(UnitType.ABSOLUTE, 5.0, 5.0, 5.0, 5.0)); plot.setDomainCrosshairVisible(true); plot.setRangeCrosshairVisible(true); // Tell the chart how we would like dates to read DateAxis axis = (DateAxis) plot.getDomainAxis(); axis.setDateFormatOverride(new SimpleDateFormat("EEE HH")); this.add(new ChartPanel(chart)); } catch (Exception e) { e.printStackTrace(); } }
private static XYDataset createDataset2A() { TimeSeries timeseries = new TimeSeries("Series 2"); timeseries.add(new Day(3, 3, 2002), 16853.200000000001D); timeseries.add(new Day(4, 3, 2002), 19642.299999999999D); timeseries.add(new Day(5, 3, 2002), 18253.5D); timeseries.add(new Day(6, 3, 2002), 15352.299999999999D); timeseries.add(new Day(7, 3, 2002), 13532D); timeseries.add(new Day(8, 3, 2002), 12635.299999999999D); timeseries.add(new Day(9, 3, 2002), 13998.200000000001D); timeseries.add(new Day(10, 3, 2002), 11943.200000000001D); timeseries.add(new Day(11, 3, 2002), 16943.900000000001D); timeseries.add(new Day(12, 3, 2002), 17843.200000000001D); timeseries.add(new Day(13, 3, 2002), 16495.299999999999D); timeseries.add(new Day(14, 3, 2002), 17943.599999999999D); timeseries.add(new Day(15, 3, 2002), 18500.700000000001D); timeseries.add(new Day(16, 3, 2002), 19595.900000000001D); TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(timeseries); timeseriescollection.setXPosition(TimePeriodAnchor.MIDDLE); return timeseriescollection; }
private static XYDataset createDataset2B() { TimeSeries timeseries = new TimeSeries("Series 2B"); timeseries.add(new Day(3, 3, 2002), 43.899999999999999D); timeseries.add(new Day(4, 3, 2002), 72.599999999999994D); timeseries.add(new Day(5, 3, 2002), 89.400000000000006D); timeseries.add(new Day(6, 3, 2002), 23.800000000000001D); timeseries.add(new Day(7, 3, 2002), 45D); timeseries.add(new Day(8, 3, 2002), 65.799999999999997D); timeseries.add(new Day(9, 3, 2002), 92.099999999999994D); timeseries.add(new Day(10, 3, 2002), 84.700000000000003D); timeseries.add(new Day(11, 3, 2002), 77.200000000000003D); timeseries.add(new Day(12, 3, 2002), 65.099999999999994D); timeseries.add(new Day(13, 3, 2002), 78.5D); timeseries.add(new Day(14, 3, 2002), 75.299999999999997D); timeseries.add(new Day(15, 3, 2002), 69.900000000000006D); timeseries.add(new Day(20, 3, 2002), 56.600000000000001D); TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(timeseries); timeseriescollection.setXPosition(TimePeriodAnchor.MIDDLE); return timeseriescollection; }