Get All-Weather Forecasts With Yahoo APIs for mobile

By Vaibhav Choudhary, May 15, 2009

With the JavaFX technology it is easy to build your own customized Weather forecasting tool by using Yahoo Webservices.

Understanding the Code

The code shown in Figure 1 creates Weather forecasting, which enables the user to see three-Day weather information from Yahoo API's.

Source Code

public class Weather extends CustomNode {
   public var city:String;
   public var tempToday: Integer=0;
   public var codeToday: String;
   public var cityID: Integer = 0;
   var url = "http://weather.yahooapis.com/forecastrss?p={city}";
   var p: PullParser;
   var h: HttpRequest;
   public var tempforecast_next_high:Integer[];
   public var tempforecast_next_low:Integer[];
   public var tempforecast_code:String[];
   public var tempforecast_day: String[];
   public var tempforecast_condition: String[];
   public var tempforecast_today: Integer;
   public var tempforecast_today_condition: String;

Figure 1: Weather.fx Class

Changing data member information such as a city name is easy in Chart.fx

This sample enables the user to view weather data in a 3-D bar chart. This weather data is taken at runtime from Yahoo Weather APIs.

Source Code
    init {
        weather.getDataFromWS();
        children = [
            HBox {
                padding: Insets { top: 50 left: 10}
                content: [
                    BarChart3D {
                        legendVisible: false
                        layoutInfo: LayoutInfo {
                            width: bind Main.STAGE_WIDTH -20
                            height: bind Main.STAGE_HEIGHT - 100
                        }
                        bottomShelfFill: Color.GREEN
                        data: [ barSeries1 ]
                        categoryAxis: CategoryAxis {
                            tickLabelsVisible: false
                            tickMarkVisible: false
                            categories: bind [
                                bar_names[0],
                                bar_names[1],
                                bar_names[2]
                            ]
                        }
                        valueAxis: NumberAxis {
                            opacity: 0
                            tickLabelsVisible: false
                            tickMarkVisible: false
                            visible: false
                            axisStroke: Color.TRANSPARENT
                            labelFill: Color.TRANSPARENT
                            lowerBound: 0
                            upperBound: 100
                            tickUnit: 10
                        }
                    },
                ]
            },
            weatherImages,
            Stack {
                visible: bind bubbleVisiblity
                translateX: bind locX
                translateY: bind locY +20
                content: [
                   ImageView {
                        image:Image { url: "{__DIR__}images/bubble.png" }
                    },
                    VBox {
                        padding: Insets { top: 4 left: 4 }
                        content: [
                            Text {
                                fill: Color.BLACK
                                font: Font {
                                    size: 10
                                    name: "Arial Bold"
                                }
                                content: bind "High: {getMax}\u00B0F\nLow:  {getMin}\u00B0F";
                            },
                            Text {
                                fill: Color.RED
                                font: Font {
                                    size: 10
                                    name: "Arial"
                                }
                                wrappingWidth: 80
                                content: bind "Condition: \n{condition}";
                            },
                        ]
                    }
                ]
            }
        ]
    }