Skip to main content

Module chart

Module chart 

Source
Expand description

Chart rendering for query results.

Converts a list of JSON rows (typically from crate::engine::Engine::execute_query_to_json) into a PNG or SVG image via the plotters crate. The output is raw bytes plus a MIME type ready to drop into an MCP ImageContent.

§Supported Chart Types

  • Bar — categorical x-axis by default; multi-series supported via series column.
  • Line — auto-detects categorical x (DATE/TIMESTAMP/TEXT); override with x_as_category.
  • Scatter — same auto-detection as line.
  • Histogram — single numeric column binned into N buckets (default 20).

§Rendering Pipeline

  1. The MCP chart tool runs a read-only SQL query via crate::engine::Engine.
  2. Rows are grouped into series via group_series (categorical x values get synthetic sequential indices; numeric x values pass through directly).
  3. The chart is drawn on either a BitMapBackend (PNG, written to a temp file) or an SVGBackend (SVG, rendered to an in-memory string).
  4. The result is returned as base64-encoded ImageContent plus a JSON stats block.

§Color Palette

Multi-series charts cycle through an 8-color palette designed for white backgrounds. The palette is defined in series_color.

Structs§

ChartOptions
User-facing chart configuration, parsed from MCP tool parameters.
ChartResult
Result of rendering a chart.

Enums§

ChartDisposition
How the chart tool should deliver the rendered image: write it to disk, return it inline in the MCP tool result, or both. This is a pure decision based on the caller’s inline / output_path flags — no I/O happens here; write_chart_to_disk does the actual write.
ChartFormat
Output format for the rendered chart.
ChartType
Supported chart types.

Functions§

auto_generated_chart_path
Synthesize a unique path under std::env::temp_dir()/hyperdb-charts/ for a default-disposition chart write. The filename encodes a monotonic counter + PID + unix-nanos so two calls in the same nanosecond (or on two hosts with sync’d clocks) don’t collide.
parse_hex_color
Parse a "#rrggbb" hex string into an RGBColor. Returns None when the string is not in the expected format so callers can log and skip rather than hard-failing.
render_chart
Render a chart from a list of JSON row objects.
resolve_chart_disposition
Decide what the chart tool should do with the rendered bytes based on the caller’s inline and output_path flags plus the already-resolved format.
resolve_chart_format
Resolve the effective output format from an explicit format parameter and/or an output_path’s extension.
write_chart_to_disk
Write chart bytes to path, creating the parent directory if needed and honoring the overwrite flag.