Google Chart 2

Google chart tool is a great tool to visualize data, you can easily to do HTTP GET or POST to get the chart image. And on Android we can use WebView to display Google chart in very easy steps.

1, Put the WebView inside the layout


            
                 android:layout_width="fill_parent"
             android:layout_height="fill_parent" 
             android:visibility="gone"/>
    


2, Use WebView.loadUrl to call Google Chart API and show the chart

WebView mCharView = (WebView) findViewById(R.id.char_view);
mUrl = "http://chart.apis.google.com/chart?cht=bvg&chs=250x150&chd=s:Monkeys&chxt=x,y&chxs=0,ff0000,12,0,lt|1,0000ff,10,1,lt";
mCharView.loadUrl(mUrl);
    The result:
Google Chart 2_第1张图片

    In another way, you can put several chart in one page, that is, to create the html by yourself, and let WebView to display it.

WebView mCharView = (WebView) findViewById(R.id.char_view);
mCharView.loadData(getData(), "text/html", "utf-8");

....

    private String getData() {
        StringBuilder sb = new StringBuilder();

        sb.append("
");

        sb.append("
");

        sb.append("
");

        sb.append("
");

        sb.append("
");

        sb.append("
");

        sb.append("
");

        sb.append("
");

        sb.append("
");

        sb.append("
");

        return sb.toString();
    }
    The result:

Google Chart 2_第2张图片
Google Chart 2_第3张图片
Google Chart 2_第4张图片
Google Chart 2_第5张图片

    BTW, you can also use HTTPClient to save the chart as file.

    At last, there's an open source project: chartdroid, to provide native chart support, you just need to pass the URI of your content provider to get the chart view.

你可能感兴趣的:(Android)