FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Refactor chart base classes · patternfly-java/patternfly-java@601a668 · GitHub

Commit 601a668

Browse files
committed
Refactor chart base classes
1 parent fb7b8bb commit 601a668

17 files changed

Lines changed: 357 additions & 220 deletions

File tree

‎charts/npm/src/charts.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
17+
// Base class for J2CL JsInterop base class
18+
class ChartElement extends HTMLElement {
19+
}
20+
window.ChartElement = ChartElement;
21+
1622
import './components/pfj-chart-donut.js';
1723
import './components/pfj-chart-donut-utilization.js';
1824
import './components/pfj-chart-donut-threshold.js';

‎charts/npm/src/react-wrapper.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ export class ReactWrapperElement extends LitElement {
9595
this._height = undefined; // number
9696
this._labels = undefined; // (data: any) => string
9797
this._legendAllowWrap = undefined; // boolean
98-
this._legendData = undefined; // string[]
98+
this._legendData = undefined; // { name?: string; symbol?: { fill?: string; type?: string; }; }[]
9999
this._legendOrientation = undefined; // string
100100
this._legendPosition = undefined; // string
101-
this._padding = undefined; // { top?: number, bottom?: number, left?: number, right?: number }
101+
this._padding = undefined; // { top?: number; bottom?: number; left?: number; right?: number }
102102
this._subTitle = undefined; // string
103103
this._subTitlePosition = undefined; // string
104104
this._themeColor = undefined; // string

‎charts/src/main/java/org/patternfly/chart/BaseChart.java‎

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.patternfly.chart;
1717

18+
import java.util.function.Function;
19+
1820
import org.jboss.elemento.ElementAttributeMethods;
1921
import org.jboss.elemento.ElementClassListMethods;
2022
import org.jboss.elemento.ElementConsumerMethods;
@@ -27,12 +29,12 @@
2729
import org.jboss.elemento.HTMLElementStyleMethods;
2830
import org.jboss.elemento.HTMLElementVisibilityMethods;
2931
import org.jboss.elemento.TypedBuilder;
30-
31-
import elemental2.dom.HTMLElement;
32+
import elemental2.core.JsArray;
33+
import jsinterop.base.Js;
3234

3335
import static java.util.Objects.requireNonNull;
3436

35-
public abstract class BaseChart<E extends HTMLElement, B extends TypedBuilder<E, B>> implements
37+
public abstract class BaseChart<E extends ChartElement, B extends TypedBuilder<E, B>> implements
3638
Chart,
3739
ElementAttributeMethods<E, B>,
3840
ElementClassListMethods<E, B>,
@@ -46,12 +48,14 @@ public abstract class BaseChart<E extends HTMLElement, B extends TypedBuilder<E,
4648
HTMLElementStyleMethods<E, B>,
4749
HTMLElementVisibilityMethods<E, B> {
4850

51+
// ------------------------------------------------------ instance
52+
4953
private final ChartType chartType;
5054
private final E element;
5155

5256
protected BaseChart(ChartType chartType, E element) {
5357
this.chartType = requireNonNull(chartType, "chart type required");
54-
this.element = requireNonNull(element, "element required");
58+
this.element = Js.uncheckedCast(requireNonNull(element, "element required"));
5559
Ouia.component(element, chartType);
5660
}
5761

@@ -64,4 +68,93 @@ public ChartType chartType() {
6468
public E element() {
6569
return element;
6670
}
71+
72+
// ------------------------------------------------------ builder
73+
74+
public B height(int height) {
75+
element().height = height;
76+
return that();
77+
}
78+
79+
public B labels(Function<Data, String> labels) {
80+
// There's a lot more in 'data', but we just want the 'datum' property
81+
element().labels = (data -> labels.apply(Js.cast(data.asPropertyMap().get("datum"))));
82+
return that();
83+
}
84+
85+
public B legend(String legend, String... moreLegends) {
86+
JsArray<LegendData> array = new JsArray<>();
87+
LegendData ld = new LegendData();
88+
ld.name = legend;
89+
array.push(ld);
90+
if (moreLegends != null) {
91+
for (String l : moreLegends) {
92+
ld = new LegendData();
93+
ld.name = l;
94+
array.push(ld);
95+
}
96+
}
97+
element().legendData = array;
98+
return that();
99+
}
100+
101+
public B legend(LegendData legend, LegendData... moreLegends) {
102+
JsArray<LegendData> array = new JsArray<>();
103+
array.push(legend);
104+
array.push(moreLegends);
105+
element().legendData = array;
106+
return that();
107+
}
108+
109+
public B legendOrientation(LegendOrientation orientation) {
110+
element().legendOrientation = orientation.name().toLowerCase();
111+
return that();
112+
}
113+
114+
public B legendPosition(LegendPosition position) {
115+
element().legendPosition = position.name().toLowerCase();
116+
return that();
117+
}
118+
119+
public B padding(Padding padding) {
120+
element().padding = padding;
121+
return that();
122+
}
123+
124+
public B subTitle(String subTitle) {
125+
element().subtitle = subTitle;
126+
return that();
127+
}
128+
129+
public B subTitlePosition(SubTitlePosition position) {
130+
element().subTitlePosition = position.name().toLowerCase();
131+
return that();
132+
}
133+
134+
public B themeColor(ChartThemeColor themeColor) {
135+
element().themeColor = themeColor.color;
136+
return that();
137+
}
138+
139+
public B title(String title) {
140+
element().title = title;
141+
return that();
142+
}
143+
144+
public B width(int width) {
145+
element().width = width;
146+
return that();
147+
}
148+
149+
// ------------------------------------------------------ aria
150+
151+
public B ariaTitle(String ariaTitle) {
152+
element().setAttribute("aria-title", ariaTitle);
153+
return that();
154+
}
155+
156+
public B ariaDesc(String ariaDesc) {
157+
element().setAttribute("aria-desc", ariaDesc);
158+
return that();
159+
}
67160
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.patternfly.chart;
2+
3+
import elemental2.core.JsArray;
4+
import elemental2.dom.HTMLElement;
5+
import jsinterop.annotations.JsPackage;
6+
import jsinterop.annotations.JsType;
7+
8+
@JsType(isNative = true, namespace = JsPackage.GLOBAL)
9+
public class ChartElement extends HTMLElement {
10+
11+
public JsArray<String> categories;
12+
public double height;
13+
public LabelsFn labels;
14+
public boolean legendAllowWrap;
15+
public JsArray<LegendData> legendData;
16+
public String legendOrientation;
17+
public String legendPosition;
18+
public Padding padding;
19+
public String subtitle;
20+
public String subTitlePosition;
21+
public String themeColor;
22+
public String title;
23+
public double width;
24+
}

‎charts/src/main/java/org/patternfly/chart/ChartType.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
public enum ChartType {
1919

2020
Donut("dnt", "PF6/Donut"),
21+
22+
DonutUtilization("dntu", "PF6/DonutUtilization"),
23+
24+
DonutThreshold("dntt", "PF6/DonutThreshold"),
2125
;
2226

2327
public final String id;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.patternfly.chart;
2+
3+
public enum LegendPosition {
4+
5+
bottom, right
6+
}

‎charts/src/main/java/org/patternfly/chart/Padding.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
@JsType(isNative = true, namespace = JsPackage.GLOBAL, name = "Object")
88
public class Padding {
99

10+
@JsOverlay
11+
public static Padding padding(double top, double right, double bottom, double left) {
12+
Padding padding = new Padding();
13+
padding.top = top;
14+
padding.right = right;
15+
padding.bottom = bottom;
16+
padding.left = left;
17+
return padding;
18+
}
19+
1020
public double top;
1121
public double right;
1222
public double bottom;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.patternfly.chart;
2+
3+
import jsinterop.annotations.JsOverlay;
4+
import jsinterop.annotations.JsPackage;
5+
import jsinterop.annotations.JsType;
6+
7+
@JsType(isNative = true, namespace = JsPackage.GLOBAL, name = "Object")
8+
public class Threshold {
9+
10+
@JsOverlay
11+
public static Threshold threshold(double value) {
12+
Threshold threshold = new Threshold();
13+
threshold.value = value;
14+
return threshold;
15+
}
16+
17+
@JsOverlay
18+
public static Threshold threshold(double value, String color) {
19+
Threshold threshold = new Threshold();
20+
threshold.value = value;
21+
threshold.color = color;
22+
return threshold;
23+
}
24+
25+
public double value;
26+
public String color;
27+
}

‎charts/src/main/java/org/patternfly/chart/donut/Donut.java‎

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,9 @@
1515
*/
1616
package org.patternfly.chart.donut;
1717

18-
import java.util.function.Function;
19-
2018
import org.patternfly.chart.BaseChart;
21-
import org.patternfly.chart.ChartThemeColor;
2219
import org.patternfly.chart.ChartType;
2320
import org.patternfly.chart.Data;
24-
import org.patternfly.chart.LegendData;
25-
import org.patternfly.chart.LegendOrientation;
26-
import org.patternfly.chart.Padding;
27-
import org.patternfly.chart.SubTitlePosition;
2821
import elemental2.core.JsArray;
2922
import jsinterop.base.Js;
3023

@@ -41,7 +34,7 @@ public static Donut donut() {
4134
// ------------------------------------------------------ instance
4235

4336
Donut() {
44-
super(ChartType.Donut, createHtmlElement("pfj-chart-donut", DonutElement.class));
37+
super(ChartType.Donut, Js.uncheckedCast(createHtmlElement("pfj-chart-donut", DonutElement.class)));
4538
}
4639

4740
// ------------------------------------------------------ builder
@@ -54,92 +47,8 @@ public Donut data(Data data, Data... moreData) {
5447
return this;
5548
}
5649

57-
public Donut height(int height) {
58-
element().setAttribute("height", height);
59-
return this;
60-
}
61-
62-
public Donut labels(Function<Data, String> labels) {
63-
// There's a lot more in 'data', but we just want the 'datum' property
64-
element().labels = (data -> labels.apply(Js.cast(data.asPropertyMap().get("datum"))));
65-
return this;
66-
}
67-
68-
public Donut legend(String legend, String... moreLegends) {
69-
JsArray<LegendData> array = new JsArray<>();
70-
LegendData ld = new LegendData();
71-
ld.name = legend;
72-
array.push(ld);
73-
if (moreLegends != null) {
74-
for (String more : moreLegends) {
75-
ld = new LegendData();
76-
ld.name = more;
77-
array.push(ld);
78-
}
79-
}
80-
element().legendData = array;
81-
return this;
82-
}
83-
84-
public Donut legendOrientation(LegendOrientation orientation) {
85-
element().setAttribute("legend-orientation", orientation.name().toLowerCase());
86-
return this;
87-
}
88-
89-
public Donut legendPosition(SubTitlePosition position) {
90-
element().setAttribute("legend-position", position.name().toLowerCase());
91-
return this;
92-
}
93-
94-
public Donut padding(int top, int right, int bottom, int left) {
95-
Padding padding = new Padding();
96-
padding.top = top;
97-
padding.right = right;
98-
padding.bottom = bottom;
99-
padding.left = left;
100-
element().padding = padding;
101-
return this;
102-
}
103-
104-
public Donut subTitle(String subTitle) {
105-
element().setAttribute("sub-title", subTitle);
106-
return this;
107-
}
108-
109-
public Donut subTitlePosition(SubTitlePosition position) {
110-
element().setAttribute("sub-title-position", position.name().toLowerCase());
111-
return this;
112-
}
113-
114-
public Donut themeColor(ChartThemeColor themeColor) {
115-
element().setAttribute("theme-color", themeColor.color);
116-
return this;
117-
}
118-
119-
public Donut title(String title) {
120-
element().setAttribute("title", title);
121-
return this;
122-
}
123-
124-
public Donut width(int width) {
125-
element().setAttribute("width", width);
126-
return this;
127-
}
128-
12950
@Override
13051
public Donut that() {
13152
return this;
13253
}
133-
134-
// ------------------------------------------------------ aria
135-
136-
public Donut ariaTitle(String ariaTitle) {
137-
element().setAttribute("aria-title", ariaTitle);
138-
return this;
139-
}
140-
141-
public Donut ariaDesc(String ariaDesc) {
142-
element().setAttribute("aria-desc", ariaDesc);
143-
return this;
144-
}
14554
}
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
package org.patternfly.chart.donut;
22

3+
import org.patternfly.chart.ChartElement;
34
import org.patternfly.chart.Data;
4-
import org.patternfly.chart.LabelsFn;
5-
import org.patternfly.chart.LegendData;
6-
import org.patternfly.chart.Padding;
75
import elemental2.core.JsArray;
8-
import elemental2.dom.HTMLElement;
96
import jsinterop.annotations.JsPackage;
107
import jsinterop.annotations.JsType;
118

129
@JsType(isNative = true, namespace = JsPackage.GLOBAL, name = "HTMLElement")
13-
public class DonutElement extends HTMLElement {
10+
public class DonutElement extends ChartElement {
1411

1512
public JsArray<Data> data;
16-
public LabelsFn labels;
17-
public JsArray<LegendData> legendData;
18-
public Padding padding;
1913
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL