├── .gitignore
├── SECURITY.md
├── PULL_REQUEST_TEMPLATE.md
├── .github
└── ISSUE_TEMPLATE
│ ├── bug_report.md
│ └── feature_request.md
├── template(s)
├── template (1).py
├── template (2).py
├── template (3).py
├── template (7).py
├── template (4).py
├── template (5).py
├── template (8).py
└── template (6).py
├── src
└── tkchart
│ ├── __init__.py
│ ├── FontStyle.py
│ ├── Utils.py
│ ├── Validate.py
│ └── Line.py
├── examples
├── 1. Simple.py
├── 2. width height.py
├── 3. data showing.py
├── 4. change colors.py
├── 5. change fonts.py
├── 6. change sections labels.py
├── 7. y axis precision.py
├── 8. y x space.py
├── 9. data position.py
├── 10. change line style.py
├── 12. change section style.py
├── 1000. Complex.py
└── 11. pointing values.py
├── setup.py
├── LICENSE
├── CONTRIBUTING.md
├── tests
├── test.py
└── main test.py
├── CODE_OF_CONDUCT.md
├── CHANGES_zh.md
├── CHANGES_en.md
├── README_zh.md
├── README.md
└── documentation
└── DOCUMENTATION_zh.md
/.gitignore:
--------------------------------------------------------------------------------
1 | *.pyc
2 | *.pyd
--------------------------------------------------------------------------------
/SECURITY.md:
--------------------------------------------------------------------------------
1 | # Security Policy
2 |
--------------------------------------------------------------------------------
/PULL_REQUEST_TEMPLATE.md:
--------------------------------------------------------------------------------
1 | ## Pull Request Checklist
2 |
3 | - [ ] Have you tested your changes?
4 | - [ ] Have you updated the documentation?
5 | - [ ] ...
6 |
7 | ## Description
8 |
9 | Briefly describe the changes introduced by this pull request.
10 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug report
3 | about: Create a report to help us improve
4 | title: ''
5 | labels: ''
6 | assignees: ''
7 |
8 | ---
9 |
10 | **Describe the bug**
11 | A clear and concise description of what the bug is.
12 |
13 | **To Reproduce**
14 | Steps to reproduce the behavior:
15 | 1. Go to '...'
16 | 2. Click on '....'
17 | 3. Scroll down to '....'
18 | 4. See error
19 |
20 | **Expected behavior**
21 | A clear and concise description of what you expected to happen.
22 |
23 | **Screenshots**
24 | If applicable, add screenshots to help explain your problem.
25 |
26 | **Additional context**
27 | Add any other context about the problem here.
28 |
--------------------------------------------------------------------------------
/template(s)/template (1).py:
--------------------------------------------------------------------------------
1 | import tkinter as tk
2 | import tkchart
3 | import random
4 |
5 | #root
6 | root = tk.Tk()
7 | root.configure(bg="#151515")
8 |
9 | #creating a chart
10 | chart = tkchart.LineChart(master=root,
11 | x_axis_values = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
12 | y_axis_values = (-100,100))
13 | chart.pack()
14 |
15 | #creating a line
16 | line = tkchart.Line(master=chart)
17 |
18 | data = [x for x in range(-100,100)]
19 | #dipslay data (random)
20 | def loop():
21 | chart.show_data(line=line, data=random.choices(data, k=1))
22 | root.after(500, loop)
23 | loop()
24 |
25 | root.mainloop()
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/feature_request.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Feature request
3 | about: Suggest an idea for this project
4 | title: ''
5 | labels: ''
6 | assignees: ''
7 |
8 | ---
9 |
10 | **Is your feature request related to a problem? Please describe.**
11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12 |
13 | **Describe the solution you'd like**
14 | A clear and concise description of what you want to happen.
15 |
16 | **Describe alternatives you've considered**
17 | A clear and concise description of any alternative solutions or features you've considered.
18 |
19 | **Additional context**
20 | Add any other context or screenshots about the feature request here.
21 |
--------------------------------------------------------------------------------
/src/tkchart/__init__.py:
--------------------------------------------------------------------------------
1 | """
2 | tkchart: A library to create live-update charts for Tkinter GUIs.
3 | """
4 |
5 | from .LineChart import LineChart
6 | from .Line import Line
7 |
8 | # Constants for common string values
9 | ENABLED = "enabled"
10 | DISABLED = "disabled"
11 |
12 | NORMAL = "normal"
13 | DASHED = "dashed"
14 | DOTTED = "dotted"
15 |
16 | TOP = "top"
17 | SIDE = "side"
18 |
19 | AUTO = "auto"
20 |
21 | __title__ = "tkchart"
22 | __version__ = "2.1.8"
23 | __authors__ = ("Thisal Dilmith", "childeyouyu (有语)")
24 |
25 | __all__ = [
26 | "LineChart",
27 | "Line",
28 | "ENABLED",
29 | "DISABLED",
30 | "NORMAL",
31 | "DASHED",
32 | "DOTTED",
33 | "TOP",
34 | "SIDE",
35 | "AUTO",
36 | ]
37 |
--------------------------------------------------------------------------------
/template(s)/template (2).py:
--------------------------------------------------------------------------------
1 | import tkinter as tk
2 | import tkchart
3 | import random
4 |
5 | #root
6 | root = tk.Tk()
7 | root.geometry("700x400+500+300")
8 |
9 | root.configure(bg="#151515")
10 |
11 | #creating a chart
12 | chart = tkchart.LineChart(master=root,y_axis_label_count=4,
13 | x_axis_values = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
14 | y_axis_values = (-100,100))
15 | chart.pack()
16 |
17 | #creating a line
18 | line = tkchart.Line(master=chart, fill="enabled")
19 |
20 | data = [x for x in range(-100,100)]
21 | #dipslay data (random)
22 | def loop():
23 | chart.show_data(line=line, data=random.choices(data, k=1))
24 | root.after(500, loop)
25 | loop()
26 |
27 | root.mainloop()
--------------------------------------------------------------------------------
/examples/1. Simple.py:
--------------------------------------------------------------------------------
1 | import tkchart
2 | import customtkinter
3 |
4 | root = customtkinter.CTk()
5 | root.geometry("1280x720")
6 |
7 | # values for chart x axis
8 | x_axis_values = ('2020 Year', '2021 Year', '2022 Year', '2023 Year', '2024 Year')
9 | #create line chart
10 | linechart = tkchart.LineChart(master=root,
11 | y_axis_values=(0,1000),
12 | x_axis_values=x_axis_values
13 | )
14 | #place line chart
15 | linechart.place(x=50, y=50)
16 |
17 | #create line
18 | line = tkchart.Line(master=linechart)
19 |
20 | #display data
21 | display_data = [200, 100, 800, 400, 600, 700]
22 |
23 | #displaying data
24 | linechart.show_data(line=line, data=display_data)
25 |
26 | root.mainloop()
--------------------------------------------------------------------------------
/setup.py:
--------------------------------------------------------------------------------
1 | from setuptools import setup, find_packages
2 |
3 | setup(
4 | name='tkchart', # Replace with your package name
5 | version='2.1.8', # Replace with your package version
6 | author='Thisal-D', # Replace with your name
7 | author_email='', # Replace with your email
8 | description='Python library for creating live updating line charts in Tkinter.',
9 | long_description=open('README.md').read(), # Ensure you have a README.md file
10 | long_description_content_type='text/markdown',
11 | url='https://github.com/Thisal-D/tkchart',
12 | packages=find_packages(),
13 | classifiers=[
14 | 'Programming Language :: Python :: 3',
15 | 'License :: OSI Approved :: MIT License',
16 | 'Operating System :: OS Independent',
17 | ],
18 | python_requires='>=3.0',
19 | install_requires=[],
20 | include_package_data=True,
21 | )
22 |
--------------------------------------------------------------------------------
/examples/2. width height.py:
--------------------------------------------------------------------------------
1 | import tkchart
2 | import customtkinter
3 |
4 | root = customtkinter.CTk()
5 | root.geometry("1280x720")
6 |
7 | # values for chart x axis
8 | x_axis_values = ('2020 Year', '2021 Year', '2022 Year', '2023 Year', '2024 Year')
9 | #create line chart
10 | linechart = tkchart.LineChart(master=root,
11 | y_axis_values=(0,1000),
12 | x_axis_values=x_axis_values,
13 |
14 | width=1000, height=500,
15 | axis_size=5,
16 | )
17 | #place line chart
18 | linechart.place(x=50, y=50)
19 |
20 | #create line
21 | line = tkchart.Line(master=linechart, size=2)
22 |
23 | #display data
24 | display_data = [200, 100, 800, 400, 600, 700]
25 |
26 | #displaying data
27 | linechart.show_data(line=line, data=display_data)
28 |
29 | root.mainloop()
--------------------------------------------------------------------------------
/template(s)/template (3).py:
--------------------------------------------------------------------------------
1 | import tkinter as tk
2 | import tkchart
3 | import random
4 |
5 | #root
6 | root = tk.Tk()
7 | root.geometry("700x400+500+300")
8 |
9 | root.configure(bg="#151515")
10 |
11 | #creating a chart
12 | chart = tkchart.LineChart(master=root,
13 | x_axis_values = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
14 | y_axis_values = (0,100))
15 | chart.pack()
16 |
17 | #creating a line
18 | line = tkchart.Line(master=chart,
19 | size=3,
20 | style_type=(10,4),
21 | color="#ef8ccb",
22 | style="dashed",
23 | fill="enabled",
24 | fill_color="#b46d9a")
25 |
26 | data = [x for x in range(0,100)]
27 | #dipslay data (random)
28 | def loop():
29 | chart.show_data(line=line, data=random.choices(data, k=1))
30 | root.after(500, loop)
31 | loop()
32 |
33 | root.mainloop()
--------------------------------------------------------------------------------
/template(s)/template (7).py:
--------------------------------------------------------------------------------
1 | import tkinter as tk
2 | import tkchart
3 | import random
4 |
5 | #root
6 | root = tk.Tk()
7 | root.geometry("700x400+500+300")
8 | root.configure(bg="#151515")
9 |
10 | #creating a chart
11 | chart = tkchart.LineChart(master=root,
12 | x_axis_values = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
13 | y_axis_values = (0,100),
14 | )
15 | chart.pack()
16 |
17 | #creating a line
18 | line1 = tkchart.Line(master=chart,
19 | size=3,
20 | point_highlight="enabled",
21 | point_highlight_size=10,
22 | fill="enabled",
23 | style="normal")
24 |
25 |
26 |
27 |
28 | data = [x for x in range(0,100)]
29 | #dipslay data (random)
30 | def loop():
31 | chart.show_data(line=line1, data=random.choices(data, k=1))
32 |
33 | root.after(500, loop)
34 | loop()
35 |
36 | root.mainloop()
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2024 The Python Packaging Authority
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is
8 | furnished to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in all
11 | copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 | SOFTWARE.
--------------------------------------------------------------------------------
/examples/3. data showing.py:
--------------------------------------------------------------------------------
1 | import tkchart
2 | import customtkinter
3 | import random
4 | import threading
5 | import time
6 |
7 | root = customtkinter.CTk()
8 | root.geometry("1280x720")
9 |
10 | # values for chart x axis
11 | x_axis_values = ('2020 Year', '2021 Year', '2022 Year', '2023 Year', '2024 Year')
12 | #create line chart
13 | linechart = tkchart.LineChart(master=root,
14 | y_axis_values=(0,1000),
15 | x_axis_values=x_axis_values,
16 |
17 | width=1000, height=500,
18 | axis_size=5,
19 | )
20 | #place line chart
21 | linechart.place(x=50, y=50)
22 |
23 | #create line
24 | line = tkchart.Line(master=linechart, size=2)
25 |
26 | #display data
27 | data = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
28 |
29 |
30 |
31 | def display_data():
32 | while True:
33 | #displaying data
34 | linechart.show_data(line=line, data=random.choices(data,k=1))
35 | time.sleep(0.5)
36 |
37 | threading.Thread(target=display_data).start()
38 |
39 | root.mainloop()
--------------------------------------------------------------------------------
/template(s)/template (4).py:
--------------------------------------------------------------------------------
1 | import tkinter as tk
2 | import tkchart
3 | import random
4 |
5 | #root
6 | root = tk.Tk()
7 | root.geometry("700x400+500+300")
8 |
9 | root.configure(bg="#151515")
10 |
11 | #creating a chart
12 | chart = tkchart.LineChart(master=root,
13 | x_axis_values = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
14 | y_axis_values = (0,100))
15 | chart.pack()
16 |
17 | #creating a line
18 | line1 = tkchart.Line(master=chart,
19 | size=3,
20 | color="#ef8ccb",
21 | style="normal",
22 | fill="enabled",
23 | fill_color="#b46d9a")
24 |
25 | line2 = tkchart.Line(master=chart,
26 | style="dashed",
27 | size=3,
28 | style_type=(10,5),
29 | )
30 |
31 | data = [x for x in range(0,100)]
32 | #dipslay data (random)
33 | def loop():
34 | chart.show_data(line=line1, data=random.choices(data, k=1))
35 | chart.show_data(line=line2, data=random.choices(data, k=1))
36 | root.after(500, loop)
37 | loop()
38 |
39 | root.mainloop()
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # Contributing to tkchart
2 |
3 | Thank you for considering contributing to tkchart! Please follow these guidelines to contribute effectively.
4 |
5 | ## Getting Started
6 |
7 | 1. Fork the repository.
8 | 2. Clone your forked repository: `git clone https://github.com/Thisal-D/tkchart.git`
9 | 3. Create a new branch for your changes: `git checkout -b feature-branch`
10 |
11 | ## Making Changes
12 |
13 | 1. Make your changes and ensure they follow the project's coding standards.
14 | 2. Test your changes locally.
15 | 3. Commit your changes: `git commit -m "Brief description of your changes"`
16 |
17 | ## Submitting Changes
18 |
19 | 1. Push your changes to your forked repository: `git push origin feature-branch`
20 | 2. Create a pull request on the main repository.
21 |
22 | ## Code of Conduct
23 |
24 | Please adhere to the [Code of Conduct](CODE_OF_CONDUCT.md) to maintain a respectful and inclusive community.
25 |
26 | ## Issues and Discussions
27 |
28 | If you encounter issues or have questions, please check the [issue tracker](https://github.com/Thisal-D/tkchart/issues) or start a discussion in the [GitHub Discussions](https://github.com/Thisal-D/tkchart/discussions) section.
29 |
30 | ## License
31 |
32 | By contributing, you agree that your contributions will be licensed under the project's [LICENSE](LICENSE).
33 |
34 | Thank you for your contribution!
--------------------------------------------------------------------------------
/template(s)/template (5).py:
--------------------------------------------------------------------------------
1 | import tkinter as tk
2 | import tkchart
3 | import random
4 |
5 | #root
6 | root = tk.Tk()
7 | root.configure(bg="#151515")
8 | root.geometry("700x400+500+300")
9 |
10 |
11 | #creating a chart
12 | chart = tkchart.LineChart(master=root,
13 | x_axis_values = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
14 | y_axis_values = (0,100))
15 | chart.pack()
16 |
17 | #creating a line
18 | line1 = tkchart.Line(master=chart,
19 | size=3,
20 | color="#ef8ccb",
21 | style="normal",
22 | fill="enabled",
23 | fill_color="#b46d9a")
24 |
25 | line2 = tkchart.Line(master=chart,
26 | style="dashed",
27 | size=3,
28 | style_type=(10,5),
29 | )
30 |
31 | line3 = tkchart.Line(master=chart,
32 | style="dotted",
33 | style_type=(6,10),
34 | color="#22eb48"
35 | )
36 |
37 | data = [x for x in range(0,100)]
38 | #dipslay data (random)
39 | def loop():
40 | chart.show_data(line=line1, data=random.choices(data, k=1))
41 | chart.show_data(line=line2, data=random.choices(data, k=1))
42 | chart.show_data(line=line3, data=random.choices(data, k=1))
43 |
44 | root.after(500, loop)
45 | loop()
46 |
47 | root.mainloop()
--------------------------------------------------------------------------------
/examples/4. change colors.py:
--------------------------------------------------------------------------------
1 | import tkchart
2 | import customtkinter
3 | import random
4 | import threading
5 | import time
6 |
7 | root = customtkinter.CTk()
8 | root.geometry("1280x720")
9 |
10 | # values for chart x axis
11 | x_axis_values = ('2020 Year', '2021 Year', '2022 Year', '2023 Year', '2024 Year')
12 | #create line chart
13 | linechart = tkchart.LineChart(master=root,
14 | y_axis_values=(0,1000),
15 | x_axis_values=x_axis_values,
16 |
17 | width=1000, height=500,
18 | axis_size=5,
19 |
20 |
21 | bg_color="#202020",
22 | fg_color="#202020",
23 |
24 | axis_color="#707070",
25 |
26 | x_axis_data_font_color="lightblue",
27 | y_axis_data_font_color="lightblue",
28 |
29 | x_axis_font_color="#707070",
30 | y_axis_font_color="#707070",
31 |
32 | x_axis_section_color="#404040",
33 | y_axis_section_color="#404040"
34 | )
35 | #place line chart
36 | linechart.place(x=50, y=50)
37 |
38 | #create line
39 | line = tkchart.Line(master=linechart, size=2, color="lightblue")
40 |
41 | #display data
42 | data = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
43 |
44 |
45 |
46 | def display_data():
47 | while True:
48 | #displaying data
49 | linechart.show_data(line=line, data=random.choices(data,k=1))
50 | time.sleep(0.5)
51 |
52 | threading.Thread(target=display_data).start()
53 |
54 | root.mainloop()
--------------------------------------------------------------------------------
/template(s)/template (8).py:
--------------------------------------------------------------------------------
1 | import tkinter as tk
2 | import tkchart
3 | import random
4 |
5 | #root
6 | root = tk.Tk()
7 | root.geometry("700x400+500+300")
8 | root.configure(bg="#151515")
9 |
10 | #creating a chart
11 | chart = tkchart.LineChart(master=root,
12 | width=1700,
13 | height=800,
14 | axis_size=5,
15 | x_axis_values = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
16 | y_axis_values = (-1000,1000),
17 | y_axis_label_count=10,
18 | x_axis_section_count=10,
19 | y_axis_section_count=10,
20 | x_axis_section_color="#555555",
21 | y_axis_section_color="#555555",
22 | data_font_style=("arial", 15, "bold"),
23 | axis_font_style=("arial", 11, "bold"),
24 | x_axis_font_color="#efefef",
25 | y_axis_font_color="#efefef",
26 | axis_color="#909090",
27 | x_axis_data_font_color="#efefef",
28 | y_axis_data_font_color="#efefef",
29 | y_space=20,
30 | x_space=20,
31 | )
32 | chart.pack(pady=20)
33 |
34 | #creating a line
35 | line1 = tkchart.Line(master=chart,
36 | size=3,
37 | style="normal",
38 | fill_color="#5d6db6",
39 | fill="enabled",
40 | point_highlight="enabled",
41 | point_highlight_size=15,
42 | )
43 |
44 |
45 |
46 |
47 | data = [x for x in range(-1000,1000)]
48 | #dipslay data (random)
49 | def loop():
50 | chart.show_data(line=line1, data=random.choices(data, k=1))
51 |
52 | root.after(500, loop)
53 | loop()
54 |
55 | root.mainloop()
--------------------------------------------------------------------------------
/template(s)/template (6).py:
--------------------------------------------------------------------------------
1 | import tkinter as tk
2 | import tkchart
3 | import random
4 |
5 | #root
6 | root = tk.Tk()
7 | root.geometry("700x400+500+300")
8 | root.configure(bg="#151515")
9 |
10 | #creating a chart
11 | chart = tkchart.LineChart(master=root,
12 | x_axis_values = ("100\nS", "200\nS", "300\nS", "400\nS", "500\nS", "600\nS", "700\nS", "800\nS", "900\nS"),
13 | y_axis_values = (0,100),
14 | y_axis_label_count=10,
15 | x_axis_section_count=10,
16 | y_axis_section_count=10,
17 | x_axis_section_color="#5d6db6",
18 | y_axis_section_color="#5d6db6",
19 | data_font_style=("arial", 10, "bold"),
20 | axis_font_style=("arial", 9, "italic"),
21 | x_axis_font_color="#efefef",
22 | y_axis_font_color="#efefef",
23 | axis_color="#5d6db6",
24 | x_axis_data_font_color="#efefef",
25 | y_axis_data_font_color="#efefef",
26 | y_space=20,
27 | x_space=20,
28 | x_axis_data="Seconds\n(s)",
29 | x_axis_data_position="side",
30 | y_axis_data_position="top",
31 |
32 |
33 | )
34 | chart.pack()
35 |
36 | #creating a line
37 | line1 = tkchart.Line(master=chart,
38 | size=3,
39 | style="normal",
40 | fill_color="#5d6db6",
41 | fill="enabled")
42 |
43 |
44 |
45 |
46 | data = [x for x in range(0,100)]
47 | #dipslay data (random)
48 | def loop():
49 | chart.show_data(line=line1, data=random.choices(data, k=1))
50 |
51 | root.after(500, loop)
52 | loop()
53 |
54 | root.mainloop()
--------------------------------------------------------------------------------
/examples/5. change fonts.py:
--------------------------------------------------------------------------------
1 | import tkchart
2 | import customtkinter
3 | import random
4 | import threading
5 | import time
6 |
7 | root = customtkinter.CTk()
8 | root.geometry("1280x720")
9 |
10 | # values for chart x axis
11 | x_axis_values = ('2020 Year', '2021 Year', '2022 Year', '2023 Year', '2024 Year')
12 | #create line chat
13 | linechart = tkchart.LineChart(master=root,
14 | y_axis_values=(0 ,1000),
15 | x_axis_values=x_axis_values,
16 |
17 | width=1000, height=500,
18 | axis_size=5,
19 |
20 |
21 | bg_color="#202020",
22 | fg_color="#202020",
23 |
24 | axis_color="#707070",
25 |
26 | x_axis_data_font_color="lightblue",
27 | y_axis_data_font_color="lightblue",
28 |
29 | x_axis_font_color="#707070",
30 | y_axis_font_color="#707070",
31 |
32 | x_axis_section_color="#404040",
33 | y_axis_section_color="#404040",
34 |
35 | data_font_style=("arial","20","bold"),
36 | axis_font_style=("arial","11","bold"),
37 |
38 |
39 | )
40 | #place line chart
41 | linechart.place(x=50, y=50)
42 |
43 | #create line
44 | line = tkchart.Line(master=linechart, size=2, color="lightblue")
45 |
46 | #display data
47 | data = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
48 |
49 |
50 |
51 | def display_data():
52 | while True:
53 | #displaying data
54 | linechart.show_data(line=line, data=random.choices(data,k=1))
55 | time.sleep(0.5)
56 |
57 | threading.Thread(target=display_data).start()
58 |
59 | root.mainloop()
--------------------------------------------------------------------------------
/examples/6. change sections labels.py:
--------------------------------------------------------------------------------
1 | import tkchart
2 | import customtkinter
3 | import random
4 | import threading
5 | import time
6 |
7 | root = customtkinter.CTk()
8 | root.geometry("1280x720")
9 |
10 | # values for chart x axis
11 | x_axis_values = ('2020 Year', '2021 Year', '2022 Year', '2023 Year', '2024 Year')
12 | #create line chart
13 | linechart = tkchart.LineChart(master=root,
14 | y_axis_values=(0 ,1000),
15 | x_axis_values=x_axis_values,
16 |
17 | width=1000, height=500,
18 | axis_size=5,
19 |
20 |
21 | bg_color="#202020",
22 | fg_color="#202020",
23 |
24 | axis_color="#707070",
25 |
26 | x_axis_data_font_color="lightblue",
27 | y_axis_data_font_color="lightblue",
28 |
29 | x_axis_font_color="#707070",
30 | y_axis_font_color="#707070",
31 |
32 | x_axis_section_color="#404040",
33 | y_axis_section_color="#404040",
34 |
35 | data_font_style=("arial","20","bold"),
36 | axis_font_style=("arial","11","bold"),
37 |
38 | x_axis_section_count=10,
39 | y_axis_section_count=10,
40 |
41 | y_axis_label_count=15,
42 | )
43 | #place line chart
44 | linechart.place(x=50, y=50)
45 |
46 | #create line
47 | line = tkchart.Line(master=linechart, size=2, color="lightblue")
48 |
49 | #display data
50 | data = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
51 |
52 |
53 |
54 | def display_data():
55 | while True:
56 | #displaying data
57 | linechart.show_data(line=line, data=random.choices(data,k=1))
58 | time.sleep(0.5)
59 |
60 | threading.Thread(target=display_data).start()
61 |
62 | root.mainloop()
--------------------------------------------------------------------------------
/examples/7. y axis precision.py:
--------------------------------------------------------------------------------
1 | import tkchart
2 | import customtkinter
3 | import random
4 | import threading
5 | import time
6 |
7 | root = customtkinter.CTk()
8 | root.geometry("1280x720")
9 |
10 | # values for chart x axis
11 | x_axis_values = ('2020 Year', '2021 Year', '2022 Year', '2023 Year', '2024 Year')
12 | #create line chart
13 | linechart = tkchart.LineChart(master=root,
14 | y_axis_values=(0, 1000),
15 | x_axis_values=x_axis_values,
16 |
17 | width=1000, height=500,
18 | axis_size=5,
19 |
20 |
21 | bg_color="#202020",
22 | fg_color="#202020",
23 |
24 | axis_color="#707070",
25 |
26 | x_axis_data_font_color="lightblue",
27 | y_axis_data_font_color="lightblue",
28 |
29 | x_axis_font_color="#707070",
30 | y_axis_font_color="#707070",
31 |
32 | x_axis_section_color="#404040",
33 | y_axis_section_color="#404040",
34 |
35 | data_font_style=("arial","20","bold"),
36 | axis_font_style=("arial","11","bold"),
37 |
38 | x_axis_section_count=10,
39 | y_axis_section_count=10,
40 |
41 | y_axis_label_count=15,
42 |
43 | y_axis_precision=3,
44 |
45 | )
46 | #place line chart
47 | linechart.place(x=50, y=50)
48 |
49 | #create line
50 | line = tkchart.Line(master=linechart, size=2, color="lightblue")
51 |
52 | #display data
53 | data = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
54 |
55 |
56 | def display_data():
57 | while True:
58 | #displaying data
59 | linechart.show_data(line=line, data=random.choices(data,k=1))
60 | time.sleep(0.5)
61 |
62 | threading.Thread(target=display_data).start()
63 |
64 | root.mainloop()
--------------------------------------------------------------------------------
/src/tkchart/FontStyle.py:
--------------------------------------------------------------------------------
1 | class FontStyle:
2 | _colors = {
3 | "black": ("30", "40"),
4 | "gray": ("90", "100"),
5 | "red": ("31", "41"),
6 | "green": ("32", "42"),
7 | "yellow": ("33", "43"),
8 | "blue": ("34", "44"),
9 | "magenta": ("35", "45"),
10 | "cyan": ("36", "46"),
11 | "white": ("37", "47"),
12 | "bright red": ("91", "101"),
13 | "bright green": ("92", "102"),
14 | "bright yellow": ("93", "103"),
15 | "bright blue": ("94", "104"),
16 | "bright magenta": ("95", "105"),
17 | "bright cyan": ("96", "106"),
18 | "bright white": ("97", "107"),
19 | }
20 |
21 | _styles = {
22 | "normal": "0",
23 | "italic": "3",
24 | "underline": "4"
25 | }
26 |
27 | @staticmethod
28 | def _get_font_style_code(fg_color: str, bg_color: str, style: str) -> str:
29 | """
30 | Returns the ANSI escape code for the given foreground color,
31 | background color, and style.
32 |
33 | Raises:
34 | ValueError: If any of the colors or style are invalid.
35 | """
36 | fg_color = fg_color.lower()
37 | bg_color = bg_color.lower()
38 | style = style.lower()
39 |
40 | if fg_color not in FontStyle._colors:
41 | raise ValueError(f"Invalid foreground color: {fg_color}")
42 | if bg_color not in FontStyle._colors:
43 | raise ValueError(f"Invalid background color: {bg_color}")
44 | if style not in FontStyle._styles:
45 | raise ValueError(f"Invalid style: {style}")
46 |
47 | fg_code = FontStyle._colors[fg_color][0]
48 | bg_code = FontStyle._colors[bg_color][1]
49 | style_code = FontStyle._styles[style]
50 |
51 | return f"\x1b[{style_code};{fg_code};{bg_code}m"
52 |
53 | @staticmethod
54 | def _apply(value: str, fg_color: str, bg_color: str, style: str = "normal") -> str:
55 | """
56 | Apply the font style to the given string.
57 |
58 | Args:
59 | value (str): The text to style.
60 | fg_color (str): Foreground color name.
61 | bg_color (str): Background color name.
62 | style (str, optional): Style name. Defaults to "normal".
63 |
64 | Returns:
65 | str: The styled text with ANSI escape codes.
66 | """
67 | code = FontStyle._get_font_style_code(fg_color, bg_color, style)
68 | reset = "\x1b[0m"
69 | return f"{code}{value}{reset}"
70 |
--------------------------------------------------------------------------------
/examples/8. y x space.py:
--------------------------------------------------------------------------------
1 | import tkchart
2 | import customtkinter
3 | import random
4 | import threading
5 | import time
6 |
7 | root = customtkinter.CTk()
8 | root.geometry("1280x720")
9 |
10 | # values for chart x axis
11 | x_axis_values = ('2020 Year', '2021 Year', '2022 Year', '2023 Year', '2024 Year')
12 | #create line chart
13 | linechart = tkchart.LineChart(master=root,
14 | y_axis_values=(0, 1000),
15 | x_axis_values=x_axis_values,
16 |
17 | width=1000, height=500,
18 | axis_size=5,
19 |
20 |
21 | bg_color="#202020",
22 | fg_color="#202020",
23 |
24 | axis_color="#707070",
25 |
26 | x_axis_data_font_color="lightblue",
27 | y_axis_data_font_color="lightblue",
28 |
29 | x_axis_font_color="#707070",
30 | y_axis_font_color="#707070",
31 |
32 | x_axis_section_color="#404040",
33 | y_axis_section_color="#404040",
34 |
35 | data_font_style=("arial","20","bold"),
36 | axis_font_style=("arial","11","bold"),
37 |
38 | x_axis_section_count=10,
39 | y_axis_section_count=10,
40 |
41 | y_axis_label_count=15,
42 |
43 | y_axis_precision=3,
44 |
45 | y_space=20,
46 | x_space=20
47 |
48 | )
49 | #place line chart
50 | linechart.place(x=50, y=50)
51 |
52 | #create line
53 | line = tkchart.Line(master=linechart, size=2, color="lightblue")
54 |
55 | #display data
56 | data = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
57 |
58 |
59 | def display_data():
60 | while True:
61 | #displaying data
62 | linechart.show_data(line=line, data=random.choices(data,k=1))
63 | time.sleep(0.5)
64 |
65 | threading.Thread(target=display_data).start()
66 |
67 | root.mainloop()
--------------------------------------------------------------------------------
/examples/9. data position.py:
--------------------------------------------------------------------------------
1 | import tkchart
2 | import customtkinter
3 | import random
4 | import threading
5 | import time
6 |
7 | root = customtkinter.CTk()
8 | root.geometry("1280x720")
9 |
10 | # values for chat x axis
11 | x_axis_values = ('2020 Year', '2021 Year', '2022 Year', '2023 Year', '2024 Year')
12 | #create line chart
13 | linechart = tkchart.LineChart(master=root,
14 | y_axis_values=(0, 1000),
15 | x_axis_values=x_axis_values,
16 |
17 | width=1000, height=500,
18 | axis_size=5,
19 |
20 |
21 | bg_color="#202020",
22 | fg_color="#202020",
23 |
24 | axis_color="#707070",
25 |
26 | x_axis_data_font_color="lightblue",
27 | y_axis_data_font_color="lightblue",
28 |
29 | x_axis_font_color="#707070",
30 | y_axis_font_color="#707070",
31 |
32 | x_axis_section_color="#404040",
33 | y_axis_section_color="#404040",
34 |
35 | data_font_style=("arial","20","bold"),
36 | axis_font_style=("arial","11","bold"),
37 |
38 | x_axis_section_count=10,
39 | y_axis_section_count=10,
40 |
41 | y_axis_label_count=15,
42 |
43 | y_axis_precision=3,
44 |
45 | y_space=20,
46 | x_space=20,
47 |
48 | x_axis_data_position="side",
49 | y_axis_data_position="side"
50 |
51 | )
52 | #place line chart
53 | linechart.place(x=50, y=50)
54 |
55 | #create line
56 | line = tkchart.Line(master=linechart, size=2, color="lightblue")
57 |
58 | #display data
59 | data = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
60 |
61 |
62 | def display_data():
63 | while True:
64 | #displaying data
65 | linechart.show_data(line=line, data=random.choices(data,k=1))
66 | time.sleep(0.5)
67 |
68 | threading.Thread(target=display_data).start()
69 |
70 | root.mainloop()
--------------------------------------------------------------------------------
/examples/10. change line style.py:
--------------------------------------------------------------------------------
1 | import tkchart
2 | import customtkinter
3 | import random
4 | import threading
5 | import time
6 |
7 | root = customtkinter.CTk()
8 | root.geometry("1280x720")
9 |
10 | # values for chart x axis
11 | x_axis_values = ('2020 Year', '2021 Year', '2022 Year', '2023 Year', '2024 Year')
12 | #create line chart
13 | linechart = tkchart.LineChart(master=root,
14 | y_axis_values=(0, 1000),
15 | x_axis_values=x_axis_values,
16 |
17 | width=1000, height=500,
18 | axis_size=5,
19 |
20 |
21 | bg_color="#202020",
22 | fg_color="#202020",
23 |
24 | axis_color="#707070",
25 |
26 | x_axis_data_font_color="lightblue",
27 | y_axis_data_font_color="lightblue",
28 |
29 | x_axis_font_color="#707070",
30 | y_axis_font_color="#707070",
31 |
32 | x_axis_section_color="#404040",
33 | y_axis_section_color="#404040",
34 |
35 | data_font_style=("arial","20","bold"),
36 | axis_font_style=("arial","11","bold"),
37 |
38 | x_axis_section_count=10,
39 | y_axis_section_count=10,
40 |
41 | y_axis_label_count=15,
42 |
43 | y_axis_precision=3,
44 |
45 | y_space=20,
46 | x_space=20,
47 |
48 | x_axis_data_position="side",
49 | y_axis_data_position="side"
50 | )
51 | #place line chart
52 | linechart.place(x=50, y=50)
53 |
54 | #create line
55 | line = tkchart.Line(master=linechart, size=2, color="lightblue" ,style="dashed", style_type=(10,10))
56 |
57 | #display data
58 | data = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
59 |
60 |
61 | def display_data():
62 | while True:
63 | #displaying data
64 | linechart.show_data(line=line, data=random.choices(data,k=1))
65 | time.sleep(0.5)
66 |
67 | threading.Thread(target=display_data).start()
68 |
69 | root.mainloop()
--------------------------------------------------------------------------------
/tests/test.py:
--------------------------------------------------------------------------------
1 | import random
2 |
3 | import customtkinter as ctk
4 |
5 | import tkchart
6 |
7 | root = ctk.CTk()
8 |
9 | charts: list[tkchart.LineChart] = []
10 | lines: list[tkchart.Line] = []
11 |
12 | line_chart = tkchart.LineChart(
13 | master=root,
14 | x_axis_values=(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
15 | y_axis_values=(0, 100),
16 | y_axis_label_count=10,
17 | width=500, height=200,
18 | axis_color="red", axis_size=10)
19 | line_chart.pack()
20 |
21 | line = tkchart.Line(master=line_chart, color="red")
22 | line2 = tkchart.Line(master=line_chart, color="blue")
23 | line_destroyed = False
24 | line2_destroyed = False
25 |
26 |
27 | def loop():
28 | global line2_destroyed, line_destroyed
29 | if not line_destroyed:
30 | line_chart.show_data(line=line, data=[random.choice(range(0, 101))])
31 | if not line2_destroyed:
32 | line_chart.show_data(line=line2, data=[random.choice(range(0, 101))])
33 |
34 | root.after(1000, loop)
35 |
36 |
37 | loop()
38 |
39 | line_chart2 = tkchart.LineChart(
40 | master=root,
41 | x_axis_values=(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
42 | y_axis_values=(0, 100),
43 | y_axis_label_count=10,
44 | width=500, height=200,
45 | axis_color="red", axis_size=10)
46 | line_chart2.pack()
47 |
48 | def hide(line: tkchart.Line):
49 | line.set_visible(False)
50 |
51 |
52 | def show(line: tkchart.Line):
53 | line.set_visible(True)
54 |
55 |
56 | def destroy1():
57 | global line_destroyed
58 | line_destroyed = True
59 | line.destroy()
60 |
61 |
62 | def destroy2():
63 | global line2_destroyed
64 | line2_destroyed = True
65 | line2.destroy()
66 |
67 | def hide_all():
68 | line_chart.set_lines_visibility(False)
69 |
70 |
71 | def show_all():
72 | line_chart.set_lines_visibility(True)
73 |
74 | def destroy_chart():
75 | line_chart.destroy()
76 |
77 | def destroy_chart2():
78 | line_chart2.destroy()
79 |
80 | def reset_chart():
81 | line_chart.reset()
82 |
83 | ctk.CTkButton(master=root, text="hide line1", command=lambda: hide(line)).pack()
84 | ctk.CTkButton(master=root, text="hide line2", command=lambda: hide(line2)).pack()
85 | ctk.CTkButton(master=root, text="show line1", command=lambda: show(line)).pack()
86 | ctk.CTkButton(master=root, text="show line2", command=lambda: show(line2)).pack()
87 | ctk.CTkButton(master=root, text="show lines", command=show_all).pack()
88 | ctk.CTkButton(master=root, text="hide lines", command=hide_all).pack()
89 |
90 | ctk.CTkButton(master=root, text="destroy line1", command=destroy1).pack()
91 | ctk.CTkButton(master=root, text="destroy line2", command=destroy2).pack()
92 | ctk.CTkButton(master=root, text="destroy chart", command=destroy_chart).pack()
93 | ctk.CTkButton(master=root, text="destroy chart2", command=destroy_chart2).pack()
94 | ctk.CTkButton(master=root, text="reset chart", command=reset_chart).pack()
95 |
96 |
97 | root.mainloop()
98 |
--------------------------------------------------------------------------------
/examples/12. change section style.py:
--------------------------------------------------------------------------------
1 | import tkchart
2 | import customtkinter
3 | import random
4 | import threading
5 | import time
6 |
7 | root = customtkinter.CTk()
8 | root.geometry("1280x720")
9 |
10 | # values for chart x axis
11 | x_axis_values = ('2020 Year', '2021 Year', '2022 Year', '2023 Year', '2024 Year')
12 | #create line chart
13 | linechart = tkchart.LineChart(master=root,
14 | y_axis_values=(0, 1000),
15 | x_axis_values=x_axis_values,
16 |
17 | width=1000, height=500,
18 | axis_size=5,
19 |
20 | bg_color="#202020",
21 | fg_color="#202020",
22 |
23 | axis_color="#707070",
24 |
25 | x_axis_data_font_color="lightblue",
26 | y_axis_data_font_color="lightblue",
27 |
28 | x_axis_font_color="#707070",
29 | y_axis_font_color="#707070",
30 |
31 | x_axis_section_color="#404040",
32 | y_axis_section_color="#404040",
33 |
34 | data_font_style=("arial","20","bold"),
35 | axis_font_style=("arial","11","bold"),
36 |
37 | x_axis_section_count=10,
38 | y_axis_section_count=10,
39 |
40 | y_axis_label_count=15,
41 |
42 | y_axis_precision=3,
43 |
44 | y_space=20,
45 | x_space=20,
46 |
47 | x_axis_data_position="side",
48 | y_axis_data_position="side",
49 |
50 | x_axis_section_style="dashed",
51 | y_axis_section_style="dashed",
52 |
53 | x_axis_section_style_type=(20,10),
54 | y_axis_section_style_type=(20,10),
55 |
56 | )
57 | #place line chart
58 | linechart.place(x=50, y=50)
59 |
60 | #create line
61 | line = tkchart.Line(master=linechart, size=2, color="lightblue" ,style="dashed", style_type=(10,10))
62 |
63 | #display data
64 | data = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
65 |
66 |
67 | def display_data():
68 | while True:
69 | #displaying data
70 | linechart.show_data(line=line, data=random.choices(data,k=1))
71 | time.sleep(0.5)
72 |
73 | threading.Thread(target=display_data).start()
74 |
75 | root.mainloop()
--------------------------------------------------------------------------------
/examples/1000. Complex.py:
--------------------------------------------------------------------------------
1 | import tkchart
2 | import customtkinter
3 | import random
4 | import threading
5 | import time
6 |
7 | root = customtkinter.CTk()
8 | root.geometry("1280x720")
9 |
10 |
11 | # values for chart x axis
12 | x_axis_values = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
13 | #create line chart
14 | linechart = tkchart.LineChart(master=root,
15 | y_axis_values=(0,1000),
16 | x_axis_values=x_axis_values,
17 |
18 | width=1000, height=500,
19 | axis_size=5,
20 |
21 |
22 | bg_color="#202020",
23 | fg_color="#202020",
24 |
25 | axis_color="#707070",
26 |
27 | x_axis_data_font_color="lightblue",
28 | y_axis_data_font_color="lightblue",
29 |
30 | x_axis_font_color="#707070",
31 | y_axis_font_color="#707070",
32 |
33 | x_axis_section_color="#404040",
34 | y_axis_section_color="#404040",
35 |
36 | data_font_style=("arial","20","bold"),
37 | axis_font_style=("arial","11","bold"),
38 |
39 | x_axis_section_count=10,
40 | y_axis_section_count=10,
41 | x_axis_label_count=5,
42 | y_axis_label_count=15,
43 |
44 | y_axis_precision=3,
45 |
46 | y_space=20,
47 | x_space=20,
48 |
49 | x_axis_data_position="side",
50 | y_axis_data_position="side"
51 | )
52 | #place line chart
53 | linechart.place(x=50, y=50)
54 |
55 | #create line
56 | line1 = tkchart.Line(master=linechart, size=2, color="lightblue")
57 | line2 = tkchart.Line(master=linechart, size=2, color="lightgreen" ,style="dashed", style_type=(5,7))
58 | line3 = tkchart.Line(master=linechart, size=2, color="pink" ,style="dotted", style_type=(4,7))
59 | #display data
60 | data = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
61 |
62 |
63 |
64 | count = 0
65 |
66 | def display_data():
67 | while True:
68 | global x_axis_values , count
69 |
70 | #displaying data
71 | linechart.show_data(line=line1, data=random.choices(data,k=1))
72 | linechart.show_data(line=line2, data=random.choices(data,k=1))
73 | linechart.show_data(line=line3, data=random.choices(data,k=1))
74 | if count > len(x_axis_values):
75 | x_axis_values = tuple([(x+1) for x in x_axis_values])
76 | linechart.configure(x_axis_values=x_axis_values)
77 | count += 1
78 |
79 | time.sleep(0.5)
80 |
81 | threading.Thread(target=display_data).start()
82 |
83 | root.mainloop()
--------------------------------------------------------------------------------
/src/tkchart/Utils.py:
--------------------------------------------------------------------------------
1 | import tkinter as tk
2 | from typing import Union, Tuple, Any, List
3 |
4 |
5 | class Utils:
6 |
7 | @staticmethod
8 | def _required_width(text: Any, font: Tuple[str, int, str]) -> int:
9 | """
10 | Get the required width of a label to display the given text with the specified font.
11 |
12 | Args:
13 | text (Any): Text to measure.
14 | font (Tuple[str, int, str]): Font specification (family, size, style).
15 |
16 | Returns:
17 | int: Required label width in pixels.
18 | """
19 | label = tk.Label(font=font)
20 | label.config(text=str(text))
21 | return label.winfo_reqwidth()
22 |
23 | @staticmethod
24 | def _required_height(text: Any, font: Tuple[str, int, str]) -> int:
25 | """
26 | Get the required height of a label to display the given text with the specified font.
27 |
28 | Args:
29 | text (Any): Text to measure.
30 | font (Tuple[str, int, str]): Font specification (family, size, style).
31 |
32 | Returns:
33 | int: Required label height in pixels.
34 | """
35 | label = tk.Label(font=font)
36 | label.config(text=str(text))
37 | return label.winfo_reqheight()
38 |
39 | @staticmethod
40 | def _format_float_with_precision(float_val: Union[int, float], decimals: int) -> str:
41 | """
42 | Format a float or int value as a string with a specified number of decimal places.
43 |
44 | Args:
45 | float_val (int | float): Number to format.
46 | decimals (int): Number of decimal places.
47 |
48 | Returns:
49 | str: Number formatted as a string with exact decimals.
50 | """
51 | if decimals > 0:
52 | rounded = round(float(float_val), decimals)
53 | integer_part, dot, fraction_part = str(rounded).partition(".")
54 | fraction_part = fraction_part.ljust(decimals, "0")
55 | return f"{integer_part}.{fraction_part}"
56 | return str(int(float_val))
57 |
58 | @staticmethod
59 | def _get_max_required_label_width(data: List[Any], font: Tuple[str, int, str]) -> int:
60 | """
61 | Calculate the maximum label width needed to display all data items with given font.
62 |
63 | Args:
64 | data (List[Any]): List of data items to measure.
65 | font (Tuple[str, int, str]): Font specification.
66 |
67 | Returns:
68 | int: Maximum required label width in pixels.
69 | """
70 | return max(Utils._required_width(text=d, font=font) for d in data)
71 |
72 | @staticmethod
73 | def _get_max_required_label_height(data: List[Any], font: Tuple[str, int, str]) -> int:
74 | """
75 | Calculate the maximum label height needed to display all data items with given font.
76 |
77 | Args:
78 | data (List[Any]): List of data items to measure.
79 | font (Tuple[str, int, str]): Font specification.
80 |
81 | Returns:
82 | int: Maximum required label height in pixels.
83 | """
84 | return max(Utils._required_height(text=d, font=font) for d in data)
85 |
86 | @staticmethod
87 | def _sort_tuple(values: Tuple[int, ...]) -> Tuple[int, ...]:
88 | """
89 | Sort a tuple of integers and remove duplicates.
90 |
91 | Args:
92 | values (Tuple[int, ...]): Tuple of integers.
93 |
94 | Returns:
95 | Tuple[int, ...]: Sorted tuple with unique integers.
96 | """
97 | return tuple(sorted(set(values)))
98 |
99 | @staticmethod
100 | def _to_int(value: Union[int, str]) -> int:
101 | """
102 | Convert a string or integer to an integer.
103 |
104 | Args:
105 | value (Union[int, str]): Value to convert.
106 |
107 | Returns:
108 | int: Converted integer.
109 | """
110 | return int(value)
111 |
--------------------------------------------------------------------------------
/examples/11. pointing values.py:
--------------------------------------------------------------------------------
1 | import tkchart
2 | import customtkinter
3 | import random
4 |
5 | root = customtkinter.CTk()
6 |
7 | root.geometry("1048x599+442+239")
8 |
9 |
10 | def func(x, y):
11 | label.configure(text=y[0])
12 | label2.configure(text=y[1])
13 | label3.configure(text=y[2])
14 | label4.configure(text=y[3])
15 |
16 |
17 | linechart = tkchart.LineChart(master=root,
18 | width=1500,
19 | height=700,
20 | axis_size=5,
21 |
22 | y_axis_section_count=10,
23 | x_axis_section_count=0,
24 | y_axis_label_count=10,
25 |
26 | y_axis_data="GB",
27 | x_axis_data="S",
28 | x_axis_values=tuple([x for x in range(1,11,1)]),
29 | x_axis_label_count=10,
30 | y_axis_values=(0,1000),
31 | y_axis_precision=4,
32 |
33 | x_axis_section_color="#404040",
34 | y_axis_section_color="#404040",
35 | y_axis_font_color="#707070",
36 | x_axis_font_color="#707070",
37 | x_axis_data_font_color="lightblue",
38 | y_axis_data_font_color="lightblue",
39 | bg_color="#202020",
40 | fg_color="#202020",
41 | axis_color="#707070",
42 |
43 | data_font_style= ("Arial", 15,"bold"),
44 | axis_font_style= ("Arial", 10,"bold"),
45 |
46 | pointing_callback_function = func,
47 | pointer_color="#00ffff",
48 | pointer_state="enabled",
49 | pointing_values_precision=4
50 | )
51 |
52 | linechart.place(x=100, y=10)
53 |
54 |
55 | line = tkchart.Line(master=linechart,
56 | color="#ff0000",
57 | size=1,
58 | style="normal",
59 | style_type=(4,10),
60 | )
61 |
62 | line2 = tkchart.Line(master=linechart,
63 | color="#00ff00",
64 | size=1,
65 | style="normal",
66 | style_type=(4,10))
67 |
68 | line3 = tkchart.Line(master=linechart,
69 | color="#0000ff",
70 | size=1,
71 | style="normal",
72 | style_type=(4,10))
73 |
74 | line4 = tkchart.Line(master=linechart,
75 | color="#ffff00",
76 | size=1,
77 | style="normal",
78 | style_type=(4,10))
79 |
80 | frame = customtkinter.CTkFrame(master=root, width=30, height=30, fg_color="#ff0000")
81 | frame.place(x=1650, y=300)
82 | label = customtkinter.CTkLabel(master=root, font=("arial",13, "bold"), text="")
83 | label.place(x=1700, y=300)
84 |
85 | frame2 = customtkinter.CTkFrame(master=root, width=30, height=30, fg_color="#00ff00")
86 | frame2.place(x=1650, y=350)
87 | label2 = customtkinter.CTkLabel(master=root, font=("arial",13, "bold"), text="")
88 | label2.place(x=1700, y=350)
89 |
90 | frame3 = customtkinter.CTkFrame(master=root, width=30, height=30, fg_color="#0000ff")
91 | frame3.place(x=1650, y=400)
92 | label3 = customtkinter.CTkLabel(master=root, font=("arial",13, "bold"), text="")
93 | label3.place(x=1700, y=400)
94 |
95 | frame4 = customtkinter.CTkFrame(master=root, width=30, height=30, fg_color="#ffff00")
96 | frame4.place(x=1650, y=450)
97 | label4 = customtkinter.CTkLabel(master=root, font=("arial",13, "bold"), text="")
98 | label4.place(x=1700, y=450)
99 |
100 |
101 | data = [0,100,200,300,400,500,600,700,800,900,1000]
102 | def loop2():
103 | if start:
104 | linechart.show_data(data=[random.choice(data)], line=line)
105 | linechart.show_data(data=[random.choice(data)], line=line2)
106 | linechart.show_data(data=[random.choice(data)], line=line3)
107 | linechart.show_data(data=[random.choice(data)], line=line4)
108 | root.after(1000,loop2)
109 |
110 | start = True
111 | def stop_start():
112 | global start
113 | if start:
114 | start= False
115 | else:
116 | start= True
117 | #calling to loop
118 | btn = customtkinter.CTkButton(master=root, text="Stop/Start", command=stop_start)
119 | btn.place(x=1700,y=600)
120 |
121 | loop2()
122 | root.mainloop()
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Contributor Covenant Code of Conduct
2 |
3 | ## Our Pledge
4 |
5 | We as members, contributors, and leaders pledge to make participation in our
6 | community a harassment-free experience for everyone, regardless of age, body
7 | size, visible or invisible disability, ethnicity, sex characteristics, gender
8 | identity and expression, level of experience, education, socio-economic status,
9 | nationality, personal appearance, race, religion, or sexual identity
10 | and orientation.
11 |
12 | We pledge to act and interact in ways that contribute to an open, welcoming,
13 | diverse, inclusive, and healthy community.
14 |
15 | ## Our Standards
16 |
17 | Examples of behavior that contributes to a positive environment for our
18 | community include:
19 |
20 | * Demonstrating empathy and kindness toward other people
21 | * Being respectful of differing opinions, viewpoints, and experiences
22 | * Giving and gracefully accepting constructive feedback
23 | * Accepting responsibility and apologizing to those affected by our mistakes,
24 | and learning from the experience
25 | * Focusing on what is best not just for us as individuals, but for the
26 | overall community
27 |
28 | Examples of unacceptable behavior include:
29 |
30 | * The use of sexualized language or imagery, and sexual attention or
31 | advances of any kind
32 | * Trolling, insulting or derogatory comments, and personal or political attacks
33 | * Public or private harassment
34 | * Publishing others' private information, such as a physical or email
35 | address, without their explicit permission
36 | * Other conduct which could reasonably be considered inappropriate in a
37 | professional setting
38 |
39 | ## Enforcement Responsibilities
40 |
41 | Community leaders are responsible for clarifying and enforcing our standards of
42 | acceptable behavior and will take appropriate and fair corrective action in
43 | response to any behavior that they deem inappropriate, threatening, offensive,
44 | or harmful.
45 |
46 | Community leaders have the right and responsibility to remove, edit, or reject
47 | comments, commits, code, wiki edits, issues, and other contributions that are
48 | not aligned to this Code of Conduct, and will communicate reasons for moderation
49 | decisions when appropriate.
50 |
51 | ## Scope
52 |
53 | This Code of Conduct applies within all community spaces, and also applies when
54 | an individual is officially representing the community in public spaces.
55 | Examples of representing our community include using an official e-mail address,
56 | posting via an official social media account, or acting as an appointed
57 | representative at an online or offline event.
58 |
59 | ## Enforcement
60 |
61 | Instances of abusive, harassing, or otherwise unacceptable behavior may be
62 | reported to the community leaders responsible for enforcement at
63 | .
64 | All complaints will be reviewed and investigated promptly and fairly.
65 |
66 | All community leaders are obligated to respect the privacy and security of the
67 | reporter of any incident.
68 |
69 | ## Enforcement Guidelines
70 |
71 | Community leaders will follow these Community Impact Guidelines in determining
72 | the consequences for any action they deem in violation of this Code of Conduct:
73 |
74 | ### 1. Correction
75 |
76 | **Community Impact**: Use of inappropriate language or other behavior deemed
77 | unprofessional or unwelcome in the community.
78 |
79 | **Consequence**: A private, written warning from community leaders, providing
80 | clarity around the nature of the violation and an explanation of why the
81 | behavior was inappropriate. A public apology may be requested.
82 |
83 | ### 2. Warning
84 |
85 | **Community Impact**: A violation through a single incident or series
86 | of actions.
87 |
88 | **Consequence**: A warning with consequences for continued behavior. No
89 | interaction with the people involved, including unsolicited interaction with
90 | those enforcing the Code of Conduct, for a specified period of time. This
91 | includes avoiding interactions in community spaces as well as external channels
92 | like social media. Violating these terms may lead to a temporary or
93 | permanent ban.
94 |
95 | ### 3. Temporary Ban
96 |
97 | **Community Impact**: A serious violation of community standards, including
98 | sustained inappropriate behavior.
99 |
100 | **Consequence**: A temporary ban from any sort of interaction or public
101 | communication with the community for a specified period of time. No public or
102 | private interaction with the people involved, including unsolicited interaction
103 | with those enforcing the Code of Conduct, is allowed during this period.
104 | Violating these terms may lead to a permanent ban.
105 |
106 | ### 4. Permanent Ban
107 |
108 | **Community Impact**: Demonstrating a pattern of violation of community
109 | standards, including sustained inappropriate behavior, harassment of an
110 | individual, or aggression toward or disparagement of classes of individuals.
111 |
112 | **Consequence**: A permanent ban from any sort of public interaction within
113 | the community.
114 |
115 | ## Attribution
116 |
117 | This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118 | version 2.0, available at
119 | https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
120 |
121 | Community Impact Guidelines were inspired by [Mozilla's code of conduct
122 | enforcement ladder](https://github.com/mozilla/diversity).
123 |
124 | [homepage]: https://www.contributor-covenant.org
125 |
126 | For answers to common questions about this code of conduct, see the FAQ at
127 | https://www.contributor-covenant.org/faq. Translations are available at
128 | https://www.contributor-covenant.org/translations.
129 |
--------------------------------------------------------------------------------
/CHANGES_zh.md:
--------------------------------------------------------------------------------
1 | [](CHANGES_en.md)
2 |
3 | ## v2.1.6
4 |
5 | - ### 新方法添加到 `LineChart` 对象
6 | | 方法名 | 描述 | 参数 | 返回类型 |
7 | |------------------------------|------------------------------------------------------------|------------------------------------------|-----------------|
8 | | `get_lines_data` | 获取指定范围内所有线条的数据点,可以选择步长值。 | start: `int`
end: `int`
step: `int` | `Dict[tkchart.Line, Tuple[int]]` |
9 | | `get_line_data` | 获取指定范围和步长值下某一条线的数据点。 | line: `tkchart.Line`
start: `int`
end: `int`
step: `int` | `Tuple[int \| float]` |
10 | | `get_x_axis_visible_point_count` | 获取X轴上可见数据点的最大数量。 | - | `int` |
11 | | `get_lines_visible_data` | 获取所有线条当前可见的数据点,基于最大数据长度和可见点数。 | - | `Dict[tkchart.Line, Tuple[int \| float]]` |
12 | | `get_line_visible_data` | 获取某一条线当前可见的数据点。 | line: `tkchart.Line` | `Tuple[int \| float]` |
13 |
14 | - ### 新方法添加到 `Line` 对象
15 | | 方法名 | 描述 | 参数 | 返回类型 |
16 | |------------------------------|------------------------------------------------------------|------------------------------------------|-----------------|
17 | | `get_data` | 获取指定范围的数据点,可以选择步长值。如果没有提供参数,将返回所有可用数据。 | start: `int`
end: `int`
step: `int` | `Tuple[int \| float]` |
18 | | `get_current_visible_data` | 根据所有线条的最大数据长度和最大可见点数,返回当前可见的数据点。 | - | `Tuple[int \| float]` |
19 | | `get_x_axis_visible_point_count` | 获取X轴上可见数据点的最大数量。 | - | `int` |
20 |
21 |
22 | ## v2.1.5
23 |
24 | - ### 新增方法到 `LineChart` 对象
25 | | 方法名称 | 描述 | 参数 | 返回类型 |
26 | |------------------|------------------------------------------------------------|----------------|-------------|
27 | | `clear_data` | 清除图表中所有线的数据,确保只保留最新的可见数据点。如果数据点总数超过最大可见点,则会从每条线的数据中移除旧数据。此方法确保图表仅显示基于最大可见范围的相关数据部分。 | - | `None` |
28 |
29 | - ### 新增方法到 `Line` 对象
30 | | 方法名称 | 描述 | 参数 | 返回类型 |
31 | |------------------|------------------------------------------------------------|----------------|-------------|
32 | | `clear_data` | 清除特定线的数据,确保只保留最新的可见数据点。如果线的数据超过最大可见点,则会修剪旧数据。此方法允许每条线独立清除其数据,确保它始终保持在可见范围内。 | - | `None` |
33 |
34 | ---
35 |
36 | ## v2.1.4
37 |
38 | - ### 新增方法到 `LineChart` 对象
39 | | 方法名称 | 描述 | 参数 | 返回类型 |
40 | |------------------|------------------------------------------------------------|----------------|-------------|
41 | | `get_line_area` | 获取特定线的区域大小 | line: `tkchart.Line` | `float` |
42 | | `get_lines_area` | 获取所有线的区域大小 | - | `float` |
43 |
44 | ---
45 |
46 | ## v2.1.3
47 |
48 | - ### 新增方法到 `LineChart` 对象
49 | | 方法名称 | 描述 | 参数 | 返回类型 |
50 | |------------------|------------------------------------------------------------|----------------|-------------|
51 | | `destroy` | 销毁线图及其所有线 | - | `None` |
52 |
53 | - ### 新增方法到 `Line` 对象
54 | | 方法名称 | 描述 | 参数 | 返回类型 |
55 | |------------------|------------------------------------------------------------|----------------|-------------|
56 | | `destroy` | 销毁线对象 | - | `None` |
57 |
58 | ---
59 |
60 | ## v2.1.2
61 |
62 | - ### 新增方法到 `Line` 对象
63 |
64 | | 方法名称 | 描述 | 参数 | 返回类型 |
65 | |------------------|------------------------------------------------|------------------------------------------|-------------|
66 | | `cget` | 获取指定参数的值 | attribute_name: `str \| "__all__"` | `any` |
67 | | `set_visible` | 更改线的可见性 | state: `bool` | `None` |
68 | | `get_visibility` | 获取线的可见性 | - | `bool` |
69 |
70 | - ### 新增方法到 `LineChart` 对象
71 |
72 | | 方法名称 | 描述 | 参数 | 返回类型 |
73 | |------------------------|------------------------------------------------|--------------------------------------------------|-------------|
74 | | `set_lines_visibility` | 更改所有线的可见性 | state: `bool` | `None` |
75 | | `set_line_visibility` | 更改特定线的可见性 | line: `tkchart.Line`
state: `bool` | `None` |
76 | | `get_line_visibility` | 获取特定线的可见性 | line: `tkchart.Line` | `bool` |
77 | | `cget` | 获取指定参数的值 | attribute_name: `str \| "__all__"` | `any` |
78 | | `place_info` | 获取位置相关信息 | attribute_name: `str \| "__all__"` | `any` |
79 | | `pack_info` | 获取打包相关信息 | attribute_name: `str \| "__all__"` | `any` |
80 | | `grid_info` | 获取网格相关信息 | attribute_name: `str \| "__all__"` | `any` |
81 |
82 | - ### 移除 `LineChart` 对象的方法
83 |
84 | | 方法名称 | 描述 | 参数 | 返回类型 |
85 | |-------------|----------------------|----------------------------------------------|-------------|
86 | | hide_all | 隐藏所有的线 | state: `bool` | None |
87 | | hide | 隐藏特定的线 | line: `tkchart.Line`
state: `bool` | None |
88 |
--------------------------------------------------------------------------------
/CHANGES_en.md:
--------------------------------------------------------------------------------
1 | [](CHANGES_zh.md)
2 |
3 |
4 | ## v2.1.6
5 |
6 | - ### New Methods Added to `LineChart` Object
7 | | Method Name | Description | Parameters | Return Type |
8 | |------------------------------|------------------------------------------------------------|------------------------------------------|-----------------|
9 | | `get_lines_data` | Retrieves data points for all lines within a specified range with an optional step value. | start: `int`
end: `int`
step: `int` | `Dict[tkchart.Line, Tuple[int]]` |
10 | `get_line_data` | Retrieves data points for a specific line within a specified range and step. | line: `tkchart.Line`
start: `int`
end: `int`
step: `int` | `Tuple[int \| float]` |
11 | | `get_x_axis_visible_point_count` | Retrieves the maximum number of data points that can be visible along the X-axis. | - | `int` |
12 | | `get_lines_visible_data` | Retrieves currently visible data points for all lines based on the maximum data length and visible points. | - | `Dict[tkchart.Line, Tuple[int \| float]]` |
13 | | `get_line_visible_data` | Retrieves currently visible data points for a specific line. | line: `tkchart.Line` | `Tuple[int \| float]` |
14 |
15 |
16 |
17 | - ### New Methods Added to `Line` Object
18 | | Method Name | Description | Parameters | Return Type |
19 | |------------------------------|------------------------------------------------------------|---------------------|-----------------|
20 | | `get_data` | Retrieves data points from a specified range with an optional step value. If no parameters are given, it returns all available data. | start: `int`
end: `int`
step: `int` | `Tuple[int \| float]` |
21 | | `get_current_visible_data` | Returns the currently visible data points based on the maximum data length across all lines and the maximum number of visible points. | - | `Tuple[int \| float]` |
22 | | `get_x_axis_visible_point_count` | Retrieves the maximum number of data points that can be visible along the X-axis. | - | `int` |
23 |
24 |
25 | ## v2.1.5
26 |
27 | - ### New Method Added to `LineChart` Object
28 | | Method Name | Description | Parameters | Return Type |
29 | |------------------|------------------------------------------------------------|----------------|-------------|
30 | | `clear_data` | Clears the data for all lines within the chart, ensuring that only the most recent visible data points are retained. If the total data points exceed the maximum visible points, the older data is removed from each line's data. This method ensures that the chart displays only the relevant portion of data based on the maximum visible range. | - | ``None`` |
31 |
32 | - ### New Method Added to `Line` Object
33 | | Method Name | Description | Parameters | Return Type |
34 | |------------------|------------------------------------------------------------|----------------|-------------|
35 | | `clear_data` | Clears the data for a specific line, ensuring that only the most recent visible data points are retained. If the line's data exceeds the maximum visible points, the older data is trimmed. This method allows each line to independently clean its data, ensuring it remains within the visible range. | - | ``None`` |
36 |
37 | ---
38 |
39 | ## v2.1.4
40 |
41 | - ### New Methods Added to `LineChart` Object
42 | | Method Name | Description | Parameters | Return Type |
43 | |------------------|------------------------------------------------------------|----------------|-------------|
44 | | `get_line_area` | Get the are of specific line | line: `tkchart.Line` | ``float`` |
45 | | `get_lines_area` | Get the are of all lines | - | ``float`` |
46 |
47 | ---
48 |
49 | ## v2.1.3
50 |
51 | - ### New Method Added to `LineChart` Object
52 | | Method Name | Description | Parameters | Return Type |
53 | |------------------|------------------------------------------------------------|----------------|-------------|
54 | | `destroy` | Destroy the line chart, along with its lines | - | `None` |
55 |
56 | - ### New Method Added to `Line` Object
57 | | Method Name | Description | Parameters | Return Type |
58 | |------------------|------------------------------------------------------------|----------------|-------------|
59 | | `destroy` | Destroy the line object | - | `None` |
60 |
61 | ---
62 |
63 | ## v2.1.2
64 |
65 | - ### New Method Added to `Line` Object
66 |
67 | | Method Name | Description | Parameters | Return Type |
68 | |------------------|------------------------------------------------|------------------------------------------|-------------|
69 | | `cget` | Get the value of the specified parameter | attribute_name: `str \| "__all__"` | `any` |
70 | | `set_visible` | Change the visibility of the line | state: `bool` | `None` |
71 | | `get_visibility` | Get the visibility of the line | - | `bool` |
72 |
73 | - ### New Methods Added to `LineChart` Object
74 |
75 | | Method Name | Description | Parameters | Return Type |
76 | |------------------------|------------------------------------------------|--------------------------------------------------|-------------|
77 | | `set_lines_visibility` | Change the visibility of all the lines | state: `bool` | `None` |
78 | | `set_line_visibility` | Change the visibility of a specific line | line: `tkchart.Line`
state: `bool` | `None` |
79 | | `get_line_visibility` | Get the visibility of a specific line | line: `tkchart.Line` | `bool` |
80 | | `cget` | Get the value of the specified parameter | attribute_name: `str \| "__all__"` | `any` |
81 | | `place_info` | Get info about place | attribute_name: `str \| "__all__"` | `any` |
82 | | `pack_info` | Get info about pack | attribute_name: `str \| "__all__"` | `any` |
83 | | `grid_info` | Get info about grid | attribute_name: `str \| "__all__"` | `any` |
84 |
85 | - ### Removed Methods in `LineChart` Object
86 |
87 | | Method Name | Description | Parameters | Return Type |
88 | |-------------|----------------------|----------------------------------------------|-------------|
89 | | hide_all | Hide all the lines | state: ``bool`` | None |
90 | | hide | hide a specific line | line: ``tkchart.Line``
state: ``bool`` | None |
--------------------------------------------------------------------------------
/src/tkchart/Validate.py:
--------------------------------------------------------------------------------
1 | import tkinter as tk
2 | from typing import Any, Tuple
3 | from .FontStyle import FontStyle
4 |
5 |
6 | class Validate:
7 |
8 | @staticmethod
9 | def _error_font(value: str) -> str:
10 | """Return a styled error message."""
11 | return FontStyle._apply(value, "red", "black", "underline")
12 |
13 | @staticmethod
14 | def _var_font(value: str) -> str:
15 | """Return a styled variable name."""
16 | return FontStyle._apply(value, "green", "black", "italic")
17 |
18 | # --- Basic Type Checkers ---
19 |
20 | @staticmethod
21 | def _is_tuple(value: Any, var: str) -> None:
22 | """Check if value is a tuple."""
23 | if not isinstance(value, tuple):
24 | raise TypeError(f"{Validate._var_font(var)} {Validate._error_font('must be a tuple.')}")
25 |
26 | @staticmethod
27 | def _is_list(value: Any, var: str) -> None:
28 | """Check if value is a list."""
29 | if not isinstance(value, list):
30 | raise TypeError(f"{Validate._var_font(var)} {Validate._error_font('must be a list.')}")
31 |
32 | @staticmethod
33 | def _is_int(value: Any, var: str) -> None:
34 | """Check if value is an integer."""
35 | if not isinstance(value, int):
36 | raise TypeError(f"{Validate._var_font(var)} {Validate._error_font('must be an int.')}")
37 |
38 | @staticmethod
39 | def _is_bool(value: Any, var: str) -> None:
40 | """Check if value is a boolean."""
41 | if not isinstance(value, bool):
42 | raise TypeError(f"{Validate._var_font(var)} {Validate._error_font('must be a bool.')}")
43 |
44 | @staticmethod
45 | def _is_float(value: Any, var: str) -> None:
46 | """Check if value is a float."""
47 | if not isinstance(value, float):
48 | raise TypeError(f"{Validate._var_font(var)} {Validate._error_font('must be a float.')}")
49 |
50 | @staticmethod
51 | def _is_str(value: Any, var: str) -> None:
52 | """Check if value is a string."""
53 | if not isinstance(value, str):
54 | raise TypeError(f"{Validate._var_font(var)} {Validate._error_font('must be a str.')}")
55 |
56 | # --- Complex Validators ---
57 |
58 | @staticmethod
59 | def _is_valid_color(value: Any, var: str) -> None:
60 | """Check if value is a valid color string."""
61 | Validate._is_str(value, var)
62 | try:
63 | tk.Label(fg=value)
64 | except tk.TclError:
65 | raise ValueError(
66 | f"{Validate._var_font(var)} {Validate._error_font('must be a valid color, e.g. \'red\' or \'#ff0000\'.')}"
67 | )
68 |
69 | @staticmethod
70 | def _is_valid_font(value: Any, var: str) -> None:
71 | """Check if value is a valid font tuple."""
72 | Validate._is_tuple(value, var)
73 | try:
74 | tk.Label(font=value)
75 | except tk.TclError:
76 | raise ValueError(
77 | f"{Validate._var_font(var)} {Validate._error_font('must be a valid font, e.g. (\'Arial\', 10, \'bold\').')}"
78 | )
79 |
80 | @staticmethod
81 | def _is_valid_function(value: Any, var: str) -> None:
82 | """Check if value is callable or None."""
83 | if value is not None and not callable(value):
84 | raise TypeError(
85 | f"{Validate._var_font(var)} {Validate._error_font('must be a callable function or None.')}"
86 | )
87 |
88 | @staticmethod
89 | def _is_valid_indices(value: Any, var: str) -> None:
90 | """Check if all values in iterable are integers."""
91 | if not all(isinstance(v, int) for v in value):
92 | raise TypeError(f"{Validate._var_font(var)} {Validate._error_font('all values must be int.')}")
93 |
94 | @staticmethod
95 | def _is_valid_x_axis_indices(values: Tuple[int, ...], indices: Any, var: str) -> None:
96 | """Validate that indices are within bounds of values."""
97 | if indices is not None:
98 | Validate._is_tuple(indices, var)
99 | Validate._is_valid_indices(indices, var)
100 | for index in indices:
101 | if index >= len(values):
102 | raise ValueError(
103 | f"{Validate._var_font(var)} {Validate._error_font('index must be less than length of x_axis_values.')}"
104 | )
105 |
106 | @staticmethod
107 | def _is_valid_x_axis_label_count(value: Any, var: str) -> None:
108 | """Check if x-axis label count is an int if provided."""
109 | if value is not None:
110 | Validate._is_int(value, var)
111 |
112 | @staticmethod
113 | def _is_valid_style_type(value: Any, var: str) -> None:
114 | """Check if style type is a tuple of two integers."""
115 | Validate._is_tuple(value, var)
116 | if len(value) != 2 or not all(isinstance(v, int) for v in value):
117 | raise TypeError(
118 | f"{Validate._var_font(var)} {Validate._error_font('must be a tuple of two integers.')}"
119 | )
120 |
121 | @staticmethod
122 | def _is_valid_data_position(value: Any, var: str) -> None:
123 | """Check if data position is 'top' or 'side'."""
124 | Validate._is_str(value, var)
125 | if value not in {"top", "side"}:
126 | raise ValueError(
127 | f"{Validate._var_font(var)} {Validate._error_font('must be \'top\' or \'side\'.')}"
128 | )
129 |
130 | @staticmethod
131 | def _is_valid_line_style(value: Any, var: str) -> None:
132 | """Check if line style is one of the accepted strings."""
133 | Validate._is_str(value, var)
134 | if value not in {"normal", "dashed", "dotted"}:
135 | raise ValueError(
136 | f"{Validate._var_font(var)} {Validate._error_font('must be \'normal\', \'dashed\', or \'dotted\'.')}"
137 | )
138 |
139 | @staticmethod
140 | def _is_valid_section_style(value: Any, var: str) -> None:
141 | """Check if section style is either 'normal' or 'dashed'."""
142 | Validate._is_str(value, var)
143 | if value not in {"normal", "dashed"}:
144 | raise ValueError(
145 | f"{Validate._var_font(var)} {Validate._error_font('must be \'normal\' or \'dashed\'.')}"
146 | )
147 |
148 | @staticmethod
149 | def _is_valid_x_axis_point_spacing(value: Any, var: str) -> None:
150 | """Check if x-axis point spacing is int or 'auto'."""
151 | if not (isinstance(value, int) or (isinstance(value, str) and value == "auto")):
152 | raise TypeError(
153 | f"{Validate._var_font(var)} {Validate._error_font('must be int or \'auto\'.')}"
154 | )
155 |
156 | @staticmethod
157 | def _is_valid_pointer_state_lock(value: Any, var: str) -> None:
158 | """Check if pointer lock state is 'enabled' or 'disabled'."""
159 | Validate._is_str(value, var)
160 | if value not in {"enabled", "disabled"}:
161 | raise ValueError(
162 | f"{Validate._var_font(var)} {Validate._error_font('must be \'enabled\' or \'disabled\'.')}"
163 | )
164 |
165 | @staticmethod
166 | def _is_valid_line_highlight(value: Any, var: str) -> None:
167 | """Validate line highlight state."""
168 | Validate._is_valid_pointer_state_lock(value, var)
169 |
170 | @staticmethod
171 | def _is_valid_line_fill(value: Any, var: str) -> None:
172 | """Validate line fill state."""
173 | Validate._is_valid_pointer_state_lock(value, var)
174 |
175 | @staticmethod
176 | def _is_valid_y_axis_values(value: Any, var: str) -> None:
177 | """Validate y-axis values as a tuple of two numbers, first less than second."""
178 | Validate._is_tuple(value, var)
179 | if value == (None, None):
180 | raise ValueError(f"{Validate._var_font(var)} {Validate._error_font('must be provided.')}")
181 | if len(value) != 2 or not all(isinstance(v, (int, float)) for v in value):
182 | raise TypeError(
183 | f"{Validate._var_font(var)} {Validate._error_font('must be a tuple of two numbers.')}"
184 | )
185 | if value[0] >= value[1]:
186 | raise ValueError(
187 | f"{Validate._var_font(var)} {Validate._error_font('first value must be less than second.')}"
188 | )
189 |
190 | @staticmethod
191 | def _is_valid_x_axis_values(value: Any, var: str) -> None:
192 | """Validate x-axis values tuple is provided and not placeholders."""
193 | if value in [(None, None), ("None", "None")]:
194 | raise ValueError(f"{Validate._var_font(var)} {Validate._error_font('must be provided.')}")
195 | Validate._is_tuple(value, var)
196 |
197 | @staticmethod
198 | def _is_valid_line(value: Any, var: str) -> None:
199 | """Check if value is an instance of Line."""
200 | from .Line import Line
201 | if not isinstance(value, Line):
202 | raise TypeError(
203 | f"{Validate._var_font(var)} {Validate._error_font('must be a tkchart.Line instance.')}"
204 | )
205 |
206 | @staticmethod
207 | def _is_valid_line_chart(value: Any, var: str) -> None:
208 | """Check if value is an instance of LineChart."""
209 | from .LineChart import LineChart
210 | if not isinstance(value, LineChart):
211 | raise TypeError(
212 | f"{Validate._var_font(var)} {Validate._error_font('must be a tkchart.LineChart instance.')}"
213 | )
214 |
215 | @staticmethod
216 | def _is_valid_data(value: Any, var: str) -> None:
217 | """Validate that value is a list of ints or floats."""
218 | Validate._is_list(value, var)
219 | if not all(isinstance(v, (int, float)) for v in value):
220 | raise TypeError(
221 | f"{Validate._var_font(var)} {Validate._error_font('all values must be int or float.')}"
222 | )
223 |
224 | # --- Error Helpers ---
225 |
226 | @staticmethod
227 | def _invalid_cget(var: str) -> None:
228 | """Raise error for invalid attribute."""
229 | raise ValueError(
230 | f"{Validate._var_font(var)} {Validate._error_font('Invalid attribute.')}"
231 | )
232 |
233 | @staticmethod
234 | def _invalid_line(line: Any) -> None:
235 | """Raise error if line is not part of chart."""
236 | raise ValueError(
237 | f"{Validate._var_font(str(line))} {Validate._error_font('The line is not part of this chart.')}"
238 | )
239 |
240 | @staticmethod
241 | def _master_att_not_provided_for_line(value: Any) -> None:
242 | """Raise error if master is not provided for Line."""
243 | raise ValueError(
244 | f"{Validate._var_font(str(value))} {Validate._error_font('master must be provided for Line.')}"
245 | )
246 |
--------------------------------------------------------------------------------
/README_zh.md:
--------------------------------------------------------------------------------
1 | [](README.md)
2 |
3 |