简体   繁体   English

如何获得浮点值以显示在 Google 图表中?

[英]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...免责声明:我对 Python 和 Javascript 都很陌生,所以请记住这一点......

I am using a Raspberry Pi connected to an air quality sensor to collect both PM2.5 and PM10 values.我正在使用连接到空气质量传感器的 Raspberry Pi 来收集 PM2.5 和 PM10 值。 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/ ).我已经有代码收集原始 PM 数据,然后将其转换为显示在网页上的 AQI 数字( 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.该表显示了 PM2.5 和 PM10 的原始 PM 数据以及计算出的 AQI 值。

The raw PM data is collected in a .json file, but it only contains the raw PM data, not the calculated AQI numbers.原始 PM 数据收集在 .json 文件中,但它只包含原始 PM 数据,而不包含计算出的 AQI 数字。 My intent is to show a graph of only the AQI numbers over time.我的目的是只显示 AQI 数字随时间变化的图表。 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.为了做到这一点,我修改了 Python 代码来计算 AQI 数值并将该数据以及原始 PM 数据保存到 .json 文件中。

At first the PM10 AQI numbers were all "0".起初,PM10 AQI 数字都是“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".结果证明这是因为 AQI 计算中的值之一小于 1,因此 Python 向下舍入为“0”。 Here's the code that does the AQI calculation:这是进行 AQI 计算的代码:

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.我通过将分母转换为浮点数并将值作为带有 2 个小数位的浮点数返回来解决了这个问题。 If I print out the values going into the .json file they look just fine:如果我打印出进入 .json 文件的值,它们看起来很好:

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):但是当我查看实际的 .json 文件时,我看到了这个(不是相同的数据 - 只需查看格式):

[{"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.请注意, aqipm10的值在引号中。 The other values do not have quotes.其他值没有引号。 When I graph the values using Google Chart, all the values except the aqipm10 values show up.当我使用 Google Chart 绘制值时,除了aqipm10值之外的所有值都显示出来。 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.但是,如果我创建了一个编辑过的.json文件,其中我已经从aqipm10值中删除了引号,那么这些值就会显示在图表上。

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.在从 Python 代码(计算值并将其放入 .json 文件)到读取 .json 文件(使用 javascript 创建网页和 Google 图表)的过渡过程中,这些“浮动”值的问题突然出现向上。 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.我确信可能有几种方法可以解决这个问题,但我对 Python 和 Javascript 都足够新,此时会迷失方向。

Looking for a way to clean up this mess.寻找一种清理这个烂摊子的方法。

Well, I did say that I was new to Python...好吧,我确实说过我是 Python 的新手……

The solution to my problem was actually quite simple.我的问题的解决方案实际上很简单。 My initial 'return' statement from the calcAQIpm10 routine was:我从 calcAQIpm10 例程中的初始“返回”语句是:

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.这以正确的精度打印出正确的信息,但是当查看 .json 文件中的值时,它周围有双引号。

After talking with a friend, he suggested that I look at python functions that might do what I needed.在与朋友交谈后,他建议我查看可能满足我需要的 Python 函数。 I found that the 'round' function did the right trick.我发现“round”函数做了正确的把戏。 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.通过进行此更改,.json 文件中的值周围没有双引号,并且很好地传递给了我正在使用的 Google Chart 代码。

Sorry to waste the bandwidth.抱歉浪费了带宽。

Rick瑞克

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Google 表格图表中查看零值? - How do I get see the zero value in a Google Sheets Chart? 如何在 JavaScript 中将商作为 int 并将余数作为浮点 - How do I get the quotient as int and remainder as a floating point in JavaScript 如何在Javascript中获取浮点数的小数位? - How do I get the decimal places of a floating point number in Javascript? 如何获得元素的精确浮点宽度? - How do I get the exact, floating point width of an element? Google Charts:如何在散点图中设置一个点的样式? - Google Charts: how do I style one point in a scatter chart? 如何在此Geocoder Google Maps API示例中显示搜索栏? - How do I get the search bar to show up in this Geocoder Google maps api example? 如何在google api图表的折线图笔划中显示点值? - How to display the point value in the line chart stroke of google api chart? 如何获取Java中只有零的浮点数的小数位? - How do I get the decimal places of a floating point number which has only zeros in Javascript? 如何从电子表格中获取值以显示在我的 Web 应用程序上? - How do I get a value from a spreadsheet to show up on my web app? 我如何让我的Google地图指向应该的位置? - How do i get my google map to point to the location it is supposed to?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM