Android Studio平臺下使用hellochart實現從txt檔案讀取資料繪折線圖
阿新 • • 發佈:2018-12-20
Android Studio平臺下使用hellochart實現從文字讀取資料繪折線圖
本人是一個剛剛接觸Android不超過兩個月的小白,最近在做的論文是關於這一塊的相關內容。所有的東西都是自學的,聽導師的建議也是第一次留個這樣的資料,可能有很多地方理解不到位,也歡迎大家給我更多的建議,讓我學習進步。
首先先匯入了一個第三方包:hellocharts-library-1.5.8.jar,目的是用來繪圖。然後在res資料夾下新建raw資料夾,在raw資料夾內放入文字資料,我自己這裡是a.txt
a.txt內是從串列埠接收到的資料再儲存下來的,這一塊因為是從學長那裡接手的,目前還沒太搞懂,留待以後解決。現在還是回來讀取資料在繪折線圖這一塊。
/** * 初始化表格設定 */ private void initLineChart() { Line line =new Line(mPointValues).setColor(Color.BLUE);//將資料點填充到線上,並設定線的顏色為藍色 List<Line> lines = new ArrayList<Line>(); //建立了一個ArrayList物件;<>用於向引數化類 // 型傳遞引數;傳遞進入一個Line型別的變數作為引數,詳細可見泛型 // List是Collection介面的子介面 List有一個重要的實現類--ArrayList類,List中的元素是有序排列的而且可重複,所以被稱為是序列。 line.setShape(ValueShape.CIRCLE); //折線上每個資料點的形狀,設定為圓形 line.setCubic(false); //曲線是否圓滑 line.setFilled(false); //是否填充曲線的面積 line.setHasLabels(true);//曲線的資料座標是否加上備註 line.setHasLabelsOnlyForSelected(true);//點選資料座標提示資料(設定了這個line.setHasLabels(true);就無效) line.setHasLines(true);//是否用線顯示。如果為false 則沒有曲線只有點顯示 line.setHasPoints(true);//是否顯示圓點 如果為false 則沒有原點只有點顯示(每個資料點都是個大的圓點) lines.add(line);//個人理解,將以上設定的 應用物件“line”的欄位傳給"lines" LineChartData data = new LineChartData(); data.setLines(lines);//以上三行程式碼,均為匯入折線的初始設定 //座標軸 Axis axisX = new Axis();//建立X軸的引用變數 Axis axisY = new Axis();//Y軸 axisX.setName("道址數"); axisY.setName("計數"); data.setAxisXBottom(axisX);//X軸設定在底部 data.setAxisYLeft(axisY);//Y軸設定在左側 //資料新增 data.setBaseValue(Float.NEGATIVE_INFINITY); lineChart.setLineChartData(data); //設定平行縮放等行為屬性 lineChart.setInteractive(true); lineChart.setZoomType(ZoomType.HORIZONTAL_AND_VERTICAL);//水平跟垂直方向上的放大 lineChart.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL); //上一行程式碼為設定水平方向可以滾動 lineChart.setVisibility(View.VISIBLE); //動畫效果 // prepareDataAnimation(); lineChart.startDataAnimation(1000); }
初始化表格屬性完成之後,就是新增資料,具體程式碼如下:
/**
* 設定X 軸的顯示
*/
private void getAxisXLables(){
for (int i = 0; i < Xdate.length; i++) {
mAxisXValues.add(new AxisValue(i).setLabel(Xdate[i]));
}
}
/** * 圖表的每個點的顯示 */ private void getAxisPoints() { for (int i = 0; i < showdata.length; i++) { mPointValues.add(new PointValue(i, showdata[i])); } }
最後是從檔案中讀取資料:
public String getString (InputStream inputStream) {
InputStreamReader inputStreamReader = null;
try {
inputStreamReader = new InputStreamReader(inputStream, "gbk");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
BufferedReader reader = new BufferedReader(inputStreamReader); //建立一個輸入流
StringBuffer sb = new StringBuffer();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
score=sb.toString();
return sb.toString();
}
}
佈局檔案:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="match_parent"
tools:context=".MainActivity">
<lecho.lib.hellocharts.view.LineChartView
android:id="@+id/chart_01"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
最後,整個Mainactivity.java放上來,裡面有部分內容還在測試,是為後續的演算法做的準備,例如HashMap這一塊。
package com.example.administrator.exchangetype;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import lecho.lib.hellocharts.gesture.ContainerScrollType;
import lecho.lib.hellocharts.gesture.ZoomType;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.ValueShape;
import lecho.lib.hellocharts.view.LineChartView;
- public class MainActivity extends AppCompatActivity {
//折線圖
private LineChartView lineChart;
private List<PointValue> mPointValues = new ArrayList<PointValue>();
private List<AxisValue> mAxisXValues = new ArrayList<AxisValue>();
static String score;
String[] Xdate=new String[1023];
int[] showdata=new int[1023];
int anum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lineChart = (LineChartView) findViewById(R.id.chart_01);
InputStream inputStream = getResources().openRawResource(R.raw.a);
getString(inputStream);
for (anum=0;anum<1023;anum++){
Xdate[anum]= anum+"";
}
String ss[] = score.split(" ");
byte scorechang[] = new byte[ss.length];
for (int i = 0; i < ss.length-1; i++) {
scorechang[i] = (byte) Integer.parseInt(ss[i],16);
showdata[i]=(int)scorechang[i];
}
// tvtext.setText(scorechang.toString()); 測試用
initLineChart();//初始化設定
getAxisPoints();//X軸的顯示
//paixv
Map<String,Integer> map = new HashMap<>();
for(String str : ss ){
Integer num = map.get(str);
map.put(str,num == null ? 1 : num + 1);
}
Set set = map.entrySet();
Iterator it = set.iterator();
while (it.hasNext()){
Map.Entry<String,Integer> entry =(Map.Entry<String, Integer>)it.next();
System.out.println("單詞 " + entry.getKey() + " 出現次數 : " + entry.getValue());
}
}
/**
* 初始化表格設定
*/
private void initLineChart() {
Line line =new Line(mPointValues).setColor(Color.BLUE);//將資料點填充到線上,並設定線的顏色為藍色
List<Line> lines = new ArrayList<Line>(); //建立了一個ArrayList物件;<>用於向引數化類
// 型傳遞引數;傳遞進入一個Line型別的變數作為引數,詳細可見泛型
// List是Collection介面的子介面 List有一個重要的實現類--ArrayList類,List中的元素是有序排列的而且可重複,所以被稱為是序列。
line.setShape(ValueShape.CIRCLE); //折線上每個資料點的形狀,設定為圓形
line.setCubic(false); //曲線是否圓滑
line.setFilled(false); //是否填充曲線的面積
line.setHasLabels(true);//曲線的資料座標是否加上備註
line.setHasLabelsOnlyForSelected(true);//點選資料座標提示資料(設定了這個line.setHasLabels(true);就無效)
line.setHasLines(true);//是否用線顯示。如果為false 則沒有曲線只有點顯示
line.setHasPoints(true);//是否顯示圓點 如果為false 則沒有原點只有點顯示(每個資料點都是個大的圓點)
lines.add(line);//個人理解,將以上設定的 應用物件“line”的欄位傳給"lines"
LineChartData data = new LineChartData();
data.setLines(lines);//以上三行程式碼,均為匯入折線的初始設定
//座標軸
Axis axisX = new Axis();//建立X軸的引用變數
Axis axisY = new Axis();//Y軸
axisX.setName("道址數");
axisY.setName("計數");
data.setAxisXBottom(axisX);//X軸設定在底部
data.setAxisYLeft(axisY);//Y軸設定在左側
//資料新增
data.setBaseValue(Float.NEGATIVE_INFINITY);
lineChart.setLineChartData(data);
//設定平行縮放等行為屬性
lineChart.setInteractive(true);
lineChart.setZoomType(ZoomType.HORIZONTAL_AND_VERTICAL);//水平跟垂直方向上的放大
lineChart.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL);
//上一行程式碼為設定水平方向可以滾動
lineChart.setVisibility(View.VISIBLE);
//動畫效果
// prepareDataAnimation();
lineChart.startDataAnimation(1000);
}
/**
* 圖表的每個點的顯示
*/
private void getAxisPoints() {
for (int i = 0; i < showdata.length; i++) {
mPointValues.add(new PointValue(i, showdata[i]));
}
}
/**
* 設定X 軸的顯示
*/
private void getAxisXLables(){
for (int i = 0; i < Xdate.length; i++) {
mAxisXValues.add(new AxisValue(i).setLabel(Xdate[i]));
}
}
public String getString (InputStream inputStream) {
InputStreamReader inputStreamReader = null;
try {
inputStreamReader = new InputStreamReader(inputStream, "gbk");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
BufferedReader reader = new BufferedReader(inputStreamReader); //建立一個輸入流
StringBuffer sb = new StringBuffer();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
score=sb.toString();
return sb.toString();
}
}
我使用的Android Studio版本是3.1.2,Gradle版本4.4,使用Android 8.0,華為mate8真機與模擬器都進行過測試。檔案放在網盤:https://pan.baidu.com/s/114jzmu3mp5pvwGON3OlQVg