我想使用Mp Chart
创建折线图我正在使用的代码是
private fun setData() {
val entries: MutableList<Entry> = ArrayList()
entries.add(Entry(0f, 4f))
entries.add(Entry(1f, 3f))
entries.add(Entry(2f, 2f))
entries.add(Entry(3f, 1f))
entries.add(Entry(4f, 5f))
entries.add(Entry(5f, 4f))
entries.add(Entry(6f, 6f))
val dataSet = LineDataSet(entries, "Customized values")
dataSet.color = ContextCompat.getColor(context!!, R.color.colorPrimary)
dataSet.valueTextColor = ContextCompat.getColor(context!!, R.color.colorPrimaryDark)
//****
// Controlling X axis
val xAxis = line_chart.xAxis
// Set the xAxis position to bottom. Default is top
xAxis.position = XAxis.XAxisPosition.BOTTOM
//Customizing x axis value
val months = arrayOf("M", "T", "W", "T", "F", "S", "S")
val formatter = IAxisValueFormatter { value, axis -> months[value.toInt()] }
xAxis.granularity = 1f // minimum axis-step (interval) is 1
xAxis.valueFormatter = formatter
//***
// Controlling right side of y axis
val yAxisRight = line_chart.axisRight
yAxisRight.isEnabled = false
//***
// Controlling left side of y axis
val yAxisLeft = line_chart.axisLeft
yAxisLeft.granularity = 1f
// Setting Data
val data = LineData(dataSet)
line_chart.data = data
line_chart.invalidate()
}
xml文件是简单的Mp android xml视图
<com.github.mikephil.charting.charts.LineChart
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/line_chart"/>
请帮助我实现以上图像折线图。
答案 0 :(得分:2)
要获得渐变效果,
dataSet.drawFilled = true
dataSet.filledDrawable = ContextCompat.getDrawable(this, R.drawable.gradient)
还有gradient.xml
:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="90"
android:startColor="#00cfcce3"
android:endColor="#ffcfcce3" />
</shape>