Sparkline
Sparkline in UIToolbox is a chart as small as a line of text: a line, bars, or wins and losses, with no axes, grid or legend. Use it for a trend in a list row, a price in a market, the last ten matches of a player, the frame time in a HUD, or a column of a DataGrid.


Video tutorial: Sparkline. All videos: Video tutorials.
<tb:Sparkline values="3,5,4,7,6,8,7,9" />
<tb:Sparkline chart-type="Bar" values="4,-2,3,5,-1,-3,6" />
<tb:Sparkline chart-type="WinLoss" values="1,1,-1,1,-1,1" />
<tb:Sparkline
min-value="0"
max-value="100"
max-items="60"
show-last="false" />
var spark = root.Q<Sparkline>("fps");
spark.MaxItems = 60;
// each frame; the oldest value drops off
spark.AddValue(1f / Time.unscaledDeltaTime);
// a float[] or a List<float>
priceSpark.SetData(prices);
// above 0 is a win, below 0 a loss
streak.ChartType = SparklineType.WinLoss;
The chart fills the content box of the element. The default size is 80 × 20 pixels; set width and height in USS for another size.
Settings
| Setting (UXML) | Type | Default | Does |
|---|---|---|---|
chart-type |
SparklineType |
Line |
Line (a line through the values, with a dot on the last one), Area (the line with a soft fill under it, down to the bottom), Bar (a bar from 0 for each value) or WinLoss (a bar up for a value above 0 and a bar down for a value below 0, all of one height; 0 draws nothing). |
no-data-text |
string | "" | The text of the "No data" pill. Empty shows the translated "No data". See the No data section. |
show-no-data |
bool | true | Shows the faint wave and the pill while there are no values. |
values |
string | "" | The values as a comma-separated list, such as 3, 5, 4. A part that is not a number counts as 0. |
min-value, max-value |
float | NaN | The bottom and the top of the range. NaN fits the values. Bars always hold 0. WinLoss has no range. |
max-items |
int | 0 | When above 0, the oldest values drop off past this count. For live data. |
line-width |
float | 1.5 | The width of the line in pixels, at least 0.5. |
show-last |
bool | true | Draws a dot on the last value of a line. |
color |
Color |
clear | The color of the line and the bars. Clear takes --tb-sparkline-color. |
negative-color |
Color |
clear | The color of the bars below 0 and of the losses. Clear takes --tb-sparkline-negative. |
When all values are equal, the line is flat in the middle. A value outside a fixed range draws at the edge.
Properties and methods
| Member | Description |
|---|---|
ChartType, Values, MinValue, MaxValue, MaxItems, LineWidth, ShowLast, Color, NegativeColor |
The settings above. |
Data |
The values. Setting it does what SetData does, so a binding to a float[] or a List<float> works. Reading it gives a read-only list, without a copy. |
Count, GetValue(index) |
The number of values, and the value at an index. |
SetData(values) |
Replaces the values. An array or a list that is equal to the values changes nothing, so a call each frame with the same data is free. |
AddValue(value) |
Appends a value and drops the oldest values past MaxItems. |
ClearValues() |
Removes all the values. |
Range |
The bottom and the top that the values draw in, fitted or fixed. |
DisplayColor, DisplayNegativeColor |
The colors in use: Color and NegativeColor, or the USS colors when they are clear. |
Events
A sparkline has no events.
Mouse and keyboard
A sparkline has no input and does not take the focus. For a value under the pointer, put it in a button or a row with a tooltip, or use a Graph.
In a DataGrid
A DataGrid column of a float[], a List<float> or another list of floats shows a sparkline in each cell, with no setup:
<tb:DataGrid name="market">
<tb:DataGridColumn header="Good" binding-path="Name" width="100" />
<tb:DataGridColumn header="Week" binding-path="Prices" width="120" />
<tb:DataGridColumn
header="Streak"
binding-path="Results"
width="120"
sparkline-type="WinLoss" />
</tb:DataGrid>
sparkline-typeon the column sets the kind of the cells:Line(the default),BarorWinLoss.cell-type="Sparkline"sets the cell type by hand.- The cells never edit, and the column ignores
read-only. - A sort on the column sorts the rows by the last value of each list. The search and
ToCsvread the numbers, such as3,5,4. - The cell takes the width of the column and a height of 16 pixels.
No data
A sparkline with no values draws a faint wave and a centered pill with the text "No data", so an empty cell or tile never looks broken.
<tb:Sparkline />
<tb:Sparkline no-data-text="Offline" />
<tb:Sparkline show-no-data="false" />
| 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 sparkline is empty while it has no values.
SetDatawith no values,ClearValuesand an emptyvaluesattribute all make it empty. - 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 default size is 80 × 20 pixels, so a sparkline in a table cell shows the wave alone. Give it more room, as the StatTile does, to show the pill.
- 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.
Performance
- The control draws only when a value or a setting changes. At rest it does no work, and no scheduled item runs.
SetDatawith equal data,AddValuewith aMaxItemslimit, and the draw allocate nothing. ASetDatawith anIEnumerable<float>that is not a list makes one copy.- A sparkline is one mesh: one path for a line, one quad for each bar.
Styling
| Class | Element |
|---|---|
tb-sparkline |
The sparkline (80 × 20 pixels; does not shrink). |
tb-sparkline--empty |
The sparkline while it has no values. |
| Variable | On | Description |
|---|---|---|
--tb-sparkline-color |
tb-sparkline |
The color of the line and the bars. Default --tb-color-accent. |
--tb-sparkline-negative |
tb-sparkline |
The color of the bars below 0 and of the losses. Default --tb-color-danger. |
The two colors follow the theme and the style family of the workspace. For prices that are green when they rise:
.market-row .tb-sparkline {
width: 120px;
height: 24px;
--tb-sparkline-color: var(--tb-color-success);
}
In the UI Builder, a sparkline without values shows a sample line in place of the "No data" pill. The sample does not show in Play mode or in a build, and the UI Builder does not save it to the UXML file.
The SparklineDemo scene shows the week of a hero in sparklines with only their values, a market with a sparkline in each row and the prices of the selected row in a large graph, a raid HUD that adds a value every 50 ms, and three looks with the color attributes.