mirror of
https://github.com/haris-musa/excel-mcp-server.git
synced 2025-12-08 17:12:41 +08:00
feat: Enhance chart data labels and default to values only
This commit is contained in:
@ -66,6 +66,12 @@ def create_chart_in_sheet(
|
|||||||
style: Optional[Dict] = None
|
style: Optional[Dict] = None
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Create chart in sheet with enhanced styling options"""
|
"""Create chart in sheet with enhanced styling options"""
|
||||||
|
# Ensure style dict exists and defaults to showing data labels
|
||||||
|
if style is None:
|
||||||
|
style = {"show_data_labels": True}
|
||||||
|
else:
|
||||||
|
# If caller omitted the flag, default to True
|
||||||
|
style.setdefault("show_data_labels", True)
|
||||||
try:
|
try:
|
||||||
wb = load_workbook(filepath)
|
wb = load_workbook(filepath)
|
||||||
if sheet_name not in wb.sheetnames:
|
if sheet_name not in wb.sheetnames:
|
||||||
@ -165,22 +171,36 @@ def create_chart_in_sheet(
|
|||||||
|
|
||||||
# Apply style if provided
|
# Apply style if provided
|
||||||
try:
|
try:
|
||||||
if style:
|
if style.get("show_legend", True):
|
||||||
if style.get("show_legend", True):
|
chart.legend = Legend()
|
||||||
chart.legend = Legend()
|
chart.legend.position = style.get("legend_position", "r")
|
||||||
chart.legend.position = style.get("legend_position", "r")
|
else:
|
||||||
else:
|
chart.legend = None
|
||||||
chart.legend = None
|
|
||||||
|
|
||||||
if style.get("show_data_labels", False):
|
if style.get("show_data_labels", False):
|
||||||
chart.dataLabels = DataLabelList()
|
data_labels = DataLabelList()
|
||||||
chart.dataLabels.showVal = True
|
# Gather optional overrides
|
||||||
|
dlo = style.get("data_label_options", {}) if isinstance(style.get("data_label_options", {}), dict) else {}
|
||||||
|
|
||||||
if style.get("grid_lines", False):
|
# Helper to read bool with fallback
|
||||||
if hasattr(chart, "x_axis"):
|
def _opt(name: str, default: bool) -> bool:
|
||||||
chart.x_axis.majorGridlines = ChartLines()
|
return bool(dlo.get(name, default))
|
||||||
if hasattr(chart, "y_axis"):
|
|
||||||
chart.y_axis.majorGridlines = ChartLines()
|
# Apply options – Excel will concatenate any that are set to True
|
||||||
|
data_labels.showVal = _opt("show_val", True)
|
||||||
|
data_labels.showCatName = _opt("show_cat_name", False)
|
||||||
|
data_labels.showSerName = _opt("show_ser_name", False)
|
||||||
|
data_labels.showLegendKey = _opt("show_legend_key", False)
|
||||||
|
data_labels.showPercent = _opt("show_percent", False)
|
||||||
|
data_labels.showBubbleSize = _opt("show_bubble_size", False)
|
||||||
|
|
||||||
|
chart.dataLabels = data_labels
|
||||||
|
|
||||||
|
if style.get("grid_lines", False):
|
||||||
|
if hasattr(chart, "x_axis"):
|
||||||
|
chart.x_axis.majorGridlines = ChartLines()
|
||||||
|
if hasattr(chart, "y_axis"):
|
||||||
|
chart.y_axis.majorGridlines = ChartLines()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to apply chart style: {e}")
|
logger.error(f"Failed to apply chart style: {e}")
|
||||||
raise ChartError(f"Failed to apply chart style: {str(e)}")
|
raise ChartError(f"Failed to apply chart style: {str(e)}")
|
||||||
|
|||||||
Reference in New Issue
Block a user