generate_polar_chart
Create polar charts (rose, radar, pie) to visualize numerical differences across categories using radius and angle coordinates.
Instructions
Generate a polar chart (rose, radar, pie) to display numerical differences among different categories using radius and angle in polar coordinates.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| output | No | Chart output type. Defaults to 'image'. | image |
| width | No | Chart width. Optional, defaults to 500. | |
| height | No | Chart height. Optional, defaults to 500. | |
| dataTable | Yes | Data for the chart, e.g., [{ category: 'Category 01', value: 10 }]. | |
| chartType | Yes | ||
| transpose | No | ||
| categoryField | Yes | Dimension field. Must exist in the data. | |
| valueField | Yes | Measure field. Must be numeric and exist in the data. | |
| colorField | No | Color grouping field. Should not duplicate the dimension field. | |
| chartTheme | No | Chart theme. Optional, defaults to 'light'. | |
| title | No | Chart title text. | |
| subTitle | No | Chart subtitle text. | |
| titleOrient | No | Title position in the chart. | |
| angleAxisTitle | No | Angle axis title. | |
| angleAxisHasGrid | No | Show grid lines for the angle axis. | |
| angleAxisHasLabel | No | Show angle axis labels. | |
| angleAxisHasTick | No | Show angle axis ticks. | |
| angleAxisType | No | Angle axis type: categorical ('band') or continuous ('linear'). | |
| radiusAxisHasGrid | No | Show grid lines for the radius axis. | |
| radiusAxisHasLabel | No | Show radius axis labels. | |
| radiusAxisHasTick | No | Show radius axis ticks. | |
| radiusAxisType | No | Radius axis type: categorical ('band') or continuous ('linear'). | |
| radiusAxisTitle | No | Radius axis title. | |
| background | No | Chart background color (hex). Optional, defaults to white. | |
| colors | No | Color palette for chart elements. | |
| stackOrPercent | No | Stacking mode: 'stack' for stacked data, 'percent' for percentage stacking. Requires 'color' field. |
Implementation Reference
- src/utils/generateChart.ts:42-202 (handler)The core handler logic for generating polar charts. Destructures options, configures polar-specific axes (angle, radius), generates VChart spec using @visactor/generate-vchart, and renders to image/html/spec based on output.export async function generateChartByType(chartType: string, options: any) { const { title, subTitle, titleOrient, xAxisType, xAxisOrient, xAxisTitle, xAxisHasGrid, xAxisHasLabel, xAxisHasTick, yAxisType, yAxisOrient, yAxisTitle, yAxisHasGrid, yAxisHasLabel, yAxisHasTick, leftYAxisTitle, leftYAxisHasGrid, leftYAxisHasLabel, leftYAxisHasTick, rightYAxisTitle, rightYAxisHasGrid, rightYAxisHasLabel, rightYAxisHasTick, angleAxisTitle, angleAxisHasGrid, angleAxisHasLabel, angleAxisHasTick, angleAxisType, radiusAxisHasGrid, radiusAxisHasLabel, radiusAxisHasTick, radiusAxisType, radiusAxisTitle, output, width, height, ...res } = options; const opts = { ...res }; const titleObj = filterValidAttributes({ text: title, subText: subTitle, orient: titleOrient, }); const xAxisObj = filterValidAttributes({ type: xAxisType, orient: xAxisOrient, title: xAxisTitle, hasGrid: xAxisHasGrid, hasLabel: xAxisHasLabel, hasTick: xAxisHasTick, }); const yAxisObj = filterValidAttributes({ type: yAxisType, orient: yAxisOrient, title: yAxisTitle, hasGrid: yAxisHasGrid, hasLabel: yAxisHasLabel, hasTick: yAxisHasTick, }); const leftYAxisObj = filterValidAttributes({ title: leftYAxisTitle, hasGrid: leftYAxisHasGrid, hasLabel: leftYAxisHasLabel, hasTick: leftYAxisHasTick, }); const rightYAxisObj = filterValidAttributes({ title: rightYAxisTitle, hasGrid: rightYAxisHasGrid, hasLabel: rightYAxisHasLabel, hasTick: rightYAxisHasTick, }); const angleAxisObj = filterValidAttributes({ title: angleAxisTitle, hasGrid: angleAxisHasGrid, hasLabel: angleAxisHasLabel, hasTick: angleAxisHasTick, type: angleAxisType, }); const radiusAxisObj = filterValidAttributes({ hasGrid: radiusAxisHasGrid, hasLabel: radiusAxisHasLabel, hasTick: radiusAxisHasTick, type: radiusAxisType, title: radiusAxisTitle, }); const cell: Record<string, string> = {}; [ "xField", "yField", "colorField", "categoryField", "valueField", "wordField", "sizeField", "timeField", "sourceField", "targetField", "setsField", "radiusField", ].forEach((fieldName) => { if (isValid(options[fieldName])) { cell[fieldName.replace("Field", "")] = options[fieldName]; delete opts[fieldName]; } }); opts.cell = cell; if (!isEmpty(titleObj)) { opts.title = titleObj; } const axes = [ xAxisObj, yAxisObj, leftYAxisObj, rightYAxisObj, angleAxisObj, radiusAxisObj, ]; if (axes.some((axis) => !isEmpty(axis))) { opts.axes = axes.filter((axis) => !isEmpty(axis)); } const { spec } = generateChart(options.chartType ?? chartType, opts); if (!spec) { return null; } if (output === "spec") { if (isValid(width)) { spec.width = width; } if (isValid(height)) { spec.height = height; } return { spec: spec, }; } return gentrateChartImageOrHtml(output, spec, { width: `${width ?? 500}px`, height: `${height ?? 500}px`, }); }
- src/server.ts:43-125 (handler)MCP CallToolRequest handler that dispatches generate_polar_chart to chartType='polar', validates input with polar.schema, calls generateChartByType, and returns the chart output as MCP content.server.setRequestHandler(CallToolRequestSchema, async request => { const toolName = request.params.name; const chartType = Object.keys(Charts).find( key => (Charts as any)[key].tool.name === toolName ); if (!chartType) { throw new McpError( ErrorCode.MethodNotFound, `Unknown tool: ${request.params.name}.` ); } try { // Validate input using Zod before generating chart const args = request.params.arguments || {}; // Select the appropriate schema based on the chart type const schema = Charts[chartType as keyof typeof Charts].schema; if (schema) { const result = schema.safeParse(args); if (!result.success) { throw new McpError( ErrorCode.InvalidParams, `Invalid parameters: ${result.error.message}` ); } } const res = await generateChartByType(chartType, args); if (res && (res as any).spec) { return { content: [ { type: 'text', text: JSON.stringify((res as any).spec, null, 2), }, ], }; } if (res && (res as any).image) { return { content: [ { type: 'text', text: (res as any).image, }, ], }; } if (res && (res as any).html) { return { content: [ { type: 'text', text: (res as any).html, }, ], }; } return { content: [ { type: 'text', text: 'Failed to generate chart', }, ], }; } catch (error: any) { if (error instanceof McpError) { throw error; } throw new McpError( ErrorCode.InternalError, `Failed to generate chart: ${error?.message || 'Unknown error.'}` ); } });
- src/charts/polar.ts:29-67 (schema)Zod schema for input validation of generate_polar_chart tool parameters, including data, fields, chartType (rose/radar/pie), and polar-specific axis configurations.const schema = z.object({ output: ChartOutputSchema, width: WidthSchema, height: HeightSchema, dataTable: z .array(z.any()) .describe( "Data for the chart, e.g., [{ category: 'Category 01', value: 10 }]." ) .nonempty({ message: "Data must not be empty." }), chartType: z.enum(["rose", "radar", "pie"]), transpose: z.boolean().optional(), categoryField: XFieldSchema, valueField: YFieldSchema, colorField: ColorFieldSchema, chartTheme: ThemeSchema, title: TitleTextSchema, subTitle: TitleSubTextSchema, titleOrient: TitleOrientSchema, angleAxisTitle: AngleAxisTitleSchema, angleAxisHasGrid: AngleAxisHasGridSchema, angleAxisHasLabel: AngleAxisHasLabelSchema, angleAxisHasTick: AngleAxisHasTickSchema, angleAxisType: AngleAxisTypeSchema, radiusAxisHasGrid: RadiusAxisHasGridSchema, radiusAxisHasLabel: RadiusAxisHasLabelSchema, radiusAxisHasTick: RadiusAxisHasTickSchema, radiusAxisType: RadiusAxisTypeSchema, radiusAxisTitle: RadiusAxisTitleSchema, background: BackgroundSchema, colors: ColorsSchema, stackOrPercent: StackSchema, });
- src/charts/polar.ts:69-79 (registration)Tool registration metadata: name, description, and JSON schema derived from Zod schema.const tool = { name: "generate_polar_chart", description: "Generate a polar chart (rose, radar, pie) to display numerical differences among different categories using radius and angle in polar coordinates.", inputSchema: convertZodToJsonSchema(schema), }; export const polar = { schema, tool, };
- src/server.ts:34-37 (registration)Registers generate_polar_chart in MCP tools list by including polar.tool from imported Charts.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: Object.values(Charts).map(chart => (chart as any).tool), };