Merge pull request #2378 from PySimpleGUI/Dev-latest

Draws Axis, moves the individual lines instead of the graph.
This commit is contained in:
PySimpleGUI 2019-12-15 12:15:16 -05:00 committed by GitHub
commit 814d88fb59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 5 deletions

View File

@ -7,6 +7,8 @@ import sys
Example of random line in Graph element. Example of random line in Graph element.
''' '''
sg.change_look_and_feel('black')
STEP_SIZE = 1 STEP_SIZE = 1
SAMPLES = 300 SAMPLES = 300
SAMPLE_MAX = 300 SAMPLE_MAX = 300
@ -17,7 +19,7 @@ def main():
global g_exit, g_response_time global g_exit, g_response_time
layout = [[sg.Text('Enter width, height of graph')], layout = [[sg.Text('Enter width, height of graph')],
[sg.Input(size=(6, 1), key='w'), sg.Input(size=(6, 1), key='h')], [sg.Input(300, size=(6, 1), key='w'), sg.Input(300, size=(6, 1), key='h')],
[sg.Ok(), sg.Cancel()]] [sg.Ok(), sg.Cancel()]]
window = sg.Window('Enter graph size', layout) window = sg.Window('Enter graph size', layout)
@ -34,18 +36,22 @@ def main():
sg.set_options(element_padding=(0, 0)) sg.set_options(element_padding=(0, 0))
layout = [[sg.Button('Quit', button_color=('white', 'black'))], layout = [[sg.Button('Quit', button_color=('white', 'black'))],
[sg.Graph(CANVAS_SIZE, (0, 0), (SAMPLES, SAMPLE_MAX) [sg.Graph(CANVAS_SIZE, (0, 0), (SAMPLES, SAMPLE_MAX),
background_color='black', key='graph')], ] background_color='black', key='graph')], ]
window = sg.Window('Canvas test', layout, grab_anywhere=True, window = sg.Window('Canvas test', layout, grab_anywhere=True,
background_color='black', no_titlebar=False, background_color='black', no_titlebar=False,
use_default_focus=False, finalize=True) use_default_focus=False, finalize=True)
graph = window['graph'] graph = window['graph'] # type:sg.Graph
graph.draw_line((SAMPLES//2, 0), (SAMPLES//2,SAMPLE_MAX),color='white')
graph.draw_line((0,SAMPLE_MAX//2), (SAMPLES, SAMPLE_MAX//2),color='white')
prev_response_time = None prev_response_time = None
i = 0 i = 0
prev_x, prev_y = 0, 0 prev_x, prev_y = 0, 0
graph_value = 250 graph_value = 250
figures = []
while True: while True:
event, values = window.read(timeout=0) event, values = window.read(timeout=0)
if event == 'Quit' or event is None: if event == 'Quit' or event is None:
@ -63,10 +69,15 @@ def main():
prev_value = graph_value prev_value = graph_value
if i >= SAMPLES: if i >= SAMPLES:
graph.move(-STEP_SIZE, 0) graph.delete_figure(figures[0])
figures = figures[1:]
for count, figure in enumerate(figures):
graph.move_figure(figure, -STEP_SIZE, 0)
prev_x = prev_x - STEP_SIZE prev_x = prev_x - STEP_SIZE
graph.draw_line((prev_x, prev_y), (new_x, new_y), color='white') # print(f'io={i} {prev_x,prev_y} to {new_x, new_y}')
last_figure = graph.draw_line((prev_x, prev_y), (new_x, new_y), color='white')
figures.append(last_figure)
prev_x, prev_y = new_x, new_y prev_x, prev_y = new_x, new_y
i += STEP_SIZE if i < SAMPLES else 0 i += STEP_SIZE if i < SAMPLES else 0