简体   繁体   中英

How do I get a floating point value to show up in Google Chart?

Disclaimer: I am very new to both Python and Javascript, so please keep that in mind...

I am using a Raspberry Pi connected to an air quality sensor to collect both PM2.5 and PM10 values. I already have code that collects the raw PM data and then converts it to AQI numbers that are displayed on a web page ( https://openschoolsolutions.org/measure-particulate-matter-with-a-raspberry-pi/ ). But the historical data it collects is presented in a table which I really want shown as a graph. The table shows both the raw PM data as well as calculated AQI numbers for both PM2.5 and PM10.

The raw PM data is collected in a .json file, but it only contains the raw PM data, not the calculated AQI numbers. My intent is to show a graph of only the AQI numbers over time. In order to do that I've modified the Python code to calculate the AQI number and save that data as well as the raw PM data into the .json file.

At first the PM10 AQI numbers were all "0". That turned out to be be because the one of the values in the AQI calculation was less than 1 so Python rounded down to "0". Here's the code that does the AQI calculation:

def calcAQIpm10(pm10):
pm1 = 0
pm2 = 54
pm3 = 154
pm4 = 254
pm5 = 354
pm6 = 424
pm7 = 504
pm8 = 604

aqi1 = 0
aqi2 = 50
aqi3 = 100
aqi4 = 150
aqi5 = 200
aqi6 = 300
aqi7 = 400
aqi8 = 500

aqipm10 = 0

if (pm10 >= pm1 and pm10 <= pm2):
    aqipm10 = ((aqi2 - aqi1) / float((pm2 - pm1))) * (pm10 - pm1) + aqi1
elif (pm10 >= pm2 and pm10 <= pm3):
    aqipm10 = ((aqi3 - aqi2) / float((pm3 - pm2))) * (pm10 - pm2) + aqi2
elif (pm10 >= pm3 and pm10 <= pm4):
    aqipm10 = ((aqi4 - aqi3) / float((pm4 - pm3))) * (pm10 - pm3) + aqi3
elif (pm10 >= pm4 and pm10 <= pm5):
    aqipm10 = ((aqi5 - aqi4) / float((pm5 - pm4))) * (pm10 - pm4) + aqi4
elif (pm10 >= pm5 and pm10 <= pm6):
    aqipm10 = ((aqi6 - aqi5) / float((pm6 - pm5))) * (pm10 - pm5) + aqi5
elif (pm10 >= pm6 and pm10 <= pm7):
    aqipm10 = ((aqi7 - aqi6) / float((pm7 - pm6))) * (pm10 - pm6) + aqi6
elif (pm10 >= pm7 and pm10 <= pm8):
    aqipm10 = ((aqi8 - aqi7) / float((pm8 - pm7))) * (pm10 - pm7) + aqi7

#print("pm10 = ", pm10, " aqipm10 = ", aqipm10)
return format(aqipm10, '.2f')

I solved the problem by casting the denominator as a float and returning the value as a float with 2 decimal places. If I print out the values going into the .json file they look just fine:

PM2.5:  0.3 , PM10:  0.4 , aqiPM2.5:  1.2 , aqiPM10:  0.37

But when I look at the actual .json file I see this (not the same data - just look at the formatting):

[{"aqipm10": "1.76", "time": "07/19/2021 20:42:32", "aqipm25": 4.0, "pm10": 1.9, "pm25": 1.0},

Note that the value for aqipm10 is in quotes. The other values do not have quotes. When I graph the values using Google Chart, all the values except the aqipm10 values show up. If, however, I create an edited .json file where I've removed the quotes from the aqipm10 values, these values are then displayed on the graph.

Somewhere in the transition from Python code where the values are calculated and put into the .json file, to the reading of the .json file - using javascript to create the web page and the Google Chart - the problem with these "float" values pops up. I'm sure that there may be several ways of solving this problem, but I'm new enough with both Python and Javascript to be lost at this point.

Looking for a way to clean up this mess.

Well, I did say that I was new to Python...

The solution to my problem was actually quite simple. My initial 'return' statement from the calcAQIpm10 routine was:

return aqipm10

This gave me a nice floating point value, but way too much precision. I replaced it (as shown in my original posting) with:

return format(aqipm10, '.2f')

This printed out the right information with the right precision but when looking at the value in the .json file it had double quotes around it.

After talking with a friend, he suggested that I look at python functions that might do what I needed. I found that the 'round' function did the right trick. So now the code reads as:

return round(aqipm10, 2)

By making this change the value in the .json file had no double quotes around it and was passed nicely to the Google Chart code that I was using.

Sorry to waste the bandwidth.

Rick

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM