UsoUIElements 0.1.0
Data Centric implimentation of Unity's UI Elements (UI Toolkit)
Loading...
Searching...
No Matches
GWG.UsoUIElements.CustomElements.UsoPieChart Class Reference

A custom pie chart visualization control that extends Unity's VisualElement to provide dynamic data-driven circular chart rendering. Creates interactive pie charts based on supplied percentage values and colors, with real-time update capabilities and customizable appearance. More...

Inheritance diagram for GWG.UsoUIElements.CustomElements.UsoPieChart:

Public Member Functions

 UsoPieChart ()
 Initializes a new instance of the UsoPieChart class with default settings and visual content generation. Sets up the chart for rendering and establishes the drawing callback for dynamic visual updates.
void UpdateChartData (List< PercentageColorData > newData)
 Updates the pie chart's data with a new collection of percentage and color information. Replaces the current chart data and triggers an immediate visual refresh to display the changes.

Public Attributes

List< PercentageColorDatapercentageColorData
 Collection of percentage and color data that defines the pie chart segments. Each entry represents a slice of the pie with its proportional size and display color.

Properties

float diameter [get]
 Gets the diameter of the pie chart calculated as twice the radius. This read-only property provides the total width and height of the circular chart area.
float radius [get, set]
 Gets or sets the radius of the pie chart in pixels. Automatically updates the chart's visual dimensions and triggers a repaint when changed.
float value [get, set]
 Gets or sets a legacy value property maintained for potential future functionality. Triggers a visual repaint when changed to ensure chart consistency.

Private Member Functions

void DrawCanvas (MeshGenerationContext ctx)
 Renders the pie chart visual content using Unity's 2D painter system within the provided mesh generation context. Creates individual pie slices based on the percentageColorData collection with appropriate colors and proportions.

Private Attributes

VisualElement m_Chart
 Visual element reference for the chart container. Used for managing chart-specific styling and layout properties.
float m_Radius = 100.0f
 Private backing field for the pie chart's radius in pixels. Controls the size of the circular chart area and affects the overall visual scale.
float m_Value = 40.0f
 Private backing field for a legacy value property. Maintained for potential future functionality or backward compatibility.

Detailed Description

A custom pie chart visualization control that extends Unity's VisualElement to provide dynamic data-driven circular chart rendering. Creates interactive pie charts based on supplied percentage values and colors, with real-time update capabilities and customizable appearance.

This control provides comprehensive pie chart functionality within the USO UI framework's custom elements collection. It uses Unity's 2D painter system for high-performance rendering of circular chart segments with customizable colors and percentages. The chart automatically handles percentage-to-angle conversions, supports dynamic data updates, and provides smooth visual rendering through Unity's mesh generation context. The control is designed for displaying proportional data relationships in an intuitive circular format, commonly used for statistics, progress indicators, and data visualization scenarios.

Usage Example:

data.Percentage = 40.0f;
data.Color = new Color32(182, 235, 122, 255);
chart.UpdateChartData(new List<PercentageColorData> { data });
A serializable data structure that represents a single segment of a pie chart with its percentage val...
Definition UsoPieChart.cs:212

The control supports real-time updates through the UpdateChartData method and automatically manages visual refresh through Unity's dirty repaint system for optimal performance.

Constructor & Destructor Documentation

◆ UsoPieChart()

GWG.UsoUIElements.CustomElements.UsoPieChart.UsoPieChart ( )

Initializes a new instance of the UsoPieChart class with default settings and visual content generation. Sets up the chart for rendering and establishes the drawing callback for dynamic visual updates.

The constructor registers the DrawCanvas method with Unity's generateVisualContent callback system, enabling the chart to render its visual content through Unity's 2D painter system. This approach provides high-performance rendering with automatic integration into Unity's UI rendering pipeline.

Member Function Documentation

◆ DrawCanvas()

void GWG.UsoUIElements.CustomElements.UsoPieChart.DrawCanvas ( MeshGenerationContext ctx)
private

Renders the pie chart visual content using Unity's 2D painter system within the provided mesh generation context. Creates individual pie slices based on the percentageColorData collection with appropriate colors and proportions.

Parameters
ctxThe MeshGenerationContext providing access to Unity's 2D painter for rendering operations.

This method implements the core rendering logic for the pie chart, converting percentage data into angular segments and drawing them as filled arc shapes. The rendering process:

  1. Iterates through each data entry in percentageColorData
  2. Converts percentage values to angular measurements (360° total)
  3. Sets appropriate fill colors for each segment
  4. Draws arc segments from the center point with calculated angles

The method uses cumulative angle calculation to ensure segments are positioned correctly adjacent to each other, creating a complete circular representation. Each segment is drawn as a filled path starting from the chart center to create proper pie slice geometry.

◆ UpdateChartData()

void GWG.UsoUIElements.CustomElements.UsoPieChart.UpdateChartData ( List< PercentageColorData > newData)

Updates the pie chart's data with a new collection of percentage and color information. Replaces the current chart data and triggers an immediate visual refresh to display the changes.

Parameters
newDataA list of PercentageColorData objects defining the new chart segments.

This method provides the primary interface for updating chart content dynamically. It completely replaces the existing data set and automatically triggers a repaint to ensure the visual representation matches the new data immediately. The method supports real-time data visualization scenarios where chart content needs to change based on user interaction or data updates.

Member Data Documentation

◆ m_Chart

VisualElement GWG.UsoUIElements.CustomElements.UsoPieChart.m_Chart
private

Visual element reference for the chart container. Used for managing chart-specific styling and layout properties.

◆ m_Radius

float GWG.UsoUIElements.CustomElements.UsoPieChart.m_Radius = 100.0f
private

Private backing field for the pie chart's radius in pixels. Controls the size of the circular chart area and affects the overall visual scale.

◆ m_Value

float GWG.UsoUIElements.CustomElements.UsoPieChart.m_Value = 40.0f
private

Private backing field for a legacy value property. Maintained for potential future functionality or backward compatibility.

◆ percentageColorData

List<PercentageColorData> GWG.UsoUIElements.CustomElements.UsoPieChart.percentageColorData
Initial value:
= new List<PercentageColorData>
{
new PercentageColorData { Percentage = 40.0f, Color = new Color32(182, 235, 122, 255) },
new PercentageColorData { Percentage = 60.0f, Color = new Color32(251, 120, 19, 255) }
}

Collection of percentage and color data that defines the pie chart segments. Each entry represents a slice of the pie with its proportional size and display color.

The list is initialized with default sample data showing a 40% green segment and 60% orange segment. This provides immediate visual feedback when the chart is first created and serves as an example of the expected data structure for chart customization.

Property Documentation

◆ diameter

float GWG.UsoUIElements.CustomElements.UsoPieChart.diameter
get

Gets the diameter of the pie chart calculated as twice the radius. This read-only property provides the total width and height of the circular chart area.

The diameter in pixels, calculated as radius * 2.

This property is used internally for setting chart dimensions and provides external access to the total chart size for layout calculations and positioning purposes.

◆ radius

float GWG.UsoUIElements.CustomElements.UsoPieChart.radius
getset

Gets or sets the radius of the pie chart in pixels. Automatically updates the chart's visual dimensions and triggers a repaint when changed.

The radius value in pixels. Default is 100.0f.

Setting this property automatically updates both width and height of the chart to maintain a circular appearance. The chart dimensions are set to twice the radius (diameter) and a visual repaint is triggered to reflect the size change immediately.

◆ value

float GWG.UsoUIElements.CustomElements.UsoPieChart.value
getset

Gets or sets a legacy value property maintained for potential future functionality. Triggers a visual repaint when changed to ensure chart consistency.

A floating-point value. Default is 40.0f.

This property appears to be maintained for backward compatibility or future enhancement. Currently, it triggers a repaint when changed but doesn't directly affect chart rendering, which is controlled by the percentageColorData collection.