使用Android原生WebView+Highcharts實現(xiàn)可左右滑動的折線圖
前言
今天的內(nèi)容是使用Android原生webview配合Highcharts開發(fā)折線圖左右滑動的功能,折線圖有很多優(yōu)秀的第三方庫可以使用,比如MPAndroidChart、Highcharts,MPAndroidChart可以通過拉伸折線圖界面進行滑動折線圖,親測有效,現(xiàn)在我要講的是如何使用內(nèi)嵌html頁面進行滑動折線圖的開發(fā)。效果以及源碼目錄結(jié)構(gòu)如下圖,源碼地址:gitee.com/fjjxxy/slid…
開發(fā)的思路如下:
1.編寫activity,內(nèi)含一個webview控件
2.編寫工具類,與js進行交互,傳輸一些數(shù)據(jù)(例如網(wǎng)絡(luò)請求需要的參數(shù))
3.編寫html與js代碼,通過Android與js約定好的方法名進行數(shù)據(jù)的獲取,完成Android與js的交互
4.調(diào)整html中div的寬度,達到自己想要的滑動以及折線圖效果
(一)設(shè)計Android端的View層
activity_main.xml代碼如下:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> ? <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent"/> </androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java代碼如下:如果想與js進行交互,不要忘了加上第17行進行數(shù)據(jù)傳輸
public class MainActivity extends AppCompatActivity { private WebView mWebView; ? @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mWebView = findViewById(R.id.webView); initWebView(new HashMap<>()); } ? public void initWebView(Map<String, String> map) { // 啟用支持javascript WebSettings settings = mWebView.getSettings(); settings.setJavaScriptEnabled(true); mWebView.setVerticalScrollBarEnabled(false); mWebView.addJavascriptInterface(new JsDataUtils(map), "android"); mWebView.loadUrl("file:///android_asset/line.html"); } }
(二)設(shè)計與JS交互的方法
JsDataUtils.java的代碼如下,通過@JavascriptInterface與js進行交互,傳輸數(shù)據(jù),這里傳輸?shù)氖蔷W(wǎng)絡(luò)請求需要的參數(shù),到時候可以在js中使用ajax請求數(shù)據(jù)并繪制折線圖,但是我們這里為了方便,只用模擬數(shù)據(jù)進行演示
public class JsDataUtils { private Map<String, String> map; private Gson mGson = new Gson(); ? /** * 通過構(gòu)造函數(shù)像js傳遞網(wǎng)絡(luò)請求的參數(shù),以map作為容器,后面再轉(zhuǎn)成字符串 * * @param map */ public JsDataUtils(Map<String, String> map) { this.map = map; } ? @JavascriptInterface public String stringToHtml() { return mGson.toJson(map); } }
(三)開發(fā)html以及js邏輯
line.html代碼如下?:這里通過調(diào)整div的寬度決定折線圖X軸的間隔劃分,已知如果不進行間隔設(shè)置的話默認是均分
<!doctype html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <script type="text/javascript" src="js/jquery-1.8.3.min.js"></script> <script type="text/javascript" src="js/highcharts.js"></script> <script type="text/javascript" src="js/jquery.mCustomScrollbar.min.js"></script> <script src="js/line.js"></script> ? </head> <body> <div id="container" style="width:2000px;height:400px;"></div> <script type="text/javascript" src="js/line.js" charset="utf-8"></script> </body> </html>
line.js?代碼如下:14-22行是進行ajax請求數(shù)據(jù),最后在chart的series中的data屬性進行賦值繪制,這里使用測試數(shù)據(jù)進行演示,第6行是獲取activity中傳輸?shù)臄?shù)據(jù)
var newArray=new Array(); var chart=null; $(document).ready(function() { //通過Android方法來獲取參數(shù)map,轉(zhuǎn)成json對象獲取參數(shù) var mapJson=android.stringToHtml(); var map=JSON.parse(mapJson); function ajax_data() { //這里先用死數(shù)據(jù)進行測試,網(wǎng)絡(luò)請求可以使用下面的ajax var dataArr = [400,-500,300,200,400,-500,300,200,400,-500,300,200,400,-500,300,200, 400,-500,300,200,400,-500,300,200,400,-500,300,200,400,-500,300,200, 400,-500,300,200,400,-500,300,200,400,-500,300,200,400,-500,300,200, 400,-500,300,200,400,-500,300,200,400,-500,300,200,400,-500,300,200]; /*$.ajax({ type : "GET", url : "",//網(wǎng)絡(luò)請求接口地址 async : false, dataType : "json", success : function(data){ //這里處理返回的數(shù)據(jù),即data } });*/ return dataArr; } chart = Highcharts.chart('container', { legend: { align: 'left', verticalAlign: 'top', x: 70, y: -12, itemDistance: 1, itemStyle: {color:'#666', fontWeight:'normal' } }, title: {text: ''}, credits: {enabled: false}, xAxis: [{ categories: [], crosshair: true, tickmarkPlacement: 'on', tickPosition: 'inside', tickInterval:1, labels: { // step:1, // enabled:false, style: { color: '#999' } } }], chart:{ }, scrollbar:{ enabled:false }, yAxis: [{ gridLineDashStyle: 'Dash', gridLineColor: '#c7c7c7', gridLineWidth: 1, min:-1000, tickInterval:50, title:"",//刪除y軸注釋 max:1000, }], tooltip: { shared: true }, series: [{ name: '數(shù)據(jù)', type: 'line', yAxis: 0, data: ajax_data(), color: Highcharts.getOptions().colors[3], tooltip: { valueSuffix: '' }, lineWidth:0.5, marker: { symbol: 'circle', lineWidth: 0.5, lineColor: Highcharts.getOptions().colors[3], fillColor: 'white' } }] }); });
以上三步,我們可左右滑動的折線圖就完成了,效果圖和源碼地址在開頭
總結(jié)
到此這篇關(guān)于使用Android原生WebView+Highcharts實現(xiàn)可左右滑動的折線圖的文章就介紹到這了,更多相關(guān)Android WebView Highcharts左右滑動折線圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android Handler中的休眠喚醒實現(xiàn)詳解
這篇文章主要為大家介紹了Android Handler中的休眠喚醒實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01Android下Activity全屏顯示實現(xiàn)方法
這篇文章主要介紹了Android下Activity全屏顯示實現(xiàn)方法,以兩種不同的方法來實現(xiàn)這一技巧,非常具有實用性,需要的朋友可以參考下2014-10-10Android快速開發(fā)之定制BaseTemplate
這篇文章主要為大家詳細介紹了Android快速開發(fā)之定制BaseTemplate的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02