mcp-ai-agent-guidelines
Version:
A comprehensive Model Context Protocol server providing advanced tools, resources, and prompts for implementing AI agent best practices
71 lines • 2.72 kB
JavaScript
import { BaseDiagramHandler } from "./base.handler.js";
/**
* Handler for pie chart diagrams.
* Generates pie charts showing distributions and percentages.
*/
export class PieHandler extends BaseDiagramHandler {
diagramType = "pie";
generate(description, theme) {
this.validateInput(description);
const lines = [];
if (theme)
lines.push(`%%{init: {'theme':'${theme}'}}%%`);
// Parse description to extract categories and values
const { title, data } = this.parsePieDescription(description);
lines.push(`pie title ${title || "Sample Distribution"}`);
if (data.length > 0) {
for (const item of data) {
lines.push(`"${item.label}" : ${item.value}`);
}
}
else {
// Fallback to default template
lines.push('"Category A" : 45', '"Category B" : 30', '"Category C" : 15', '"Category D" : 10');
}
return lines.join("\n");
}
/**
* Parse natural language description to extract pie chart data.
* @param description - Natural language description
* @returns Parsed pie chart configuration
*/
parsePieDescription(description) {
let title = "Distribution";
const data = [];
// Extract title if mentioned
const titleMatch = description.match(/(?:chart|distribution|breakdown)(?:\s+of|\s+for)?\s*:\s*([^.\n]+)/i);
if (titleMatch)
title = titleMatch[1].trim();
// Extract percentages or numbers
const percentMatches = description.matchAll(/(\w[\w\s]*?)[\s:]+(\d+)%/gi);
for (const match of percentMatches) {
data.push({
label: match[1].trim(),
value: Number.parseInt(match[2], 10),
});
}
// Extract explicit counts
if (data.length === 0) {
const countMatches = description.matchAll(/(\d+)\s+([A-Za-z][A-Za-z\s]*?)(?=\s+\d+|$)/gi);
const items = [];
for (const match of countMatches) {
items.push({
label: match[2].trim(),
value: Number.parseInt(match[1], 10),
});
}
// Convert to percentages
if (items.length > 0) {
const total = items.reduce((sum, item) => sum + item.value, 0);
for (const item of items) {
data.push({
label: item.label,
value: Math.round((item.value / total) * 100),
});
}
}
}
return { title, data };
}
}
//# sourceMappingURL=pie.handler.js.map