From 70f7c092ddb5540652134f0c118674b99b8f60ea Mon Sep 17 00:00:00 2001 From: 7u83 <7u83@mail.ru> Date: Sun, 1 Oct 2017 23:16:02 +0200 Subject: [PATCH] xlegend in a separate object new object ohlcchartpainter --- nbproject/private/private.xml | 1 + nbproject/project.properties | 2 +- src/chart/Chart1.java | 35 +++++++++--- src/chart/ChartDef.java | 11 +++- src/chart/ChartPainter.java | 45 ++------------- src/chart/ChartTestDialog.form | 35 ++++++++++-- src/chart/ChartTestDialog.java | 49 +++++++++++++++-- src/chart/OHLCChartPainter.java | 52 ++++++++++++++++++ src/chart/XLegendChartPainter.java | 88 ++++++++++++++++++++++++++++++ 9 files changed, 258 insertions(+), 60 deletions(-) create mode 100644 src/chart/OHLCChartPainter.java create mode 100644 src/chart/XLegendChartPainter.java diff --git a/nbproject/private/private.xml b/nbproject/private/private.xml index 61a5657..efd8e7f 100644 --- a/nbproject/private/private.xml +++ b/nbproject/private/private.xml @@ -37,6 +37,7 @@ file:/home/tube/NetBeansProjects/SeSim/src/sesim/OHLCDataItem.java file:/home/tube/NetBeansProjects/SeSim/src/gui/AboutDialog.java file:/home/tube/NetBeansProjects/SeSim/src/chart/Chart1.java + file:/home/tube/NetBeansProjects/SeSim/src/chart/ChartPainter.java file:/home/tube/NetBeansProjects/SeSim/README.md file:/home/tube/NetBeansProjects/SeSim/src/gui/TestChartdialog.java diff --git a/nbproject/project.properties b/nbproject/project.properties index 131608d..176c08e 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -1,4 +1,4 @@ -#Sun, 01 Oct 2017 11:14:27 +0200 +#Sun, 01 Oct 2017 22:43:43 +0200 annotation.processing.enabled=true annotation.processing.enabled.in.editor=false annotation.processing.processors.list= diff --git a/src/chart/Chart1.java b/src/chart/Chart1.java index d2f0ca1..4ba81cd 100644 --- a/src/chart/Chart1.java +++ b/src/chart/Chart1.java @@ -10,6 +10,7 @@ import java.awt.Rectangle; import java.awt.event.AdjustmentEvent; import java.awt.event.AdjustmentListener; import java.awt.geom.AffineTransform; +import java.util.ArrayList; import javax.swing.JScrollBar; import javax.swing.JViewport; import javax.swing.Scrollable; @@ -42,8 +43,17 @@ public class Chart1 extends javax.swing.JPanel implements QuoteReceiver, Adjustm setSize(new Dimension(9000, 500)); Globals.se.addQuoteReceiver(this); } - - private int em_width; + + + ArrayList chartPainters = new ArrayList<>(); + + /** + * + * @param p + */ + public void addChartPainter(ChartPainter p){ + chartPainters.add(p); + } private void drawChart(Graphics2D g) { @@ -95,17 +105,26 @@ public class Chart1 extends javax.swing.JPanel implements QuoteReceiver, Adjustm @Override protected void paintComponent(Graphics g) { super.paintComponent(g); + + if (Globals.se==null) + return; - // Calculate the number of pixels for 1 em - em_width = g.getFontMetrics().stringWidth("M"); //this.xbar.setMaximum(994000); - ChartPainter p = new ChartPainter(); + XLegendChartPainter p = new XLegendChartPainter(); data = Globals.se.getOHLCdata(60000); - p.drawChart((Graphics2D) g, xbar, data, this); + + ChartDef def = new ChartDef(); + def.x_unit_width = 1.0; + def.x_scrollbar=xbar; + + + for (ChartPainter painter: chartPainters){ + painter.drawChart((Graphics2D)g, xbar, data, this, def); + } + - //drawChart((Graphics2D)g); } /** @@ -134,7 +153,7 @@ public class Chart1 extends javax.swing.JPanel implements QuoteReceiver, Adjustm int s = data.size(); System.out.printf("Data size %d",s); - xbar.setMaximum(data.size()); +// xbar.setMaximum(data.size()); repaint(); } diff --git a/src/chart/ChartDef.java b/src/chart/ChartDef.java index c7dace9..9c24c5e 100644 --- a/src/chart/ChartDef.java +++ b/src/chart/ChartDef.java @@ -32,6 +32,15 @@ import javax.swing.JScrollBar; * @author 7u83 <7u83@mail.ru> */ public class ChartDef { - JScrollBar scrollbar; + JScrollBar x_scrollbar=null; + + /** + * width of an x unit in em + */ + double x_unit_width=1.0; + + ChartDef(){ + + } } diff --git a/src/chart/ChartPainter.java b/src/chart/ChartPainter.java index 766dec6..52fcf23 100644 --- a/src/chart/ChartPainter.java +++ b/src/chart/ChartPainter.java @@ -28,6 +28,7 @@ package chart; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics2D; +import javax.swing.JPanel; import javax.swing.JScrollBar; import sesim.OHLCData; @@ -35,7 +36,7 @@ import sesim.OHLCData; * * @author 7u83 <7u83@mail.ru> */ -public class ChartPainter { +abstract public class ChartPainter { int em_width; @@ -49,45 +50,9 @@ public class ChartPainter { int big_tick = 10; int y = 0; - String getAt(OHLCData data, int unit) { - int fs = data.getFrameSize(); - return sesim.Scheduler.formatTimeMillis(0 + unit * fs); - - } - - public void drawChart(Graphics2D g, JScrollBar sb, OHLCData data, Chart1 p) { - init(g); - g.setColor(Color.MAGENTA); - Dimension size = p.getSize(); - //g.drawLine(0, 0, size.width, 100); - - System.out.printf("SIZE %d %d\n",size.width,size.height); - - - - int n; - int x; - for (n = 0, x = 0; x < size.width; x += em_width * 1.0) { - if (n % big_tick == 1) { - g.drawLine((int) x, y, (int) x, y + em_width); - String text; - text = getAt(data,n); - - int swidth = g.getFontMetrics().stringWidth(text); - g.drawString(text, (int) x - swidth / 2, y + em_width * 2); - - } else { - g.drawLine((int) x, y, (int) x, y + em_width / 2); - } - - if (n % big_tick == 0) { - - } - - n += 1; - } - - } + + + abstract public void drawChart(Graphics2D g, JScrollBar sb, OHLCData data, JPanel p, ChartDef def); } diff --git a/src/chart/ChartTestDialog.form b/src/chart/ChartTestDialog.form index fc701b5..1bc311e 100644 --- a/src/chart/ChartTestDialog.form +++ b/src/chart/ChartTestDialog.form @@ -33,7 +33,8 @@ - + + @@ -43,7 +44,9 @@ - + + + @@ -54,10 +57,12 @@ + + - + @@ -67,7 +72,29 @@ - + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/chart/ChartTestDialog.java b/src/chart/ChartTestDialog.java index a9ce203..c6abf45 100644 --- a/src/chart/ChartTestDialog.java +++ b/src/chart/ChartTestDialog.java @@ -25,11 +25,16 @@ */ package chart; +import gui.Globals; +import sesim.Exchange.QuoteReceiver; +import sesim.OHLCData; +import sesim.Quote; + /** * * @author 7u83 <7u83@mail.ru> */ -public class ChartTestDialog extends javax.swing.JDialog { +public class ChartTestDialog extends javax.swing.JDialog implements QuoteReceiver{ /** * Creates new form ChartTestDialog @@ -38,8 +43,12 @@ public class ChartTestDialog extends javax.swing.JDialog { super(parent, modal); initComponents(); setLocationRelativeTo(parent); - this.chart11.xbar=this.jScrollBar1; - this.jScrollBar1.addAdjustmentListener(chart11); + this.chart12.xbar=this.jScrollBar1; + this.jScrollBar1.addAdjustmentListener(chart12); + + ChartPainter p = new XLegendChartPainter(); + chart12.addChartPainter(p); + Globals.se.addQuoteReceiver(this); } /** @@ -52,13 +61,29 @@ public class ChartTestDialog extends javax.swing.JDialog { private void initComponents() { jScrollBar1 = new javax.swing.JScrollBar(); + chart12 = new chart.Chart1(); chart11 = new chart.Chart1(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setMaximumSize(new java.awt.Dimension(800, 600)); setMinimumSize(new java.awt.Dimension(100, 100)); + jScrollBar1.setMaximum(0); jScrollBar1.setOrientation(javax.swing.JScrollBar.HORIZONTAL); + jScrollBar1.setName(""); // NOI18N + + javax.swing.GroupLayout chart12Layout = new javax.swing.GroupLayout(chart12); + chart12.setLayout(chart12Layout); + chart12Layout.setHorizontalGroup( + chart12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGap(0, 0, Short.MAX_VALUE) + ); + chart12Layout.setVerticalGroup( + chart12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGap(0, 52, Short.MAX_VALUE) + ); + + chart11.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true)); javax.swing.GroupLayout chart11Layout = new javax.swing.GroupLayout(chart11); chart11.setLayout(chart11Layout); @@ -68,7 +93,7 @@ public class ChartTestDialog extends javax.swing.JDialog { ); chart11Layout.setVerticalGroup( chart11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGap(0, 318, Short.MAX_VALUE) + .addGap(0, 252, Short.MAX_VALUE) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); @@ -79,14 +104,17 @@ public class ChartTestDialog extends javax.swing.JDialog { .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 586, Short.MAX_VALUE) - .addComponent(chart11, javax.swing.GroupLayout.DEFAULT_SIZE, 586, Short.MAX_VALUE)) + .addComponent(chart12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(chart11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap() - .addComponent(chart11, javax.swing.GroupLayout.DEFAULT_SIZE, 318, Short.MAX_VALUE) + .addComponent(chart11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(chart12, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) @@ -139,6 +167,15 @@ public class ChartTestDialog extends javax.swing.JDialog { // Variables declaration - do not modify//GEN-BEGIN:variables private chart.Chart1 chart11; + private chart.Chart1 chart12; private javax.swing.JScrollBar jScrollBar1; // End of variables declaration//GEN-END:variables + + @Override + public void UpdateQuote(Quote q) { + OHLCData data = Globals.se.getOHLCdata(60000); + this.jScrollBar1.setMaximum(data.size()); + repaint(); + System.out.printf("SETMAXIMUM: %d", data.size()); + } } diff --git a/src/chart/OHLCChartPainter.java b/src/chart/OHLCChartPainter.java new file mode 100644 index 0000000..20ab4bb --- /dev/null +++ b/src/chart/OHLCChartPainter.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2017, 7u83 <7u83@mail.ru> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package chart; + +import java.awt.Dimension; +import java.awt.Graphics2D; +import javax.swing.JPanel; +import javax.swing.JScrollBar; +import sesim.OHLCData; + +/** + * + * @author 7u83 <7u83@mail.ru> + */ +public class OHLCChartPainter extends ChartPainter { + + @Override + public void drawChart(Graphics2D g, JScrollBar sb, OHLCData data, JPanel p, ChartDef def) { + init(g); + int first_bar = def.x_scrollbar.getValue(); + Dimension size = p.getSize(); + int bars = (int) (size.width / (def.x_unit_width * em_width)); + for (int b=0; b + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package chart; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics2D; +import javax.swing.JPanel; +import javax.swing.JScrollBar; +import sesim.OHLCData; + +/** + * + * @author 7u83 <7u83@mail.ru> + */ +public class XLegendChartPainter extends ChartPainter { + + private String getTimeStrAt(OHLCData data, int unit) { + + int fs = data.getFrameSize(); + return sesim.Scheduler.formatTimeMillis(0 + unit * fs); + + } + + public void drawChart(Graphics2D g, JScrollBar sb, OHLCData data, JPanel p, ChartDef def) + { + init(g); + g.setColor(Color.black); + Dimension size = p.getSize(); + //g.drawLine(0, 0, size.width, 100); + + System.out.printf("SIZE %d %d\n", size.width, size.height); + + int bars = (int) (size.width / (def.x_unit_width * em_width)); + System.out.printf("Units = %d\n", bars); + + int first_bar = def.x_scrollbar.getValue(); + //if (first_bar<0) + // first_bar=0; + + int n; + int x; + for (n = first_bar, x = 0; x < size.width; x += em_width * def.x_unit_width) { + if (n % big_tick == 1) { + g.drawLine((int) x, y, (int) x, y + em_width); + String text; + text = getTimeStrAt(data, n); + + int swidth = g.getFontMetrics().stringWidth(text); + g.drawString(text, (int) x - swidth / 2, y + em_width * 2); + + } else { + g.drawLine((int) x, y, (int) x, y + em_width / 2); + } + + if (n % big_tick == 0) { + + } + + n += 1; + } + + } + +}