Displaying Charts on Android with WebView

19 篇文章 0 订阅
12 篇文章 0 订阅

http://www.techsfo.com/blog/2014/01/charts-on-android-with-webview/

We are building an Android app and want to render charts. Charts are diagrammatic representations of numeric data, which include bar and pie charts. We found two methods for rendering charts: native Android and JavaScript hosted in WebView. The native Android method requires Java packaged jar libraries such as AChartEngine and AndroidPlot. The WebView method uses HTML 5, CSS3, and JavaScript such as Chart.js and Flot.js. We took the WebView method because of simplicity and to reduce development time. The remainder of this article will focus on the WebView method.

Android OS Version

Due to many Android operating system (OS) versions, we have to choose which OSes to support. In each revision of the OS, UI components such as WebView behave differently. WebView in Android 4.4 (KitKat, API 19) is based on Chromium, which supports standard HTML 5 and CSS31,2. Previous Android versions (API 18 or lower) will operate in quirks mode with non-standard web features2.

Focusing on displaying charts locally on the device requires allowing WebView to access files from the URL. This is only required on Android 4.3 (Jelly Bean, API 18) or higher:

We also noted that older OSes below Android 3.0 (Honeycomb, API 11) have a non-conformant WebView to standard web standards. For example, the performance is subpar. In order to compensate for these behavioural differences,we decided to disable the chart feature for these older OSes.

Project Setup for WebView

Like all Android applications, we create an XML layout file for the UI and set the necessary permissions in the app manifest.

Setting up the Android layout is straightforward:

Note: There is no need to include <uses-permission android:name="android.permission.INTERNET" /> in the AndroidManifest.xml because we are loading HTML files locally, which does not require Internet access.

Loading a WebView Chart

Next, we’ll add the HTML to load in the proper directory and configure the WebView control. We’ll also have to change the HTML slightly for showing in a mobile web browser.

To load files from the URL requires placing files in <project dir>/src/assets/ and then loading HTML:

Displaying charts in WebView requires JavaScript:

To make sure that the chart is displayed properly, include the following in the HTML:

Interacting with the WebView

For our application, the charts are not interactive. We disable touch events on the HTML page. By disabling touch events, we do not have to deal with unnecessary zooming, scrolling, and text selection:

WebView Lifecycle

We ran into a problem when leaving the activity. WebView will display the following message in logcat3:

EventHub.removeMessages(int what = 107) is not supported before WebViewCore is set up

To prevent this message from showing up use WebView stopLoading() method:

Web Chart Libraries

Now that we are able to load a WebView to display HTML, several alternative libraries for rendering JavaScript charts are available. We tried two JavaScript libraries, Chart.js and Flot.js.

Chart.js

We selected Chart.js to display the bar and pie charts. This chart package is easy and simple to use. It does not require jQuery. We loaded the chart package in our own chart using the following HTML:

Chart.js does not provide axis titles or a legend in the bar chart. For the pie chart, it does not provide the option to label the pie chart sections. However, a forked version of Chart.js adds these features.

An example using the pie chart with the forked Chart.js:

Flot.js 

Flot.js provides customization through plugins. It requires jQuery, which is included in the package or can be downloaded directly from the JQuery website. Load the script just before the end of the HTML:

jquery.colorhelpers.js: makes it easier to add color to the chart
jquery.flot.js: the chart script
jquery.flot.categories.js: allows plotting text or categories with the data
jquery.flot.tickrotor.js: allows rotation of the x- and/ or y-axis tick labels4
jquery.flot.axislabels.js: display x- and y-axis labels5

The following shows how to use the plugins, this is placed after loading the .js files:

Concluding Remarks

The WebView approach is effective for displaying charts. Our method, however, only works for displaying non-interactive charts with low development time. Interactive WebViews may encounter the same performance issues as the PhoneGap approach6,7 such as include lack of responsiveness and non-native appearance. Since we do not require user interactivity, the WebView approach enables us to render charts using well-developed JavaScript libraries instead of platform-specific Android libraries.

Google Developers: “WebView for Android”

Android.com: “Migrating to WebView in Android 4.4″

Stack Overflow: “Android : EventHub.removeMessages(int what = 107) is not supported before the WebViewCore is set up”

GitHub Mark Côté: flot-tickrotor

GitHub Mark Côté: flot-axislabels

Andrew Trice: “Performance & UX Considerations For Successful PhoneGap Apps”

Greg Avola: “Creating apps with PhoneGap: Lessons learned”

Android.com: “Building Web Apps in WebView”

Android.com: “WebView”


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值