JSoup ve Google Chart Kullanarak Türkiye nüfusu harita üzerinde nasılgösterilir?


Merhaba,

Öncelikle JSoup'tan bahsetmek gerekirse;

Jsoup, Java programlama diline ait açık kaynak kodlu bir html parser kütüphanesidir.

GoogleChart ise Google'ın hazırlamış olduğu ve ücretsiz sunduğu grafik kütüphanesidir.



MapBean.java


/**
 *
 * @author Omer Faruk KURT
 */
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

@ManagedBean(name = "mapBean")
@SessionScoped
public class MapBean implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 89459996582054769L;
    private List<MapClass> mapList;

    @PostConstruct
    public void init() {
        calistir();
    }

    public List<MapClass> getMapList() {
        return mapList;
    }

    public void setMapList(List<MapClass> mapList) {
        this.mapList = mapList;
    }

    public void calistir() {
        try {

            Document doc = Jsoup.connect("http://www.nufusu.com").get();
            Element table = doc.select("table").get(2); //select the first table.
            Elements rows = table.select("tr");
            mapList = new ArrayList<>();

            for (int i = 1; i < rows.size(); i++) { //first row is the col names so skip it.
                Element row = rows.get(i);
                Elements cols = row.select("td");

                /**
                 * baş harfi İ olanları I olarak degistir. Bunun sebebi ise
                 * GeoChart Istanbul ve Izmir olarak tanimaktadir.
                 */
                if (cols.get(1).text().startsWith("İ")) {
                    cols.get(1).text(cols.get(1).text().replace("İ", "I"));
                }

                /**
                 * Mersin'i Icel olarak degistir. Bunun sebebi ise GeoChart
                 * Mersin'i tanimiyor.
                 */
                if (cols.get(1).text().equals("Mersin")) {
                    cols.get(1).text("Içel");
                }

                /**
                 * Afyon ile baslayani Afyon olarak degistir.
                 * http://www.nufusu.com adresinde Afyonkarahisar olarak gelen
                 * veriyi GeoChart tanimiyor.
                 */
                if (cols.get(1).text().startsWith("Afyon")) {
                    cols.get(1).text("Afyon");
                }

                /**
                 * Replace yapmamizin amaci noktali sayi degerlerini duz hale
                 * cevirmek. Bu da yine GeoChart'in istedigi sekil
                 */
                mapList.add(new MapClass(cols.get(1).text(), cols.get(4).text().replace(".", "")));

            }

            setMapList(mapList);

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}



MapClass.java


/**
 *
 * @author Omer Faruk KURT
 */
public class MapClass {

    private String country;
    private String count;

    public MapClass(String country, String count) {
        this.country = country;
        this.count = count;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getCount() {
        return count;
    }

    public void setCount(String count) {
        this.count = count;
    }

}



index.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:c="http://java.sun.com/jsp/jstl/core">
    <h:head><title>PrimeFaces Test</title>
        <script type='text/javascript' src='https://www.google.com/jsapi'></script>
        <script type='text/javascript'>
            google.load('visualization', '1', {'packages': ['geochart']});
            google.setOnLoadCallback(drawRegionsMap);
            function drawRegionsMap() {
                var data = google.visualization.arrayToDataTable([
                        ['Şehir', 'Nüfus'],
                        < c:forEach var = "resultAnalysis" items = "${mapBean.mapList}" >
                        ['${resultAnalysis.country}',${resultAnalysis.count}],
                        < /c:forEach>
                        ]);
                        var options = {};
                var opts = {
                    region: 'TR',
                    displayMode: 'regions',
                    resolution: 'provinces'
                };

                var chart = new google.visualization.GeoChart(document.getElementById('chart_div'));
                chart.draw(data, opts);
            }
            ;
        </script>
    </h:head>
    <h:body>
        <div id="chart_div" style="width: 1200px; height: 800px;margin: 0 auto;"></div>
    </h:body>
</html>







Kaynak kodları

Demo

http://demo-kurtomerfaruk.rhcloud.com/Demo/jsoupgchart.xhtml



Sorunsuz javalı günler



Yorum Gönder

0 Yorumlar