Android圖表之-Echarts

ECharts是一款開源、功能強大的數據可視化產品,緊跟着大數據時代的步伐,是我接觸過的最優秀的可視化工具,也是進步最快的軟件,希望它早日成爲世界級的開源項目,之前使用過MPAndroidChart,achartengine 等android下的圖形圖標,相對而言Echart樣式更多,通過JS的形式來實現還是第一次見 ,話不多說直接上代碼 

 

  1.佈局文件比較簡單定時3個Button ,和一個Webview :


  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="com.hzbst.echartst.MainActivity" >

<LinearLayout 

    android:id="@+id/bt_ly"

    android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:gravity="center_horizontal"

    >

<Button

    android:id="@+id/linechart_bt"

    style="?android:attr/buttonStyleSmall"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_weight="1"

    android:text="折線圖" />

    

<Button

    android:id="@+id/barchart_bt"

    style="?android:attr/buttonStyleSmall"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_weight="1"

    android:text="柱狀圖" />

<Button

    android:id="@+id/piechart_bt"

    style="?android:attr/buttonStyleSmall"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_weight="1"

    android:text="餅狀圖" />

</LinearLayout>


<WebView

    android:id="@+id/chartshow_wb"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:layout_alignRight="@+id/bt_ly"

    android:layout_below="@+id/bt_ly" 

    android:layout_marginTop="43dp" />

    

</RelativeLayout>

2. 在代碼中去架子Echarts圖形

  MainActivity類 

找到Webview,然後設置一些屬性

chartshow_wb=(WebView)findViewById(R.id.chartshow_wb);

//進行webwiev的一堆設置

//開啓本地文件讀取(默認爲true,不設置也可以)

chartshow_wb.getSettings().setAllowFileAccess(true);

//開啓腳本支持

chartshow_wb.getSettings().setJavaScriptEnabled(true);

chartshow_wb.getSettings().setSupportZoom(true);

chartshow_wb.getSettings().setBuiltInZoomControls(true);

chartshow_wb.loadUrl("file:///android_asset/echart/myechart.html");

通過loadUrl加載,myecharts文件

mycharts文件保存在assets目錄下

通過三個按鈕來切換不同的視圖 :

折線圖調用:chartshow_wb.loadUrl("javascript:createChart('line',[29.2,29.2,29.2,29.2,29.2,29.1,29.1,]," +

"['2017-05-22','2017-05-22','2017-05-22','2017-05-22','2017-05-22','2017-05-22','2017-05-22',]);"); 上面有兩個參數,分別爲X,y軸對應的數據

其他兩個 :

chartshow_wb.loadUrl("javascript:createChart('bar',[100,100,100]);");

chartshow_wb.loadUrl("javascript:createChart('pie",[100,100,100]);");

這樣就可以了


源代碼下載地址:http://down.51cto.com/data/2309209

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章