From 9e4d46ee9979be2e50c3c5c4c96726159e672788 Mon Sep 17 00:00:00 2001 From: 7u83 <7u83@mail.ru> Date: Sat, 7 Oct 2017 02:40:22 +0200 Subject: [PATCH] Working ylegend now --- nbproject/private/private.xml | 50 ++++++++++++++--- nbproject/project.properties | 2 +- src/chart/MasterChart.java | 7 +-- src/chart/painter/ChartPainter.java | 71 +++++++++++++++++++------ src/chart/painter/OHLCChartPainter.java | 34 ++++++------ src/chart/painter/XLegendPainter.java | 4 +- src/chart/painter/YLegendPainter.java | 34 +++++++----- 7 files changed, 142 insertions(+), 60 deletions(-) diff --git a/nbproject/private/private.xml b/nbproject/private/private.xml index 69dfcfa..031f52b 100644 --- a/nbproject/private/private.xml +++ b/nbproject/private/private.xml @@ -3,13 +3,49 @@ - file:/home/tobias/v/SeSim/src/chart/OHLCChartPainter.java - file:/home/tobias/v/SeSim/src/sesim/OHLCData.java - file:/home/tobias/v/SeSim/src/sesim/MinMax.java - file:/home/tobias/v/SeSim/src/chart/Chart.java - file:/home/tobias/v/SeSim/src/chart/XLegendChartPainter.java - file:/home/tobias/v/SeSim/src/gui/MainChart.java - file:/home/tobias/v/SeSim/src/chart/ChartTestDialog.java + file:/home/tube/NetBeansProjects/SeSim/src/traders/RandomTraderA.java + file:/home/tube/NetBeansProjects/SeSim/test/sesim/Test.java + file:/home/tube/NetBeansProjects/SeSim/src/chart/MasterChart.java + file:/home/tube/NetBeansProjects/SeSim/src/chart/painter/ChartCrossPainter.java + file:/home/tube/NetBeansProjects/SeSim/src/gui/StatisticsPanel.java + file:/home/tube/NetBeansProjects/SeSim/src/gui/tools/NummericCellRenderer.java + file:/home/tube/NetBeansProjects/SeSim/src/sesim/AutoTraderInterface.java + file:/home/tube/NetBeansProjects/SeSim/src/traders/ManTrader/ManTraderConsoleDialog.java + file:/home/tube/NetBeansProjects/SeSim/src/sesim/AutoTraderLoader.java + file:/home/tube/NetBeansProjects/SeSim/src/traders/ManTrader/ManTrader.java + file:/home/tube/NetBeansProjects/SeSim/src/gui/SeSimApplication.java + file:/home/tube/NetBeansProjects/SeSim/src/gui/Globals.java + file:/home/tube/NetBeansProjects/SeSim/src/chart/painter/ChartPainter.java + file:/home/tube/NetBeansProjects/SeSim/src/chart/painter/OHLCChartPainter.java + file:/home/tube/NetBeansProjects/SeSim/src/traders/RandomTraderB.java + file:/home/tube/NetBeansProjects/SeSim/src/sesim/Logger.java + file:/home/tube/NetBeansProjects/SeSim/src/gui/EditPreferencesDialog.java + file:/home/tube/NetBeansProjects/SeSim/src/gui/QuotePanel.java + file:/home/tube/NetBeansProjects/SeSim/src/sesim/AutoTraderBase.java + file:/home/tube/NetBeansProjects/SeSim/src/gui/Statistics.java + file:/home/tube/NetBeansProjects/SeSim/src/gui/EditStrategies.java + file:/home/tube/NetBeansProjects/SeSim/src/gui/MainChart.java + file:/home/tube/NetBeansProjects/SeSim/src/chart/Chart.java + file:/home/tube/NetBeansProjects/SeSim/src/sesim/Clock.java + file:/home/tube/NetBeansProjects/SeSim/src/chart/ChartDef.java + file:/home/tube/NetBeansProjects/SeSim/src/sesim/OHLCData.java + file:/home/tube/NetBeansProjects/SeSim/src/sesim/IDGenerator.java + file:/home/tube/NetBeansProjects/SeSim/src/sesim/MinMax.java + file:/home/tube/NetBeansProjects/SeSim/src/chart/painter/XLegendPainter.java + file:/home/tube/NetBeansProjects/SeSim/src/chart/painter/LineChartPainter.java + file:/home/tube/NetBeansProjects/SeSim/src/sesim/AutoTraderGui.java + file:/home/tube/NetBeansProjects/SeSim/src/sesim/Exchange.java + file:/home/tube/NetBeansProjects/SeSim/src/gui/jp99.java + file:/home/tube/NetBeansProjects/SeSim/src/sesim/Locker.java + file:/home/tube/NetBeansProjects/SeSim/src/chart/ChartPanel.java + file:/home/tube/NetBeansProjects/SeSim/src/gui/orderbook/OrderBook.java + 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/painter/CandleStickChartPainter.java + file:/home/tube/NetBeansProjects/SeSim/src/chart/painter/YLegendPainter.java + file:/home/tube/NetBeansProjects/SeSim/src/chart/SuperDlg.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 2d706e7..723b684 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -1,4 +1,4 @@ -#Thu, 05 Oct 2017 22:01:54 +0200 +#Sat, 07 Oct 2017 02:38:10 +0200 annotation.processing.enabled=true annotation.processing.enabled.in.editor=false annotation.processing.processors.list= diff --git a/src/chart/MasterChart.java b/src/chart/MasterChart.java index 605be42..a380ba5 100644 --- a/src/chart/MasterChart.java +++ b/src/chart/MasterChart.java @@ -70,14 +70,15 @@ public class MasterChart extends javax.swing.JPanel implements QuoteReceiver, Ch chart.addChartPainter(pc); chart.setXSCrollBar(xScrollBar); + chart.addChartPainter(new ChartCrossPainter()); ChartPainter yp = new YLegendPainter(chart); yp.setDataProvider(this); - this.chart.addChartPainter(yp); + this.yLegend.addChartPainter(yp); - this.yLegend.addChartPainter(p); - this.yLegend.addChartPainter(pc); + // this.yLegend.addChartPainter(p); + //this.yLegend.addChartPainter(pc); } diff --git a/src/chart/painter/ChartPainter.java b/src/chart/painter/ChartPainter.java index 39321fa..fc3f079 100644 --- a/src/chart/painter/ChartPainter.java +++ b/src/chart/painter/ChartPainter.java @@ -29,6 +29,7 @@ import chart.ChartDef; import chart.ChartPanel; import java.awt.Dimension; import java.awt.Graphics2D; +import sesim.MinMax; import sesim.OHLCData; /** @@ -37,24 +38,25 @@ import sesim.OHLCData; */ abstract public class ChartPainter { - int em_size; - //OHLCData data=null; - + protected int em_size; + public abstract interface DataProvider { + abstract OHLCData get(); } - DataProvider dataProvider=null; - - public void setDataProvider(DataProvider dataProvider){ + DataProvider dataProvider = null; + + public void setDataProvider(DataProvider dataProvider) { this.dataProvider = dataProvider; } - - protected OHLCData getData(){ - if (dataProvider==null) - return null; - return dataProvider.get(); - } + + protected OHLCData getData() { + if (dataProvider == null) { + return null; + } + return dataProvider.get(); + } protected int getFirstBar(ChartPanel p) { if (p.x_scrollbar != null) { @@ -62,14 +64,15 @@ abstract public class ChartPainter { } return 0; } - - protected int getBars(ChartPanel p, ChartDef def){ - Dimension dim = p.getSize(); + + protected int getBars(ChartPanel p, ChartDef def) { + Dimension dim = p.getSize(); return (int) (dim.width / (def.x_unit_width * em_size)); } /** - * Init method scould be called before painting the chart + * Init method scould be called before painting the chart + * * @param g Graphics context */ protected final void init(Graphics2D g) { @@ -79,6 +82,42 @@ abstract public class ChartPainter { } + protected float y_scaling; + protected int y_height; + protected float y_min; + + float getY(float y) { +//c_yscaling = ctx.rect.height / c_mm.getDiff(); +// float ys = dim.height / mm.getDiff(); + /* if (minmax.isLog()) { +// return rect.height + rect.y - ((float) Math.log(y) - c_mm.getMin()) * ys; + } + */ +// return (dim.height - ((y - minmax.getMin()) * y_scaling)); + return (y_height - ((y - y_min) * y_scaling)); + + } + + double getValAtY(float y) { + float val = 0; + + /* if (c_mm.isLog()) { + float ys = rect.height / c_mm.getDiff(); + + return Math.exp((rect.height + rect.y) / ys + c_mm.getMin() - y / ys); + + } + */ + return (-(y - y_height)) / y_scaling + y_min; + + } + + void initGetY(MinMax minmax, Dimension dim) { + y_height = dim.height; + y_scaling = dim.height / minmax.getDiff(); + y_min = minmax.getMin(); + } + abstract public void drawChart(Graphics2D g, ChartPanel p, ChartDef def); } diff --git a/src/chart/painter/OHLCChartPainter.java b/src/chart/painter/OHLCChartPainter.java index 9506ed8..b54d353 100644 --- a/src/chart/painter/OHLCChartPainter.java +++ b/src/chart/painter/OHLCChartPainter.java @@ -44,23 +44,13 @@ import sesim.OHLCDataItem; public abstract class OHLCChartPainter extends ChartPainter { protected float iwidth; - private MinMax mm; - private Dimension dim; - private float y_scaling; - float getY(float y) { -//c_yscaling = ctx.rect.height / c_mm.getDiff(); -// float ys = dim.height / mm.getDiff(); - if (mm.isLog()) { -// return rect.height + rect.y - ((float) Math.log(y) - c_mm.getMin()) * ys; - } - return (dim.height - ((y - mm.getMin()) * y_scaling)); - - } + abstract void drawItem(Graphics2D g, int prevx, int x, OHLCDataItem prev, OHLCDataItem i); + @Override public void drawChart(Graphics2D g, ChartPanel p, ChartDef def) { @@ -75,19 +65,25 @@ public abstract class OHLCChartPainter extends ChartPainter { int first_bar = getFirstBar(p); - dim = p.getSize(); - int bars = (int) (dim.width / (def.x_unit_width * em_size)); - + Dimension dim = p.getSize(); + //int bars = (int) (dim.width / (def.x_unit_width * em_size)); + int bars = this.getBars(p, def); + + int last_bar = first_bar + bars+1; - mm = data.getMinMax(first_bar, last_bar); - - y_scaling = dim.height / mm.getDiff(); + MinMax minmax = data.getMinMax(first_bar, last_bar); + + this.initGetY(minmax, dim); + + // y_scaling = dim.height / minmax.getDiff(); + + OHLCDataItem prevd = null; int prevx; -System.out.printf("Firstbar %d - %d",first_bar,last_bar); + if (data.size() > 0 && first_bar < data.size()) { prevd = data.get(first_bar); diff --git a/src/chart/painter/XLegendPainter.java b/src/chart/painter/XLegendPainter.java index 38d8545..73c8eb9 100644 --- a/src/chart/painter/XLegendPainter.java +++ b/src/chart/painter/XLegendPainter.java @@ -61,8 +61,10 @@ public class XLegendPainter extends ChartPainter { g.setColor(Color.black); Dimension size = p.getSize(); - int bars = (int) (size.width / (def.x_unit_width * em_size)); + //int bars = (int) (size.width / (def.x_unit_width * em_size)); + int bars = this.getBars(p, def); + int first_bar = getFirstBar(p); int n; diff --git a/src/chart/painter/YLegendPainter.java b/src/chart/painter/YLegendPainter.java index 2ffa633..64c0d4d 100644 --- a/src/chart/painter/YLegendPainter.java +++ b/src/chart/painter/YLegendPainter.java @@ -31,6 +31,8 @@ import chart.ChartPanel; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.Rectangle; +import sesim.MinMax; +import sesim.OHLCDataItem; /** * @@ -50,7 +52,11 @@ public class YLegendPainter extends ChartPainter { Dimension dim = master.getSize(); int first_bar = getFirstBar(master); - int bars = getBars(master,def); + int last_bar = first_bar + getBars(master,def); + MinMax minmax = this.getData().getMinMax(first_bar, last_bar); + + this.initGetY(minmax, dim); + //Rectangle dim; // dim = p.getSize(); @@ -61,25 +67,27 @@ public class YLegendPainter extends ChartPainter { // g.drawLine(dim.width + dim.x - yw, 0, dim.width + dim.x - yw, dim.height); -/* - float y1 = ctx.getY(mm.getMin(false)); - float y2 = ctx.getY(c_mm.getMax(false)); + + float y1 = getY(minmax.getMin(false)); + float y2 = getY(minmax.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); - - for (int yp = (int) y2; yp < y1; yp += em_width * 5) { - g.drawLine(dim.width + dim.x - yw, yp, dim.width + dim.x - yw + em_width, yp); - double v1 = ctx.getValAtY(yp); - g.drawString(String.format("%.2f", v1), dim.width + dim.x - yw + em_width * 1.5f, yp + c_font_height / 3); + int c_font_height = g.getFontMetrics().getHeight(); + + for (int yp = (int) y2; yp < y1; yp += em_size * 3) { + g.drawLine(0, yp, em_size, yp); + double v1 = getValAtY(yp); + g.drawString(String.format("%.2f", v1), em_size * 1.5f, yp + c_font_height / 3); } double v1, v2; - v1 = ctx.getValAtY(y1); - v2 = ctx.getValAtY(y2); -*/ +// v1 = ctx.getValAtY(y1); +// v2 = ctx.getValAtY(y2); + } - + + }