https://echarts.baidu.com/download.html
将下载好的echarts.min.js文件放入工程中assets目录下。如果没有assets目录,可以先在mian目录下,通过右击 new -> Folder -> Assets Folder 创建。
编写echarts.html文件,并将echarts.html放入assets目录。
EChart 依赖库
dependencies {
compile ‘com.github.abel533:ECharts:3.0.0.2’
implementation ‘com.google.code.gson:gson:2.8.1’
}
因为Echarts需要在WebView中显示,所以我们直接自定义一个EchartView继承自WebView用来显示图表。
public class EchartView extends WebView {
private static final String TAG = EchartView.class.getSimpleName();
public EchartView(Context context) {
this(context, null);
}
public EchartView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public EchartView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
WebSettings webSettings = getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setSupportZoom(false);
webSettings.setDisplayZoomControls(false);
loadUrl(“file:///android_asset/echarts.html”);
}
public void refreshEchartsWithOption(GsonOption option) {
if (option == null) {
return;
}
String optionString = option.toString();
String call = “javascript:loadEcharts('” + optionString + “')”;
loadUrl(call);
}
}
最后设置图表参数
public class MainActivity extends AppCompatActivity {
private EchartView lineChart;