Created
July 5, 2019 11:55
-
-
Save reuniware/d9be0c92da335456b8fd0fb7f7dd24f6 to your computer and use it in GitHub Desktop.
Kotlin Google Charts API easy and basic implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.activity | |
import android.os.Bundle | |
import kotlinx.android.synthetic.main.activity_statistiques.* | |
class StatsActivity : BaseActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_statistiques) | |
val content = ("<html>" | |
+ " <head>" | |
+ " <script type=\"text/javascript\" src=\"https://www.gstatic.com/charts/loader.js\"></script>" | |
+ " <script type=\"text/javascript\">" | |
+ " google.load(\"visualization\", \"1\", {packages:[\"corechart\"]});" | |
+ " google.setOnLoadCallback(drawChart);" | |
+ " function drawChart() {" | |
+ " var data = google.visualization.arrayToDataTable([" | |
+ " ['Year', 'Sales', 'Expenses']," | |
+ " ['2010', 1000, 400]," | |
+ " ['2011', 1170, 460]," | |
+ " ['2012', 660, 1120]," | |
+ " ['2013', 1030, 540]" | |
+ " ]);" | |
+ " var options = {" | |
+ " title: 'Truiton Performance'," | |
+ " hAxis: {title: 'Year', titleTextStyle: {color: 'red'}}" | |
+ " };" | |
+ " var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));" | |
+ " chart.draw(data, options);" | |
+ " }" | |
+ " </script>" | |
+ " </head>" | |
+ " <body>" | |
+ " <div id=\"chart_div\" style=\"width: " + webView1.width + "px; height: " + webView1.height + "px;\"></div>" | |
+ " <img style=\"padding: 0; margin: 0 0 0 330px; display: block;\" src=\"truiton.png\"/>" | |
+ " </body>" + "</html>") | |
val webSettings = webView1.settings | |
webSettings.javaScriptEnabled = true | |
webView1.requestFocusFromTouch() | |
webView1.loadDataWithBaseURL("file:///android_asset/", content, "text/html", "utf-8", null) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is interesting, but the hard part is that most of the use cases for this the Data would not be fixed in the code, but usually requested at runtime, so you need a way to "inject" that data on the JS part and sync everything. That's the harder part.