/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package chart; import sesim.OHLCDataItem; import sesim.OHLCData; import java.awt.*; import sesim.Exchange.*; import sesim.Quote; import gui.Globals; import java.awt.geom.Rectangle2D; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import javax.swing.JMenuItem; import javax.swing.Scrollable; import sesim.MinMax; /** * * @author 7u83 <7u83@mail.ru> */ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollable { protected int em_size = 1; protected float bar_width = 0.8f; protected float bar_width_em = 1; protected float y_legend_width = 8; protected int num_bars = 4000; protected Rectangle clip_bounds = new Rectangle(); protected Dimension gdim; 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); } /** * Creates new form Chart */ public Chart() { initComponents(); initChart(); initCtxMenu(); //setCompression(60000); if (Globals.se == null) { return; } Globals.se.addQuoteReceiver(this); } private String [] ctxMenuCompressionText = { "5 s", "10 s", "15 s", "30 s", "1 m", "2 m", "5 m", "10 m", "15 m", "30 m", "1 h", "2 h", "4 h", "1 d", "2 d" }; private Integer[] ctxMenuCompressionValues ={ 5*1000, 10*1000, 15*1000, 30*1000, 60*1000, 2*60*1000, 5*60*1000, 10*60*1000, 15*60*1000, 30*60*1000, 1*3600*1000,2*3600*1000, 4*3600*1000, 1*24*3600*1000, 2*24*3600*1000 }; void initCtxMenu() { for (int i=0; i { ctxMenuCompActionPerformed(evt); }); this.compMenu.add(item); } } private void ctxMenuCompActionPerformed(java.awt.event.ActionEvent evt) { String cmd = evt.getActionCommand(); for (int i=0;i it = data.iterator(); OHLCDataItem prev = null; // int myi = 0; RenderCtx ctx = new RenderCtx(); // MinMax mm = data.getMinMax(first_bar, last_bar); // if(mm==null) // return ; ctx.rect = c_rect; ctx.scaling = (float) r.height / (c_mm.getMax() - c_mm.getMin()); ctx.min = c_mm.getMin(); ctx.g = g; ctx.iwidth = (bar_width * em_size) * 0.9f; // em_width - em_width / 5f; //g.setClip(clip_bounds.x, clip_bounds.y, (int)(clip_bounds.width-this.y_legend_width*this.em_size), clip_bounds.height); for (int i = first_bar; i < last_bar && i < data.size(); i++) { OHLCDataItem di = data.get(i); int x = (int) (i * em_size * bar_width); //em_width; this.drawItem(ctx, x - em_width, x, prev, di); //, ctx.scaling, data.getMin()); // myi++; prev = di; } //System.out.printf("Scaling: %f %f %f %f %f\n",diff,(float)r.height,data.getMin(),data.getMax(),yscaling); /* while (it.hasNext()) { OHLCDataItem di = it.next(); int x = myi * em_width; this.drawItem(ctx, x - em_width, x, prev, di); //, ctx.scaling, data.getMin()); myi++; prev = di; } */ } protected void initEmSize(Graphics g) { em_size = g.getFontMetrics().stringWidth("M"); } private float c_font_height; @Override public final void paintComponent(Graphics g) { super.paintComponent(g); this.initEmSize(g); this.gdim = this.getParent().getSize(gdim); this.getParent().setPreferredSize(gdim); this.clip_bounds = g.getClipBounds(this.clip_bounds); // System.out.printf("X:%d %d\n",gdim.width,gdim.height); first_bar = (int) (clip_bounds.x / (this.bar_width * this.em_size)); last_bar = 1 + (int) ((clip_bounds.x + clip_bounds.width - (this.y_legend_width * em_size)) / (this.bar_width * this.em_size)); c_font_height = g.getFontMetrics().getHeight(); draw((Graphics2D) g); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents private void initComponents() { ctxMenu = new javax.swing.JPopupMenu(); compMenu = new javax.swing.JMenu(); compMenu.setText("Compression"); ctxMenu.add(compMenu); setBackground(java.awt.Color.white); setBorder(null); setOpaque(false); setPreferredSize(new java.awt.Dimension(300, 300)); setRequestFocusEnabled(false); addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(java.awt.event.MouseEvent evt) { formMousePressed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 300, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 300, Short.MAX_VALUE) ); }// //GEN-END:initComponents private void formMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMousePressed System.out.printf("Mouse ohlc was pressed\n"); if (!evt.isPopupTrigger()) { return; }; this.ctxMenu.show(this, evt.getX(), evt.getY()); }//GEN-LAST:event_formMousePressed void setCompression(int timeFrame) { data = Globals.se.getOHLCdata(timeFrame); 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(); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JMenu compMenu; private javax.swing.JPopupMenu ctxMenu; // End of variables declaration//GEN-END:variables }