Skip to content

Commit 35c2ada

Browse files
authored
Merge pull request #436 from AppDevNext/KotlinCustomView
Kotlin custom view
2 parents 284be55 + 4f4c188 commit 35c2ada

File tree

8 files changed

+149
-216
lines changed

8 files changed

+149
-216
lines changed

app/src/main/java/info/appdev/chartexample/custom/MyMarkerView.java

Lines changed: 0 additions & 52 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package info.appdev.chartexample.custom
2+
3+
import android.annotation.SuppressLint
4+
import android.content.Context
5+
import android.widget.TextView
6+
import com.github.mikephil.charting.components.MarkerView
7+
import com.github.mikephil.charting.data.CandleEntry
8+
import com.github.mikephil.charting.data.Entry
9+
import com.github.mikephil.charting.highlight.Highlight
10+
import com.github.mikephil.charting.utils.MPPointF
11+
import com.github.mikephil.charting.utils.Utils
12+
import info.appdev.chartexample.R
13+
14+
/**
15+
* Custom implementation of the MarkerView.
16+
*/
17+
@SuppressLint("ViewConstructor")
18+
class MyMarkerView(context: Context?, layoutResource: Int) : MarkerView(context, layoutResource) {
19+
private val tvContent: TextView = findViewById(R.id.tvContent)
20+
21+
// runs every time the MarkerView is redrawn, can be used to update the
22+
// content (user-interface)
23+
override fun refreshContent(e: Entry, highlight: Highlight?) {
24+
if (e is CandleEntry) {
25+
tvContent.text = Utils.formatNumber(e.high, 0, true)
26+
} else {
27+
tvContent.text = Utils.formatNumber(e.y, 0, true)
28+
}
29+
30+
super.refreshContent(e, highlight)
31+
}
32+
33+
override fun getOffset(): MPPointF {
34+
return MPPointF(-(width / 2).toFloat(), -height.toFloat())
35+
}
36+
}

app/src/main/java/info/appdev/chartexample/custom/RadarMarkerView.java

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package info.appdev.chartexample.custom
2+
3+
import android.annotation.SuppressLint
4+
import android.content.Context
5+
import android.graphics.Typeface
6+
import android.widget.TextView
7+
import com.github.mikephil.charting.components.MarkerView
8+
import com.github.mikephil.charting.data.Entry
9+
import com.github.mikephil.charting.highlight.Highlight
10+
import com.github.mikephil.charting.utils.MPPointF
11+
import info.appdev.chartexample.R
12+
import java.text.DecimalFormat
13+
14+
/**
15+
* Custom implementation of the MarkerView.
16+
*/
17+
@SuppressLint("ViewConstructor")
18+
class RadarMarkerView(context: Context, layoutResource: Int) : MarkerView(context, layoutResource) {
19+
private val tvContent: TextView = findViewById(R.id.tvContent)
20+
private val format = DecimalFormat("##0")
21+
22+
init {
23+
tvContent.setTypeface(Typeface.createFromAsset(context.assets, "OpenSans-Light.ttf"))
24+
}
25+
26+
// runs every time the MarkerView is redrawn, can be used to update the
27+
// content (user-interface)
28+
override fun refreshContent(e: Entry, highlight: Highlight?) {
29+
tvContent.text = String.format("%s %%", format.format(e.y.toDouble()))
30+
31+
super.refreshContent(e, highlight)
32+
}
33+
34+
override fun getOffset(): MPPointF {
35+
return MPPointF(-(width / 2).toFloat(), (-height - 10).toFloat())
36+
}
37+
}

app/src/main/java/info/appdev/chartexample/custom/StackedBarsMarkerView.java

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package info.appdev.chartexample.custom
2+
3+
import android.annotation.SuppressLint
4+
import android.content.Context
5+
import android.widget.TextView
6+
import com.github.mikephil.charting.components.MarkerView
7+
import com.github.mikephil.charting.data.BarEntry
8+
import com.github.mikephil.charting.data.Entry
9+
import com.github.mikephil.charting.highlight.Highlight
10+
import com.github.mikephil.charting.utils.MPPointF
11+
import com.github.mikephil.charting.utils.Utils
12+
import info.appdev.chartexample.R
13+
14+
/**
15+
* Custom implementation of the MarkerView.
16+
*/
17+
@Suppress("unused")
18+
@SuppressLint("ViewConstructor")
19+
class StackedBarsMarkerView(context: Context?, layoutResource: Int) : MarkerView(context, layoutResource) {
20+
private val tvContent: TextView = findViewById(R.id.tvContent)
21+
22+
// runs every time the MarkerView is redrawn, can be used to update the
23+
// content (user-interface)
24+
override fun refreshContent(entry: Entry, highlight: Highlight) {
25+
if (entry is BarEntry) {
26+
27+
if (entry.yVals != null) {
28+
// draw the stack value
29+
tvContent.text = Utils.formatNumber(entry.yVals!![highlight.stackIndex], 0, true)
30+
} else {
31+
tvContent.text = Utils.formatNumber(entry.y, 0, true)
32+
}
33+
} else {
34+
tvContent.text = Utils.formatNumber(entry.y, 0, true)
35+
}
36+
37+
super.refreshContent(entry, highlight)
38+
}
39+
40+
override fun getOffset(): MPPointF {
41+
return MPPointF(-(width / 2).toFloat(), -height.toFloat())
42+
}
43+
}

app/src/main/java/info/appdev/chartexample/custom/XYMarkerView.java

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package info.appdev.chartexample.custom
2+
3+
import android.annotation.SuppressLint
4+
import android.content.Context
5+
import android.widget.TextView
6+
import com.github.mikephil.charting.components.MarkerView
7+
import com.github.mikephil.charting.data.Entry
8+
import com.github.mikephil.charting.formatter.IAxisValueFormatter
9+
import com.github.mikephil.charting.highlight.Highlight
10+
import com.github.mikephil.charting.utils.MPPointF
11+
import info.appdev.chartexample.R
12+
import java.text.DecimalFormat
13+
14+
/**
15+
* Custom implementation of the MarkerView.
16+
*/
17+
@SuppressLint("ViewConstructor")
18+
class XYMarkerView(context: Context?, private val xAxisValueFormatter: IAxisValueFormatter) : MarkerView(context, R.layout.custom_marker_view) {
19+
private val tvContent: TextView = findViewById(R.id.tvContent)
20+
21+
private val format: DecimalFormat = DecimalFormat("###.0")
22+
23+
// runs every time the MarkerView is redrawn, can be used to update the content (user-interface)
24+
override fun refreshContent(e: Entry, highlight: Highlight?) {
25+
tvContent.text = String.format("x: %s, y: %s", xAxisValueFormatter.getFormattedValue(e.x, null), format.format(e.y.toDouble()))
26+
27+
super.refreshContent(e, highlight)
28+
}
29+
30+
override fun getOffset(): MPPointF {
31+
return MPPointF(-(width / 2).toFloat(), -height.toFloat())
32+
}
33+
}

0 commit comments

Comments
 (0)