0

trying to lineplot simple temperature data from a sqlite3 database. New to python, not sure what I'm doing wrong. my data from sql looks like:

[(70.8,), (70.8,), (70.9,), (71.0,), (71.0,), (71.2,), (71.2,), (71.2,), (71.4,), (71.7,), (71.7,), (72.0,), (72.0,), (72.0,), (72.2,), (72.2,), (71.9,), (72.0,), (72.0,), (72.2,), (72.2,), (72.2,), (72.2,), (71.7,), (71.9,), (71.9,), (72.0,), (72.0,), (72.3,), (72.0,), (72.0,), (72.2,), (72.2,), (72.2,), (72.3,), (72.2,), (72.2,), (72.3,), (72.3,), (72.3,), (72.3,), (72.4,), (72.5,), (72.4,), (72.5,), (72.5,), (72.6,), (72.7,), (72.7,), (73.0,), (73.0,), (73.0,), (73.1,), (73.1,), (73.3,), (73.5,), (73.5,), (73.6,), (73.7,), (73.8,), (73.7,), (73.7,), (73.7,), (73.8,), (73.7,), (73.7,), (73.6,), (73.7,), (73.8,), (73.8,), (73.8,), (73.8,), (73.7,), (73.6,), (73.6,), (73.5,), (73.6,), (73.5,), (73.5,), (73.5,), (73.5,), (73.2,), (73.1,), (73.0,), (73.0,), (72.7,), (72.8,), (72.8,), (72.8,), (72.7,), (72.7,), (72.6,), (72.6,), (72.7,), (72.7,), (72.7,), (72.7,), (72.7,), (72.8,), (72.7,), (72.8,), (72.8,), (72.8,), (72.8,), (72.8,), (72.8,), (72.7,), (72.7,), (72.7,), (73.0,), (73.0,), (73.0,), (73.0,), (73.4,), (73.5,), (73.5,), (73.6,), (73.5,), (73.6,), (73.5,), (73.5,), (73.6,), (73.6,), (73.5,), (73.5,), (73.6,), (73.5,), (73.6,), (73.6,), (73.6,), (73.6,), (73.5,)]

The error message I get is:

Traceback (most recent call last):
File "db-test5.py", line 93, in <module>
 renderPM.drawToFile(d, 'rework6.png')
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/renderPM.py", line 655, in drawToFile
c = drawToPMCanvas(d, dpi=dpi, bg=bg, configPIL=configPIL,showBoundary=showBoundary)
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/renderPM.py", line 641, in drawToPMCanvas 
draw(d, c, 0, 0, showBoundary=showBoundary)
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/renderPM.py", line 50, in draw
R.draw(renderScaledDrawing(drawing), canvas, x, y, showBoundary=showBoundary)
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/renderbase.py", line 199, in draw
self.drawNode(drawing)
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/renderPM.py", line 109, in drawNode
self.drawNodeDispatcher(node)
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/renderbase.py", line 280, in drawNodeDispatcher
self.drawGroup(node)
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/renderbase.py", line 297, in drawGroup
node = _expandUserNode(node,canvas)
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/renderbase.py", line 161, in _expandUserNode
node = node.provideNode()
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/widgetbase.py", line 150, in provideNode
return self.draw()
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/charts/lineplots.py", line 351, in draw
self.calcPositions()
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/charts/lineplots.py", line 194, in calcPositions
if isinstance(datum[0],str):
TypeError: 'float' object has no attribute '__getitem__'

my code is:

#!/usr/bin/python
from reportlab.graphics.charts.lineplots import LinePlot, AreaLinePlot
from reportlab.graphics.shapes import Drawing
from reportlab.lib import colors
from random import randint
from datetime import date, timedelta
from reportlab.graphics import renderPM
import sqlite3, os, datetime, time

##############################################
########   time functions   ##################
##############################################

def RecYear():
    return datetime.date.today().strftime("%Y")

def RecMonth():
    return datetime.date.today().strftime("%m")

def RecFullDateTime():
    return time.time()

def RecTime():
    return datetime.datetime.now().strftime("%H:%M:%S")

def RecDate():
    return datetime.datetime.now().strftime("%m-%d-%Y")

def RecDay():
    return datetime.date.today().strftime("%d")

currentdate_ds18b20=int(datetime.date.today().strftime("%Y"))*1000+ int(time.localtime().tm_yday)


os.system('clear')

sqlite_file="2-database_archived/archived_2017-02-27-HMS-ds18b20-v2.2-DATABASE.db"

table_name = "DS18B20temps"
print "filename = ", sqlite_file
print "table = ", table_name

conn = sqlite3.connect(sqlite_file)
c = conn.cursor()
print "db open"


### Calculate total length of table
c.execute('SELECT * FROM {tn}'.\
        format(tn=table_name))
print "the length of the database is:", len(c.fetchall()), " rows\n"


###Calculate total number of kitchen temp readings
c.execute('SELECT ({coi}) FROM {tn} WHERE {cn}="Kitchen"'.\
        format(coi="temp", tn=table_name, cn="sensor_name"))
kitchen_temp_list = c.fetchall()
print "the number of kitchen temp readings = ", len(kitchen_temp_list)
new_kitchen_list = list(sum(kitchen_temp_list, ()))
print "averge temp = ", (sum(new_kitchen_list))/len(new_kitchen_list)
print ""

# 4) Retrieve all IDs of entries between 6am and noon
c.execute("SELECT {idf} FROM {tn} WHERE {cnn}='Kitchen' AND {cn} BETWEEN '18:00:00' AND '23:59:59'".\
    format(idf="temp", tn=table_name, cnn="sensor_name", cn="recorded_time"))
all_date_times = c.fetchall()
print "readings from 6 pm to midnight = ", len(all_date_times)

print all_date_times

width=800 
height=500
d = Drawing(width, height)
lp = LinePlot()
lp.data=all_date_times
lp.width= 700
lp.height = 400
lp.xValueAxis.valueMin = 0
lp.xValueAxis.valueMax =400
#lp.xValueAxis.valueSteps = [6,8,10,12,14,16,18,20]

lp.yValueAxis.valueMin = 0
lp.yValueAxis.valueMax =200

lp.strokeColor=colors.black
lp.fillColor=colors.grey

#lp.reversePlotOrder = False

lp.joinedLines=1

d.add(lp)
renderPM.drawToFile(d, 'rework6.png')

It seams like whenever I pass bad data it craps out at renderPM, but I'm having a hard time understanding the error. My hunch is the data I'm passing is not appropriate for a line plot?

any help greatly appreciated......

user3808752
  • 169
  • 4
  • 14
  • It looks as if some float value ends up in `datum`, and that code then tries to do `datum[0]`. – CL. Mar 05 '17 at 06:38
  • I don't think I understand the structure of the data my data above is: [(70.8,), (70.8,), (70.9,), (71.0,) but when I look in the documentation it looks like: data = [((1,1), (2,2), (2.5,1), (3,3), (4,5)), ((1,2), (2,3), (2.5,2), (3.5,5), (4,6))] I think I need to change my query to pull out to data points, in this case temperature and date or temperature and ID.... – user3808752 Mar 05 '17 at 15:28
  • OK, I edited my sql statement to retrieve data from 2 columns, my data now looks like: [(70.8, 1401.0), (70.8, 1402.0), (70.9, 1403.0), (71.0, 1404.0)] but same error, really stumped now. – user3808752 Mar 05 '17 at 19:50

1 Answers1

0

after going through the report lab documentation and experimenting with their examples I determined the problem was with how my data was presented. Using the sample data from the report lab documentation I was able to eliminate the error I was getting when renderPM was called.

Then based on the help from Muhammad Haseeb Khan and Paul Rooney in this post

I was able to data from [(70.1,), (71.1,),(71.1,)] to [(70.1, 71.1, 71.1)] by modifying the about code to:

c.execute("SELECT {idf} FROM {tn} WHERE {cnn}='Kitchen' AND {cn} BETWEEN '18:00:00' AND '23:59:59'".\
format(idf="temp", time="read_ID", tn=table_name, cnn="sensor_name",        
cn="recorded_time"))
all_date_times = c.fetchall()
print "readings from 6 pm to midnight = ", len(all_date_times)

level1 = [list(row) for row in all_date_times]

level2=[i[0] for i in all_date_times]

level3=[tuple(level2)]


#### build report lab chart

drawing = Drawing(600, 400)
data = level3
lc = HorizontalLineChart()
lc.x = 25
lc.y = 25
lc.height = 450
lc.width = 550
lc.data = data
lc.joinedLines = 1
lc.valueAxis.valueMin = 70
lc.valueAxis.valueMax = 75
drawing.add(lc)
renderPM.drawToFile(drawing, 'chartgraphicfile.png')
Community
  • 1
  • 1
user3808752
  • 169
  • 4
  • 14