From df0eba2c8b0649a21b0b41fae991c7ce302c9154 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Thu, 10 Mar 2022 08:25:24 -0500 Subject: [PATCH] New Demo - Dual Axis bar chart --- .../Demo_Graph_Bar_Chart_Dual_Axis.py | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 DemoPrograms/Demo_Graph_Bar_Chart_Dual_Axis.py diff --git a/DemoPrograms/Demo_Graph_Bar_Chart_Dual_Axis.py b/DemoPrograms/Demo_Graph_Bar_Chart_Dual_Axis.py new file mode 100644 index 00000000..224a8522 --- /dev/null +++ b/DemoPrograms/Demo_Graph_Bar_Chart_Dual_Axis.py @@ -0,0 +1,51 @@ +import PySimpleGUI 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() \ No newline at end of file