Skip to content

Commit 4e83861

Browse files
committed
feat (call/factory): new factory method example with components
1 parent 5ac6744 commit 4e83861

23 files changed

Lines changed: 462 additions & 1 deletion

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Todos os exemplos no diretório `notebook` são preparados para o ambiente Jupyt
88
## Abrir branch específico em uma instância do [binderhub](https://github.com/jupyterhub/binderhub)
99

1010
* Última versão testada e estável:
11-
[![launch @ mybinder.org][badge-jupyterlab-mybinder-org]](https://mybinder.org/v2/gh/santanche/java2learn/v1.1.5)
11+
[![launch @ mybinder.org][badge-jupyterlab-mybinder-org]](https://mybinder.org/v2/gh/santanche/java2learn/v1.1.6)
1212

1313
* Última versão disponível:
1414
[![launch @ mybinder.org][badge-jupyterlab-mybinder-org]](https://mybinder.org/v2/gh/santanche/java2learn/master)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package pt.c04gui.s20chart;
2+
3+
import java.awt.Color;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
import org.knowm.xchart.SwingWrapper;
8+
import org.knowm.xchart.XYChart;
9+
import org.knowm.xchart.XYChartBuilder;
10+
import org.knowm.xchart.XYSeries;
11+
import org.knowm.xchart.XYSeries.XYSeriesRenderStyle;
12+
import org.knowm.xchart.style.markers.SeriesMarkers;
13+
14+
public class XYChart02 {
15+
16+
public static void main(String[] args) {
17+
XYChart02 exampleChart = new XYChart02();
18+
XYChart chart = exampleChart.getChart();
19+
new SwingWrapper<XYChart>(chart).displayChart();
20+
}
21+
22+
public XYChart getChart() {
23+
24+
// Create Chart
25+
XYChart chart = new XYChartBuilder().width(800).height(600).title("Line Chart").xAxisTitle("X").yAxisTitle("Y").build();
26+
27+
// Customize Chart
28+
chart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.Line);
29+
chart.getStyler().setChartTitleVisible(false);
30+
chart.getStyler().setLegendVisible(false);
31+
chart.getStyler().setAxisTitlesVisible(false);
32+
chart.getStyler().setXAxisDecimalPattern("0.0000000");
33+
34+
// Series
35+
int size = 10;
36+
List<Double> xData = new ArrayList<Double>();
37+
List<Double> yData = new ArrayList<Double>();
38+
for (int i = 0; i <= size; i++) {
39+
xData.add(((double) i) / 1000000);
40+
yData.add(10 * Math.exp(-i));
41+
}
42+
XYSeries series = chart.addSeries("10^(-x)", xData, yData);
43+
series.setMarkerColor(Color.RED);
44+
series.setMarker(SeriesMarkers.SQUARE);
45+
46+
return chart;
47+
}
48+
49+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package pt.c06patterns.factory.s10components;
2+
3+
import java.util.Scanner;
4+
5+
import pt.c06patterns.factory.s10components.chart.BarChartFactory;
6+
import pt.c06patterns.factory.s10components.chart.IBarChart;
7+
import pt.c06patterns.factory.s10components.chart.exception.PlotException;
8+
import pt.c06patterns.factory.s10components.sequence.IMathSequenceRatio;
9+
import pt.c06patterns.factory.s10components.sequence.MathSequenceFactory;
10+
11+
public class App05ChartSequence {
12+
public static void main(String args[]) {
13+
System.out.print("Progression type (arithmetic or geometric): ");
14+
Scanner keyboard = new Scanner(System.in);
15+
String sequenceType = keyboard.nextLine();
16+
17+
System.out.print("Chart type (console ou graphic): ");
18+
String chartType = keyboard.nextLine();
19+
20+
keyboard.close();
21+
22+
IMathSequenceRatio gp = MathSequenceFactory.createSequenceRatio(sequenceType);
23+
gp.setInitial(1);
24+
gp.setRatio(2);
25+
26+
try {
27+
IBarChart bcg = BarChartFactory.create(chartType);
28+
bcg.setFilled(true);
29+
bcg.setN(7);
30+
bcg.connect(gp);
31+
bcg.plot();
32+
} catch (PlotException error) {
33+
System.err.println("*** Error: " + error.getMessage());
34+
}
35+
}
36+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package pt.c06patterns.factory.s10components.chart;
2+
3+
import pt.c06patterns.factory.s10components.sequence.ISequence;
4+
5+
public abstract class BarChart implements IBarChart {
6+
protected boolean filled;
7+
protected int n;
8+
9+
protected ISequence sequence;
10+
11+
public BarChart() {
12+
filled = true;
13+
n = 3;
14+
}
15+
16+
public boolean isFilled() {
17+
return filled;
18+
}
19+
20+
public void setFilled(boolean filled) {
21+
this.filled = filled;
22+
}
23+
24+
public int getN() {
25+
return n;
26+
}
27+
28+
public void setN(int n) {
29+
this.n = n;
30+
}
31+
32+
public void connect(ISequence sequence) {
33+
this.sequence = sequence;
34+
}
35+
36+
public abstract void plot();
37+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package pt.c06patterns.factory.s10components.chart;
2+
3+
public class BarChartFactory {
4+
5+
public static IBarChart create(String type) {
6+
IBarChart chart = null;
7+
8+
if (type.equalsIgnoreCase("console"))
9+
chart = new ConsoleBarChart();
10+
else if (type.equalsIgnoreCase("graphic"))
11+
chart = new GraphicBarChart();
12+
13+
return chart;
14+
}
15+
16+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package pt.c06patterns.factory.s10components.chart;
2+
3+
import pt.c06patterns.factory.s10components.chart.exception.InvalidBlankChar;
4+
import pt.c06patterns.factory.s10components.chart.exception.UnsupportedNegativeNumber;
5+
6+
public class ConsoleBarChart extends BarChart {
7+
private char character;
8+
9+
public ConsoleBarChart() {
10+
super();
11+
character = '*';
12+
}
13+
14+
public char getCharacter() {
15+
return character;
16+
}
17+
18+
public void setCharacter(char character) throws InvalidBlankChar {
19+
if (character == ' ')
20+
throw new InvalidBlankChar("the chart does not support blank character");
21+
this.character = character;
22+
}
23+
24+
public void plot() {
25+
if (sequence != null) {
26+
int value = sequence.first();
27+
for (int s = 1; s <= n; s++) {
28+
if (value > 0) {
29+
for (int v = 1; v < value; v++)
30+
System.out.print((filled) ? character : ' ');
31+
System.out.print(character);
32+
} else if (value < 0) {
33+
System.out.println("?");
34+
throw new UnsupportedNegativeNumber("the chart does not support a negative number");
35+
}
36+
System.out.println();
37+
value = sequence.next();
38+
}
39+
}
40+
}
41+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package pt.c06patterns.factory.s10components.chart;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.knowm.xchart.CategoryChart;
7+
import org.knowm.xchart.CategoryChartBuilder;
8+
import org.knowm.xchart.SwingWrapper;
9+
10+
public class GraphicBarChart extends BarChart {
11+
12+
public void plot() {
13+
// Create Chart
14+
CategoryChart chart =
15+
new CategoryChartBuilder().width(800).height(600).
16+
title("Chart").xAxisTitle("X").yAxisTitle("Y").build();
17+
18+
// Customize Chart
19+
chart.getStyler().setChartTitleVisible(false);
20+
chart.getStyler().setLegendVisible(false);
21+
chart.getStyler().setAxisTitlesVisible(false);
22+
chart.getStyler().setXAxisDecimalPattern("0");
23+
24+
// Series
25+
List<Integer> xData = new ArrayList<Integer>();
26+
List<Integer> yData = new ArrayList<Integer>();
27+
if (sequence != null) {
28+
int value = sequence.first();
29+
for (int s = 1; s <= n; s++) {
30+
xData.add(s);
31+
yData.add(value);
32+
value = sequence.next();
33+
}
34+
}
35+
chart.addSeries("series", xData, yData);
36+
37+
new SwingWrapper<CategoryChart>(chart).displayChart();
38+
}
39+
40+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package pt.c06patterns.factory.s10components.chart;
2+
3+
public interface IBarChart
4+
extends IChart, IRSequence, IBarChartProperties {
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package pt.c06patterns.factory.s10components.chart;
2+
3+
public interface IBarChartProperties {
4+
public boolean isFilled();
5+
public void setFilled(boolean filled);
6+
7+
public int getN();
8+
public void setN(int n);
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package pt.c06patterns.factory.s10components.chart;
2+
3+
public interface IChart {
4+
public void plot();
5+
}

0 commit comments

Comments
 (0)