Graph
Graph in UIToolbox draws line, area, bar or scatter charts of its GraphSeries children. Each series is also its legend row. A CandleSeries child draws candles: the open, the high, the low and the close of each point, as for a price.


Video tutorial: Bar chart. All videos: Video tutorials.
<tb:Graph
name="usage"
chart-type="Line"
y-title="Usage %"
x-title="Time (s)"
y-min="0"
y-max="100"
style="height: 260px;">
<tb:GraphSeries text="CPU" max-items="60" />
<tb:GraphSeries text="Memory" color="#33C4F2" line-width="3" />
</tb:Graph>
<tb:Graph
y-title="Usage %"
y2-title="Disk MB/s"
y2-min="0"
animate="true">
<tb:GraphSeries text="CPU" values="30, 45, 40" />
<tb:GraphSeries
text="Disk"
values="120, 80, 200"
secondary-axis="true" />
</tb:Graph>
<tb:Graph chart-type="Bar" categories="Q1, Q2, Q3" show-values="true">
<tb:GraphSeries text="Sales" values="10, 20, 30" />
</tb:Graph>
<tb:Graph chart-type="Bar" stacked="true" categories="Q1, Q2, Q3">
<tb:GraphSeries text="Shop" values="10, 20, 30" />
<tb:GraphSeries text="Web" values="5, 15, 25" />
</tb:Graph>
var graph = root.Q<Graph>("usage");
var cpu = graph.GetSeries("CPU");
// drops the oldest point past MaxItems
cpu.AddPoint(time, load);
graph.DataPointSelected += (series, index) =>
Debug.Log(series == null ? "none"
: $"{series.Label} {series.GetDataPoint(index)}");
graph.SetAxisRange(GraphAxis.X, 0, 120);
// X is the index
graph.AddSeries("Disk").SetData(new float[] { 5, 8, 6 });
| Member | Description |
|---|---|
Series, AddSeries(label, color), GetSeries(label) |
The series (a copy of the list, so a loop can remove series), a new series at the end, and the first series with a label (null if none). |
Refresh() |
Updates the legend, the ticks and the drawing. The series call it after a change; call it after you add or remove a series element with Add or Remove. |
GraphSeries.Text (text), Color (color), LineWidth (line-width, 2), Hidden (hidden) |
The name, the color (clear takes the palette color by position), the line width in pixels, and whether the series is drawn. |
GraphSeries.Values (values), Data, SetData(xs, ys), SetData(ys), AddPoint(x, y), ClearPoints |
The data. values, Data and SetData(ys) use the index as X. Data is for a binding: it changes the points only when a value differs, and a bound array or list is compared in place. Reading Data gives a read-only view of the Y values, not a copy. |
GraphSeries.MaxItems (max-items), Points, GetDataPoint, DisplayColor |
A limit for AddPoint (0 means no limit), the points, one point, and the drawn color. |
GraphSeries.SecondaryAxis (secondary-axis) |
Plots the series on the right Y axis, with its own fitted or fixed range. The left axis then fits only the other series. |
GraphSeries.Reveal |
How far the series has grown in, 0 to 1. Below 1 only during an Animate grow-in. |
ChartType (chart-type) |
Line, Area, Bar or Scatter. Bars of the series sit side by side in each X slot and grow from 0. |
Stacked (stacked) |
Puts the bars of a Bar chart on top of each other. Off by default. See Stacked bars. |
XMin, XMax, YMin, YMax (x-min ...), SetAxisRange |
Fixed axis ends. NaN (the default) fits the data. An auto Y range rounds out to the ticks. |
XTitle, YTitle (x-title, y-title), SetAxisTitle, XFormat, YFormat |
The axis titles and the tick number formats ("0.##"). |
Y2Min, Y2Max, Y2Title, Y2Format (y2-min ...), GraphAxis.Y2 |
The right Y axis of the secondary series: fixed ends (NaN fits), the title at the top right, and the number format of its ticks, bar values and tooltips. The right axis line and tick labels show while a secondary series shows. |
SecondaryView |
The range of the secondary series now. A Y zoom or pan moves it by the same share of the range. |
Animate (animate) |
Grows each series up from the baseline when it gets new data (SetData, values), shows again, or the graph is attached. AddPoint does not grow it again. Off by default. |
Categories (categories) |
Comma-separated X tick labels by index, for bar charts. |
ShowGrid (show-grid), ShowLegend (show-legend), ShowValues (show-values) |
The grid lines, the legend, and a value label on each point or bar. |
Interactive, ZoomPan (zoom-pan) |
Hover, selection and legend clicks; the wheel, drag and double-click view changes. On by default. |
View, IsZoomed, SetView, ResetView, ViewChanged |
The shown data range. ResetView goes back to the fitted range. |
HoveredPoint, SelectedPoint, SelectPoint, PointAt, ToData |
The point under the pointer and the selected point, as (series, index); a hit test inside a candle, inside a bar, or within HIT_RADIUS (10 px) of a point; and a panel position in data units. |
DataPointHovered, DataPointSelected, DataPointAdded |
The events. The first two send (null, -1) when the point clears. |
Graph.NiceStep(range, target) |
The tick step: 1, 2 or 5 times a power of 10. |
- Click a point to select it. Click it again, or click empty space, to clear. The plot tooltip shows "Label: x, y".
- The graph takes focus. Left and Right (the arrow keys or the gamepad) move along the series, Escape clears, Home resets the view.
- With focus, the wheel zooms X around the pointer; Shift zooms Y. A drag pans. A double-click resets.
- A click on a legend row hides or shows the series. The view fits again.
- A series with no
Colortakes--tb-series-colorfrom its own style, for example.tb-graph__series.cpu { --tb-series-color: var(--tb-color-success); }. The order is theColorproperty, then--tb-series-color, then the theme chart color. - Series colors come from the theme variables
--tb-chart-1to--tb-chart-8(see Theme variables), withPieChart.Paletteas the fallback. - In the UI Builder, a graph without series shows two sample series in a dashed outline. The sample does not show in Play mode or in a build, and the UI Builder does not save it to the UXML file.
Stacked bars
Set Stacked on a Bar chart. At each X, the bar of the first series starts at 0, and the bar of each next series starts where the bar before it ends. Each bar is as wide as the whole group.
- The series stack in their order in the graph. A hidden series leaves the stack.
- The series stack by X. Give the series the same X values (
valuesandSetData(ys)do). - Values below 0 stack down from 0. Values above 0 stack up.
- An automatic Y range holds the whole stack. A fixed
YMaxmust be large enough for the sum. - The tooltip,
SelectedPoint,GetDataPointand the value label of a bar give the value of that series, not the end of the stack. Left and Right move along one series. - The series of the right axis have a stack of their own. It stands beside the stack of the left axis.
Line,AreaandScattercharts ignoreStacked.
No data
A graph with no data draws a faint wave in its plot and a centered pill with the text "No data".
<tb:Graph y-title="Usage %">
<tb:GraphSeries text="CPU" />
</tb:Graph>
<tb:Graph no-data-text="Waiting for the first run" />
| Member | Description |
|---|---|
NoDataText (no-data-text) |
The text of the pill. Empty (the default) shows the translated "No data" (the ToolboxText key tb.no-data, in nine languages), which follows a language change. |
ShowNoData (show-no-data) |
True (the default) shows the faint shape and the pill. False leaves an empty chart. |
- A graph is empty while no series has a point. A graph with series that have no data yet shows the legend and the pill together.
- The pill hides as soon as data arrives, and shows again when the data goes. No code runs per frame.
- The pill needs a chart of at least 72 × 36 pixels (for a graph and a Sankey chart, the plot). A smaller chart shows no pill.
- The pill sits in the plot, inside the axes, so the axis titles stay where they are.
- The classes are
tb-chart-no-data(the overlay, in--tb-color-text-muted) andtb-chart-no-data__pill(the pill). Restyle them for the look of your game.
Styling
| Class | Element |
|---|---|
tb-graph |
The graph (column, grows). --tb-graph-grid and --tb-graph-axis set the grid and axis line colors, and --tb-graph-candle-up and --tb-graph-candle-down the colors of rising and falling candles. tb-graph--panning is added during a drag. |
tb-graph--secondary |
Added while a secondary series shows. The chart then has 40 px right padding for the right tick labels. |
tb-graph__y-titles |
The row over the chart with the left and right Y titles. |
tb-graph__y-title, tb-graph__y2-title, tb-graph__x-title |
The axis titles (small muted text). The right Y title sits at the right end of its row. |
tb-graph__chart |
The area around the plot. Its padding makes room for the tick labels. |
tb-graph__plot |
The data area (accent border when the graph has focus). It clips the canvas. |
tb-graph__canvas |
The drawing surface, which fills the plot. |
tb-graph__tick-label, --x, --y, --y2 |
An axis tick label, below the plot, left of it, or right of it. |
tb-graph__value-label |
A value above a point or bar. |
tb-graph__legend |
The wrapping row of legend entries. |
tb-graph__series, --hidden |
A series and its legend entry; a hidden series (dimmed). |
tb-graph__series-swatch, -label |
The color bar and the name of an entry. |