xlegend in a separate object

new object ohlcchartpainter
This commit is contained in:
7u83 2017-10-01 23:16:02 +02:00
parent caeb2c4060
commit 70f7c092dd
9 changed files with 258 additions and 60 deletions

View File

@ -37,6 +37,7 @@
<file>file:/home/tube/NetBeansProjects/SeSim/src/sesim/OHLCDataItem.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/gui/AboutDialog.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/chart/Chart1.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/chart/ChartPainter.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/README.md</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/gui/TestChartdialog.java</file>
</group>

View File

@ -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=

View File

@ -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 <ChartPainter> 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();
}

View File

@ -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(){
}
}

View File

@ -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);
}

View File

@ -33,7 +33,8 @@
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="jScrollBar1" pref="586" max="32767" attributes="0"/>
<Component id="chart11" pref="586" max="32767" attributes="0"/>
<Component id="chart12" alignment="0" max="32767" attributes="0"/>
<Component id="chart11" alignment="0" max="32767" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
</Group>
@ -43,7 +44,9 @@
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="1" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Component id="chart11" pref="318" max="32767" attributes="0"/>
<Component id="chart11" max="32767" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="chart12" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="jScrollBar1" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
@ -54,10 +57,12 @@
<SubComponents>
<Component class="javax.swing.JScrollBar" name="jScrollBar1">
<Properties>
<Property name="maximum" type="int" value="0"/>
<Property name="orientation" type="int" value="0"/>
<Property name="name" type="java.lang.String" value="" noResource="true"/>
</Properties>
</Component>
<Container class="chart.Chart1" name="chart11">
<Container class="chart.Chart1" name="chart12">
<Layout>
<DimensionLayout dim="0">
@ -67,7 +72,29 @@
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<EmptySpace min="0" pref="318" max="32767" attributes="0"/>
<EmptySpace min="0" pref="52" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
</Layout>
</Container>
<Container class="chart.Chart1" name="chart11">
<Properties>
<Property name="border" type="javax.swing.border.Border" editor="org.netbeans.modules.form.editors2.BorderEditor">
<Border info="org.netbeans.modules.form.compat2.border.LineBorderInfo">
<LineBorder roundedCorners="true"/>
</Border>
</Property>
</Properties>
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<EmptySpace min="0" pref="252" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
</Layout>

View File

@ -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());
}
}

View File

@ -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<bars; b++){
}
}
}

View File

@ -0,0 +1,88 @@
/*
* 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.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;
}
}
}