Charts
A chart node holds the data of a chart: its axes, its series and their scale. How the chart looks is yours: like a control, a chart is an abstract class that a skin of yours draws.
You will learn
- How to draw a line chart from its axes and series
- How to update the data of a chart
- How to draw a radar chart
- What a chart shows while it has no data
Draw a line chart
Section titled “Draw a line chart”A ChartNode holds an X axis with its labels, a Y axis and named series of values.
A skin overrides draw and reads them:
public class ChartsLineChartNode extends ChartNode {
private static final Color ELEMENT = new Color(38, 43, 53).lock(); private static final Color MUTED = new Color(139, 147, 163).lock(); private static final Color ACCENT = new Color(91, 140, 255).lock();
protected ChartsLineChartNode(final double x, final double y, final double width, final double height) { super(x, y, width, height); }
public static ChartsLineChartNode create(final double x, final double y, final double width, final double height) { return new ChartsLineChartNode(x, y, width, height); }
@Override public void draw(final double mouseX, final double mouseY) { final TextInfo info = TextInfo.create(ExampleFont.REGULAR, 18F).color(ChartsLineChartNode.MUTED); final double min = super.getMin().doubleValue(); final double max = super.getMax().doubleValue(); final double left = super.getX() + 60D; final double top = super.getY() + 15D; final double width = super.getWidth() - 85D; final double height = super.getHeight() - 60D; final double step = super.getLabels().size() > 1 ? width / (super.getLabels().size() - 1) : 0D;
final String high = super.getYAxis().format(Math.round(max)); final String low = super.getYAxis().format(Math.round(min));
final Color background = ChartsLineChartNode.ELEMENT; DrawUtils.SHAPE.drawRect(super.getX(), super.getY(), super.getWidth(), super.getHeight(), background); DrawUtils.TEXT.drawText(super.getX() + 8D, top, high, info, Align.START, Align.CENTER); DrawUtils.TEXT.drawText(super.getX() + 8D, top + height, low, info, Align.START, Align.CENTER);
int column = 0; for (final String label : super.getLabels()) { DrawUtils.TEXT.drawText(left + step * column, top + height + 12D, label, info, Align.CENTER, Align.START); column++; }
int series = 0; for (final ChartData data : super.getDataMap().values()) { final Color color = series % 2 == 0 ? ChartsLineChartNode.ACCENT : ChartsLineChartNode.MUTED; Point previous = null; column = 0; for (final String label : super.getLabels()) { if (data.has(label)) { final double ratio = (data.get(label).doubleValue() - min) / (max - min); final Point point = Point.of(left + step * column, top + height - height * ratio); if (previous != null) { DrawUtils.SHAPE.drawLine(color, 2F, previous, point); } DrawUtils.SHAPE.drawCircle(point.getX(), point.getY(), color, 4D); previous = point; } column++; } series++; } }
}Full file: ChartsLineChartNode.java
The skin is a custom node that draws with DrawUtils (Drawing shapes and text teaches the calls).
Give the chart its axes, then its series:
final ChartsLineChartNode chart = ChartsLineChartNode.create(100, 100, 400, 240).xAxis(ChartAxis.x("day", "Mon", "Tue", "Wed", "Thu", "Fri")).yAxis(ChartAxis.y("visits").suffix(" k")).data("Visits", ChartData.create().add("Mon", 2).add("Tue", 6).add("Wed", 4).add("Thu", 8).add("Fri", 7)).data("Orders", ChartData.create().add("Mon", 1).add("Tue", 2).add("Wed", 3).add("Thu", 3).add("Fri", 5)).attach(this);
ChartNode.ChartAxis creates the axes.
ChartAxis.x(name, labels...) creates the X axis: its labels are the columns of the chart, in this order.
ChartAxis.y(name) creates the Y axis; prefix(...) and suffix(...) decorate the values that format(value) returns, here 8 k.
data(name, data) adds a series, or replaces the series of that name in place; call it after xAxis(...), which holds the series.
A ChartNode.ChartData maps each label to a number, and a label it does not hold has no value there: the skin decides what to draw.
A skin reads the chart through its getters, which ChartNode lists: getLabels() in order, getDataMap() by series, getMin(), getMax() and getAverage() over every series or for one, and the axes for format(value).
getAverage() over every series is the average of the averages of the series.
When every value is the same, the scale stretches to 0, so that the chart keeps a height.
A chart throws when it is attached without its two axes.
Update the data
Section titled “Update the data”JOID calls draw on every frame, so the chart shows a change of its data on the next frame:
RectNode.create(100, 380, 200, 50).color(ChartsUI.ACCENT).label(Text.create("Next week", label)).onClick(event -> { week.increment(); chart .data("Visits", ChartData.create().add("Mon", 3).add("Tue", 5).add("Wed", 7).add("Thu", 6).add("Fri", 9)) .removeData("Orders");}).attach(this);data(name, data)replaces a whole series, andremoveData(name)removes one.add(label, value)andremove(label)change one value of aChartNode.ChartDatayou kept.ChartData.create(map)keeps your map itself, so a change you make to the map shows too.labelSet(labels...)of the X axis changes the columns.
The axes take a Supplier too, so a chart can follow a signal that picks its axes (Signals explains reactive setters).
Draw a radar chart
Section titled “Draw a radar chart”A RadarChartNode holds a list of values, each with its label, drawn as the axes of a radar:
public class ChartsRadarChartNode extends RadarChartNode<RadarChartData> {
private static final Color ELEMENT = new Color(38, 43, 53).lock(); private static final Color MUTED = new Color(139, 147, 163).lock(); private static final Color ACCENT = new Color(91, 140, 255).lock();
protected ChartsRadarChartNode(final double x, final double y, final double width, final double height) { super(x, y, width, height); }
public static ChartsRadarChartNode create(final double x, final double y, final double size) { return new ChartsRadarChartNode(x, y, size, size); }
@Override public void draw(final double mouseX, final double mouseY) { final TextInfo info = TextInfo.create(ExampleFont.REGULAR, 18F).color(ChartsRadarChartNode.MUTED); final int size = super.getDataList().size(); final Point[] outline = new Point[size]; final Point[] values = new Point[size]; for (int i = 0; i < size; i++) { final RadarChartData data = super.getDataList().get(i); final double ratio = data.getValue().doubleValue() / super.getMax().doubleValue(); outline[i] = this.getPoint(i, super.dw(2D)); values[i] = this.getPoint(i, super.dw(2D) * ratio); }
DrawUtils.SHAPE.drawPolygon(ChartsRadarChartNode.ELEMENT, outline); DrawUtils.SHAPE.drawPolygon(ChartsRadarChartNode.ACCENT.withAlpha(0.5F), values); for (int i = 0; i < size; i++) { final Point label = this.getPoint(i, super.dw(2D) + 32D); final String text = super.getDataList().get(i).getLabel(); DrawUtils.TEXT.drawText(label.getX(), label.getY(), text, info, Align.CENTER, Align.CENTER); } }
private Point getPoint(final int index, final double radius) { final double angle = index * 2D * Math.PI / super.getDataList().size() - Math.PI / 2D; final double centerX = super.getX() + super.dw(2D); final double centerY = super.getY() + super.dh(2D); return Point.of(centerX + radius * Math.cos(angle), centerY + radius * Math.sin(angle)); }
}Full file: ChartsRadarChartNode.java
ChartsRadarChartNode.create(640, 120, 200).data(RadarChartData.create("Speed", 4)).data(RadarChartData.create("Power").value(() -> 2 + week.get() % 4)).data(RadarChartData.create("Range", 3)).data(RadarChartData.create("Grip", 5)).data(RadarChartData.create("Armor", 2)).attach(this);
data(...) adds an axis, removeData(...) removes one and clear() removes them all; getMin(), getMax() and getAverage() cover every value.
A RadarChartNode.RadarChartData takes its label and its value as a value or as a Supplier, read each time the skin calls getValue() and getLabel().
Here the Power axis reads a signal: the click of the previous section lengthens it.
The type parameter of RadarChartNode is the class of its data: extend RadarChartData to give each axis more fields, such as a color, and read them in the skin.
Know what a chart shows without data
Section titled “Know what a chart shows without data”A chart waits for its data before it draws.
While it waits, it draws its skeleton, a pulsing grey block, instead of calling draw (Rebuilding the tree explains skeletons):
- A
ChartNodewaits while it has no series, or while one of its series is empty. - A
RadarChartNodewaits while it has fewer than 3 axes, or while one of them has no value;clear()of aRadarChartDataempties its value.
So a skin never draws an empty chart, and needs no test for one.
- A chart is an abstract node: a skin overrides
drawand reads the axes, the series and the scale. - A
ChartNodetakes an X axis with its labels, a Y axis that formats the values, then series by name. - The chart reads its data on every frame: replace a series, remove one, or change the values you kept.
- A
RadarChartNodedraws one axis perRadarChartData, whose value can follow aSupplier. - A chart draws its skeleton until it has data to draw.
Try it
Section titled “Try it”An average line. Draw a dashed horizontal line at the average of every series.
Solution
At the end of draw:
final double average = top + height - height * (super.getAverage().doubleValue() - min) / (max - min);final Point start = Point.of(left, average);DrawUtils.SHAPE.drawDashedLine(ChartsLineChartNode.MUTED, 8, 1F, start, Point.of(left + width, average));A missing day. Leave Wednesday out of the Orders series, and see what the chart draws.
Solution
Remove .add("Wed", 3) from the series.
The skin skips a label that the series does not hold, so the line goes straight from Tuesday to Thursday.
