plot_pie_chart
Generate pie charts from Teradata database tables to visualize data distribution across categories using specified labels and values.
Instructions
Function to generate a pie chart plot for labels and columns. Columns mentioned in labels are used as labels and column is used to plot.
PARAMETERS: table_name: Required Argument. Specifies the name of the table to generate the donut plot. Types: str
labels:
Required Argument.
Specifies the labels to be used for the line plot.
Types: str
column:
Required Argument.
Specifies the column to be used for generating the line plot.
Types: str
RETURNS: dict
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| column | Yes | ||
| labels | Yes | ||
| table_name | Yes |
Implementation Reference
- The core handler function for the 'plot_pie_chart' tool. It validates input, calls the helper get_plot_json_data with chart_type='pie', and returns the plot data as JSON.def handle_plot_pie_chart(conn: TeradataConnection, table_name: str, labels: str, column: str): """ Function to generate a pie chart plot for labels and columns. Columns mentioned in labels are used as labels and column is used to plot. PARAMETERS: table_name: Required Argument. Specifies the name of the table to generate the donut plot. Types: str labels: Required Argument. Specifies the labels to be used for the line plot. Types: str column: Required Argument. Specifies the column to be used for generating the line plot. Types: str RETURNS: dict """ if not isinstance(labels, str): raise ValueError("labels must be a string representing the column name for x-axis.") return get_plot_json_data(conn, table_name, labels, column, 'pie')
- src/teradata_mcp_server/app.py:273-282 (registration)Registers all handler functions (handle_*) as MCP tools by stripping the 'handle_' prefix to derive the tool name (e.g., 'plot_pie_chart'), wraps them for MCP compatibility, and adds to the FastMCP server if matching profile.for name, func in all_functions.items(): if not (inspect.isfunction(func) and name.startswith("handle_")): continue tool_name = name[len("handle_"):] if not any(re.match(p, tool_name) for p in config.get('tool', [])): continue wrapped = make_tool_wrapper(func) mcp.tool(name=tool_name, description=wrapped.__doc__)(wrapped) logger.info(f"Created tool: {tool_name}") else:
- Supporting utility that executes the SQL query, fetches data from the specified table, processes it into Chart.js compatible JSON format for various chart types including 'pie', and wraps in a response.def get_plot_json_data(conn, table_name, labels, columns, chart_type='line'): """ Helper function to fetch data from a Teradata table and formats it for plotting. Right now, designed only to support line plots from chart.js . """ # Define the colors first. colors = ['rgb(75, 192, 192)', '#99cbba', '#d7d0c4', '#fac778', '#e46c59', '#F9CB99', '#280A3E', '#F2EDD1', '#689B8A'] # Chart properties. Every chart needs different property for colors. chart_properties = {'line': 'borderColor', 'polar': 'backgroundColor', 'pie': 'backgroundColor'} columns = [columns] if isinstance(columns, str) else columns sql = "select {labels}, {columns} from {table_name} order by {labels}".format( labels=labels, columns=','.join(columns), table_name=table_name) # Prepare the statement. with conn.cursor() as cur: recs = cur.execute(sql).fetchall() # Define the structure of the chart data. Below is the structure expected by chart.js # { # labels: labels, # datasets: [{ # label: 'My First Dataset', # data: [65, 59, 80, 81, 56, 55, 40], # fill: false, # borderColor: 'rgb(75, 192, 192)', # tension: 0.1 # }] # } labels = [] datasets = [[] for _ in range(len(columns))] for rec in recs: labels.append(rec[0]) for i_, val in enumerate(rec[1:]): datasets[i_].append(val) # Prepare the datasets for chart.js datasets_ = [] for i, dataset in enumerate(datasets): datasets_.append({ 'label': columns[i], 'data': dataset, 'borderColor': colors[i], 'fill': False }) # For polar plot, every dataset needs different colors. if chart_type in ('polar', 'pie'): for i, dataset in enumerate(datasets_): # Remove borderColor and add backgroundColor dataset.pop('borderColor', None) dataset['backgroundColor'] = colors chart_data = {"labels": [str(l) for l in labels], "datasets": datasets_} logger.debug("Chart data: %s", json.dumps(chart_data, indent=2)) return create_response(data=chart_data, metadata={ "tool_description": "chart js {} plot data".format(chart_type), "table_name": table_name, "labels": labels, "columns": columns })