Raxol.Terminal.Graphics.DataVisualization (Raxol v2.0.1)

View Source

Advanced data visualization component library for terminal graphics.

This module extends the existing chart capabilities with:

  • Real-time data streaming visualizations
  • Interactive data exploration tools
  • Advanced chart types (heatmaps, scatter plots, histograms, etc.)
  • Multi-dimensional data visualization
  • Export capabilities for graphics
  • Performance-optimized rendering for large datasets

Built on top of existing Raxol visualization infrastructure, this module provides enterprise-grade data visualization capabilities.

Features

Advanced Chart Types

  • Heatmaps with customizable color scales
  • Scatter plots with clustering visualization
  • Histograms and distribution plots
  • Multi-series time-series charts
  • Bubble charts with size/color encoding
  • Treemaps for hierarchical data

Real-time Capabilities

  • Streaming data visualization with configurable buffers
  • Live updates with smooth animations
  • Automatic scaling and zooming
  • Performance monitoring and throttling
  • Memory-efficient data windowing

Interactive Features

  • Mouse-driven zoom and pan
  • Data point selection and highlighting
  • Dynamic filtering and grouping
  • Tooltip information on hover
  • Click-to-drill-down functionality

Usage

# Create a real-time line chart
{:ok, chart} = DataVisualization.create_streaming_chart(:line, %{
  title: "CPU Usage",
  max_points: 100,
  update_interval: 1000,  # 1 second
  auto_scale: true
})

# Stream data points
DataVisualization.add_data_point(chart, %{
  timestamp: System.system_time(:millisecond),
  value: 75.5,
  series: "cpu_usage"
})

# Create interactive heatmap
{:ok, heatmap} = DataVisualization.create_heatmap(data, %{
  width: 80, height: 24,
  color_scale: :viridis,
  interactive: true
})

Summary

Functions

Adds a data point to a streaming chart.

Adds multiple data points efficiently.

Returns a specification to start this module under a supervisor.

Creates an interactive heatmap visualization.

Creates a histogram from data values.

Creates a scatter plot with optional clustering visualization.

Creates a real-time streaming chart.

Enables interactive features for a chart.

Exports a chart to various formats.

Gets performance statistics for visualization operations.

Types

chart_id()

@type chart_id() :: String.t()

chart_state()

@type chart_state() :: %{
  id: chart_id(),
  type: visualization_type(),
  config: map(),
  data_buffer: [data_point()],
  bounds: map(),
  graphics_id: non_neg_integer(),
  last_update: non_neg_integer(),
  interactive: boolean(),
  animation_state: map()
}

data_point()

@type data_point() :: %{
  timestamp: non_neg_integer(),
  value: number(),
  series: String.t(),
  metadata: map()
}

streaming_config()

@type streaming_config() :: %{
  max_points: non_neg_integer(),
  update_interval: non_neg_integer(),
  auto_scale: boolean(),
  buffer_size: non_neg_integer(),
  animation_enabled: boolean()
}

visualization_type()

@type visualization_type() ::
  :line
  | :bar
  | :scatter
  | :heatmap
  | :histogram
  | :bubble
  | :treemap
  | :sparkline

Functions

add_data_point(chart_id, data_point)

@spec add_data_point(chart_id(), data_point()) :: :ok | {:error, term()}

Adds a data point to a streaming chart.

Examples

DataVisualization.add_data_point(chart_id, %{
  timestamp: System.system_time(:millisecond),
  value: 85.2,
  series: "cpu_usage",
  metadata: %{core: 0, temperature: 65}
})

add_data_points(chart_id, data_points)

@spec add_data_points(chart_id(), [data_point()]) :: :ok | {:error, term()}

Adds multiple data points efficiently.

Examples

points = [
  %{timestamp: t1, value: 10, series: "cpu"},
  %{timestamp: t2, value: 20, series: "cpu"},
  %{timestamp: t3, value: 15, series: "memory"}
]

DataVisualization.add_data_points(chart_id, points)

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

create_heatmap(data, config)

@spec create_heatmap([[number()]], map()) :: {:ok, chart_id()} | {:error, term()}

Creates an interactive heatmap visualization.

Parameters

  • data - 2D array of numerical data
  • config - Heatmap configuration

Examples

# Create temperature heatmap
data = generate_2d_temperature_data(24, 60)  # 24 hours x 60 minutes

{:ok, heatmap_id} = DataVisualization.create_heatmap(data, %{
  title: "Temperature Over Time",
  x_labels: hour_labels,
  y_labels: minute_labels,
  color_scale: :thermal,
  bounds: %{x: 5, y: 5, width: 70, height: 20},
  interactive: true,
  tooltip_enabled: true
})

create_histogram(values, config)

@spec create_histogram([number()], map()) :: {:ok, chart_id()} | {:error, term()}

Creates a histogram from data values.

Examples

values = [1.2, 1.5, 1.8, 2.1, 2.3, 2.8, 3.1, 3.5]

{:ok, hist_id} = DataVisualization.create_histogram(values, %{
  title: "Value Distribution",
  bins: 10,
  show_statistics: true,
  bounds: %{x: 0, y: 0, width: 60, height: 15}
})

create_scatter_plot(points, config)

@spec create_scatter_plot([map()], map()) :: {:ok, chart_id()} | {:error, term()}

Creates a scatter plot with optional clustering visualization.

Examples

points = [
  %{x: 10, y: 20, size: 5, color: :blue, cluster: 1},
  %{x: 15, y: 25, size: 8, color: :red, cluster: 2}
]

{:ok, scatter_id} = DataVisualization.create_scatter_plot(points, %{
  title: "Data Clustering",
  show_clusters: true,
  cluster_colors: [:blue, :red, :green],
  interactive: true
})

create_streaming_chart(type, config)

@spec create_streaming_chart(visualization_type(), map()) ::
  {:ok, chart_id()} | {:error, term()}

Creates a real-time streaming chart.

Parameters

  • type - Chart type (:line, :bar, :scatter, etc.)
  • config - Chart configuration including streaming settings

Returns

  • {:ok, chart_id} - Successfully created streaming chart
  • {:error, reason} - Failed to create chart

Examples

{:ok, chart_id} = DataVisualization.create_streaming_chart(:line, %{
  title: "Network Throughput",
  max_points: 200,
  update_interval: 500,  # 500ms updates
  series: ["download", "upload"],
  bounds: %{x: 0, y: 0, width: 80, height: 20},
  auto_scale: true,
  animation: %{enabled: true, duration: 300}
})

enable_interaction(chart_id, interaction_config)

@spec enable_interaction(chart_id(), map()) :: :ok | {:error, term()}

Enables interactive features for a chart.

Examples

DataVisualization.enable_interaction(chart_id, %{
  zoom: true,
  pan: true,
  select: true,
  hover_tooltip: true
})

export_chart(chart_id, format)

@spec export_chart(chart_id(), :ascii | :json | :svg | :png) ::
  {:ok, binary()} | {:error, term()}

Exports a chart to various formats.

Examples

# Export as ASCII art
{:ok, ascii_data} = DataVisualization.export_chart(chart_id, :ascii)

# Export as JSON data
{:ok, json_data} = DataVisualization.export_chart(chart_id, :json)

# Export as SVG (if supported)
{:ok, svg_data} = DataVisualization.export_chart(chart_id, :svg)

get_performance_stats()

@spec get_performance_stats() :: map()

Gets performance statistics for visualization operations.

start_link(init_opts \\ [])