Impreved chart window

This commit is contained in:
7u83 2017-04-02 15:02:22 +02:00
parent a5cf49d767
commit bfefc1598e
5 changed files with 237 additions and 39 deletions

View File

@ -1,3 +1,4 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
@ -45,7 +46,6 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
protected int num_bars = 4000;
protected Rectangle clip_bounds = new Rectangle();
protected Dimension gdim;
private int first_bar, last_bar;
@ -220,15 +220,19 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
Graphics2D g;
float iwidth;
float getYc(float y) {
return getY(y);
//return rect.height - ((y - min) * scaling);
float getY(float y) {
float ys = rect.height / c_mm.getDiff();
if (c_mm.isLog()) {
return rect.height + rect.y - ((float) Math.log(y) - c_mm.getMin()) * ys;
}
return (rect.height - ((y - c_mm.getMin()) * c_yscaling)) + rect.y;
}
}
boolean logs = false;
float getY(float y) {
float getY0(float y) {
float ys = c_rect.height / c_mm.getDiff();
// ys = c_rect.height / c_mm.getDiff();
@ -282,8 +286,6 @@ 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 drawCandleItem(RenderCtx ctx, int prevx, int x, OHLCDataItem prev, OHLCDataItem i) {
Graphics2D g = ctx.g;
@ -294,29 +296,29 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
int xl = (int) (x + ctx.iwidth / 2);
g.setColor(Color.BLACK);
g.drawLine(xl, (int) ctx.getYc(i.close), xl, (int) ctx.getYc(i.high));
g.drawLine(xl, (int) ctx.getYc(i.low), xl, (int) ctx.getYc(i.open));
g.drawLine(xl, (int) ctx.getY(i.close), xl, (int) ctx.getY(i.high));
g.drawLine(xl, (int) ctx.getY(i.low), xl, (int) ctx.getY(i.open));
float w = ctx.iwidth;
float h = (int) (ctx.getYc(i.open) - ctx.getYc(i.close));
float h = (int) (ctx.getY(i.open) - ctx.getY(i.close));
g.setColor(Color.GREEN);
g.fillRect((int) (x), (int) ctx.getYc(i.close), (int) w, (int) h);
g.fillRect((int) (x), (int) ctx.getY(i.close), (int) w, (int) h);
g.setColor(Color.BLACK);
g.drawRect((int) (x), (int) ctx.getYc(i.close), (int) w, (int) h);
g.drawRect((int) (x), (int) ctx.getY(i.close), (int) w, (int) h);
} else {
int xl = (int) (x + ctx.iwidth / 2);
g.setColor(Color.RED);
g.drawLine(xl, (int) ctx.getYc(i.high), xl, (int) ctx.getYc(i.close));
g.drawLine(xl, (int) ctx.getYc(i.open), xl, (int) ctx.getYc(i.low));
g.drawLine(xl, (int) ctx.getY(i.high), xl, (int) ctx.getY(i.close));
g.drawLine(xl, (int) ctx.getY(i.open), xl, (int) ctx.getY(i.low));
float w = ctx.iwidth;
float h = (int) (ctx.getYc(i.close) - ctx.getYc(i.open));
float h = (int) (ctx.getY(i.close) - ctx.getY(i.open));
g.fillRect((int) (x), (int) ctx.getYc(i.open), (int) w, (int) h);
g.fillRect((int) (x), (int) ctx.getY(i.open), (int) w, (int) h);
g.setColor(Color.BLACK);
g.drawRect((int) (x), (int) ctx.getYc(i.open), (int) w, (int) h);
g.drawRect((int) (x), (int) ctx.getY(i.open), (int) w, (int) h);
}
@ -326,7 +328,7 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
Graphics2D g = ctx.g;
g.setColor(Color.BLACK);
g.drawLine(x, (int) ctx.getYc(0), x, (int) ctx.getYc(i.volume));
g.drawLine(x, (int) ctx.getY(0), x, (int) ctx.getY(i.volume));
Rectangle r = ctx.rect;
}
@ -363,14 +365,13 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
Rectangle dim;
dim = this.clip_bounds;
// Dimension rv = this.getSize();
// Dimension rv = this.getSize();
int yw = (int) (this.y_legend_width * this.em_width);
g.drawLine(dim.width + dim.x - yw, 0, dim.width + dim.x - yw, dim.height);
float y1 = getY(c_mm.getMin(false));
float y2 = getY(c_mm.getMax(false));
float y1 = ctx.getY(c_mm.getMin(false));
float y2 = ctx.getY(c_mm.getMax(false));
float ydiff = y1 - y2;
System.out.printf("%s y1: %f, y2: %f, diff %f\n", Boolean.toString(c_mm.isLog()), y1, y2, ydiff);
@ -405,6 +406,8 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
void drawChart(RenderCtx ctx) {
c_yscaling = c_rect.height / c_mm.getDiff();
ctx.g.setClip(null);
// ctx.g.setColor(Color.ORANGE);
@ -434,6 +437,106 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
boolean autoScroll = true;
int lastvpos = 0;
class ChartDef {
float height;
ChartType type;
OHLCData data;
Color bgcolor=null;
}
ArrayList<ChartDef> charts = new ArrayList<>();
void addChart(ChartDef d) {
charts.add(d);
}
void setupCharts() {
charts = new ArrayList<>();
ChartDef main = new ChartDef();
main.height = 0.8f;
main.type = ChartType.CANDLESTICK;
main.data = this.data;
//main.bgcolor =Color.WHITE;
addChart(main);
ChartDef vol = new ChartDef();
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;
this.setPreferredSize(new Dimension(pwidth, gdim.height));
this.revalidate();
int h1 = 0;
int loops = 0;
for (ChartDef d : charts) {
switch (d.type) {
case VOL:
c_mm = d.data.getVolMinMax(first_bar, last_bar);
c_mm.setMin(0);
break;
default:
c_mm = d.data.getMinMax(first_bar, last_bar);
}
int cheight = gdim.height - 6 * em_width;
int h = (int) (cheight * d.height);
c_rect = new Rectangle(0, h1, pwidth, h);
System.out.printf("Nananan %d\n", loops++);
RenderCtx ctx = new RenderCtx();
ctx.rect = c_rect;
ctx.scaling = (float) c_rect.height / (c_mm.getMax() - c_mm.getMin());
ctx.min = c_mm.getMin();
ctx.g = g;
ctx.iwidth = (float) ((x_unit_width * em_width) * 0.9f);
this.ct = d.type;
logs = false;
c_mm.setLog(false);
if (d.bgcolor!=null){
Color cur=g.getColor();
ctx.g.setColor(d.bgcolor);
ctx.g.fillRect(ctx.rect.x,ctx.rect.y,ctx.rect.width,ctx.rect.height);
ctx.g.drawRect(ctx.rect.x,ctx.rect.y,ctx.rect.width,ctx.rect.height);
ctx.g.setColor(cur);
}
drawChart(ctx);
h1 = h + em_width;
}
}
private void draw(Graphics2D g) {
if (data == null) {
@ -443,6 +546,36 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
return;
}
this.setupCharts();
num_bars = data.size();
// c_mm = data.getMinMax(first_bar, last_bar);
// if (c_mm == null) {
// return;
// }
em_height = g.getFontMetrics().getHeight();
em_width = g.getFontMetrics().stringWidth("M");
XLegendDef xld = new XLegendDef();
this.drawXLegend(g, xld);
drawAll(g);
}
Dimension gdim;
private void draw2(Graphics2D g) {
if (data == null) {
return;
}
if (data.size() == 0) {
return;
}
num_bars = data.size();
c_mm = data.getMinMax(first_bar, last_bar);
@ -450,8 +583,6 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
return;
}
// c_mm.min/= 1; //-= c_mm.getMin()/ 2.0f;
// c_mm.max *= 1; //+= c_mm.getMax() / 10.0f;
em_height = g.getFontMetrics().getHeight();
em_width = g.getFontMetrics().stringWidth("M");
@ -491,7 +622,6 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
int cheight = gdim.height - 6 * em_width;
int h = (int) (cheight * 0.8);
Rectangle r = new Rectangle(0, 0, pwidth, h);
c_rect = r;

28
src/gui/MainChart.form Normal file
View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JPanelFormInfo">
<AuxValues>
<AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
<AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
<AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
</AuxValues>
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<EmptySpace min="0" pref="400" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<EmptySpace min="0" pref="300" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
</Layout>
</Form>

45
src/gui/MainChart.java Normal file
View File

@ -0,0 +1,45 @@
/*
* 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 gui;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class MainChart extends chart.Chart {
/**
* Creates new form MainChart
*/
public MainChart() {
initComponents();
}
/**
* 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
}

View File

@ -419,6 +419,7 @@
<SubComponents>
<Container class="javax.swing.JSplitPane" name="jSplitPane4">
<Properties>
<Property name="dividerLocation" type="int" value="300"/>
<Property name="orientation" type="int" value="0"/>
</Properties>
<Constraints>
@ -445,22 +446,17 @@
<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
<SubComponents>
<Container class="chart.Chart" name="chart">
<Properties>
<Property name="preferredSize" type="java.awt.Dimension" editor="org.netbeans.beaninfo.editors.DimensionEditor">
<Dimension value="[892, 410]"/>
</Property>
</Properties>
<Container class="gui.MainChart" name="chart">
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<EmptySpace min="0" pref="892" max="32767" attributes="0"/>
<EmptySpace min="0" pref="100" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<EmptySpace min="0" pref="410" max="32767" attributes="0"/>
<EmptySpace min="0" pref="100" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
</Layout>

View File

@ -162,7 +162,7 @@ public class NewMDIApplication extends javax.swing.JFrame {
jSplitPane4 = new javax.swing.JSplitPane();
orderBooksHorizontal1 = new gui.orderbook.OrderBooksHorizontal();
jChartScrollPane = new javax.swing.JScrollPane();
chart = new chart.Chart();
chart = new gui.MainChart();
quoteVertical1 = new gui.orderbook.QuoteVertical();
jSplitPane5 = new javax.swing.JSplitPane();
statistics1 = new gui.Statistics();
@ -295,20 +295,19 @@ public class NewMDIApplication extends javax.swing.JFrame {
getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_START);
jSplitPane4.setDividerLocation(300);
jSplitPane4.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
jSplitPane4.setBottomComponent(orderBooksHorizontal1);
chart.setPreferredSize(new java.awt.Dimension(892, 410));
javax.swing.GroupLayout chartLayout = new javax.swing.GroupLayout(chart);
chart.setLayout(chartLayout);
chartLayout.setHorizontalGroup(
chartLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 892, Short.MAX_VALUE)
.addGap(0, 100, Short.MAX_VALUE)
);
chartLayout.setVerticalGroup(
chartLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 410, Short.MAX_VALUE)
.addGap(0, 100, Short.MAX_VALUE)
);
jChartScrollPane.setViewportView(chart);
@ -769,7 +768,7 @@ public class NewMDIApplication extends javax.swing.JFrame {
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JMenuItem aboutMenuItem;
private javax.swing.JSpinner accelSpinner;
private chart.Chart chart;
private gui.MainChart chart;
private gui.Clock clock;
private javax.swing.JMenuItem contentMenuItem;
private javax.swing.JMenuItem deleteMenuItem;