1###############################################################################
2#
3# ChartTitle - A class for representing Excel chart titles.
4#
5# SPDX-License-Identifier: BSD-2-Clause
6#
7# Copyright (c) 2013-2025, John McNamara, jmcnamara@cpan.org
8#
9
10from typing import Any, Dict, Optional
11
12
13class ChartTitle:
14 """
15 A class to represent an Excel chart title.
16
17 This class encapsulates all title related properties and methods for the
18 chart title and axis titles.
19 """
20
21 def __init__(self) -> None:
22 """
23 Initialize a ChartTitle instance.
24 """
25 self.font: Optional[Dict[str, Any]] = None
26 self.name: Optional[str] = None
27 self.formula: Optional[str] = None
28 self.data_id: Optional[int] = None
29 self.layout: Optional[Dict[str, Any]] = None
30 self.overlay: Optional[bool] = None
31 self.hidden: bool = False
32 self.line: Optional[Dict[str, Any]] = None
33 self.fill: Optional[Dict[str, Any]] = None
34 self.pattern: Optional[Dict[str, Any]] = None
35 self.gradient: Optional[Dict[str, Any]] = None
36
37 def has_name(self) -> bool:
38 """
39 Check if the title has a text name set.
40
41 Returns:
42 True if name has been set.
43 """
44 return self.name is not None and self.name != ""
45
46 def has_formula(self) -> bool:
47 """
48 Check if the title has a formula set.
49
50 Returns:
51 True if formula has been set.
52 """
53 return self.formula is not None
54
55 def has_formatting(self) -> bool:
56 """
57 Check if the title has any formatting properties set.
58
59 Returns:
60 True if the title has line, fill, pattern, or gradient formatting.
61 """
62 has_line = self.line is not None and self.line.get("defined", False)
63 has_fill = self.fill is not None and self.fill.get("defined", False)
64 has_pattern = self.pattern
65 has_gradient = self.gradient
66
67 return has_line or has_fill or has_pattern or has_gradient
68
69 def get_formatting(self) -> Dict[str, Any]:
70 """
71 Get a dictionary containing the formatting properties.
72
73 Returns:
74 A dictionary with line, fill, pattern, and gradient properties.
75 """
76 return {
77 "line": self.line,
78 "fill": self.fill,
79 "pattern": self.pattern,
80 "gradient": self.gradient,
81 }
82
83 def is_hidden(self) -> bool:
84 """
85 Check if the title is explicitly hidden.
86
87 Returns:
88 True if title is hidden.
89 """
90 return self.hidden
91
92 def __repr__(self) -> str:
93 """
94 Return a string representation of the ChartTitle.
95 """
96 return (
97 f"ChartTitle(\n"
98 f" name = {self.name!r},\n"
99 f" formula = {self.formula!r},\n"
100 f" hidden = {self.hidden!r},\n"
101 f" font = {self.font!r},\n"
102 f" line = {self.line!r},\n"
103 f" fill = {self.fill!r},\n"
104 f" pattern = {self.pattern!r},\n"
105 f" gradient = {self.gradient!r},\n"
106 f" layout = {self.layout!r},\n"
107 f" overlay = {self.overlay!r},\n"
108 f" has_formatting = {self.has_formatting()!r},\n"
109 f")\n"
110 )