Skip to content

Instantly share code, notes, and snippets.

@reuniware
Created July 5, 2019 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reuniware/d9be0c92da335456b8fd0fb7f7dd24f6 to your computer and use it in GitHub Desktop.
Save reuniware/d9be0c92da335456b8fd0fb7f7dd24f6 to your computer and use it in GitHub Desktop.
Kotlin Google Charts API easy and basic implementation
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)
}
}
@pedromalta
Copy link

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.

@reuniware
Copy link
Author

Yes you are right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment