can switch between line/candle chart, and log scale, ...

This commit is contained in:
7u83 2017-04-04 01:17:51 +02:00
parent 52e359f028
commit 4b35628079
3 changed files with 295 additions and 63 deletions

View File

@ -49,12 +49,6 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
private int first_bar, last_bar;
public final void initChart() {
// data = new OHLCData(60000*30);
//data = new OHLCData(60000*30);
//data = Globals.se.getOHLCdata(60000 * 30);
this.setCompression(10000);
}
/**
* Creates new form Chart
@ -65,7 +59,7 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
}
initComponents();
initChart();
//initChart();
// initCtxMenu();
//setCompression(60000);
if (Globals.se == null) {
@ -78,7 +72,7 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
// scrollPane.setViewportView(this);
}
OHLCData data;
@Override
public Dimension getPreferredScrollableViewportSize() {
@ -169,6 +163,8 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
if (xl_color != null) {
g.setColor(xl_color);
}
g.drawLine(clip_bounds.x, y,clip_bounds.width,y);
Dimension dim = getSize();
@ -249,7 +245,7 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
}
boolean logs = false;
//boolean logs = false;
/* float getY0(float y) {
@ -297,12 +293,24 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
// return (y+c_rect.y-c_rect.height)/c_yscaling+c_mm.getMin();
}
*/
private void drawLineItem(RenderCtx ctx, int prevx, int x, OHLCDataItem prev, OHLCDataItem i){
Graphics2D g = ctx.g;
if (prev==null)
prev=i;
int y1 = (int)ctx.getY(prev.close);
int y2 = (int)ctx.getY(i.close);
Color cur = g.getColor();
g.setColor(Color.RED);
g.drawLine(prevx,y1,x,y2);
g.setColor(cur);
}
private void drawCandleItem(RenderCtx ctx, int prevx, int x, OHLCDataItem prev, OHLCDataItem i) {
Graphics2D g = ctx.g;
Rectangle r = ctx.rect;
if (i.open < i.close) {
int xl = (int) (x + ctx.iwidth / 2);
@ -349,6 +357,7 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
*/
protected enum ChartType {
CANDLESTICK,
LINE,
BAR,
VOL,
}
@ -360,6 +369,9 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
case CANDLESTICK:
this.drawCandleItem(ctx, prevx, x, prev, i);
break;
case LINE:
this.drawLineItem(ctx, prevx, x, prev, i);
break;
case VOL:
this.drawBarItem(ctx, prevx, x, prev, i);
break;
@ -434,7 +446,7 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
for (int i = first_bar; i < last_bar && i < data.size(); i++) {
OHLCDataItem di = data.get(i);
int x = (int) (i * em_width * x_unit_width); //em_width;
this.drawItem(ctx, x - em_width, x, prev, di); //, ctx.scaling, data.getMin());
this.drawItem(ctx, (int) (x - em_width * x_unit_width), x, prev, di); //, ctx.scaling, data.getMin());
// myi++;
prev = di;
@ -449,54 +461,44 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
/**
* definition for a sub-chart window
*/
class SubChartDef {
public class SubChartDef {
/**
* Height of sub-chart in percent
*/
float height;
public float height;
/**
* top padding in em_height
*/
float padding_top = 0;
public float padding_top = 0;
/**
* bottom padding in em_height
* bottom padding in em_height (not implemented yet)
*/
float padding_bottom = 0;
public float padding_bottom = 0;
ChartType type;
OHLCData data;
public ChartType type;
public OHLCData data;
Color bgcolor = null;
public Color bgcolor = null;
/**
* logarithmic scaling
*/
public boolean log=false;
}
protected OHLCData data;
ArrayList<SubChartDef> charts = new ArrayList<>();
void addChart(SubChartDef d) {
protected void addChart(SubChartDef d) {
charts.add(d);
}
void setupCharts() {
charts = new ArrayList<>();
SubChartDef main = new SubChartDef();
main.height = 0.8f;
main.type = ChartType.CANDLESTICK;
main.data = this.data;
main.bgcolor = Color.BLUE;
main.padding_top = 0.02f;
addChart(main);
SubChartDef vol = new SubChartDef();
vol.height = 0.2f;
vol.type = ChartType.VOL;
vol.data = this.data;
// vol.bgcolor = Color.GRAY;
addChart(vol);
}
void drawAll(Graphics2D g) {
int pwidth = (int) (em_width * x_unit_width * (num_bars + 1)) + clip_bounds.width;
@ -504,8 +506,15 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
this.revalidate();
int h1 = 0;
for (SubChartDef d : charts) {
if (d.data==null){
System.out.printf("Data is null\n");
System.exit(0);
}
// calclulate the min/max values
switch (d.type) {
@ -545,8 +554,8 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
ctx.iwidth = (float) ((x_unit_width * em_width) * 0.9f);
this.ct = d.type;
logs = false;
c_mm.setLog(false);
// logs = false;
c_mm.setLog(d.log);
drawChart(ctx);
@ -555,6 +564,10 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
}
}
protected void setupSubCharts(){
}
private void draw(Graphics2D g) {
@ -567,7 +580,8 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
// g.setColor(Color.RED);
// g.drawRect(0,0,gdim.width,gdim.height);
this.setupCharts();
this.charts = new ArrayList<>();
setupSubCharts();
num_bars = data.size();
@ -658,7 +672,7 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
ctx.iwidth = (float) ((x_unit_width * em_width) * 0.9f);
this.ct = ChartType.CANDLESTICK;
logs = true;
// logs = true;
c_mm.setLog(true);
drawChart(ctx);
@ -682,7 +696,7 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
ctx.g = g;
ctx.iwidth = (float) ((x_unit_width * em_width) * 0.9f);
logs = false;
// logs = false;
c_mm.setLog(false);
this.ct = ChartType.VOL;
drawChart(ctx);
@ -770,22 +784,24 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
// TODO add your handling code here:
}//GEN-LAST:event_jCheckBoxMenuItem1ActionPerformed
protected void setCompression(int timeFrame) {
/* protected void setCompression(int timeFrame) {
javax.swing.SwingUtilities.invokeLater(() -> {
data = Globals.se.getOHLCdata(timeFrame);
System.out.printf("Getting ohls data \n");
if (data == null){
System.out.printf("it is null\n");
}
invalidate();
repaint();
});
}
*/
@Override
public void UpdateQuote(Quote q) {
// System.out.print("Quote Received\n");
// this.realTimeAdd(q.time, (float) q.price, (float)q.volume);
// data.realTimeAdd(q.time, (float) q.price, (float) q.volume);
// this.invalidate();
this.repaint();
}

View File

@ -13,16 +13,56 @@
<Property name="text" type="java.lang.String" value="Compression"/>
</Properties>
</Menu>
<MenuItem class="javax.swing.JCheckBoxMenuItem" name="jCheckBoxMenuItem1">
<Menu class="javax.swing.JMenu" name="typeMenu">
<Properties>
<Property name="text" type="java.lang.String" value="Chart Type"/>
</Properties>
<SubComponents>
<MenuItem class="javax.swing.JRadioButtonMenuItem" name="lineTypeItem">
<Properties>
<Property name="buttonGroup" type="javax.swing.ButtonGroup" editor="org.netbeans.modules.form.RADComponent$ButtonGroupPropertyEditor">
<ComponentRef name="typeButtonGroup"/>
</Property>
<Property name="mnemonic" type="int" value="108"/>
<Property name="text" type="java.lang.String" value="Line"/>
<Property name="actionCommand" type="java.lang.String" value="LINE"/>
</Properties>
<Events>
<EventHandler event="itemStateChanged" listener="java.awt.event.ItemListener" parameters="java.awt.event.ItemEvent" handler="lineTypeItemItemStateChanged"/>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="lineTypeItemActionPerformed"/>
</Events>
</MenuItem>
<MenuItem class="javax.swing.JRadioButtonMenuItem" name="candleTypeMEnuItem">
<Properties>
<Property name="buttonGroup" type="javax.swing.ButtonGroup" editor="org.netbeans.modules.form.RADComponent$ButtonGroupPropertyEditor">
<ComponentRef name="typeButtonGroup"/>
</Property>
<Property name="mnemonic" type="int" value="99"/>
<Property name="text" type="java.lang.String" value="Candle Stick"/>
<Property name="actionCommand" type="java.lang.String" value="CNADLESTICK"/>
</Properties>
<Events>
<EventHandler event="itemStateChanged" listener="java.awt.event.ItemListener" parameters="java.awt.event.ItemEvent" handler="candleTypeMEnuItemItemStateChanged"/>
</Events>
</MenuItem>
</SubComponents>
</Menu>
<Component class="javax.swing.JPopupMenu$Separator" name="jSeparator1">
</Component>
<MenuItem class="javax.swing.JCheckBoxMenuItem" name="logMenu">
<Properties>
<Property name="mnemonic" type="int" value="108"/>
<Property name="selected" type="boolean" value="true"/>
<Property name="text" type="java.lang.String" value="Log Scale"/>
<Property name="toolTipText" type="java.lang.String" value=""/>
</Properties>
<Events>
<EventHandler event="itemStateChanged" listener="java.awt.event.ItemListener" parameters="java.awt.event.ItemEvent" handler="logMenuItemStateChanged"/>
</Events>
</MenuItem>
</SubComponents>
</Container>
<Component class="javax.swing.ButtonGroup" name="typeButtonGroup">
</Component>
</NonVisualComponents>
<Events>
<EventHandler event="mouseWheelMoved" listener="java.awt.event.MouseWheelListener" parameters="java.awt.event.MouseWheelEvent" handler="formMouseWheelMoved"/>

View File

@ -5,25 +5,66 @@
*/
package gui;
import chart.Chart;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Objects;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import sesim.Exchange;
import sesim.OHLCData;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class MainChart extends chart.Chart {
ButtonGroup typeGroup=new ButtonGroup();
/**
* Creates new form MainChart
*/
public MainChart() {
System.out.printf("This is the main chart constructor\n");
initComponents();
initCtxMenu();
setCompression();
this.xl_bgcolor=Color.ORANGE;
this.xl_height=3;
this.candleTypeMEnuItem.setSelected(true);
//xl_bgcolor = Color.ORANGE;
// xl_color = Color.RED;
this.xl_height = 3;
}
//OHLCData data;
@Override
protected void setupSubCharts() {
// charts = new ArrayList<>();
Chart.SubChartDef main = new Chart.SubChartDef();
main.height = 0.8f;
main.type = this.chart_type;
main.data = data;
// main.bgcolor = Color.BLUE;
main.padding_top = 0.02f;
main.log = logMenu.isSelected();
addChart(main);
Chart.SubChartDef vol = new Chart.SubChartDef();
vol.height = 0.2f;
vol.padding_top = 0.08f;
vol.type = ChartType.VOL;
vol.data = data;
// vol.bgcolor = Color.GRAY;
addChart(vol);
}
private void showCtxMenu(java.awt.event.MouseEvent evt) {
@ -47,9 +88,17 @@ public class MainChart extends chart.Chart {
1 * 24 * 3600 * 1000, 2 * 24 * 3600 * 1000
};
private final Integer default_cmopression = 60 * 1000;
private void initCtxMenu() {
ButtonGroup group = new ButtonGroup();
for (int i = 0; i < this.ctxMenuCompressionValues.length; i++) {
JMenuItem item = new JMenuItem(this.ctxMenuCompressionText[i]);
JRadioButtonMenuItem item = new JRadioButtonMenuItem(this.ctxMenuCompressionText[i]);
group.add(item);
if (Objects.equals(this.ctxMenuCompressionValues[i], this.default_cmopression)) {
item.setSelected(true);
}
item.addActionListener((java.awt.event.ActionEvent evt) -> {
ctxMenuCompActionPerformed(evt);
@ -58,12 +107,69 @@ public class MainChart extends chart.Chart {
}
}
protected final void setCompression0(int timeFrame) {
javax.swing.SwingUtilities.invokeLater(() -> {
data = Globals.se.getOHLCdata(timeFrame);
invalidate();
repaint();
});
}
void setCompression() {
for (int i = 0; i < this.ctxMenuCompressionText.length; i++) {
JRadioButtonMenuItem item = (JRadioButtonMenuItem) compMenu.getItem(i);
if (item.isSelected()) {
setCompression0(this.ctxMenuCompressionValues[i]);
}
}
}
private ChartType chart_type=ChartType.CANDLESTICK;
/*
void setType(){
for (int i = 0; i < this.typeMenu.getItemCount(); i++) {
JRadioButtonMenuItem item = (JRadioButtonMenuItem) compMenu.getItem(i);
if (item.isSelected()) {
if ("LINE".equals(item.getActionCommand())){
chart_type=ChartType.LINE;
}
if ("CNADLESTICK".equals(item.getActionCommand())){
chart_type=ChartType.CANDLESTICK;
}
}
}
doRedraw();
}
*/
// boolean log = false;
protected void doRedraw() {
// log = this.logMenu.isSelected();
javax.swing.SwingUtilities.invokeLater(() -> {
invalidate();
repaint();
});
}
public void initChart() {
setCompression();
doRedraw();
}
private void ctxMenuCompActionPerformed(java.awt.event.ActionEvent evt) {
setCompression();
String cmd = evt.getActionCommand();
for (int i = 0; i < this.ctxMenuCompressionText.length; i++) {
if (this.ctxMenuCompressionText[i].equals(cmd)) {
setCompression(this.ctxMenuCompressionValues[i]);
setCompression0(this.ctxMenuCompressionValues[i]);
}
}
@ -80,16 +186,57 @@ public class MainChart extends chart.Chart {
ctxMenu = new javax.swing.JPopupMenu();
compMenu = new javax.swing.JMenu();
jCheckBoxMenuItem1 = new javax.swing.JCheckBoxMenuItem();
typeMenu = new javax.swing.JMenu();
lineTypeItem = new javax.swing.JRadioButtonMenuItem();
candleTypeMEnuItem = new javax.swing.JRadioButtonMenuItem();
jSeparator1 = new javax.swing.JPopupMenu.Separator();
logMenu = new javax.swing.JCheckBoxMenuItem();
typeButtonGroup = new javax.swing.ButtonGroup();
compMenu.setText("Compression");
ctxMenu.add(compMenu);
jCheckBoxMenuItem1.setMnemonic('l');
jCheckBoxMenuItem1.setSelected(true);
jCheckBoxMenuItem1.setText("Log Scale");
jCheckBoxMenuItem1.setToolTipText("");
ctxMenu.add(jCheckBoxMenuItem1);
typeMenu.setText("Chart Type");
typeButtonGroup.add(lineTypeItem);
lineTypeItem.setMnemonic('l');
lineTypeItem.setText("Line");
lineTypeItem.setActionCommand("LINE");
lineTypeItem.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
lineTypeItemItemStateChanged(evt);
}
});
lineTypeItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
lineTypeItemActionPerformed(evt);
}
});
typeMenu.add(lineTypeItem);
typeButtonGroup.add(candleTypeMEnuItem);
candleTypeMEnuItem.setMnemonic('c');
candleTypeMEnuItem.setText("Candle Stick");
candleTypeMEnuItem.setActionCommand("CNADLESTICK");
candleTypeMEnuItem.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
candleTypeMEnuItemItemStateChanged(evt);
}
});
typeMenu.add(candleTypeMEnuItem);
ctxMenu.add(typeMenu);
ctxMenu.add(jSeparator1);
logMenu.setMnemonic('l');
logMenu.setText("Log Scale");
logMenu.setToolTipText("");
logMenu.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
logMenuItemStateChanged(evt);
}
});
ctxMenu.add(logMenu);
addMouseWheelListener(new java.awt.event.MouseWheelListener() {
public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
@ -146,10 +293,39 @@ public class MainChart extends chart.Chart {
showCtxMenu(evt);
}//GEN-LAST:event_formMousePressed
private void logMenuItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_logMenuItemStateChanged
doRedraw();
}//GEN-LAST:event_logMenuItemStateChanged
private void lineTypeItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lineTypeItemActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_lineTypeItemActionPerformed
private void candleTypeMEnuItemItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_candleTypeMEnuItemItemStateChanged
if (this.candleTypeMEnuItem.isSelected()){
this.chart_type=ChartType.CANDLESTICK;
System.out.printf("Set Set Candlestick\n");
}
doRedraw();
}//GEN-LAST:event_candleTypeMEnuItemItemStateChanged
private void lineTypeItemItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_lineTypeItemItemStateChanged
if (this.lineTypeItem.isSelected()){
this.chart_type=ChartType.LINE;
System.out.printf("Set LIne\n");
}
doRedraw();
}//GEN-LAST:event_lineTypeItemItemStateChanged
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JRadioButtonMenuItem candleTypeMEnuItem;
private javax.swing.JMenu compMenu;
private javax.swing.JPopupMenu ctxMenu;
private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem1;
private javax.swing.JPopupMenu.Separator jSeparator1;
private javax.swing.JRadioButtonMenuItem lineTypeItem;
private javax.swing.JCheckBoxMenuItem logMenu;
private javax.swing.ButtonGroup typeButtonGroup;
private javax.swing.JMenu typeMenu;
// End of variables declaration//GEN-END:variables
}