Override 'writeCsv' of PlotItem class

49 views
Skip to first unread message

Guillaume William Bres

unread,
Dec 6, 2017, 5:11:24 AM12/6/17
to pyqtgraph
Hello, 

I think what I am trying to do should be doable,

I would like to override the export to csv feature of a PlotWidget that I usally build this way:

self.main_plot = pg.PlotWidget(name="main_plot")
self.main_plot.seLogMode..
self.main_plot.showGrid.. 

because I would like the new function to only write the content of curves objects (plotDataItems) that do not have a specific name,

here's how I usually build my curves objects:

self.curve = self.main_plot.plot(pen=color)

 So I looked into the library code, and if I got it right, I need to create a custom PlotItem class with proper inheritance, because I do not want to break any functionality, so far I wrote something like this:

class CustomPlotItem(pg.PlotItem):

# use very same arguments
def __init__(self, parent=None, name=None, labels=None, title=None, viewBox=None, axisItems=None, enableMenu=True, **kargs):

# pass all arguments to super so I do not break anything
super(customPlotItem, self).__init__(parent=parent, name=name, ....)

# use very same name to override
def writeCsv(self, fileName=None):
 
# >> previous code
        if fileName is None:
            self.fileDialog = FileDialog()
            if PlotItem.lastFileDir is not None:
                self.fileDialog.setDirectory(PlotItem.lastFileDir)
            self.fileDialog.setFileMode(QtGui.QFileDialog.AnyFile)
            self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)
            self.fileDialog.show()
            self.fileDialog.fileSelected.connect(self.writeCsv)
            return
 
# new functionality
        fileName = str(fileName)
        PlotItem.lastFileDir = os.path.dirname(fileName)
        fd = open(fileName, 'w')
        data = [c.getData() for c in self.curves]

      data = []
                          for c in self.curves:
                               if (c.name() != "name_to_drop_out"):
                                     data.append(c.getData()) 
 
                         # previous code 
        i = 0
        while True:
            done = True
            for d in data:
                if i < len(d[0]):
                    fd.write('%g,%g,'%(d[0][i], d[1][i]))
                    done = False
                else:
                    fd.write(' , ,')
            fd.write('\n')
            if done:
                break
            i += 1
        fd.close()

so my problem is, I am using a pg.PlotWidget in my code, how can I make this pg.PlotWidget not to build a usual pg.PlotItem but a custom one?

I tried something dirty so far:

self.main_plot = pg.PlotWidget(name="test")
          customClass = CustomPlotItem() # could use some args? 
          self.main_plot.plotItem = customClass # kill default pointer, point to custom stuff
          self.curve = self.main_plot.plot() # try to use it

I guess you guys get the idea,

  • Is this the right approach, do I got the class tree right?
  • since the functionality I want to override is located in plotItem, How do I get my plotWidget class (which is eventually what I use) to actually use it?
  • note: I might be wrong & the functionality writeCsv might be located somewhere else
>> Just want to acknowledge how cool and impressive PyQtGraph is, I have been using it for years now, and it's just flawless. Keep up the good work guys

thank you

Luke Campagnola

unread,
Dec 20, 2017, 12:35:39 AM12/20/17
to pyqt...@googlegroups.com
The writeCsv method is actually not called by the export system (this is just leftover from long before there was an export system at all). I suggest looking over the classes in pyqtgraph.exporters. 

1. Within your project, define a new Exporter subclass
2. Call YourExporter.register() to make it available from the list of exporters
3. If you want, modify the Exporter.Exporters list to remove or reorder as needed

--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/72499450-ca05-48ce-8fa6-903333b4957e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages