android - 将数据从 csv 文件传递到折线图 | 科特林
问题描述
我有一个 MPAndroidChart 库中的折线图,我希望使用 scv 文件中的数据更新它的数据。有人可以教我怎么做,或者给我看一个我可以做的图书馆。我是初学者,我不知道如何去做
我在这里制作图表的页面
我的 MainActivity.kt
package com.example.aplikacjadlataty
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.Switch
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.github.mikephil.charting.animation.Easing
import com.github.mikephil.charting.charts.LineChart
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.LineData
import com.github.mikephil.charting.data.LineDataSet
import com.google.android.gms.ads.MobileAds
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//1
val button = findViewById<Button>(R.id.button_loadCsv)
val text = findViewById<TextView>(R.id.text)
val sw1 = findViewById<Switch>(R.id.switch1)
sw1?.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) screen.setBackgroundColor(Color.DKGRAY)
else screen.setBackgroundColor(Color.WHITE)
if (isChecked) layout.setBackgroundColor(Color.DKGRAY)
else layout.setBackgroundColor(Color.WHITE)
if (isChecked) AdsLayout.setBackgroundColor(Color.DKGRAY)
else AdsLayout.setBackgroundColor(Color.WHITE)
if (isChecked) text.text = "Set light mode"
else text.text = "Set dark mode"
if (isChecked) text.setTextColor(Color.WHITE)
else text.setTextColor(Color.BLACK)
if (isChecked) button.setTextColor(Color.WHITE)
else button.setTextColor(Color.BLACK)
if (isChecked) button.setBackgroundColor(R.color.DGray)
else button.setBackgroundColor(R.color.LGray)
}
//2
Log.d(
"MainActivity", "onCreate Called \n" +
" \n" +
" \n" +
" \n" +
" __ ___ ___ __ __ _____ ______ __ __ ____ __ _ __ ____ ___ __ __ \n" +
" / |/ / / | / //_/ / ___/ / ____/ / / / / / __ \\ / / / | / / / _/ / | / //_/ \n" +
" / /|_/ / / /| | / ,< \\__ \\ / / / /_/ / / / / / __ / / / |/ / / / / /| | / ,< \n" +
" / / / / / ___ | / /| | ___/ / / /___ / __ / / /_/ / / /_/ / / /| / _/ / / ___ | / /| | \n" +
"/_/ /_/ /_/ |_|/_/ |_| /____/ \\____/ /_/ /_/ \\____/ \\____/ /_/ |_/ /___/ /_/ |_|/_/ |_| \n" +
" \n" +
" \n" +
" "
)
//3
MobileAds.initialize(this) {}
//4
val lineChart = findViewById<LineChart>(R.id.lineChart)
val entries = ArrayList<Entry>()
entries.add(Entry(1f, 3.4121f))
entries.add(Entry(2f, 3.8195f))
entries.add(Entry(3f, 3.8253f))
entries.add(Entry(4f, 3.9150f))
entries.add(Entry(5f, 4f))
entries.add(Entry(6f, 3.5252f))
entries.add(Entry(7f, 3.8956f))
entries.add(Entry(8f, 3.8561f))
entries.add(Entry(9f, 4.0200f))
entries.add(Entry(10f, 3.0392f))
entries.add(Entry(11f, 3.0420f))
entries.add(Entry(12f, 3.5034f))
entries.add(Entry(13f, 4.3270f))
entries.add(Entry(14f, 4.6201f))
entries.add(Entry(15f, 3.9295f))
entries.add(Entry(16f, 3.7232f))
entries.add(Entry(17f, 3.0725f))
entries.add(Entry(18f, 3.5252f))
entries.add(Entry(19f, 4.0371f))
entries.add(Entry(20f, 3.5252f))
entries.add(Entry(21f, 4.1403f))
entries.add(Entry(22f, 4.4237f))
entries.add(Entry(23f, 3.8392f))
entries.add(Entry(24f, 3.5252f))
val vl = LineDataSet(entries, "USD/PLN")
vl.setDrawValues(false)
vl.setDrawFilled(true)
vl.lineWidth = 3f
vl.fillColor = R.color.gray
vl.fillAlpha = R.color.red
lineChart.xAxis.labelRotationAngle = 0f
lineChart.data = LineData(vl)
lineChart.axisRight.isEnabled = false
lineChart.xAxis.axisMaximum = 24 + 0.1f
lineChart.setTouchEnabled(true)
lineChart.setPinchZoom(true)
lineChart.description.text = "Hour"
lineChart.setNoDataText("No forex yet!")
lineChart.animateX(1800, Easing.EaseInExpo)
val markerView = CustomMarker(this@MainActivity, R.layout.marker_view)
lineChart.marker = markerView
//5 p1
findViewById<Button>(R.id.button_loadCsv)?.setOnClickListener {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "text/*"
startActivityForResult(intent, READ_REQUEST_CODE)
}
}
//5 p2
companion object {
const val READ_REQUEST_CODE = 123
}
}
.csv 文件
84500;2238
84500;2238
84500;2238
84500;2238
84500;2238
84500;2238
84500;2238
解决方案
您可以将其视为一个由两部分组成的问题:
为用户提供一种在其设备上选择 CSV 文件的方法。这可以使用存储访问框架轻松完成。
实际逐行读取 CSV 文件。OpenCSV 库使这非常容易。
如果您需要了解更多关于存储访问框架的信息,我推荐这个链接。
推荐阅读
- node.js - 如何解决您需要启用 javascript 才能在 React 应用程序中运行此应用程序的问题?
- java - 没有CALL_LOG权限无法获取Android P来电号码
- python - 如何在 python 中创建一个显示所有可用命令的命令?喜欢终端的帮助
- javascript - 如何使用函数 removeData 添加和删除嵌套数组“项目”中的数据
- python - 如何以最佳方式处理不在 word2vec 词汇中的单词
- php - 用于多租户的 Laravel Spatie/权限过滤器
- c# - 使用 ContinueDialogAsync 时字典中不存在给定键“对话框”
- c# - 有条件地创建对象
- delphi - 表单控件(编辑、组合框、备忘录等)查询是否已修改?
- sql - Firebird/Lazarus SQL 视图与选择 iif?