Skip to content

Commit

Permalink
fead: add label title and pie chart
Browse files Browse the repository at this point in the history
  • Loading branch information
SeptiawanAjiP committed Sep 8, 2023
1 parent c8b32a3 commit cd2cfb5
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.dewakoding.androidchartjsapp

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.dewakoding.androidchartjs.util.CommonConst
import com.dewakoding.androidchartjs.util.ChartType
import com.dewakoding.androidchartjsapp.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
Expand All @@ -12,7 +12,7 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(binding.root)

binding.androidChart.setChart(CommonConst.CHART_JS_BAR, arrayOf("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"), arrayOf(10, 3, 5, 8, 9))
binding.androidChart.setChart(ChartType.PIE, arrayOf("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"), arrayOf(10, 3, 5, 8, 9), "of Orders")
}

}
2 changes: 1 addition & 1 deletion chartjs/src/main/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
labels: JSON.parse(Android.getLabel()),
datasets: [
{
label: "# of Votes",
label: Android.getLabelTitle(),
data: JSON.parse(Android.getData()),
borderWidth: 1,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ class AndroidChartJSView @JvmOverloads constructor(
webView = WebView(context)
}

fun setChart(type: String,label: Array<String>, data: Array<Int>) {

jsi = JavascriptInterface(getContext(), type, Gson().toJson(label) , Gson().toJson(data))
fun setChart(type: String,label: Array<String>, data: Array<Int>, labelTitle: String) {
jsi = JavascriptInterface(getContext(), type, Gson().toJson(label) , Gson().toJson(data), labelTitle)
val webSettings: WebSettings = webView.getSettings()
webSettings.javaScriptEnabled = true
webView.addJavascriptInterface(jsi!!, JavascriptInterface.TAG_HANDLER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ class JavascriptInterface(
context: Context,
type: String,
label: String,
data: String

data: String,
labelTitle: String
) {
companion object {
const val TAG_HANDLER = "Android"
}
private val mType = type
private val mLabel = label
private val mData = data
private val mLabelTitle = labelTitle


@JavascriptInterface
Expand All @@ -42,5 +43,10 @@ class JavascriptInterface(
return mData.toString()
}

@JavascriptInterface
fun getLabelTitle(): String {
return mLabelTitle
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ email : [email protected]
website : dewakoding.com
**/
class CommonConst {
class ChartType {
companion object {
val CHART_JS_BAR = "bar"
val CHART_JS_LINE = "line"
val BAR = "bar"
val LINE = "line"
val PIE = "pie"
}
}

0 comments on commit cd2cfb5

Please sign in to comment.