/[H9]/trunk/tntnet/dynamic/temperaturegraph.ecpp
ViewVC logotype

Annotation of /trunk/tntnet/dynamic/temperaturegraph.ecpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 195 - (hide annotations) (download)
Sat Dec 8 16:47:03 2007 UTC (16 years, 5 months ago) by torben
File size: 2257 byte(s)
Use persistant connections / connection pooling for faster db access

1 torben 118 <%pre>
2     #include <tntdb/connect.h>
3     #include <tntdb/connection.h>
4     #include <tntdb/result.h>
5     #include <tntdb/row.h>
6    
7    
8     #include "gd-cpp/gdpp.h"
9     #include <gdfonts.h>
10     #include <sstream>
11    
12     </%pre>
13     <%config>
14     dburl;
15     </%config>
16     <%args>
17     int id;
18     </%args>
19     <{
20 torben 177 reply.setHeader("Cache-Control", "no-cache, must-revalidate"); // Http/1.1
21     reply.setHeader("Expires", "Mon, 26 Jul 1997 05:00:00 GMT"); //date in the past
22 torben 118
23     GD::Image image;
24 torben 122 image.CreateTrueColor( GD::Size(600,360));
25 torben 118
26 torben 120 //Black color scheme
27     //GD::TrueColor decoration(45,95,0);
28     //GD::TrueColor text(200,200,200);
29     //GD::TrueColor line(255,0,0);
30 torben 118
31 torben 120 //White color scheme
32     image.Fill( GD::Point(1,1), GD::TrueColor(255,255,255).Int() );
33     GD::TrueColor decoration(60,60,60);
34     GD::TrueColor text(0,0,0);
35     GD::TrueColor line(255,0,0);
36 torben 118
37 torben 120
38 torben 118 image.String(gdFontSmall, 250,5, "Temperature graph", text.Int());
39 torben 122 image.String(gdFontSmall, 200,18, "Latest 20 samples, in degrees Celcius", text.Int());
40 torben 118
41     for (int i=0; i<11; ++i)
42     {
43     std::stringstream marker;
44     marker << (100- (i*10));
45 torben 122 int y = (i*30) + 40;
46 torben 120 image.Line( 30, y, 590, y, decoration.Int());
47 torben 118
48     image.String(gdFontSmall, 5, y-7, marker.str().c_str(), text.Int());
49    
50     }
51     /////////////////////////////////////////////////
52    
53    
54 torben 195 tntdb::Connection conn = tntdb::connectCached(dburl);
55 torben 118
56     std::stringstream query;
57     query << "SELECT temperature FROM ( ";
58     query << " SELECT messagenr,temperature FROM logtable ";
59     query << " WHERE installationnr = " << id << " ";
60     query << " ORDER BY messagenr DESC LIMIT 20 ";
61     query << ") query ";
62     query << "ORDER BY messagenr ASC";
63    
64     tntdb::Result res = conn.select(query.str());
65    
66     GD::Point previous;
67 torben 126 for (unsigned int i=0; i<res.size(); ++i)
68 torben 118 {
69     int current = res[i].getInt(0);
70 torben 122 int x = (i*28) + 45;
71     int y = ((100-current) * 3) + 40;
72 torben 118
73 torben 120 //image.FilledArc( GD::Point(x,y), GD::Size(3,3), 0, 360, line.Int(), gdPie);
74     image.FilledRectangle( GD::Point(x-1,y-1), GD::Point(x+1,y+1), line.Int() );
75 torben 118
76     if (i>0)
77     {
78 torben 120 image.Line( GD::Point(x,y), previous, line.Int());
79 torben 118 }
80    
81     previous = GD::Point(x,y);
82    
83     }
84    
85     //////////////////////////////////////////////////
86     // echo the image to the browser...
87     reply.setContentType("image/png");
88     int size;
89     unsigned char* ptr = (unsigned char*) image.Png(&size);
90    
91     for (int i=0; i<size; ++i)
92     reply.out() << ptr[i];
93    
94     gdFree(ptr);
95    
96     }>

  ViewVC Help
Powered by ViewVC 1.1.20