我正在使用GraphView库(http://www.android-graphview.org/legend/)构建图表。我的图表中有一个侦听器,目的是从一开始就以零个数据点开始。我想单击图表中的特定位置以显示单击位置的数据点图标。
我所拥有的例子:
我想要的例子:
这是我的代码:
class MainActivity : AppCompatActivity() {
lateinit var series:LineGraphSeries<DataPoint>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val series = LineGraphSeries(
arrayOf(
DataPoint(9.0, 9.0),
DataPoint(10.0, 10.0),
DataPoint(11.0, 11.0),
DataPoint(12.0, 12.0),
DataPoint(13.0, 13.0),
DataPoint(14.0, 14.0)))
graph.addSeries(series)
var staticLabelsFormatter = StaticLabelsFormatter(graph)
staticLabelsFormatter.setHorizontalLabels(arrayOf<String>("2018", "2019", "2020","2021","2022","2023"))
graph.viewport.isScrollable = true
graph.gridLabelRenderer.gridStyle = GridLabelRenderer.GridStyle.NONE
graph.gridLabelRenderer.isVerticalLabelsVisible = false
graph.gridLabelRenderer.labelFormatter = staticLabelsFormatter
series.color = Color.BLACK
series.thickness = 6
series.isDrawBackground = true
series.isDrawDataPoints = true
series.isDrawDataPoints = false
series.backgroundColor = Color.parseColor("#0D000000")
series.setOnDataPointTapListener { series, dataPoint ->
Toast.makeText(
applicationContext,
"Series1: On Data Point clicked: $dataPoint",
Toast.LENGTH_SHORT
).show()
}
}
}