Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Update custom series plog #1885

Closed
qizheng-1998 opened this issue Sep 16, 2022 · 10 comments
Closed

Update custom series plog #1885

qizheng-1998 opened this issue Sep 16, 2022 · 10 comments

Comments

@qizheng-1998
Copy link

qizheng-1998 commented Sep 16, 2022

Hi,

How do we I update the custom_series plot after it is created? Do I just do set_value to set the x and y? I tried but it crashed without error.

import dearpygui.dearpygui as dpg

dpg.create_context()
dpg.create_viewport()
dpg.setup_dearpygui()

x_data = [0.0, 1.0, 2.0, 4.0, 5.0]
y_data = [0.0, 10.0, 20.0, 40.0, 50.0]

def callback(sender, app_data):

    _helper_data = app_data[0]
    transformed_x = app_data[1]
    transformed_y = app_data[2]
    #transformed_y1 = app_data[3] # for channel = 3
    #transformed_y2 = app_data[4] # for channel = 4
    #transformed_y3 = app_data[5] # for channel = 5
    mouse_x_plot_space = _helper_data["MouseX_PlotSpace"]   # not used in this example
    mouse_y_plot_space = _helper_data["MouseY_PlotSpace"]   # not used in this example
    mouse_x_pixel_space = _helper_data["MouseX_PixelSpace"]
    mouse_y_pixel_space = _helper_data["MouseY_PixelSpace"]
    dpg.delete_item(sender, children_only=True, slot=2)
    dpg.push_container_stack(sender)
    dpg.configure_item("demo_custom_series", tooltip=False)
    for i in range(0, len(transformed_x)):
        dpg.draw_text((transformed_x[i]+15, transformed_y[i]-15), str(i), size=20)
        dpg.draw_circle((transformed_x[i], transformed_y[i]), 15, fill=(50+i*5, 50+i*50, 0, 255))
        if mouse_x_pixel_space < transformed_x[i]+15 and mouse_x_pixel_space > transformed_x[i]-15 and mouse_y_pixel_space > transformed_y[i]-15 and mouse_y_pixel_space < transformed_y[i]+15:
            dpg.draw_circle((transformed_x[i], transformed_y[i]), 30)
            dpg.configure_item("demo_custom_series", tooltip=True)
            dpg.set_value("custom_series_text", "Current Point: " + str(i))
    dpg.pop_container_stack()

with dpg.window(label="Tutorial") as win:
    dpg.add_text("Hover an item for a custom tooltip!")
    with dpg.plot(label="Custom Series", height=400, width=-1):
        dpg.add_plot_legend()
        xaxis = dpg.add_plot_axis(dpg.mvXAxis)
        with dpg.plot_axis(dpg.mvYAxis):
            with dpg.custom_series(x_data, y_data, 2, label="Custom Series", callback=callback, tag="demo_custom_series"):
                dpg.add_text("Current Point: ", tag="custom_series_text")
            dpg.fit_axis_data(dpg.top_container_stack())

new_x = [10,20,30,50]
new_y = [70,50,70,80]

#The line below crash
dpg.set_value("demo_custom_series", [allGraphs[graph]['X'], allGraphs[graph]['Y'], 2])

dpg.set_primary_window(win, True)
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()

@IvanNazaruk
Copy link

IvanNazaruk commented Sep 19, 2022

Try use: dpg.configure_item()

Replace this:

#The line below crash
dpg.set_value("demo_custom_series", [allGraphs[graph]['X'], allGraphs[graph]['Y'], 2])

To:

#The line below crash
dpg.configure_item("demo_custom_series", x=new_x, y=new_y, channel_count=2)

or:

#The line below crash
dpg.configure_item("demo_custom_series", x=allGraphs[graph]['X'], y=allGraphs[graph]['Y'], channel_count=2)

@qizheng-1998
Copy link
Author

If my y_axis contains both custom plot and line series plot, how to delete all the plots? It crash when I do dpg.delete_item("y_axis", children_only=True), but works fine if I don't have the custom plot

import dearpygui.dearpygui as dpg

dpg.create_context()
dpg.create_viewport()
dpg.setup_dearpygui()

x_data = [0.0, 1.0, 2.0, 4.0, 5.0]
y_data = [0.0, 10.0, 20.0, 40.0, 50.0]

def callback(sender, app_data):

    _helper_data = app_data[0]
    transformed_x = app_data[1]
    transformed_y = app_data[2]
    #transformed_y1 = app_data[3] # for channel = 3
    #transformed_y2 = app_data[4] # for channel = 4
    #transformed_y3 = app_data[5] # for channel = 5
    mouse_x_plot_space = _helper_data["MouseX_PlotSpace"]   # not used in this example
    mouse_y_plot_space = _helper_data["MouseY_PlotSpace"]   # not used in this example
    mouse_x_pixel_space = _helper_data["MouseX_PixelSpace"]
    mouse_y_pixel_space = _helper_data["MouseY_PixelSpace"]
    dpg.delete_item(sender, children_only=True, slot=2)
    dpg.push_container_stack(sender)
    dpg.configure_item("demo_custom_series", tooltip=False)
    for i in range(0, len(transformed_x)):
        dpg.draw_text((transformed_x[i]+15, transformed_y[i]-15), str(i), size=20)
        dpg.draw_circle((transformed_x[i], transformed_y[i]), 15, fill=(50+i*5, 50+i*50, 0, 255))
        if mouse_x_pixel_space < transformed_x[i]+15 and mouse_x_pixel_space > transformed_x[i]-15 and mouse_y_pixel_space > transformed_y[i]-15 and mouse_y_pixel_space < transformed_y[i]+15:
            dpg.draw_circle((transformed_x[i], transformed_y[i]), 30)
            dpg.configure_item("demo_custom_series", tooltip=True)
            dpg.set_value("custom_series_text", "Current Point: " + str(i))
    dpg.pop_container_stack()

with dpg.window(label="Tutorial") as win:
    dpg.add_text("Hover an item for a custom tooltip!")
    with dpg.plot(label="Custom Series", height=400, width=-1):
        dpg.add_plot_legend()
        xaxis = dpg.add_plot_axis(dpg.mvXAxis)
        with dpg.plot_axis(dpg.mvYAxis, tag = 'y_axis'):
            with dpg.custom_series(x_data, y_data, 2, label="Custom Series", callback=callback, tag="demo_custom_series"):
                dpg.add_text("Current Point: ", tag="custom_series_text")
            dpg.fit_axis_data(dpg.top_container_stack())
            dpg.add_line_series([10,30,50], [70,50,80], tag = 'series_line') #new line series


new_x = [10,20,30,50]
new_y = [70,50,70,80]

#The line below crash
dpg.configure_item("demo_custom_series", x=new_x, y=new_y, channel_count=2)

dpg.delete_item("y_axis", children_only=True)

dpg.set_primary_window(win, True)
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()

@IvanNazaruk
Copy link

I also see a crash, but only at runtime: (and it does not affect the work in any way)

0

I also failed to catch a crash during startup:

1

I am using:
Window 10
Python 3.10.3 x64
DearPyGui v1.7.0

@qizheng-1998
Copy link
Author

qizheng-1998 commented Sep 30, 2022

I still have the same problem when I try to update both custom and add_line_series plot. Several bugs occurs:

  1. In 'clear_y', program crash when I set slot to 1 and no behavior when set to others.
  2. In 'update_graph', when I set the for loop range to a higher number, it either crash OR not do the custom plot OR if it plot, it only plot the same amount of points as the original plot.
import dearpygui.dearpygui as dpg

dpg.create_context()
dpg.create_viewport()
dpg.setup_dearpygui()



def callback(sender, app_data):

    _helper_data = app_data[0]
    transformed_x = app_data[1]
    transformed_y = app_data[2]
    #transformed_y1 = app_data[3] # for channel = 3
    #transformed_y2 = app_data[4] # for channel = 4
    #transformed_y3 = app_data[5] # for channel = 5
    mouse_x_plot_space = _helper_data["MouseX_PlotSpace"]   # not used in this example
    mouse_y_plot_space = _helper_data["MouseY_PlotSpace"]   # not used in this example
    mouse_x_pixel_space = _helper_data["MouseX_PixelSpace"]
    mouse_y_pixel_space = _helper_data["MouseY_PixelSpace"]
    dpg.delete_item(sender, children_only=True, slot=2)
    dpg.push_container_stack(sender)
    dpg.configure_item("demo_custom_series", tooltip=False)
    for i in range(0, len(transformed_x)):
        dpg.draw_text((transformed_x[i]+15, transformed_y[i]-15), str(i), size=20)
        dpg.draw_circle((transformed_x[i], transformed_y[i]), 15, fill=(50+i*5, 50+i*50, 0, 255))
        if mouse_x_pixel_space < transformed_x[i]+15 and mouse_x_pixel_space > transformed_x[i]-15 and mouse_y_pixel_space > transformed_y[i]-15 and mouse_y_pixel_space < transformed_y[i]+15:
            dpg.draw_circle((transformed_x[i], transformed_y[i]), 30)
            dpg.configure_item("demo_custom_series", tooltip=True)
            dpg.set_value("custom_series_text", "Current Point: " + str(i))
    dpg.pop_container_stack()


import random



def clear_y():
    dpg.delete_item("y_axis", children_only=True, slot = 1)
    print('clear done')

def update_graph():
    new_x = []
    new_y = []
    
    for i in range(10):
        new_x.append(i)
        new_y.append(random.randint(0, 500))

    dpg.configure_item("demo_custom_series", x=new_x, y=new_y)
    dpg.configure_item("series_line", x=new_x, y=new_y)
    print('updated')




x_data = [0.0, 1.0, 2.0, 4.0, 5.0]
y_data = [0.0, 10.0, 20.0, 40.0, 50.0]

with dpg.window(label="Tutorial") as win:
    dpg.add_button(label = 'update', callback = update_graph)
    dpg.add_button(label = 'cleary', callback = clear_y)

    dpg.add_text("Hover an item for a custom tooltip!")
    with dpg.plot(label="Custom Series", height=400, width=-1):
        dpg.add_plot_legend()
        xaxis = dpg.add_plot_axis(dpg.mvXAxis)
        with dpg.plot_axis(dpg.mvYAxis, tag = 'y_axis'):
            with dpg.custom_series(x_data, y_data, 2, callback=callback, tag="demo_custom_series"):
                dpg.add_text("Current Point: ", tag="custom_series_text")

            dpg.add_line_series(x_data, y_data, tag = 'series_line') #new line series
            dpg.fit_axis_data(dpg.top_container_stack())



dpg.set_primary_window(win, True)
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()

@IvanNazaruk
Copy link

  1. DPG behaves very strangely if buttons are near dpg.plot, but in a separate window everything works fine

  2. When you delete children you delete them, i.e. you can't use them tag. In this case you need to recreate all the elements that were in that object before you deleted the children. I was too lazy to write checks that the object was cleared, so I clear it during update_graph. (See NEW comment)

(I also "removed" the output of the errors, I do not know how to fix them quickly and they do not seem to affect the program in any way. See NEW-2 comment)

import dearpygui.dearpygui as dpg

dpg.create_context()
dpg.create_viewport()
dpg.setup_dearpygui()


def callback(sender, app_data):
    _helper_data = app_data[0]
    transformed_x = app_data[1]
    transformed_y = app_data[2]
    # transformed_y1 = app_data[3] # for channel = 3
    # transformed_y2 = app_data[4] # for channel = 4
    # transformed_y3 = app_data[5] # for channel = 5
    mouse_x_plot_space = _helper_data["MouseX_PlotSpace"]  # not used in this example
    mouse_y_plot_space = _helper_data["MouseY_PlotSpace"]  # not used in this example
    mouse_x_pixel_space = _helper_data["MouseX_PixelSpace"]
    mouse_y_pixel_space = _helper_data["MouseY_PixelSpace"]
    ################## NEW-2 ##################
    try:
        dpg.delete_item(sender, children_only=True, slot=2)
    except Exception:
        return
    ###########################################
    dpg.push_container_stack(sender)
    dpg.configure_item("demo_custom_series", tooltip=False)
    for i in range(0, len(transformed_x)):
        dpg.draw_text((transformed_x[i] + 15, transformed_y[i] - 15), str(i), size=20)
        dpg.draw_circle((transformed_x[i], transformed_y[i]), 15, fill=(50 + i * 5, 50 + i * 50, 0, 255))
        if mouse_x_pixel_space < transformed_x[i] + 15 and mouse_x_pixel_space > transformed_x[i] - 15 and mouse_y_pixel_space > transformed_y[i] - 15 and mouse_y_pixel_space < transformed_y[i] + 15:
            dpg.draw_circle((transformed_x[i], transformed_y[i]), 30)
            dpg.configure_item("demo_custom_series", tooltip=True)
            dpg.set_value("custom_series_text", "Current Point: " + str(i))
    dpg.pop_container_stack()


import random

def clear_y():
    dpg.delete_item("y_axis", children_only=True, slot=1)
    print('clear done')


def update_graph():
    new_x = []
    new_y = []

    for i in range(10):
        new_x.append(i)
        new_y.append(random.randint(0, 500))

    ################## NEW ##################
    clear_y()
    with dpg.custom_series(new_x, new_y, 2, callback=callback, tag="demo_custom_series", parent='y_axis'):  # added parameter parentparent
        dpg.add_text("Current Point: ", tag="custom_series_text")

    dpg.add_line_series(new_x, new_y, tag='series_line', parent='y_axis')  # added parameter parent # new line series
    dpg.fit_axis_data('y_axis')
    #########################################

    # dpg.configure_item("demo_custom_series", x=new_x, y=new_y)
    # dpg.configure_item("series_line", x=new_x, y=new_y)
    print('updated')

x_data = [0.0, 1.0, 2.0, 4.0, 5.0]
y_data = [0.0, 10.0, 20.0, 40.0, 50.0]

with dpg.window(label="Tutorial") as win:
    dpg.add_button(label='update', callback=update_graph)
    dpg.add_button(label='cleary', callback=clear_y)
    dpg.add_text("Hover an item for a custom tooltip!")
    with dpg.plot(label="Custom Series", height=400, width=-1):
        dpg.add_plot_legend()
        xaxis = dpg.add_plot_axis(dpg.mvXAxis)
        with dpg.plot_axis(dpg.mvYAxis, tag='y_axis'):
            with dpg.custom_series(x_data, y_data, 2, callback=callback, tag="demo_custom_series"):
                dpg.add_text("Current Point: ", tag="custom_series_text")

            dpg.add_line_series(x_data, y_data, tag='series_line')  # new line series
            dpg.fit_axis_data(dpg.top_container_stack())



with dpg.window(pos=(100, 0)):
    dpg.add_button(label='update', callback=update_graph)
    dpg.add_button(label='cleary', callback=clear_y)

dpg.set_primary_window(win, True)
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()

With "loop range to a higher number" it takes longer to find the problem (although I think the problem is in the callback, specifically in the for loop: draw_text, draw_circle)

@qizheng-1998
Copy link
Author

Okay. I see. Thank you for looking into that! My data have a size of 2048. It seems to plot fine but when I try to move or auto scale the graph it crash. Is there a way that I can use line_series only or scatter_series only to show the tooltip of at a specific point? My goal to view the exact x and y point at a point on the line.

@IvanNazaruk
Copy link

IvanNazaruk commented Oct 1, 2022

So I was able to fix the crashes at high values of the loop, but there was a problem with performance dpg.show_metrics(): (At 2048 I have 27 fps)

def callback(sender, app_data):
    _helper_data = app_data[0]
    transformed_x = app_data[1]
    transformed_y = app_data[2]
    # transformed_y1 = app_data[3] # for channel = 3
    # transformed_y2 = app_data[4] # for channel = 4
    # transformed_y3 = app_data[5] # for channel = 5
    mouse_x_plot_space = _helper_data["MouseX_PlotSpace"]  # not used in this example
    mouse_y_plot_space = _helper_data["MouseY_PlotSpace"]  # not used in this example
    mouse_x_pixel_space = _helper_data["MouseX_PixelSpace"]
    mouse_y_pixel_space = _helper_data["MouseY_PixelSpace"]
    ################## NEW-2 ##################
    try:
        dpg.delete_item(sender, children_only=True, slot=2)
    except Exception:
        return
    ###########################################
    with dpg.mutex():
        dpg.push_container_stack(sender)
        dpg.configure_item("demo_custom_series", tooltip=False)
        for i in range(0, len(transformed_x)):
            dpg.draw_text((transformed_x[i] + 15, transformed_y[i] - 15), str(i), size=20)
            dpg.draw_circle((transformed_x[i], transformed_y[i]), 15, fill=(50 + i * 5, 50 + i * 50, 0, 255))
            if mouse_x_pixel_space < transformed_x[i] + 15 and mouse_x_pixel_space > transformed_x[i] - 15 and mouse_y_pixel_space > transformed_y[i] - 15 and mouse_y_pixel_space < transformed_y[i] + 15:
                dpg.draw_circle((transformed_x[i], transformed_y[i]), 30)
                dpg.configure_item("demo_custom_series", tooltip=True)
                dpg.set_value("custom_series_text", "Current Point: " + str(i))
        dpg.pop_container_stack()

I think you should optimize the callback loop somehow. Maybe with threads or other means. I suggest you choose your own option by searching for a solution on the internet, since this is already a python question and there are many solutions.

@sedenka
Copy link

sedenka commented Oct 1, 2022

Check #1864.

@qizheng-1998
Copy link
Author

Thanks @Ivanzzzc for taking the time to look into that!

So there is no way I can use add_line_series with tooltip to show the value ON the line? All I want is to have tooltip to show the value on the line. Maybe I can try to draw the line using the custom_series with the tooltip option instead of draw 2048 circles.

And how can I use thread with dearpygui? I think I read it somewhere that dearpygui handles the thread differently and all the resources that I found is using the old dearpygui structure.

@hoffstadt
Copy link
Owner

Please take this to discord.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants