13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package test.javafx.scene.chart;
27
28
29 import org.junit.Test;
30 import static org.junit.Assert.assertEquals;
31 import javafx.collections.*;
32
33 import test.com.sun.javafx.pgstub.StubToolkit;
34 import java.util.Arrays;
35
36 import javafx.scene.Node;
37 import javafx.scene.Scene;
38 import javafx.scene.chart.CategoryAxis;
39 import javafx.scene.chart.Chart;
40 import javafx.scene.chart.NumberAxis;
41 import javafx.scene.chart.StackedBarChart;
42 import javafx.scene.chart.XYChart;
43 import javafx.scene.chart.XYChartShim;
44 import javafx.stage.Stage;
45 import javafx.scene.layout.Region;
46
47 import org.junit.Ignore;
48
49 @Ignore
50 public class StackedBarChartTest extends XYChartTestBase {
51
52 private Scene scene;
53 private StubToolkit toolkit;
54 private Stage stage;
55 StackedBarChart<String,Number> sbc;
56 final XYChart.Series<String,Number> series1 = new XYChart.Series<String,Number>();
57 final XYChart.Series<String,Number> series2 = new XYChart.Series<String,Number>();
58 final XYChart.Series<String,Number> series3 = new XYChart.Series<String,Number>();
59 final String[] years = {"2007", "2008", "2009"};
60
61 protected Chart createChart() {
62 final CategoryAxis xAxis = new CategoryAxis();
63 final NumberAxis yAxis = new NumberAxis();
64 sbc = new StackedBarChart<String,Number>(xAxis,yAxis);
65 xAxis.setLabel("Year");
66 xAxis.setCategories(FXCollections.<String>observableArrayList(Arrays.asList(years)));
67 yAxis.setLabel("Price");
68 sbc.setTitle("HelloStackedBarChart");
69 // Populate Chart data
70 ObservableList<XYChart.Data> data = FXCollections.observableArrayList();
71 series1.getData().add(new XYChart.Data<String,Number>(years[0], 567));
72 series1.getData().add(new XYChart.Data<String,Number>(years[1], 1292));
73 series1.getData().add(new XYChart.Data<String,Number>(years[2], 2180));
74 series2.getData().add(new XYChart.Data<String,Number>(years[0], 956));
75 series2.getData().add(new XYChart.Data<String,Number>(years[1], 1665));
76 series2.getData().add(new XYChart.Data<String,Number>(years[2], 2450));
77 series3.getData().add(new XYChart.Data<String,Number>(years[0], 800));
78 series3.getData().add(new XYChart.Data<String,Number>(years[1], 1000));
79 series3.getData().add(new XYChart.Data<String,Number>(years[2], 2800));
80 return sbc;
81 }
82
83 @Test
84 public void testAddingAutoRangingCategoryAxis() {
85 CategoryAxis xAxis = new CategoryAxis();
86 String[] years = {"2007", "2008", "2009"};
87 NumberAxis yAxis = new NumberAxis();
88 ObservableList<StackedBarChart.Series> barChartData = FXCollections.observableArrayList(
89 new StackedBarChart.Series("Region 1", FXCollections.observableArrayList(
90 new StackedBarChart.Data(years[0], 567d),
91 new StackedBarChart.Data(years[1], 1292d),
92 new StackedBarChart.Data(years[2], 1292d)
93 )),
94 new StackedBarChart.Series("Region 2", FXCollections.observableArrayList(
95 new StackedBarChart.Data(years[0], 956),
96 new StackedBarChart.Data(years[1], 1665),
97 new StackedBarChart.Data(years[2], 2559)
98 ))
99 );
100 StackedBarChart sbc = new StackedBarChart(xAxis, yAxis, barChartData, 25.0d);
101 scene = new Scene(sbc,800,600);
102 stage = new Stage();
103 stage.setScene(scene);
104 stage.show();
105 pulse();
106 assertEquals(6, XYChartShim.getPlotChildren(sbc).size());
107 }
108
109 @Test
110 public void testSeriesAdd() {
111 startApp();
112 sbc.getData().addAll(series1, series2, series3);
113 pulse();
114 ObservableList<Node> childrenList = XYChartShim.getPlotChildren(sbc);
115 StringBuffer sb = new StringBuffer();
116 assertEquals(9, childrenList.size());
117 for (Node n : childrenList) {
118 assertEquals("chart-bar", n.getStyleClass().get(0));
119 }
120 // compute bounds for the first series
121 sb = computeBoundsString((Region)childrenList.get(0), (Region)childrenList.get(1),
122 (Region)childrenList.get(2));
123 assertEquals("10 479 234 36 254 432 234 83 499 375 234 140 ", sb.toString());
124
125 // compute bounds for the second series
126 // sb = computeBoundsString((Region)childrenList.get(3), (Region)childrenList.get(4),
127 // (Region)childrenList.get(5));
128 // assertEquals("10 421 236 62 256 328 236 108 501 220 236 158 ", sb.toString());
129 //
130 // // compute bounds for the third series
131 // sb = computeBoundsString((Region)childrenList.get(6), (Region)childrenList.get(7),
132 // (Region)childrenList.get(8));
133 // assertEquals("10 370 236 51 256 264 236 64 501 39 236 181 ", sb.toString());
134 }
135
136 @Test
137 public void testSeriesRemove() {
138 startApp();
139 sbc.getData().addAll(series1);
140 pulse();
141 assertEquals(3, XYChartShim.getPlotChildren(sbc).size());
142 if (!sbc.getData().isEmpty()) {
143 sbc.getData().remove(0);
144 pulse();
145 assertEquals(0, XYChartShim.getPlotChildren(sbc).size());
146 }
147 }
148
149 @Test
150 public void testDataItemAdd() {
151 startApp();
152 sbc.getData().addAll(series1);
153 pulse();
167 }
168
169 @Test
170 public void testDataItemRemove() {
171 startApp();
172 sbc.getData().addAll(series1);
173 pulse();
174 if (!sbc.getData().isEmpty()) {
175 series1.getData().remove(0);
176 pulse();
177 assertEquals(2, XYChartShim.getPlotChildren(sbc).size());
178 }
179 }
180
181 @Test @Ignore
182 public void testDataItemChange() {
183 startApp();
184 sbc.getData().addAll(series1);
185 pulse();
186 if (!sbc.getData().isEmpty()) {
187 StringBuffer sb = new StringBuffer();
188 ObservableList<Node> childrenList = XYChartShim.getPlotChildren(sbc);
189 // compute bounds for the first series before data change
190 sb = computeBoundsString((Region)childrenList.get(0), (Region)childrenList.get(1),
191 (Region)childrenList.get(2));
192 assertEquals("10 380 216 127 236 216 216 291 461 16 216 491 ", sb.toString());
193
194 XYChart.Data<String, Number> d = series1.getData().get(2);
195 d.setYValue(500d);
196 pulse();
197
198 // compute bounds for the first series after data change
199 sb = computeBoundsString((Region)childrenList.get(0), (Region)childrenList.get(1),
200 (Region)childrenList.get(2));
201 assertEquals("10 302 216 205 236 40 216 467 461 326 216 181 ", sb.toString());
202 }
203 }
204 }
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package test.javafx.scene.chart;
27
28
29 import org.junit.Test;
30 import static org.junit.Assert.assertEquals;
31 import javafx.collections.*;
32
33 import java.util.Arrays;
34
35 import javafx.scene.Node;
36 import javafx.scene.chart.CategoryAxis;
37 import javafx.scene.chart.Chart;
38 import javafx.scene.chart.NumberAxis;
39 import javafx.scene.chart.StackedBarChart;
40 import javafx.scene.chart.XYChart;
41 import javafx.scene.chart.XYChartShim;
42 import javafx.scene.layout.Region;
43 import org.junit.Ignore;
44
45 public class StackedBarChartTest extends XYChartTestBase {
46
47 final CategoryAxis xAxis = new CategoryAxis();
48 StackedBarChart<String,Number> sbc;
49 final XYChart.Series<String,Number> series1 = new XYChart.Series<String,Number>();
50 final XYChart.Series<String,Number> series2 = new XYChart.Series<String,Number>();
51 final XYChart.Series<String,Number> series3 = new XYChart.Series<String,Number>();
52 final String[] years = {"2007", "2008", "2009"};
53
54 protected Chart createChart() {
55 final NumberAxis yAxis = new NumberAxis();
56 sbc = new StackedBarChart<String,Number>(xAxis,yAxis);
57 xAxis.setLabel("Year");
58 xAxis.setCategories(FXCollections.<String>observableArrayList(Arrays.asList(years)));
59 yAxis.setLabel("Price");
60 sbc.setTitle("HelloStackedBarChart");
61 // Populate Chart data
62 ObservableList<XYChart.Data> data = FXCollections.observableArrayList();
63 series1.getData().add(new XYChart.Data<String,Number>(years[0], 567));
64 series1.getData().add(new XYChart.Data<String,Number>(years[1], 1292));
65 series1.getData().add(new XYChart.Data<String,Number>(years[2], 2180));
66 series2.getData().add(new XYChart.Data<String,Number>(years[0], 956));
67 series2.getData().add(new XYChart.Data<String,Number>(years[1], 1665));
68 series2.getData().add(new XYChart.Data<String,Number>(years[2], 2450));
69 series3.getData().add(new XYChart.Data<String,Number>(years[0], 800));
70 series3.getData().add(new XYChart.Data<String,Number>(years[1], 1000));
71 series3.getData().add(new XYChart.Data<String,Number>(years[2], 2800));
72 return sbc;
73 }
74
75 @Test
76 public void testAddingAutoRangingCategoryAxis() {
77 startApp();
78 ObservableList<XYChart.Series<String, Number>> barChartData = FXCollections.observableArrayList(
79 new StackedBarChart.Series("Region 1", FXCollections.observableArrayList(
80 new XYChart.Data<>(years[0], 567d),
81 new XYChart.Data<>(years[1], 1292d),
82 new XYChart.Data<>(years[2], 1292d)
83 )),
84 new StackedBarChart.Series("Region 2", FXCollections.observableArrayList(
85 new XYChart.Data<>(years[0], 956),
86 new XYChart.Data<>(years[1], 1665),
87 new XYChart.Data<>(years[2], 2559)
88 ))
89 );
90 xAxis.getCategories().clear();
91 xAxis.setAutoRanging(true);
92 sbc.getData().setAll(barChartData);
93 pulse();
94 assertEquals(6, XYChartShim.getPlotChildren(sbc).size());
95 }
96
97 @Test
98 public void testSeriesAdd() {
99 startApp();
100 sbc.getData().addAll(series1, series2, series3);
101 pulse();
102 ObservableList<Node> childrenList = XYChartShim.getPlotChildren(sbc);
103 assertEquals(9, childrenList.size());
104 for (Node n : childrenList) {
105 assertEquals("chart-bar", n.getStyleClass().get(0));
106 }
107 // compute bounds for the first series
108 String bounds = computeBoundsString((Region)childrenList.get(0), (Region)childrenList.get(1),
109 (Region)childrenList.get(2));
110 assertEquals("10 478 234 37 254 432 234 83 499 375 234 140 ", bounds);
111
112 // compute bounds for the second series
113 // bounds = computeBoundsString((Region)childrenList.get(3), (Region)childrenList.get(4),
114 // (Region)childrenList.get(5));
115 // assertEquals("10 421 236 62 256 328 236 108 501 220 236 158 ", bounds);
116 //
117 // // compute bounds for the third series
118 // bounds = computeBoundsString((Region)childrenList.get(6), (Region)childrenList.get(7),
119 // (Region)childrenList.get(8));
120 // assertEquals("10 370 236 51 256 264 236 64 501 39 236 181 ", bounds);
121 }
122
123 @Test
124 public void testSeriesRemove() {
125 startApp();
126 sbc.getData().addAll(series1);
127 pulse();
128 assertEquals(3, XYChartShim.getPlotChildren(sbc).size());
129 if (!sbc.getData().isEmpty()) {
130 sbc.getData().remove(0);
131 pulse();
132 assertEquals(0, XYChartShim.getPlotChildren(sbc).size());
133 }
134 }
135
136 @Test
137 public void testDataItemAdd() {
138 startApp();
139 sbc.getData().addAll(series1);
140 pulse();
154 }
155
156 @Test
157 public void testDataItemRemove() {
158 startApp();
159 sbc.getData().addAll(series1);
160 pulse();
161 if (!sbc.getData().isEmpty()) {
162 series1.getData().remove(0);
163 pulse();
164 assertEquals(2, XYChartShim.getPlotChildren(sbc).size());
165 }
166 }
167
168 @Test @Ignore
169 public void testDataItemChange() {
170 startApp();
171 sbc.getData().addAll(series1);
172 pulse();
173 if (!sbc.getData().isEmpty()) {
174 ObservableList<Node> childrenList = XYChartShim.getPlotChildren(sbc);
175 // compute bounds for the first series before data change
176 String bounds = computeBoundsString((Region)childrenList.get(0), (Region)childrenList.get(1),
177 (Region)childrenList.get(2));
178 assertEquals("10 380 216 127 236 216 216 291 461 16 216 491 ", bounds);
179
180 XYChart.Data<String, Number> d = series1.getData().get(2);
181 d.setYValue(500d);
182 pulse();
183
184 // compute bounds for the first series after data change
185 bounds = computeBoundsString((Region)childrenList.get(0), (Region)childrenList.get(1),
186 (Region)childrenList.get(2));
187 assertEquals("10 302 216 205 236 40 216 467 461 326 216 181 ", bounds);
188 }
189 }
190
191 @Override
192 ObservableList<XYChart.Series<?, ?>> createTestSeries() {
193 ObservableList<XYChart.Series<?, ?>> list = FXCollections.observableArrayList();
194 for (int i = 0; i != 10; i++) {
195 XYChart.Series<String, Number> series = new XYChart.Series<>();
196 series.getData().add(new XYChart.Data<>(Integer.toString(i*10), i*10));
197 series.getData().add(new XYChart.Data<>(Integer.toString(i*20), i*20));
198 series.getData().add(new XYChart.Data<>(Integer.toString(i*30), i*30));
199 list.add(series);
200 }
201 return list;
202 }
203
204 @Override
205 void checkSeriesStyleClasses(XYChart.Series<?, ?> series,
206 int seriesIndex, int colorIndex) {
207 // TODO: legend
208 }
209
210 @Override
211 void checkDataStyleClasses(XYChart.Data<?, ?> data,
212 int seriesIndex, int dataIndex, int colorIndex) {
213 Node bar = data.getNode();
214 checkStyleClass(bar, "series"+seriesIndex, "default-color"+colorIndex);
215 }
216
217 @Test
218 public void testSeriesRemoveAnimatedStyleClasses() {
219 startApp();
220 int nodesPerSeries = 3; // 3 bars
221 checkSeriesRemoveAnimatedStyleClasses(sbc, nodesPerSeries, 700);
222 }
223 }
|