1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFrame;
import lt.monarch.chart.chart2D.Chart2D;
import lt.monarch.chart.chart2D.Grid;
import lt.monarch.chart.chart2D.axis.Axis2DX;
import lt.monarch.chart.chart2D.axis.Axis2DY;
import lt.monarch.chart.chart2D.engine.PlaneMapper2D;
import lt.monarch.chart.chart2D.series.StockSeries;
import lt.monarch.chart.chart2D.series.StockStrategies;
import lt.monarch.chart.engine.ChartObject;
import lt.monarch.chart.engine.SimpleHotSpotMap;
import lt.monarch.chart.legend.Legend;
import lt.monarch.chart.mapper.DateAxisMapper;
import lt.monarch.chart.mapper.MathAxisMapper;
import lt.monarch.chart.models.ChartDataModel;
import lt.monarch.chart.models.DataValueType;
import lt.monarch.chart.plugins.DrillDown;
import lt.monarch.chart.style.enums.TitlePosition;
import lt.monarch.chart.swing.JChartPanel;
import lt.monarch.chart.view.LabeledChart;
import lt.monarch.data.binding.ColumnType;
import lt.monarch.data.binding.DataBinding;
import lt.monarch.data.binding.TextFileDataSource;
/**
*
*/
public class StockCandleDemo extends JFrame {
private JChartPanel m_chartPanel;
private Chart2D chart;
public StockCandleDemo() {
setLayout(new BorderLayout());
initChart();
add(m_chartPanel, BorderLayout.CENTER);
}
private void initChart() {
/*
* Setting text file data binding
*/
String file = "data.txt";
TextFileDataSource src = null;
try {
src = new TextFileDataSource(file, ",");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
src.setFirstRowAsLabels(true);
DataBinding db = new DataBinding();
db.addMapping("Date", ColumnType.DATETIME);
db.addMapping("High", ColumnType.DOUBLE);
db.addMapping("Low", ColumnType.DOUBLE);
db.addMapping("Open", ColumnType.DOUBLE);
db.addMapping("Close", ColumnType.DOUBLE);
SimpleDateFormat dFormat = new SimpleDateFormat("MM/dd/yyyy");
src.setDateFormat(dFormat);
/*
* Setting Chart data model
*/
ChartDataModel model = new ChartDataModel(5);
src.updateDataModel(db, model); // updates data model from text file
chart = new Chart2D();
MathAxisMapper yMapper = new MathAxisMapper(260, 410);
DateAxisMapper xMapper = new DateAxisMapper((Date) model.getValueAt(
DataColumnType.KEY, 0), (Date) model.getValueAt(
DataColumnType.KEY, model.getPointCount() - 1));
xMapper.setFinestScaleUnits(Calendar.DATE);
xMapper.setStartingScaleUnits(Calendar.YEAR);
xMapper.setVisibleRange((Date) xMapper.mapBack(0.924), (Date) xMapper
.mapBack(0.99));
/*
* Setting axes
*/
Axis2DX axisX = new Axis2DX(xMapper);
axisX.setTitle("Date");
axisX.setZoomingEnabled(true);
axisX.setScrollable(true);
axisX.getTitleSettings().setPosition(TitlePosition.BELOW);
Axis2DY axisY = new Axis2DY(yMapper);
axisY.setTitle("Price, $");
axisY.getTitleSettings().setPosition(TitlePosition.BELOW);
Grid grid = new Grid(new PlaneMapper2D(), xMapper, yMapper);
/*
* Adding drill down plug-in
*/
chart.addPlugin(new DrillDown());
chart.setLeftYAxis(axisY);
chart.setXAxis(axisX);
/*
* Setting series
*/
StockSeries stock = new StockSeries(model, xMapper, yMapper);
stock.setName("Shares Prices");
chart.setObjects(new ChartObject[] { grid, axisX, axisY, stock });
stock.setStrategy(StockStrategies.QUOTE_CANDLE_STRATEGY); //setting strategy
/*
* Labeled chart
*/
LabeledChart m_chart = new LabeledChart(chart);
m_chart.setTitle("Google Inc. Shares Prices");
chart.createLegendEntries();
/*
* Legend creation
*/
Legend legend = new Legend(chart);
legend.setMaxColumns(2);
legend.getPaintStyle().setBackground(Color.WHITE);
m_chart.setBottomView(legend);
/*
* Add m_chart to panel
*/
m_chartPanel = new JChartPanel(m_chart);
m_chartPanel.setHotSpotMap(new SimpleHotSpotMap());
}
public static void main(String[] args) {
StockCandleDemo frame = new StockCandleDemo();
frame.setSize(new Dimension(600, 400));
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setTitle("Google Inc. Shares Prices");
frame.setVisible(true);
}
} |