-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathDemo_Graph_Bar_Chart_Dual_Axis.py
51 lines (37 loc) · 1.91 KB
/
Demo_Graph_Bar_Chart_Dual_Axis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import FreeSimpleGUI as sg
import random
"""
Bar Chart - Dual Axis Version
A simple bar chart with a twist
If you've got 2 values to plot, this technique enables you to trivially plot them both.
Simply set your Graph element coordinates to be negative. Make your y=0 line run through the middle
Copyright 2022 PySimpleGUI
"""
BAR_WIDTH = 25 # width of each bar
BAR_SPACING = 30 # space between each bar
EDGE_OFFSET = 3 # offset from the left edge for first bar
GRAPH_SIZE= (500,500) # size in pixels
sg.theme('Light brown 1')
layout = [[sg.Text('Dual-Axis Bar Chart')],
[sg.Graph(GRAPH_SIZE, (0, -GRAPH_SIZE[0]//2), (GRAPH_SIZE[0]//2, GRAPH_SIZE[1]//2), k='-GRAPH-')],
[sg.Button('OK'), sg.T('Click to display more data'), sg.Exit()]]
window = sg.Window('Bar Graph', layout, finalize=True)
# get the Graph Element into a variable to make code completion easier
graph:sg.Graph = window['-GRAPH-']
while True:
graph.erase()
for i in range(8):
graph_value = random.randint(0, GRAPH_SIZE[1]//2)
graph.draw_rectangle(top_left=(i * BAR_SPACING + EDGE_OFFSET, graph_value),
bottom_right=(i * BAR_SPACING + EDGE_OFFSET + BAR_WIDTH, 0),
fill_color='green', line_width=0)
# get a second value and draw an inverted bar. Simply set the Y value to be negative and top to be 0
graph_value = random.randint(0, GRAPH_SIZE[1]//2)
graph.draw_rectangle(top_left=(i * BAR_SPACING + EDGE_OFFSET, 0),
bottom_right=(i * BAR_SPACING + EDGE_OFFSET + BAR_WIDTH, -graph_value),
fill_color='red', line_width=0)
# Normally at the top of the loop, but because we're drawing the graph first, making it at the bottom
event, values = window.read()
if event in (sg.WIN_CLOSED, 'Exit'):
break
window.close()