Air Quality Widget - New Improved Feed

Posted on July 28th 2015
Share: aqicn.org/faq/2015-07-28/air-quality-widget-new-improved-feed/
{ widget : feed }

In order to improve the integration of our AQI widgets within external websites, we have developped a new widget API feed. Compared to the existing widget feed, this new API feed provides much more flexibility for the integration, but also many more options for customzing the widget appearance.

Beijing Air Quality.
76
Moderate
Updated on Thursday 4:00
PM2.5 AQI
Beijing PM25 (fine particulate matter)  measured by Beijing Environmental Protection Monitoring Center (北京市环境保护监测中心).
Values are converted to the US EPA AQI standard.
76
PM10 AQI
Beijing PM10 (respirable particulate matter)  measured by Beijing Environmental Protection Monitoring Center (北京市环境保护监测中心).
Values are converted to the US EPA AQI standard.
47
O3 AQI
Beijing O3 (ozone)  measured by Beijing Environmental Protection Monitoring Center (北京市环境保护监测中心).
Values are converted to the US EPA AQI standard.
17

You can, for instance, display a tiny widget as simple as Beijing

76
(move your mouse over the AQI number in order view the details), or a larger widget with details about the past 24 hours pollutants as shown on the right.

The new framework also supports templating, mutiple languages (English, Simplified and traditional Chinese, Spanish, ...) as well as possibility to do programatic integration with data based javascript callbacks.

A. Basic setup

The basic integration is simple and straight forward. You first need to add the following piece of code:

  
<script  type="text/javascript"  charset="utf-8">  
	(function  (w,  d,  t,  f)  {  
		w[f]  =  w[f]  ||  function  (c,  k,  n)  {  
			s  =  w[f],  k  =  s['k']  =  (s['k']  ||  (k  ?  ('&k='  +  k)  :  ''));  s['c']  =  
				c  =  (c  instanceof  Array)  ?  c  :  [c];  s['n']  =  n  =  n  ||  0;  L  =  d.createElement(t),  e  =  d.getElementsByTagName(t)[0];  
			L.async  =  1;  L.src  =  '//feed.aqicn.org/feed/'  +  (c[n].city)  +  '/'  +  (c[n].lang  ||  '')  +  '/feed.v1.js?n='  +  n  +  k;  
			e.parentNode.insertBefore(L,  e);  
		};  
	})(window,  document,  'script',  '_aqiFeed');    
</script>

Then, just add this other code to include the widget:

  
<span  id="city-aqi-container"></span>  
  
<script  type="text/javascript"  charset="utf-8">  
	_aqiFeed({  container:  "city-aqi-container",  city:  "beijing"  });  
</script>

The function `_aqiFeed` takes as argument the configuration block for the widget. The parameter `container: "city-aqi-container"` refers to the id of the tag in which you want to insert the widget (eg `<span id='city-aqi-container'></span>`), and the parameter city the name of the city for which you want to show the current Air Quality reading (eg `city: "london"`).

B. Specifiying the Language

北京
76

You can also specify the language to be used by setting the lang option. For instance, to include the widget in Chinese (as shown on the right), use the following code:

  
_aqiFeed({  
	container:"city-aqi-container",  
	city:"beijing",  
	lang:"cn"  
});  

The currently supported languages are:

  • "en": English
  • "cn": Chinese
  • "jp": Japanese
  • "es": Spanish
  • "kr": Korean
  • "ru": Russian
  • "hk": Traditional Chinese
  • "fr": French
  • "pl": Polish
  • "de": German
  • "pt": Portuguese
  • "vn": Vietnamese
  • "it": Italian
  • "id": Indonesian
  • "nl": Dutch
  • "fa": Persian
  • "th": Thai
  • "hu": Hungarian
  • "el": Greek
  • "ro": Romanian
  • "bg": Bulgarian
  • "ur": Urdu
  • "hi": Hindi
  • "ar": Arabic
  • "sr": Serbian
  • "bn": Bangla
  • "hu": Hungarian
  • "bs": Bosnian
  • "hr": Croatian
  • "tr": Turkish
  • "uk": Ukrainian
  • "cs": Czech
  • "be": Belarusian
  • "km": Khmer
  • "lo": Lao

    If not specified or set to null, English is used.

  • C. Specifiying the display format

    Beijing AQI is
    76

    on Thursday 4:00

    It is possible to customize the text to be displayed within the widget by specifying the display option. For instance, to display with both the textual "AQI" name as well as the update time (as shown on the widget on the left), you can use the following code:

    >
      
    _aqiFeed({    
    	display:"%cityname  AQI  is  %aqi<br><small>on  %date</small>",  
    	container:"city-aqi-container-display",    
    	city:"beijing"  
    });  
    

    The parameter display is an HTML-based string, and can contain any of the following keywords:

    • `%cityname` for the name of the city (eg Beijing),
    • `%aqi` for the decorated AQI value (eg 58),
    • `%aqiv` for the undecorated (raw text) AQI value (eg 58),
    • `%style` for the css declaration of the decorated AQI details (eg background-color: #ffde33;color:#000000;),
    • `%date` for the time at which the AQI was updated (eg Wed 20:00),
    • `%impact` for the associated health impact (eg Good, Moderate...)
    • `%attribution` for the AQI data attribution ( eg Beijing Environmental Protection Monitoring Center)
    • `%details` for the full AQI details (the content of the popup displayed when moving the mouse over the AQI value).

    Here are a few examples:

    Display Configuration Result
    %cityname Air Quality is <b>%impact</b>.
    Beijing Air Quality is Moderate.
    %aqi <small>(%impact)</small>
    76
    (Moderate)
    <span style='%style;font-size:12px;padding:5px 10px;'>%cityname AQI: %impact</span>
    Beijing AQI: Moderate
    %cityname Air Quality is <span style='%style;padding:0 5px'>%impact</span> (%aqiv on %date)
    Beijing Air Quality is Moderate (
    76
    on Thursday 4:00)
    %cityname Air Quality is provided by %attribution
    Beijing Air Quality is provided by Beijing Environmental Protection Monitoring Center (北京市环境保护监测中心)
    <div style='%style;max-width:180px;text-align:center;'><small>%cityname AQI:</small> <div style='font-size:88px;height:60px;padding-top:30px;'>%aqiv</div> %impact</div>
    Beijing AQI:
    76
    Moderate
    %details
    Beijing Air Quality.
    76
    Moderate
    Updated on Thursday 4:00
    PM2.5 AQI
    Beijing PM25 (fine particulate matter)  measured by Beijing Environmental Protection Monitoring Center (北京市环境保护监测中心).
Values are converted to the US EPA AQI standard.
    76
    PM10 AQI
    Beijing PM10 (respirable particulate matter)  measured by Beijing Environmental Protection Monitoring Center (北京市环境保护监测中心).
Values are converted to the US EPA AQI standard.
    47
    O3 AQI
    Beijing O3 (ozone)  measured by Beijing Environmental Protection Monitoring Center (北京市环境保护监测中心).
Values are converted to the US EPA AQI standard.
    17

    D. Programatic callback

    If you prefer DIY integration, you can also specify a javascript function callback to be when the data is loaded. You just need to add the option callback in the parameter block, such as for instance:

      
    _aqiFeed({  city:"beijing",  callback:function(aqi){  
    	/*  Do  whatever  you  want  with  the  AQI  object  */  
    	console.log(aqi);    
    }  });

    For instance, assuming that you are also using jquery, the following code:

      
    <div  id='my-container'></div>  
    <script  type="text/javascript"  charset="utf-8">  
    	_aqiFeed({  
    		city:  "beijing",  lang:  "pl",  callback:  function  (aqi)  {  
    			$("#my-container").html(aqi.details);  
    		}  
    	});  
    </script>

    will produce the following content:

    Pekin Jakość powietrza.
    76
    Moderate
    Poprawiony: Czwartek 4:00
    PM2.5 AQI
    Pekin PM25 (fine particulate matter)  measured by Beijing Environmental Protection Monitoring Center (北京市环境保护监测中心).
Values are converted to the US EPA AQI standard.
    76
    PM10 AQI
    Pekin PM10 (respirable particulate matter)  measured by Beijing Environmental Protection Monitoring Center (北京市环境保护监测中心).
Values are converted to the US EPA AQI standard.
    47
    O3 AQI
    Pekin O3 (ozone)  measured by Beijing Environmental Protection Monitoring Center (北京市环境保护监测中心).
Values are converted to the US EPA AQI standard.
    17

    The aqi objects contains all the keywords mentioned in the section C (display format).

    E. Including multiple widgets

    Including multiple widgets in a single page is possible. All you need is to pass to the `_aqiFeed` function an array with all the widgets you need to include. For instance, to insert London, Paris and New York, can do use this code:

      
    var  cities  =  ["london",  "newyork",  "seoul",  "guangzhou",  "tokyo",  "shanghai",  "paris","hongkong"];  
      
    var  aqiWidgetConfig  =  [];    
    cities.forEach(function(city)  {  aqiWidgetConfig.push({city:city,  callback:  displayCity});  });  
    _aqiFeed(aqiWidgetConfig);  
      
    function  displayCity(aqi)  {  
    	$("#mutiple-city-aqi").append(aqi.text("<center>%cityname<br>%aqi<br><small>%date</small></center>"));  
    }  
    
    The result is:
    London
    61
    Wednesday 19:00
    New-york
    -
    Thursday 9:00
    Seoul
    127
    Thursday 6:00
    Guangzhou
    -
    Tuesday 20:00
    Meguro
    33
    Thursday 5:00
    Shanghai
    127
    Thursday 4:00
    Paris
    59
    Wednesday 19:00
    HongKong
    59
    Thursday 4:00

    F. Feed usage

    This widget feed is provided for free, and for the sole purpose of integrating the feed into other websites (i.e. apps are excluded), and under the condition of reasonable and acceptable use. We reserve the right to change the feed structure (data format) at any time and without prior notice, so better to contact us if you plan to do a custom integration.

    When doing the integration, there are few deontological rules which should be applied when using the data feed:

    • First, always make sure that the attribution to the originating EPA is present. All the Air Quality data you can find on the World Air Quality Index project is thanks to the hard work from all the EPA's all over the world, so please make sure to provide the attribution for their work too. If you use the programmatic API, the attribution is available from the attribution field.
    • If you are from an institution and using the data, please, show respect and pay back the credits for our work. If you are trying to scrape the data feed, please, keep in mind that this project relies on sponsorship and cooperation projects, so consider supporting us rather than scrapping us, especially if you are a large scale institution or university.
    • As a matter of fact, do not act like the WRI (World Resource Institute ): Scrapping the overall World Air Quality Index project data feed, and beeing so cheap that they would not even give any attribution for our work, and this despite their 80 millions annual USD fundings. Don't believe us? Check this snapshot.

    G. Future improvments

    This is the first version of this new widget feed API. There are quite a few improvements already in the pipeline, such as adjustable graphic width and height, selectable AQI scale, etc. If you would like to suggest any other specific improvement, feel free to send us a message.

    Do you know of any Air Quality stations in your area? why not participate to the map with your own air quality station?
    Learn more at
    > aqicn.org/gaia/ <
    Signup for our free monthly mailing list, and get notified when new articles are available.

    Click here to see all the FAQ entries

    About the Air Quality and Pollution Measurement:

    About the Air Quality Levels

    AQIAir Pollution LevelHealth ImplicationsCautionary Statement (for PM2.5)
    0 - 50GoodAir quality is considered satisfactory, and air pollution poses little or no riskNone
    51 -100ModerateAir quality is acceptable; however, for some pollutants there may be a moderate health concern for a very small number of people who are unusually sensitive to air pollution.Active children and adults, and people with respiratory disease, such as asthma, should limit prolonged outdoor exertion.
    101-150Unhealthy for Sensitive GroupsMembers of sensitive groups may experience health effects. The general public is not likely to be affected.Active children and adults, and people with respiratory disease, such as asthma, should limit prolonged outdoor exertion.
    151-200UnhealthyEveryone may begin to experience health effects; members of sensitive groups may experience more serious health effectsActive children and adults, and people with respiratory disease, such as asthma, should avoid prolonged outdoor exertion; everyone else, especially children, should limit prolonged outdoor exertion
    201-300Very UnhealthyHealth warnings of emergency conditions. The entire population is more likely to be affected.Active children and adults, and people with respiratory disease, such as asthma, should avoid all outdoor exertion; everyone else, especially children, should limit outdoor exertion.
    300+HazardousHealth alert: everyone may experience more serious health effectsEveryone should avoid all outdoor exertion

    To know more about Air Quality and Pollution, check the wikipedia Air Quality topic or the airnow guide to Air Quality and Your Health.

    For very useful health advices of Beijing Doctor Richard Saint Cyr MD, check www.myhealthbeijing.com blog.


    Usage Notice: All the Air Quality data are unvalidated at the time of publication, and due to quality assurance these data may be amended, without notice, at any time. The World Air Quality Index project has exercised all reasonable skill and care in compiling the contents of this information and under no circumstances will the World Air Quality Index project team or its agents be liable in contract, tort or otherwise for any loss, injury or damage arising directly or indirectly from the supply of this data.



    Settings


    Language Settings:


    Temperature unit:
    Celcius