Liniendiagramm mit plot Datum als xLabel geht das

Plattformunabhängige GUIs mit wxWidgets.
sea-live
User
Beiträge: 440
Registriert: Montag 18. Februar 2008, 12:24
Wohnort: RP

Liniendiagramm mit plot Datum als xLabel geht das

Beitragvon sea-live » Dienstag 4. März 2008, 16:00

servus mein Chartprogramm sieht wie folgt aus

1FRAGE kann ich das mit dem tuple erzeugen durch einen durchlauf des dict linien_key automatisieren zeile 50-86 geht bestimmt eleganter

2frage kann ich das datum als xlabel benutzen
DANKE

Code: Alles auswählen

# -*- coding: iso-8859-15 -*-

import wx
import wx.lib.plot as plot
import os
import types
import decimal
import csv

wx.SetDefaultPyEncoding("iso-8859-15")
pfad="C:\\boerse\\"
dateiname='AIX.DE'
listenname='tecdax'
class Fenster(wx.Frame):
   
    def __init__(
        self, parent = None, id = -1, title = "CHART", size = wx.Size(500, 500)
    ):
        wx.Frame.__init__(self, parent, id, title, size = size)
       
        panel = wx.Panel(self)
       
        vbox_main = wx.BoxSizer(wx.VERTICAL)
        panel.SetSizer(vbox_main)       
       
        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox_main.Add(vbox, 1, wx.ALL | wx.EXPAND, 5)
       
        self.client = plot.PlotCanvas(panel)
        ##new declarations
       
        #define the function for drawing pointLabels
        self.client.SetPointLabelFunc(self.DrawPointLabel)
        # Create mouse event for showing cursor coords in status bar
        self.client.canvas.Bind(wx.EVT_LEFT_DOWN, self.OnMouseLeftDown)
        # Show closest point when enabled
        self.client.canvas.Bind(wx.EVT_MOTION, self.OnMotion)
        ##end of new declarations
        vbox.Add(self.client, 1, wx.EXPAND | wx.ALL, 5)

        # Einlesen der daten ("," ist bei csv der Default-Delimiter)
        content = csv.reader(open("%s%s\\%s.csv"%(pfad,listenname,dateiname), 'r'))
        # In der ersten Zeile stehen die Spaltennamen, die speichern wir uns
        keys = content.next()
        data = []
        for line in content:
              data.append(dict(zip(keys, line)))
        print data
        cord_open=()
        cord_low=()
        cord_high=()
        cord_close=()
        linien_key={'Open':cord_open,'Low':cord_low,'High':cord_high,'Close':cord_close}     
        i=0
        liste=[]
        tmp=()
        for keys in data:
            tmp=(i,round(float(data[i]['Open']),2))
            liste.append(tmp)
            i=i+1
        cord_open=tuple(liste)
        i=0
        liste=[]
        tmp=()
        for keys in data:
            tmp=(i,round(float(data[i]['Low']),2))
            liste.append(tmp)
            i=i+1
        cord_low=tuple(liste)
        i=0
        liste=[]
        tmp=()
        for keys in data:
            tmp=(i,round(float(data[i]['High']),2))
            liste.append(tmp)
            i=i+1
        cord_high=tuple(liste)
        i=0
        liste=[]
        tmp=()
        for keys in data:
            tmp=(i,round(float(data[i]['Close']),2))
            liste.append(tmp)
            i=i+1
        cord_close=tuple(liste)
        lines = (
            plot.PolyLine(
                cord_open, colour = "green", legend = u" Open", width = 1
            ),
            plot.PolyLine(
                cord_low, colour = "blue", legend = u" Low", width = 1
            ),
            plot.PolyLine(
                cord_high, colour = "cyan", legend = u" High", width = 1
            ),
            plot.PolyLine(
                cord_close, colour = "red", legend = u" Close", width = 1
            ),
           
           
        )
       
        graphics = plot.PlotGraphics(
            objects = lines, title = u"CHART %s"%dateiname, xLabel = u"Zeit",
            yLabel = u"KURS"
        )
       
        self.client.SetFont(wx.Font(10,wx.SWISS,wx.NORMAL,wx.NORMAL))
        self.client.SetFontSizeAxis(10)
        self.client.SetFontSizeLegend(7)
        self.client.SetEnableLegend(True)
        self.client.SetXSpec('auto')
        self.client.SetYSpec('auto')
        self.client.SetEnableGrid(True)
        ##Activate Labels       
        self.client.SetEnablePointLabel(True)
        self.client.Draw(graphics)
        ##show Statusbar for Mouse Position
        self.CreateStatusBar(1)       

    ##Functions from plot.py
    def DrawPointLabel(self, dc, mDataDict):
        dc.SetPen(wx.Pen(wx.BLACK))
        dc.SetBrush(wx.Brush( wx.BLACK, wx.SOLID ) )
        sx, sy = mDataDict["scaledXY"] #scaled x,y of closest point
        dc.DrawRectangle( sx-5,sy-5, 10, 10)  #10by10 square centered on point
        px,py = mDataDict["pointXY"]
        cNum = mDataDict["curveNum"]
        pntIn = mDataDict["pIndex"]
        legend = mDataDict["legend"]
        #make a string to display
        s = "Crv# %i, '%s', Pt. (%.2f,%.2f), PtInd %i" %(cNum, legend, px, py, pntIn)
        dc.DrawText(s, sx , sy+1)

    def OnMouseLeftDown(self,event):
        s= "Left Mouse Down at Point: (%.4f, %.4f)" % self.client._getXY(event)
        self.SetStatusText(s)
        event.Skip()            #allows plotCanvas OnMouseLeftDown to be called

    def OnMotion(self, event):
        #show closest point (when enbled)
        if self.client.GetEnablePointLabel() == True:
            #make up dict with info for the pointLabel
            #I've decided to mark the closest point on the closest curve
            dlst= self.client.GetClosestPoint( self.client._getXY(event), pointScaled= True)
            if dlst != []:    #returns [] if none
                curveNum, legend, pIndex, pointXY, scaledXY, distance = dlst
                #make up dictionary to pass to my user function (see DrawPointLabel)
                mDataDict= {"curveNum":curveNum, "legend":legend, "pIndex":pIndex,\
                            "pointXY":pointXY, "scaledXY":scaledXY}
                #pass dict to update the pointLabel
                self.client.UpdatePointLabel(mDataDict)
        event.Skip()           #go to next handler
       
    def SetPointLabelFunc(self, func):
        """Sets the function with custom code for pointLabel drawing
            ******** more info needed ***************
        """
        self._pointLabelFunc= func
    ##end of new code       
     
def main():
    app = wx.PySimpleApp()
    f = Fenster()
    f.Center()
    f.Show()
    app.MainLoop()

if __name__ == "__main__":
    main()



und hier noch das data[] zum testen

Code: Alles auswählen

[{'Volume': '528700', 'Adj Close': '8.81', 'High': '8.93', 'Low': '8.68', 'Date': '2008-02-29', 'Close': '8.81', 'Open': '8.84'}, {'Volume': '747500', 'Adj Close': '8.96', 'High': '9.01', 'Low': '8.62', 'Date': '2008-02-28', 'Close': '8.96', 'Open': '8.94'}, {'Volume': '611800', 'Adj Close': '9.03', 'High': '9.32', 'Low': '8.82', 'Date': '2008-02-27', 'Close': '9.03', 'Open': '9.29'}, {'Volume': '515900', 'Adj Close': '9.23', 'High': '9.33', 'Low': '9.07', 'Date': '2008-02-26', 'Close': '9.23', 'Open': '9.07'}, {'Volume': '624100', 'Adj Close': '9.03', 'High': '9.05', 'Low': '8.83', 'Date': '2008-02-25', 'Close': '9.03', 'Open': '8.94'}, {'Volume': '639100', 'Adj Close': '8.73', 'High': '8.90', 'Low': '8.54', 'Date': '2008-02-22', 'Close': '8.73', 'Open': '8.73'}, {'Volume': '541500', 'Adj Close': '8.77', 'High': '8.95', 'Low': '8.60', 'Date': '2008-02-21', 'Close': '8.77', 'Open': '8.75'}, {'Volume': '543300', 'Adj Close': '8.59', 'High': '8.88', 'Low': '8.48', 'Date': '2008-02-20', 'Close': '8.59', 'Open': '8.55'}, {'Volume': '516700', 'Adj Close': '8.69', 'High': '8.72', 'Low': '8.37', 'Date': '2008-02-19', 'Close': '8.69', 'Open': '8.52'}, {'Volume': '483700', 'Adj Close': '8.54', 'High': '8.70', 'Low': '8.44', 'Date': '2008-02-18', 'Close': '8.54', 'Open': '8.51'}, {'Volume': '1047500', 'Adj Close': '8.33', 'High': '8.72', 'Low': '8.27', 'Date': '2008-02-15', 'Close': '8.33', 'Open': '8.63'}, {'Volume': '664100', 'Adj Close': '8.71', 'High': '9.07', 'Low': '8.60', 'Date': '2008-02-14', 'Close': '8.71', 'Open': '8.99'}, {'Volume': '818200', 'Adj Close': '8.87', 'High': '8.95', 'Low': '8.63', 'Date': '2008-02-13', 'Close': '8.87', 'Open': '8.73'}, {'Volume': '798400', 'Adj Close': '8.81', 'High': '8.82', 'Low': '8.38', 'Date': '2008-02-12', 'Close': '8.81', 'Open': '8.50'}, {'Volume': '489100', 'Adj Close': '8.43', 'High': '8.47', 'Low': '8.23', 'Date': '2008-02-11', 'Close': '8.43', 'Open': '8.23'}, {'Volume': '444500', 'Adj Close': '8.35', 'High': '8.45', 'Low': '8.25', 'Date': '2008-02-08', 'Close': '8.35', 'Open': '8.33'}, {'Volume': '963800', 'Adj Close': '8.20', 'High': '8.50', 'Low': '8.06', 'Date': '2008-02-07', 'Close': '8.20', 'Open': '8.45'}, {'Volume': '990000', 'Adj Close': '8.65', 'High': '8.71', 'Low': '7.92', 'Date': '2008-02-06', 'Close': '8.65', 'Open': '8.20'}, {'Volume': '1202000', 'Adj Close': '8.54', 'High': '8.80', 'Low': '8.31', 'Date': '2008-02-05', 'Close': '8.54', 'Open': '8.60'}, {'Volume': '761100', 'Adj Close': '8.59', 'High': '8.70', 'Low': '8.50', 'Date': '2008-02-04', 'Close': '8.59', 'Open': '8.60'}, {'Volume': '1110400', 'Adj Close': '8.42', 'High': '8.50', 'Low': '8.20', 'Date': '2008-02-01', 'Close': '8.42', 'Open': '8.35'}]

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder