0

I am reading from MySQL database this kind of data:

('c9', 2862, datetime.datetime(2016, 4, 30, 22, 34, 38))
('f2', 2862, datetime.datetime(2016, 4, 30, 22, 35, 38))
('f3', 2864, datetime.datetime(2016, 4, 30, 22, 36, 38))
('f4', 2863, datetime.datetime(2016, 4, 30, 22, 37, 38))
('c9', 2880, datetime.datetime(2016, 4, 30, 22, 38, 38))
('f2', 2862, datetime.datetime(2016, 4, 30, 22, 39, 38)) 
# and so on! 

I want to use the library pygal to make a graph that will combine all of the nodes in column 1 (c9, f2, f3...) in one graph, each with its respective time. So I tried the following:

time = []
y_c9 = []
y_f3 = []
y_f2 = []
for row in data:
   time.append(row[2])
   if row[0] == 'c9':
       y_c9.append(row[1])
   if row[0] == 'f2':
       y_f2.append(row[1])
   if row[0] == 'f3':
       y_f3.append(row[1])
   # and the same for the rest
   graph.x_labels = time
   graph.add('c9', y_c9)
   graph.add('f2', y_f2)
   graph.add('f3', y_f3

However, this doesn't cut it because I will get short lines for c9, f2 ,f3, while the time (x axis) will be of a longer value (The length of y_c9, y_f2, y_f3 is < length of time) I tried several other combinations of if statements but it didn't work as well.

What should I do to produce a proper graph?

Ahmed Al-haddad
  • 805
  • 2
  • 16
  • 41

1 Answers1

1

You need to use a XY chart, or to be more specific, a pygal.DateTimeLine chart type. This way all the values will be plotted against the same timescale and the lines will be continuous.

Like so:

graph.x_labels = [item[2] for item in data]
graph.add('c9',[(item[2],item[0] if item[1]=='c9' else 0) for item in data])
graph.add('c9',[(item[2],item[0] if item[1]=='f3' else 0) for item in data])
graph.add('c9',[(item[2],item[0] if item[1]=='f2' else 0) for item in data])
lesingerouge
  • 1,160
  • 7
  • 14
  • I tried your suggestion but I am getting this error `bad operand type for abs(): 'datetime.datetime'` How to solve it? – Ahmed Al-haddad Apr 30 '16 at 21:56
  • @Ahmed you need to use pygal.DateTimeLine as the chart type. Check the page I referenced for more details. – lesingerouge Apr 30 '16 at 21:58
  • Thank you for your patience. I already used this `datetimeline = pygal.DateTimeLine( x_label_rotation=35, truncate_label=-1, x_value_formatter=lambda dt: dt.strftime('%d, %b %Y at %I:%M:%S %p')) datetimeline.add("Time", x)` where x comes from `for line in data: x.append(line[2])` but I am still getting the same error. – Ahmed Al-haddad Apr 30 '16 at 22:07
  • items in x should be a tuple (datetime, value) not a single value. see my code for reference. – lesingerouge Apr 30 '16 at 22:15
  • Hmm, I actually removed all of my code and left yours and I still got the error. Is it because the datetime is in this form `datetime.datetime(2016, 4, 30, 22, 37, 38)`? – Ahmed Al-haddad Apr 30 '16 at 22:30
  • what error are you getting? do you have any other settings on your pygal graph object? – lesingerouge Apr 30 '16 at 22:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/110735/discussion-between-lesingerouge-and-ahmed-al-haddad). – lesingerouge Apr 30 '16 at 22:38
  • I followed up here with another question (http://stackoverflow.com/questions/36961671/typeerror-bad-operand-type-for-abs-datetime-datetime-when-rendering-using?noredirect=1#comment61480382_36961671). Just thought that you might be interested. – Ahmed Al-haddad May 01 '16 at 11:32