Skip to content

Commit 64c503b

Browse files
committed
Showcase axis formatting
#233
1 parent 37511db commit 64c503b

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright © 2023 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.chartjs.model;
17+
18+
import java.util.function.Consumer;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
import software.xdev.chartjs.model.charts.BarChart;
23+
import software.xdev.chartjs.model.color.Color;
24+
import software.xdev.chartjs.model.data.BarData;
25+
import software.xdev.chartjs.model.dataset.BarDataset;
26+
import software.xdev.chartjs.model.javascript.JavaScriptFunction;
27+
import software.xdev.chartjs.model.options.BarOptions;
28+
import software.xdev.chartjs.model.options.scale.Scales;
29+
import software.xdev.chartjs.model.options.scale.cartesian.linear.LinearScaleOptions;
30+
import software.xdev.chartjs.model.options.scale.cartesian.linear.LinearTickOptions;
31+
32+
33+
@SuppressWarnings("java:S2699") // Done in custom method
34+
class ChartAxisFormatTest extends AbstractChartTest
35+
{
36+
@Test
37+
void axisTickCallback()
38+
{
39+
this.createScreenshotAndCompare(
40+
new BarChart(data(), options(l -> l.setCallback(new JavaScriptFunction("(label) => `$${label}`")))),
41+
this.getWebContainer(),
42+
"AxisTickCallback");
43+
}
44+
45+
@Test
46+
void format()
47+
{
48+
this.createScreenshotAndCompare(
49+
new BarChart(data(), options(l -> l.setFormat(new CurrencyFormatOptions("currency", "USD")))),
50+
this.getWebContainer(),
51+
"Format");
52+
}
53+
54+
public static class CurrencyFormatOptions
55+
{
56+
private String style;
57+
private String currency;
58+
59+
public CurrencyFormatOptions()
60+
{
61+
}
62+
63+
public CurrencyFormatOptions(final String style, final String currency)
64+
{
65+
this.style = style;
66+
this.currency = currency;
67+
}
68+
69+
public String getStyle()
70+
{
71+
return this.style;
72+
}
73+
74+
public void setStyle(final String style)
75+
{
76+
this.style = style;
77+
}
78+
79+
public String getCurrency()
80+
{
81+
return this.currency;
82+
}
83+
84+
public void setCurrency(final String currency)
85+
{
86+
this.currency = currency;
87+
}
88+
}
89+
90+
static BarData data()
91+
{
92+
final BarDataset dataset1 = new BarDataset()
93+
.setData(65)
94+
.addBackgroundColors(Color.RED);
95+
96+
return new BarData()
97+
.addLabels("First")
98+
.addDataset(dataset1);
99+
}
100+
101+
static BarOptions options(final Consumer<LinearTickOptions> configureTickOptions)
102+
{
103+
final LinearTickOptions linearTickOptions = new LinearTickOptions();
104+
configureTickOptions.accept(linearTickOptions);
105+
106+
final BarOptions options = new BarOptions().setAnimation(false);
107+
options
108+
.getScales()
109+
.addScale(Scales.ScaleAxis.Y, new LinearScaleOptions()
110+
.setTicks(linearTickOptions));
111+
return options;
112+
}
113+
}
12.1 KB
Loading
11.9 KB
Loading

0 commit comments

Comments
 (0)