class function to build graphs

This commit is contained in:
Andrew Ridgway 2024-07-12 23:14:40 +10:00
parent b4e5c8806a
commit 0278942e29
4 changed files with 95 additions and 22 deletions

View File

@ -18,10 +18,10 @@
</script> </script>
</head> </head>
<body> <body>
<div id="a2129a8b-778e-490c-ba6b-d8dbb5956510" data-root-id="p1001" style="display: contents;"></div> <div id="bb5c0e1d-dc95-4d96-9cbc-a29ed2eade5c" data-root-id="p1001" style="display: contents;"></div>
<script type="application/json" id="f9173064-b3c3-4120-bd9c-e9094743f5a6"> <script type="application/json" id="af8187ed-c6d3-40c3-84a3-bbe58799b466">
{"665fa898-b488-4b51-a790-cd13b33e7a78":{"version":"3.5.0","title":"Bokeh Application","roots":[{"type":"object","name":"Figure","id":"p1001","attributes":{"height":350,"x_range":{"type":"object","name":"FactorRange","id":"p1011","attributes":{"factors":["2024-03-27","2024-03-25","2024-03-15","2024-03-13"]}},"y_range":{"type":"object","name":"DataRange1d","id":"p1003","attributes":{"start":0}},"x_scale":{"type":"object","name":"CategoricalScale","id":"p1012"},"y_scale":{"type":"object","name":"LinearScale","id":"p1013"},"title":{"type":"object","name":"Title","id":"p1004","attributes":{"text":"Pool Ph"}},"renderers":[{"type":"object","name":"GlyphRenderer","id":"p1030","attributes":{"data_source":{"type":"object","name":"ColumnDataSource","id":"p1024","attributes":{"selected":{"type":"object","name":"Selection","id":"p1025","attributes":{"indices":[],"line_indices":[]}},"selection_policy":{"type":"object","name":"UnionRenderers","id":"p1026"},"data":{"type":"map","entries":[["x",["2024-03-27","2024-03-25","2024-03-15","2024-03-13"]],["top",[7.25,7.5,7.6,7.0]]]}}},"view":{"type":"object","name":"CDSView","id":"p1031","attributes":{"filter":{"type":"object","name":"AllIndices","id":"p1032"}}},"glyph":{"type":"object","name":"VBar","id":"p1027","attributes":{"x":{"type":"field","field":"x"},"width":{"type":"value","value":0.9},"top":{"type":"field","field":"top"},"line_color":{"type":"value","value":"#1f77b4"},"fill_color":{"type":"value","value":"#1f77b4"}}},"nonselection_glyph":{"type":"object","name":"VBar","id":"p1028","attributes":{"x":{"type":"field","field":"x"},"width":{"type":"value","value":0.9},"top":{"type":"field","field":"top"},"line_color":{"type":"value","value":"#1f77b4"},"line_alpha":{"type":"value","value":0.1},"fill_color":{"type":"value","value":"#1f77b4"},"fill_alpha":{"type":"value","value":0.1},"hatch_alpha":{"type":"value","value":0.1}}},"muted_glyph":{"type":"object","name":"VBar","id":"p1029","attributes":{"x":{"type":"field","field":"x"},"width":{"type":"value","value":0.9},"top":{"type":"field","field":"top"},"line_color":{"type":"value","value":"#1f77b4"},"line_alpha":{"type":"value","value":0.2},"fill_color":{"type":"value","value":"#1f77b4"},"fill_alpha":{"type":"value","value":0.2},"hatch_alpha":{"type":"value","value":0.2}}}}}],"toolbar":{"type":"object","name":"Toolbar","id":"p1010"},"toolbar_location":null,"left":[{"type":"object","name":"LinearAxis","id":"p1019","attributes":{"ticker":{"type":"object","name":"BasicTicker","id":"p1020","attributes":{"mantissas":[1,2,5]}},"formatter":{"type":"object","name":"BasicTickFormatter","id":"p1021"},"major_label_policy":{"type":"object","name":"AllLabels","id":"p1022"}}}],"below":[{"type":"object","name":"CategoricalAxis","id":"p1014","attributes":{"ticker":{"type":"object","name":"CategoricalTicker","id":"p1015"},"formatter":{"type":"object","name":"CategoricalTickFormatter","id":"p1016"},"major_label_policy":{"type":"object","name":"AllLabels","id":"p1017"}}}],"center":[{"type":"object","name":"Grid","id":"p1018","attributes":{"axis":{"id":"p1014"},"grid_line_color":null}},{"type":"object","name":"Grid","id":"p1023","attributes":{"dimension":1,"axis":{"id":"p1019"}}}]}}]}} {"00bec54e-f52e-4b7f-b2b6-8e6e4eb73c82":{"version":"3.5.0","title":"Bokeh Application","roots":[{"type":"object","name":"Figure","id":"p1001","attributes":{"height":250,"x_range":{"type":"object","name":"FactorRange","id":"p1011","attributes":{"factors":["2024-03-27","2024-03-25","2024-03-15","2024-03-13"]}},"y_range":{"type":"object","name":"DataRange1d","id":"p1003","attributes":{"start":6}},"x_scale":{"type":"object","name":"CategoricalScale","id":"p1012"},"y_scale":{"type":"object","name":"LinearScale","id":"p1013"},"title":{"type":"object","name":"Title","id":"p1004","attributes":{"text":"Pool data"}},"renderers":[{"type":"object","name":"GlyphRenderer","id":"p1030","attributes":{"data_source":{"type":"object","name":"ColumnDataSource","id":"p1024","attributes":{"selected":{"type":"object","name":"Selection","id":"p1025","attributes":{"indices":[],"line_indices":[]}},"selection_policy":{"type":"object","name":"UnionRenderers","id":"p1026"},"data":{"type":"map","entries":[["x",["2024-03-27","2024-03-25","2024-03-15","2024-03-13"]],["y",[7.25,7.5,7.6,7.0]]]}}},"view":{"type":"object","name":"CDSView","id":"p1031","attributes":{"filter":{"type":"object","name":"AllIndices","id":"p1032"}}},"glyph":{"type":"object","name":"Line","id":"p1027","attributes":{"x":{"type":"field","field":"x"},"y":{"type":"field","field":"y"},"line_color":"#1f77b4"}},"nonselection_glyph":{"type":"object","name":"Line","id":"p1028","attributes":{"x":{"type":"field","field":"x"},"y":{"type":"field","field":"y"},"line_color":"#1f77b4","line_alpha":0.1}},"muted_glyph":{"type":"object","name":"Line","id":"p1029","attributes":{"x":{"type":"field","field":"x"},"y":{"type":"field","field":"y"},"line_color":"#1f77b4","line_alpha":0.2}}}}],"toolbar":{"type":"object","name":"Toolbar","id":"p1010"},"toolbar_location":null,"left":[{"type":"object","name":"LinearAxis","id":"p1019","attributes":{"ticker":{"type":"object","name":"BasicTicker","id":"p1020","attributes":{"mantissas":[1,2,5]}},"formatter":{"type":"object","name":"BasicTickFormatter","id":"p1021"},"major_label_policy":{"type":"object","name":"AllLabels","id":"p1022"}}}],"below":[{"type":"object","name":"CategoricalAxis","id":"p1014","attributes":{"ticker":{"type":"object","name":"CategoricalTicker","id":"p1015"},"formatter":{"type":"object","name":"CategoricalTickFormatter","id":"p1016"},"major_label_policy":{"type":"object","name":"AllLabels","id":"p1017"}}}],"center":[{"type":"object","name":"Grid","id":"p1018","attributes":{"axis":{"id":"p1014"},"grid_line_color":null}},{"type":"object","name":"Grid","id":"p1023","attributes":{"dimension":1,"axis":{"id":"p1019"}}}]}}]}}
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
(function() { (function() {
@ -29,8 +29,8 @@
Bokeh.safely(function() { Bokeh.safely(function() {
(function(root) { (function(root) {
function embed_document(root) { function embed_document(root) {
const docs_json = document.getElementById('f9173064-b3c3-4120-bd9c-e9094743f5a6').textContent; const docs_json = document.getElementById('af8187ed-c6d3-40c3-84a3-bbe58799b466').textContent;
const render_items = [{"docid":"665fa898-b488-4b51-a790-cd13b33e7a78","roots":{"p1001":"a2129a8b-778e-490c-ba6b-d8dbb5956510"},"root_ids":["p1001"]}]; const render_items = [{"docid":"00bec54e-f52e-4b7f-b2b6-8e6e4eb73c82","roots":{"p1001":"bb5c0e1d-dc95-4d96-9cbc-a29ed2eade5c"},"root_ids":["p1001"]}];
root.Bokeh.embed.embed_items(docs_json, render_items); root.Bokeh.embed.embed_items(docs_json, render_items);
} }
if (root.Bokeh !== undefined) { if (root.Bokeh !== undefined) {

View File

@ -1,5 +1,16 @@
from bokeh.models.layouts import HBox
from charts import PoolCharts from charts import PoolCharts
from bokeh.io import output_file, show
from bokeh.layouts import row
output_file("data_plot.html")
chart = PoolCharts.PoolCharts() chart = PoolCharts.PoolCharts()
chart.ph_line_chart() ph = chart.line_chart("Pool PH", "ph", 50)
total_chlorine = chart.line_chart("Pool Total Chlorine", "total_chlorine", 50)
free_chlorine = chart.line_chart("Pool Free Chlorine", "free_chlorine", 50)
show(row(ph, total_chlorine, free_chlorine))

View File

@ -1,6 +1,7 @@
import sys import sys
from bokeh.plotting import figure, show from bokeh.models.widgets import DataCube
from bokeh.plotting import figure
from mongo.get_conn import db_conn from mongo.get_conn import db_conn
@ -9,38 +10,38 @@ class PoolCharts():
self.db = db_conn() self.db = db_conn()
def ph_line_chart(self): def line_chart(self, title, field, limit):
ph = self.db.pool_db data = self.db.pool_db
data = ph.find({}).sort("date", -1).limit(50) data = data.find({}).sort("date", -1).limit(limit)
dates = [] dates = []
ph_list = [] data_list = []
date_count = {} date_count = {}
for record in data: for record in data:
if record["ph"] != "None": if record[field] != "None":
if record["date"] not in dates: if record["date"] not in dates:
print("new date record") print("new date record")
dates.append(record["date"]) dates.append(record["date"])
ph_list.append(float(record["ph"])) data_list.append(float(record[field]))
print (dates) print (dates)
print (ph_list) print (data_list)
else: else:
if record["date"] in date_count: if record["date"] in date_count:
date_count[record["date"]] += 1 date_count[record["date"]] += 1
else: else:
date_count[record["date"]] = 2 date_count[record["date"]] = 2
print(len(ph_list)) print(len(data_list))
current_ph = ph_list[len(ph_list) - 1] current_data = data_list[len(data_list) - 1]
ph_list[len(ph_list) - 1] = (float(record["ph"]) + current_ph) / date_count[record["date"]] data_list[len(data_list) - 1] = (float(record[field]) + current_data) / date_count[record["date"]]
print(dates) print(dates)
print(ph_list) print(data_list)
p = figure(x_range=dates, height=350, title="Pool Ph", p = figure(x_range=dates, height=250, title=title,
toolbar_location=None, tools="") toolbar_location=None, tools="")
p.vbar(x=dates, top=ph_list, width=0.9) p.line(x=dates, y=data_list)
p.xgrid.grid_line_color = None p.xgrid.grid_line_color = None
p.y_range.start = 0 p.y_range.start = round(min(data_list) - 1)
show(p) return p

61
src/flask/data_plot.html Normal file

File diff suppressed because one or more lines are too long