Skip to content

ChartCanvasHelper

ClassChartGuruChartGuru
public static class ChartCanvasHelper

Helper methods for easily rendering charts to a Unity Canvas. Swift Charts-style simplicity for programmatic chart creation.

Usage:

// Render to full canvas
var chart = Chart.Create(data, item => new BarMark(
    PlottableValue.Value("Category", item.Name),
    PlottableValue.Value("Value", item.Amount)
)).Build();

ChartCanvasHelper.RenderToCanvas(chart, canvas);

// Or with specific position and size
ChartCanvasHelper.RenderToCanvas(chart, canvas, new Vector2(100, 100), new Vector2(400, 300));

public static ChartGraphic RenderToCanvas(Chart chart, Canvas canvas, string name = "Chart")

Renders a chart to the full size of the canvas. Creates a hidden GameObject with ChartGraphic that stretches to fill the canvas.

Parameters

Name Type Description
chart ChartGuru.Chart The chart to render.
canvas UnityEngine.Canvas The target canvas.
name System.String Optional name for the chart GameObject.

Returns

ChartGuru.ChartGraphic — The ChartGraphic component for further customization.

RenderToCanvas(Chart, Canvas, Vector2, Vector2, string)

Section titled “RenderToCanvas(Chart, Canvas, Vector2, Vector2, string)”
public static ChartGraphic RenderToCanvas(Chart chart, Canvas canvas, Vector2 position, Vector2 size, string name = "Chart")

Renders a chart at a specific position and size within the canvas.

Parameters

Name Type Description
chart ChartGuru.Chart The chart to render.
canvas UnityEngine.Canvas The target canvas.
position UnityEngine.Vector2 Position relative to canvas (bottom-left origin).
size UnityEngine.Vector2 Size of the chart in canvas units.
name System.String Optional name for the chart GameObject.

Returns

ChartGuru.ChartGraphic — The ChartGraphic component for further customization.

RenderToRectTransform(Chart, RectTransform, bool, string)

Section titled “RenderToRectTransform(Chart, RectTransform, bool, string)”
public static ChartGraphic RenderToRectTransform(Chart chart, RectTransform parent, bool stretchToFill = true, string name = "Chart")

Renders a chart to a RectTransform, useful for embedding charts in existing UI layouts.

Parameters

Name Type Description
chart ChartGuru.Chart The chart to render.
parent UnityEngine.RectTransform The parent RectTransform to attach the chart to.
stretchToFill System.Boolean If true, stretches to fill parent; otherwise uses parent’s size.
name System.String Optional name for the chart GameObject.

Returns

ChartGuru.ChartGraphic — The ChartGraphic component for further customization.

RenderMiniChart(Chart, RectTransform, Vector2, string)

Section titled “RenderMiniChart(Chart, RectTransform, Vector2, string)”
public static ChartGraphic RenderMiniChart(Chart chart, RectTransform parent, Vector2 size, string name = "MiniChart")

Creates a compact/mini chart (sparkline, indicator, etc.) rendered to a specific size. Ideal for embedding small charts inline with text or as KPI indicators.

Parameters

Name Type Description
chart ChartGuru.Chart The compact chart to render.
parent UnityEngine.RectTransform The parent RectTransform.
size UnityEngine.Vector2 Size of the mini chart.
name System.String Optional name for the chart GameObject.

Returns

ChartGuru.ChartGraphic — The ChartGraphic component for further customization.

public static void RemoveChart(ChartGraphic graphic)

Removes a chart from the canvas and cleans up its resources.

Parameters

Name Type Description
graphic ChartGuru.ChartGraphic The ChartGraphic to remove.
public static void ClearCanvas(Canvas canvas)

Removes all charts from a canvas.

Parameters

Name Type Description
canvas UnityEngine.Canvas The canvas to clear charts from.