Skip to content

Commit 173836a

Browse files
RobTerpilowskiRobTerpilowski
RobTerpilowski
authored and
RobTerpilowski
committed
1 parent 14630a0 commit 173836a

File tree

6 files changed

+196
-36
lines changed

6 files changed

+196
-36
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/

pom.xml

+10-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.zoicapital</groupId>
66
<artifactId>StockChartsFx</artifactId>
7-
<version>0.0.1-SNAPSHOT</version>
7+
<version>1.0.1-SNAPSHOT</version>
88
<packaging>jar</packaging>
99

1010
<name>StockChartsFx</name>
@@ -15,8 +15,7 @@
1515
</properties>
1616

1717
<organization>
18-
<!-- Used as the 'Vendor' for JNLP generation -->
19-
<name>Your Organisation</name>
18+
<name>Zoi Capital, LLC</name>
2019
</organization>
2120

2221
<build>
@@ -104,5 +103,12 @@
104103
</plugin>
105104
</plugins>
106105
</build>
107-
106+
<dependencies>
107+
<dependency>
108+
<groupId>junit</groupId>
109+
<artifactId>junit</artifactId>
110+
<version>4.10</version>
111+
<scope>test</scope>
112+
</dependency>
113+
</dependencies>
108114
</project>

src/main/java/com/zoicapital/stockchartsfx/CandleStickChart.java

+73-32
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package com.zoicapital.stockchartsfx;
22
/*
3-
Copyright 2014 Zoi Capital, LLC
3+
Copyright 2014 Zoi Capital, LLC
44
5-
Licensed under the Apache License, Version 2.0 (the "License");
6-
you may not use this file except in compliance with the License.
7-
You may obtain a copy of the License at
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
88
9-
http://www.apache.org/licenses/LICENSE-2.0
9+
http://www.apache.org/licenses/LICENSE-2.0
1010
11-
Unless required by applicable law or agreed to in writing, software
12-
distributed under the License is distributed on an "AS IS" BASIS,
13-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
See the License for the specific language governing permissions and
15-
limitations under the License.
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
1616
*/
17+
1718
import java.text.SimpleDateFormat;
1819
import java.util.ArrayList;
1920
import java.util.Iterator;
@@ -38,14 +39,15 @@
3839
import javafx.scene.shape.Path;
3940
import javafx.util.Duration;
4041

41-
42-
/**
43-
* A candlestick chart is a style of bar-chart used primarily to describe
44-
* price movements of a security, derivative, or currency over time.
42+
/**
43+
* A candlestick chart is a style of bar-chart used primarily to describe price
44+
* movements of a security, derivative, or currency over time.
4545
*
46-
* The Data Y value is used for the opening price and then the close, high
47-
* and low values are stored in the Data's extra value property using a
46+
* The Data Y value is used for the opening price and then the close, high and
47+
* low values are stored in the Data's extra value property using a
4848
* CandleStickExtraValues object.
49+
*
50+
*
4951
*/
5052
public class CandleStickChart extends XYChart<String, Number> {
5153

@@ -54,31 +56,48 @@ public class CandleStickChart extends XYChart<String, Number> {
5456
protected int maxBarsToDisplay;
5557
protected ObservableList<XYChart.Series<String, Number>> dataSeries;
5658
protected BarData lastBar;
59+
protected NumberAxis yAxis;
60+
protected CategoryAxis xAxis;
5761

62+
63+
64+
/**
65+
*
66+
* @param title The chart title
67+
* @param bars The bars data to display in the chart.
68+
*/
5869
public CandleStickChart(String title, List<BarData> bars) {
5970
this(title, bars, Integer.MAX_VALUE);
6071
}
6172

73+
74+
/**
75+
*
76+
* @param title The chart title
77+
* @param bars The bars to display in the chart
78+
* @param maxBarsToDisplay The maximum number of bars to display in the chart.
79+
*/
6280
public CandleStickChart(String title, List<BarData> bars, int maxBarsToDisplay) {
6381
this(title, new CategoryAxis(), new NumberAxis(), bars, maxBarsToDisplay);
6482
}
6583

66-
// -------------- CONSTRUCTORS ----------------------------------------------
6784
/**
6885
* Construct a new CandleStickChart with the given axis.
6986
*
87+
* @param title The chart title
7088
* @param xAxis The x axis to use
7189
* @param yAxis The y axis to use
72-
* @param bars
73-
* @param maxBarsToDisplay
90+
* @param bars The bars to display on the chart
91+
* @param maxBarsToDisplay The maximum number of bars to display on the chart.
7492
*/
75-
public CandleStickChart(String title, Axis<String> xAxis, Axis<Number> yAxis, List<BarData> bars, int maxBarsToDisplay) {
93+
public CandleStickChart(String title, CategoryAxis xAxis, NumberAxis yAxis, List<BarData> bars, int maxBarsToDisplay) {
7694
super(xAxis, yAxis);
95+
this.xAxis = xAxis;
96+
this.yAxis = yAxis;
7797
this.maxBarsToDisplay = maxBarsToDisplay;
78-
//yAxis.setLabel("Yxis");
79-
NumberAxis y = (NumberAxis) yAxis;
98+
8099
yAxis.autoRangingProperty().set(true);
81-
y.forceZeroInRangeProperty().setValue(Boolean.FALSE);
100+
yAxis.forceZeroInRangeProperty().setValue(Boolean.FALSE);
82101
setTitle(title);
83102
setAnimated(true);
84103
getStylesheets().add(getClass().getResource("/styles/CandleStickChartStyles.css").toExternalForm());
@@ -101,15 +120,20 @@ public CandleStickChart(String title, Axis<String> xAxis, Axis<Number> yAxis, Li
101120
lastBar = sublist.get(sublist.size() - 1);
102121
}
103122

104-
protected List<BarData> getSubList(List<BarData> bars, int maxBars) {
105-
List<BarData> sublist;
106-
if (bars.size() > maxBars) {
107-
return bars.subList(bars.size() - 1 - maxBars, bars.size() - 1);
108-
} else {
109-
return bars;
110-
}
123+
124+
/**
125+
* Defines a formatter to use when formatting the y-axis values.
126+
* @param formatter The formatter to use when formatting the y-axis values.
127+
*/
128+
public void setYAxisFormatter(DecimalAxisFormatter formatter) {
129+
yAxis.setTickLabelFormatter(formatter);
111130
}
112131

132+
133+
/**
134+
* Appends a new bar on to the end of the chart.
135+
* @param bar The bar to append to the chart
136+
*/
113137
public void addBar(BarData bar) {
114138

115139
if (dataSeries.get(0).getData().size() >= maxBarsToDisplay) {
@@ -126,8 +150,16 @@ public void addBar(BarData bar) {
126150
lastBar = new BarData(bar.getDateTime(), bar.getClose(), bar.getClose(), bar.getClose(), bar.getClose(), 0);
127151
Data<String, Number> data = new XYChart.Data<>(label, lastBar.getOpen(), lastBar);
128152
dataSeries.get(0).getData().add(data);
153+
154+
155+
129156
}
130157

158+
159+
/**
160+
* Update the "Last" price of the most recent bar
161+
* @param price The Last price of the most recent bar.
162+
*/
131163
public void updateLast(double price) {
132164
if (lastBar != null) {
133165
lastBar.update(price);
@@ -141,6 +173,17 @@ public void updateLast(double price) {
141173
}
142174
}
143175

176+
177+
178+
protected List<BarData> getSubList(List<BarData> bars, int maxBars) {
179+
List<BarData> sublist;
180+
if (bars.size() > maxBars) {
181+
return bars.subList(bars.size() - 1 - maxBars, bars.size() - 1);
182+
} else {
183+
return bars;
184+
}
185+
}
186+
144187
// -------------- METHODS ------------------------------------------------------------------------------------------
145188
/**
146189
* Called to update and layout the content for the plot
@@ -428,5 +471,3 @@ public void update(double open, double close, double high, double low) {
428471
protected static CandleStickChart chart;
429472

430473
}
431-
432-
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.zoicapital.stockchartsfx;
7+
8+
import java.text.DecimalFormat;
9+
import java.text.ParseException;
10+
import java.util.logging.Level;
11+
import java.util.logging.Logger;
12+
import javafx.util.StringConverter;
13+
14+
/**
15+
*
16+
* @author RobTerpilowski
17+
*/
18+
public class DecimalAxisFormatter extends StringConverter<Number>{
19+
20+
protected DecimalFormat decimalFormat;
21+
22+
public DecimalAxisFormatter( String format ) {
23+
decimalFormat = new DecimalFormat(format);
24+
}
25+
26+
public DecimalAxisFormatter( DecimalFormat decimalFormat ) {
27+
this.decimalFormat = decimalFormat;
28+
}
29+
30+
31+
@Override
32+
public String toString(Number object) {
33+
return decimalFormat.format(object.doubleValue());
34+
}
35+
36+
@Override
37+
public Number fromString(String string) {
38+
try {
39+
return decimalFormat.parse(string);
40+
} catch (ParseException ex) {
41+
throw new IllegalStateException(ex);
42+
}
43+
}
44+
45+
46+
47+
}

src/main/java/com/zoicapital/stockchartsfx/MainApp.java

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818

19+
import java.text.DecimalFormat;
1920
import java.util.ArrayList;
2021
import java.util.Calendar;
2122
import java.util.GregorianCalendar;
@@ -37,6 +38,8 @@ public void start(Stage stage) throws Exception {
3738
stage.setTitle("JavaFX and Maven");
3839
stage.setScene(scene);
3940
stage.show();
41+
42+
candleStickChart.setYAxisFormatter(new DecimalAxisFormatter("#000.00"));
4043
}
4144

4245

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.zoicapital.stockchartsfx;
7+
8+
import java.text.DecimalFormat;
9+
import org.junit.After;
10+
import org.junit.AfterClass;
11+
import org.junit.Before;
12+
import org.junit.BeforeClass;
13+
import org.junit.Test;
14+
import static org.junit.Assert.*;
15+
16+
/**
17+
*
18+
* @author RobTerpilowski
19+
*/
20+
public class DecimalAxisFormatterTest {
21+
22+
23+
24+
public DecimalAxisFormatterTest() {
25+
}
26+
27+
@BeforeClass
28+
public static void setUpClass() {
29+
}
30+
31+
@AfterClass
32+
public static void tearDownClass() {
33+
}
34+
35+
@Before
36+
public void setUp() {
37+
}
38+
39+
@After
40+
public void tearDown() {
41+
}
42+
43+
44+
@Test
45+
public void testStringConstructor() {
46+
String format = "#000.00";
47+
DecimalAxisFormatter formatter = new DecimalAxisFormatter(format);
48+
49+
assertEquals( "1234.45", formatter.toString(1234.45) );
50+
assertEquals( 1234.45, formatter.fromString("1234.45") );
51+
}
52+
53+
@Test
54+
public void testFormatterConstructor() {
55+
String format = "#000.00";
56+
DecimalAxisFormatter formatter = new DecimalAxisFormatter(new DecimalFormat(format));
57+
58+
assertEquals( "1234.45", formatter.toString(1234.45) );
59+
assertEquals( 1234.45, formatter.fromString("1234.45") );
60+
}
61+
62+
}

0 commit comments

Comments
 (0)