|
1 | 1 | package graphFrame;
|
2 | 2 |
|
| 3 | +import java.awt.BorderLayout; |
| 4 | +import java.awt.Color; |
| 5 | +import java.awt.Component; |
| 6 | +import java.awt.Dimension; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Date; |
| 9 | +import java.util.Collection; |
| 10 | +import java.util.Collections; |
| 11 | + |
3 | 12 | import javax.swing.*;
|
4 | 13 |
|
| 14 | +import org.jfree.chart.*; |
| 15 | +import org.jfree.chart.axis.DateAxis; |
| 16 | +import org.jfree.chart.axis.NumberAxis; |
| 17 | +import org.jfree.chart.axis.Timeline; |
| 18 | +import org.jfree.chart.plot.PlotOrientation; |
| 19 | +import org.jfree.chart.plot.XYPlot; |
| 20 | +import org.jfree.chart.renderer.xy.CandlestickRenderer; |
| 21 | +import org.jfree.data.*; |
| 22 | +import org.jfree.data.category.DefaultCategoryDataset; |
| 23 | +import org.jfree.data.general.DatasetGroup; |
| 24 | +import org.jfree.data.time.TimeSeries; |
| 25 | +import org.jfree.data.xy.DefaultHighLowDataset; |
| 26 | +import org.jfree.data.xy.XYDataset; |
| 27 | + |
| 28 | +import black.Utils; |
5 | 29 | import gray.Global;
|
6 | 30 | import red.Stock;
|
7 | 31 |
|
8 | 32 | public class GraphFramePanel_Chart extends JPanel{
|
9 |
| - Stock stock; |
| 33 | + Stock stock; |
| 34 | + |
| 35 | + String symbol; |
| 36 | + ArrayList<String> date; |
| 37 | + ArrayList<Double> high; |
| 38 | + ArrayList<Double> low; |
| 39 | + ArrayList<Double> open; |
| 40 | + ArrayList<Double> adj_close; |
| 41 | + ArrayList<Double> volume; |
| 42 | + |
| 43 | + BorderLayout layout = new BorderLayout(); |
| 44 | + |
| 45 | + DefaultHighLowDataset data; |
| 46 | + |
| 47 | + JFreeChart chart; |
| 48 | + ChartPanel insidepanel; |
10 | 49 |
|
11 |
| - String[] column; |
12 |
| - String[][] data; |
| 50 | + Double y_axis_min; |
| 51 | + Double y_axis_max; |
| 52 | + |
13 | 53 |
|
14 | 54 | GraphFramePanel_Chart(){
|
15 |
| - stock_price_graph(); |
| 55 | + renderPanel(); |
16 | 56 | }
|
17 | 57 |
|
| 58 | + public void renderPanel() { |
| 59 | + removeAll(); |
| 60 | + setLayout(layout); |
| 61 | + |
| 62 | + data = createDataset(); |
| 63 | + chart = createChart(data); |
| 64 | + insidepanel = new ChartPanel(chart); |
| 65 | + add(insidepanel); |
| 66 | + |
| 67 | + |
| 68 | + validate(); |
| 69 | + repaint(); |
| 70 | + } |
18 | 71 |
|
| 72 | + public DefaultHighLowDataset createDataset() { |
| 73 | + stock = new Stock(Global.SYMBOL); |
| 74 | + |
| 75 | + symbol = stock.SYMBOL; |
| 76 | + date = stock.request("DATE", "2017-09-01", "2017-11-01"); |
| 77 | + high = stock.request("HIGH", "2017-09-01", "2017-11-01"); |
| 78 | + low = stock.request("LOW", "2017-09-01", "2017-11-01"); |
| 79 | + open = stock.request("OPEN", "2017-09-01", "2017-11-01"); |
| 80 | + adj_close = stock.request("ADJ_CLOSE", "2017-09-01", "2017-11-01"); |
| 81 | + volume = stock.request("VOLUME", "2017-09-01", "2017-11-01"); |
| 82 | + |
| 83 | + Date[] date_list = new Date[date.size()]; |
| 84 | + date_list = Utils.StringToDate(date); |
| 85 | + double[] high_list = new double[high.size()]; |
| 86 | + double[] low_list = new double[low.size()]; |
| 87 | + double[] open_list = new double[open.size()]; |
| 88 | + double[] adj_close_list = new double[adj_close.size()]; |
| 89 | + double[] volume_list = new double[volume.size()]; |
| 90 | + for (int i=0; i<high_list.length; i++) { |
| 91 | + high_list[i] = high.get(i); |
| 92 | + low_list[i] = low.get(i); |
| 93 | + open_list[i] = open.get(i); |
| 94 | + adj_close_list[i] = adj_close.get(i); |
| 95 | + volume_list[i] = volume.get(i); |
| 96 | + } |
| 97 | + |
| 98 | + y_axis_min = Collections.min(low); |
| 99 | + y_axis_max = Collections.max(high); |
| 100 | + |
| 101 | + DefaultHighLowDataset data = null; |
| 102 | + data = new DefaultHighLowDataset(symbol, date_list, high_list, low_list, open_list, adj_close_list, volume_list); |
| 103 | + |
| 104 | + return data; |
| 105 | + } |
| 106 | + |
| 107 | + private JFreeChart createChart(final DefaultHighLowDataset dataset) { |
| 108 | + |
| 109 | + chart = ChartFactory.createCandlestickChart("History of " + stock.SYMBOL, "Time", "Price", dataset, true); |
| 110 | + |
| 111 | + XYPlot plot = chart.getXYPlot(); |
| 112 | + CandlestickRenderer renderer = (CandlestickRenderer) plot.getRenderer(); |
| 113 | + renderer.setAutoWidthMethod(CandlestickRenderer.WIDTHMETHOD_SMALLEST); |
| 114 | + |
| 115 | + NumberAxis domain = (NumberAxis) plot.getRangeAxis(); |
| 116 | + domain.setRange(y_axis_min - 1, y_axis_max + 1); |
| 117 | + |
| 118 | + return chart; |
| 119 | + } |
19 | 120 | }
|
0 commit comments