create_sparkline
Generate compact ASCII sparklines to visualize numeric trends inline in terminal environments, using customizable width and color options for clear data representation.
Instructions
Generate compact ASCII sparklines for inline charts
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| data | Yes | Array of numeric values to plot | |
| title | No | Optional sparkline title | |
| width | No | Sparkline width (10-100, default: 40) | |
| color | No | ANSI color name |
Implementation Reference
- src/charts/sparkline.ts:10-71 (handler)The main handler function that executes the create_sparkline tool logic, generating ASCII sparklines with optional min/max labels, coloring, and titles.export function createSparkline(data: ChartData, options: SparklineOptions = {}): ChartResult { const { data: values, title, width = 40, color = 'white' } = data; const { showMinMax = true, fillChar } = options; if (values.length === 0) { throw new Error('Data array cannot be empty'); } const minValue = Math.min(...values); const maxValue = Math.max(...values); const valueRange = maxValue - minValue; // Calculate sparkline width (reserve space for min/max if shown) const sparklineWidth = showMinMax ? width - 20 : width; let sparkline = ''; // Generate sparkline using block characters if (fillChar) { // Use custom fill character for (let i = 0; i < Math.min(values.length, sparklineWidth); i++) { sparkline += fillChar; } } else { // Use gradient block characters const blocks = ASCII_CHARS.sparkBlocks; for (let i = 0; i < Math.min(values.length, sparklineWidth); i++) { const normalizedValue = valueRange === 0 ? 0.5 : normalize(values[i], minValue, maxValue); const blockIndex = Math.floor(normalizedValue * (blocks.length - 1)); sparkline += blocks[blockIndex]; } } // Add min/max values if requested if (showMinMax) { const minStr = minValue.toFixed(1); const maxStr = maxValue.toFixed(1); sparkline = `${minStr} ${sparkline} ${maxStr}`; } // Apply coloring if (color !== 'white') { sparkline = colorize(sparkline, color); } // Add title if provided let result = sparkline; if (title) { const titleLine = center(title, sparkline.length); result = titleLine + '\n' + sparkline; } return { chart: result, title, dimensions: { width: sparkline.length, height: title ? 2 : 1 } }; }
- src/index.ts:377-384 (registration)Tool dispatch/registration in the CallToolRequestSchema handler's switch statement, calling the createSparkline implementation.case 'create_sparkline': { progress.nextStep('Generating sparkline'); result = await withRequestTracking( () => Promise.resolve(createSparkline(chartData)), 'create_sparkline' )(); break; }
- src/index.ts:238-268 (schema)Input schema definition for the create_sparkline tool, listed in ListToolsRequestSchema response.name: 'create_sparkline', description: 'Generate compact ASCII sparklines for inline charts', inputSchema: { type: 'object', properties: { data: { type: 'array', items: { type: 'number' }, description: 'Array of numeric values to plot' }, title: { type: 'string', description: 'Optional sparkline title', optional: true }, width: { type: 'number', description: 'Sparkline width (10-100, default: 40)', minimum: 10, maximum: 100, optional: true }, color: { type: 'string', description: 'ANSI color name', optional: true } }, required: ['data'], examples: getToolExamples('create_sparkline') }
- src/charts/sparkline.ts:5-8 (schema)Type definition for SparklineOptions used in the handler function.export interface SparklineOptions { showMinMax?: boolean; fillChar?: string; }
- src/utils/examples.ts:89-104 (helper)Example inputs for the create_sparkline tool.create_sparkline: { compact: { data: [1, 3, 2, 5, 4, 7, 6, 8], title: "Quick Trend" }, metrics: { data: [23, 25, 22, 28, 30, 27, 31, 29, 33], title: "System Load", color: "red", width: 30 }, inline: { data: [100, 102, 98, 105, 110, 108, 112], width: 25 } }