如何使用graphview在kotlin中绘制方程式

时间:2019-04-18 18:47:30

标签: android kotlin android-graphview

我已经导入了GraphView库,并且由于其文档使用的语言很好,所以我很难将其转录为kotlin,我的目标是绘制统计分布图,因此我有以下公式:

“目标是在下图中绘制图形” formula

Hipotesismedia1Activity.kt

class Hipotesismedia1Activity : AppCompatActivity() {
    lateinit var series1: LineGraphSeries<DataPoint>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_hipotesismedia1)

        series1 = LineGraphSeries<DataPoint>()
        var x:Double = 0.0
        var y:Double = 0.0
        val sigma = 2.5
        val media = 300.0

        for ( i in 0..295){
            x += i.toDouble()
            y = Math.exp(-((x-media).pow(2))/(sigma* sqrt(2* PI)))/(sigma* sqrt(2* PI))
            series1.appendData(DataPoint(x,y),true,40)
        }
        plot.viewport.setMaxX(350.0)
        plot.addSeries(series1)
        plot.viewport.isXAxisBoundsManual = true
        plot.viewport.isScalable = true
        plot.viewport.isScrollable = true
    }
}

设计XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".Hipotesismedia1Activity">

    <com.jjoe64.graphview.GraphView
            android:id="@+id/plot"
            android:layout_width="match_parent"
            android:layout_height="match_parent" app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>
  

我的图表是一条三角形的线,请您尝试纠正我的错误吗?

0 个答案:

没有答案