如何在 Fragment 中插入 MPAndroidChart
How to insert MPAndroidChart in Fragment
我正在尝试在 Android studio 中以片段形式显示图表。
创建图表的函数是“setChart_Parameters()”
当我尝试在 Activity 中插入图表时,一切正常,但当我在片段中尝试相同时,应用程序关闭。谁能帮帮我?
我怀疑我在图表函数中做错了什么。当我不在片段中调用图表创建函数时,应用程序不会关闭。
这是我的片段代码
fragment_chart.xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".ChartFragment"
>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Data Graph"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/logchart"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
"ChartFragment.kt"
class ChartFragment : Fragment() {
private var param1: String? = null
private var param2: String? = null
var dataPoints = 30
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
Log.d("FragTest","Chart setting")
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
Log.d("FragTest","Chart setting")
var view: View = inflater.inflate(R.layout.fragment_chart, container, false)
setChart_Parameters()
return view
}
companion object {
@JvmStatic
fun newInstance(param1: String, param2: String) =
ChartFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
private fun setChart_Parameters()
{
logchart.apply{
setBackgroundColor(Color.WHITE)
description.isEnabled=false
setTouchEnabled(true)
//setOnChartValueSelectedListener(this)
setDrawGridBackground(false)
isDragEnabled = true
setScaleEnabled(true)
setPinchZoom(true)
}
var legend: Legend = logchart.legend.apply {
form = Legend.LegendForm.LINE
textSize = 11f
textColor = Color.WHITE
verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM
horizontalAlignment = Legend.LegendHorizontalAlignment.LEFT
orientation = Legend.LegendOrientation.HORIZONTAL
setDrawInside(false)
}
var xAxis: XAxis = logchart.xAxis
logchart.axisRight.isEnabled = false
var yAxis: YAxis = logchart.axisLeft.apply {
enableGridDashedLine(10f, 10f, 0f)
axisMaximum = 200f
axisMinimum = -50f
}
var llAXis = LimitLine(9f, "Index 10").apply {
setLineWidth(4f)
enableDashedLine(10f, 10f, 0f)
setLabelPosition(LimitLine.LimitLabelPosition.RIGHT_BOTTOM)
setTextSize(10f)
}
val ll1 = LimitLine(150f, "Upper Limit").apply {
lineWidth = 4f
enableDashedLine(10f, 10f, 0f)
labelPosition = LimitLine.LimitLabelPosition.RIGHT_TOP
textSize = 10f
}
val ll2 = LimitLine(-30f, "Lower Limit").apply{
lineWidth = 4f
enableDashedLine(10f, 10f, 0f)
labelPosition = LimitLine.LimitLabelPosition.RIGHT_BOTTOM
textSize = 10f
}
var value1 = ArrayList<Entry>().apply {
for(i in 1..dataPoints){
add(Entry(i.toFloat(), (i+20).toFloat()))
}
}
var value2 = ArrayList<Entry>().apply {
for(i in 1..dataPoints){
add(Entry(i.toFloat(), (i+30).toFloat()))
}
}
val set1: LineDataSet
val set2: LineDataSet
if (logchart.data != null && logchart.data.dataSetCount > 0
) {
set1 = logchart.data.getDataSetByIndex(0) as LineDataSet
set2 = logchart.data.getDataSetByIndex(1) as LineDataSet
set1.values = value1
set2.values = value2
logchart.data.notifyDataChanged()
logchart.notifyDataSetChanged()
} else {
set1 = LineDataSet(value1, "Temperature")
set1.setDrawIcons(false)
set1.enableDashedLine(10f, 5f, 0f)
set1.enableDashedHighlightLine(10f, 5f, 0f)
set1.color = Color.RED
set1.setCircleColor(Color.RED)
set1.lineWidth = 2f
set1.circleRadius = 3f
set1.setDrawCircleHole(false)
set1.valueTextSize = 9f
set1.setDrawFilled(true)
set1.formLineWidth = 1f
set1.formLineDashEffect = DashPathEffect(floatArrayOf(10f, 5f), 0f)
set1.formSize = 10f
set2 = LineDataSet(value2, "Humidity")
set2.setDrawIcons(false)
set2.enableDashedLine(10f, 5f, 0f)
set2.enableDashedHighlightLine(10f, 5f, 0f)
set2.color = Color.BLUE
set2.setCircleColor(Color.BLUE)
set2.lineWidth = 2f
set2.circleRadius = 3f
set2.setDrawCircleHole(false)
set2.valueTextSize = 9f
set2.setDrawFilled(true)
set2.formLineWidth = 1f
set2.formLineDashEffect = DashPathEffect(floatArrayOf(10f, 5f), 0f)
set2.formSize = 15f
val dataSets = LineData(set1, set2)
logchart.data = dataSets
}
}
}
我的主要activity如下:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_data)
Log.d("FragTest","Fragment begin")
var fragVar = ChartFragment()
supportFragmentManager.beginTransaction().apply {
replace(R.id.chartFrame, fragVar)
commit()
}
}
}
尝试将 setChart_Parameters()
移动到 onViewCreated()
。因为在onCreateView()
完成之前,无法到达布局中的所有视图。
我正在尝试在 Android studio 中以片段形式显示图表。
创建图表的函数是“setChart_Parameters()”
当我尝试在 Activity 中插入图表时,一切正常,但当我在片段中尝试相同时,应用程序关闭。谁能帮帮我?
我怀疑我在图表函数中做错了什么。当我不在片段中调用图表创建函数时,应用程序不会关闭。
这是我的片段代码
fragment_chart.xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".ChartFragment"
>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Data Graph"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/logchart"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
"ChartFragment.kt"
class ChartFragment : Fragment() {
private var param1: String? = null
private var param2: String? = null
var dataPoints = 30
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
Log.d("FragTest","Chart setting")
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
Log.d("FragTest","Chart setting")
var view: View = inflater.inflate(R.layout.fragment_chart, container, false)
setChart_Parameters()
return view
}
companion object {
@JvmStatic
fun newInstance(param1: String, param2: String) =
ChartFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
private fun setChart_Parameters()
{
logchart.apply{
setBackgroundColor(Color.WHITE)
description.isEnabled=false
setTouchEnabled(true)
//setOnChartValueSelectedListener(this)
setDrawGridBackground(false)
isDragEnabled = true
setScaleEnabled(true)
setPinchZoom(true)
}
var legend: Legend = logchart.legend.apply {
form = Legend.LegendForm.LINE
textSize = 11f
textColor = Color.WHITE
verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM
horizontalAlignment = Legend.LegendHorizontalAlignment.LEFT
orientation = Legend.LegendOrientation.HORIZONTAL
setDrawInside(false)
}
var xAxis: XAxis = logchart.xAxis
logchart.axisRight.isEnabled = false
var yAxis: YAxis = logchart.axisLeft.apply {
enableGridDashedLine(10f, 10f, 0f)
axisMaximum = 200f
axisMinimum = -50f
}
var llAXis = LimitLine(9f, "Index 10").apply {
setLineWidth(4f)
enableDashedLine(10f, 10f, 0f)
setLabelPosition(LimitLine.LimitLabelPosition.RIGHT_BOTTOM)
setTextSize(10f)
}
val ll1 = LimitLine(150f, "Upper Limit").apply {
lineWidth = 4f
enableDashedLine(10f, 10f, 0f)
labelPosition = LimitLine.LimitLabelPosition.RIGHT_TOP
textSize = 10f
}
val ll2 = LimitLine(-30f, "Lower Limit").apply{
lineWidth = 4f
enableDashedLine(10f, 10f, 0f)
labelPosition = LimitLine.LimitLabelPosition.RIGHT_BOTTOM
textSize = 10f
}
var value1 = ArrayList<Entry>().apply {
for(i in 1..dataPoints){
add(Entry(i.toFloat(), (i+20).toFloat()))
}
}
var value2 = ArrayList<Entry>().apply {
for(i in 1..dataPoints){
add(Entry(i.toFloat(), (i+30).toFloat()))
}
}
val set1: LineDataSet
val set2: LineDataSet
if (logchart.data != null && logchart.data.dataSetCount > 0
) {
set1 = logchart.data.getDataSetByIndex(0) as LineDataSet
set2 = logchart.data.getDataSetByIndex(1) as LineDataSet
set1.values = value1
set2.values = value2
logchart.data.notifyDataChanged()
logchart.notifyDataSetChanged()
} else {
set1 = LineDataSet(value1, "Temperature")
set1.setDrawIcons(false)
set1.enableDashedLine(10f, 5f, 0f)
set1.enableDashedHighlightLine(10f, 5f, 0f)
set1.color = Color.RED
set1.setCircleColor(Color.RED)
set1.lineWidth = 2f
set1.circleRadius = 3f
set1.setDrawCircleHole(false)
set1.valueTextSize = 9f
set1.setDrawFilled(true)
set1.formLineWidth = 1f
set1.formLineDashEffect = DashPathEffect(floatArrayOf(10f, 5f), 0f)
set1.formSize = 10f
set2 = LineDataSet(value2, "Humidity")
set2.setDrawIcons(false)
set2.enableDashedLine(10f, 5f, 0f)
set2.enableDashedHighlightLine(10f, 5f, 0f)
set2.color = Color.BLUE
set2.setCircleColor(Color.BLUE)
set2.lineWidth = 2f
set2.circleRadius = 3f
set2.setDrawCircleHole(false)
set2.valueTextSize = 9f
set2.setDrawFilled(true)
set2.formLineWidth = 1f
set2.formLineDashEffect = DashPathEffect(floatArrayOf(10f, 5f), 0f)
set2.formSize = 15f
val dataSets = LineData(set1, set2)
logchart.data = dataSets
}
}
}
我的主要activity如下:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_data)
Log.d("FragTest","Fragment begin")
var fragVar = ChartFragment()
supportFragmentManager.beginTransaction().apply {
replace(R.id.chartFrame, fragVar)
commit()
}
}
}
尝试将 setChart_Parameters()
移动到 onViewCreated()
。因为在onCreateView()
完成之前,无法到达布局中的所有视图。