完善信用分历史记录折线图

0515
panqihua 5 years ago
parent d9b949d55b
commit f4fe963f85
  1. 55
      app/src/main/java/com/community/pocket/data/model/CreditScore.java
  2. 16
      app/src/main/java/com/community/pocket/data/model/MyInfo.java
  3. 8
      app/src/main/java/com/community/pocket/ui/BaseActivity.java
  4. 6
      app/src/main/java/com/community/pocket/ui/BaseFragment.java
  5. 3
      app/src/main/java/com/community/pocket/ui/main/ui/forum/ForumPost.java
  6. 3
      app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataActivity.java
  7. 135
      app/src/main/java/com/community/pocket/ui/main/ui/info/InfoFragment.java
  8. 35
      app/src/main/java/com/community/pocket/ui/main/ui/info/MyChartView.java
  9. 2
      app/src/main/java/com/community/pocket/ui/main/ui/visitor/VisitorMyVisitor.java
  10. 24
      app/src/main/res/layout/chart.xml
  11. 9
      app/src/main/res/layout/main/layout/info/layout/info_fragment.xml
  12. 3
      app/src/main/res/values-en-rUS/strings.xml
  13. 3
      app/src/main/res/values-zh-rCN/strings.xml
  14. 3
      app/src/main/res/values/strings.xml
  15. 2
      build.gradle

@ -0,0 +1,55 @@
package com.community.pocket.data.model;
//信用分历史记录
public class CreditScore {
//用户名
private String username;
//信用分变化
private Integer score;
//变化前分数
private Integer beforeScore;
//备注
private String notes;
//记录时间
private Long time;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
public Integer getBeforeScore() {
return beforeScore;
}
public void setBeforeScore(Integer beforeScore) {
this.beforeScore = beforeScore;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
public Long getTime() {
return time;
}
public void setTime(Long time) {
this.time = time;
}
}

@ -4,7 +4,7 @@ import java.util.List;
public class MyInfo { public class MyInfo {
//用户名 //用户名
private String username; private String id;
//信用分 //信用分
private Integer creditScore; private Integer creditScore;
//头像 //头像
@ -19,14 +19,14 @@ public class MyInfo {
private String email; private String email;
//信用分历史记录 //信用分历史记录
private List<Integer> scoreHistory; private List<CreditScore> scoreHistory;
public String getUsername() { public String getId() {
return username; return id;
} }
public void setUsername(String username) { public void setId(String id) {
this.username = username; this.id = id;
} }
public Integer getCreditScore() { public Integer getCreditScore() {
@ -77,11 +77,11 @@ public class MyInfo {
this.email = email; this.email = email;
} }
public List<Integer> getScoreHistory() { public List<CreditScore> getScoreHistory() {
return scoreHistory; return scoreHistory;
} }
public void setScoreHistory(List<Integer> scoreHistory) { public void setScoreHistory(List<CreditScore> scoreHistory) {
this.scoreHistory = scoreHistory; this.scoreHistory = scoreHistory;
} }
} }

@ -2,10 +2,13 @@ package com.community.pocket.ui;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.text.format.DateFormat;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import com.community.pocket.R;
import org.xutils.x; import org.xutils.x;
/** /**
@ -22,4 +25,9 @@ public abstract class BaseActivity extends AppCompatActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
x.view().inject(this);//加载布局,控件 x.view().inject(this);//加载布局,控件
} }
//格式化日期
protected String formatUnix(Long time) {
return DateFormat.format(getString(R.string.dateformat), time).toString();
}
} }

@ -1,6 +1,7 @@
package com.community.pocket.ui; package com.community.pocket.ui;
import android.os.Bundle; import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -49,4 +50,9 @@ public abstract class BaseFragment extends Fragment {
return (T) fragment; return (T) fragment;
} }
} }
//格式化日期
protected String formatUnix(Long time) {
return DateFormat.format(getString(R.string.dateformat), time).toString();
}
} }

@ -1,7 +1,6 @@
package com.community.pocket.ui.main.ui.forum; package com.community.pocket.ui.main.ui.forum;
import android.content.Intent; import android.content.Intent;
import android.text.format.DateFormat;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Button; import android.widget.Button;
@ -67,7 +66,7 @@ public abstract class ForumPost extends BaseFragment {
TextView postReply = childView.findViewById(R.id.post_reply); TextView postReply = childView.findViewById(R.id.post_reply);
postReply.setText(getString(R.string.post_reply, forum.getReply())); postReply.setText(getString(R.string.post_reply, forum.getReply()));
TextView time = childView.findViewById(R.id.post_time); TextView time = childView.findViewById(R.id.post_time);
time.setText(getString(R.string.post_time, DateFormat.format(getString(R.string.dateformat), forum.getTime()))); time.setText(getString(R.string.post_time, formatUnix(forum.getTime())));
ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
layoutParams.setMargins(0, 0, 0, 50); layoutParams.setMargins(0, 0, 0, 50);
childView.setLayoutParams(layoutParams); childView.setLayoutParams(layoutParams);

@ -7,7 +7,6 @@ import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.os.Message; import android.os.Message;
import android.text.Editable; import android.text.Editable;
import android.text.format.DateFormat;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Button; import android.widget.Button;
@ -282,7 +281,7 @@ public class ForumDataActivity extends BaseActivity {
TextView score = view.findViewById(R.id.score); TextView score = view.findViewById(R.id.score);
score.setText(getString(R.string.credit_score, forumContent.getInfo().getCreditScore())); score.setText(getString(R.string.credit_score, forumContent.getInfo().getCreditScore()));
TextView time = view.findViewById(R.id.time); TextView time = view.findViewById(R.id.time);
time.setText(DateFormat.format(getString(R.string.dateformat), forumContent.getTime())); time.setText(formatUnix(forumContent.getTime()));
TextView tower = view.findViewById(R.id.tower); TextView tower = view.findViewById(R.id.tower);
tower.setText(getString(R.string.tower, forumContent.getTower())); tower.setText(getString(R.string.tower, forumContent.getTower()));
TextView content = view.findViewById(R.id.content); TextView content = view.findViewById(R.id.content);

@ -2,6 +2,8 @@ package com.community.pocket.ui.main.ui.info;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
@ -23,6 +25,7 @@ import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
import com.community.pocket.R; import com.community.pocket.R;
import com.community.pocket.data.model.CreditScore;
import com.community.pocket.data.model.LocalToken; import com.community.pocket.data.model.LocalToken;
import com.community.pocket.data.model.MyInfo; import com.community.pocket.data.model.MyInfo;
import com.community.pocket.ui.BaseFragment; import com.community.pocket.ui.BaseFragment;
@ -33,10 +36,14 @@ import com.community.pocket.util.AppDatabase;
import com.community.pocket.util.Param; import com.community.pocket.util.Param;
import com.community.pocket.util.PropertiesUtil; import com.community.pocket.util.PropertiesUtil;
import com.github.mikephil.charting.charts.LineChart; import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData; import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet; import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.utils.Utils;
import org.xutils.view.annotation.ContentView; import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event; import org.xutils.view.annotation.Event;
@ -110,6 +117,8 @@ public class InfoFragment extends BaseFragment {
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
initChart();
viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(InfoViewModel.class); viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(InfoViewModel.class);
viewModel.loadInfo(); viewModel.loadInfo();
@ -199,14 +208,14 @@ public class InfoFragment extends BaseFragment {
* @param myInfo 个人信息 * @param myInfo 个人信息
*/ */
private void loadInfo(MyInfo myInfo) { private void loadInfo(MyInfo myInfo) {
nickname.setText(getString(R.string.nick_name, myInfo.getUsername())); nickname.setText(getString(R.string.nick_name, myInfo.getId()));
creditScore.setText(getString(R.string.credit_score, myInfo.getCreditScore())); creditScore.setText(getString(R.string.credit_score, myInfo.getCreditScore()));
recentVisitors.setText(getString(R.string.recent_visitors, myInfo.getVisitors())); recentVisitors.setText(getString(R.string.recent_visitors, myInfo.getVisitors()));
recentPosts.setText(getString(R.string.recent_posts, myInfo.getPosts())); recentPosts.setText(getString(R.string.recent_posts, myInfo.getPosts()));
mobie.setText(myInfo.getMobie()); mobie.setText(myInfo.getMobie());
email.setText(myInfo.getEmail()); email.setText(myInfo.getEmail());
if (myInfo.getScoreHistory() != null) { if (myInfo.getScoreHistory() != null && myInfo.getScoreHistory().size() >= 2) {
loadChart(myInfo.getScoreHistory()); loadChart(myInfo.getScoreHistory());
} }
} }
@ -277,31 +286,121 @@ public class InfoFragment extends BaseFragment {
}).start(); }).start();
} }
/**
* 计算文本宽度
*/
private float computeTextWidth(String text, float textSize) {
Paint pain = new Paint();
pain.setAntiAlias(true);
pain.setTextSize(textSize);
Rect rect = new Rect();
pain.getTextBounds(text, 0, text.length(), rect);
return Utils.convertPixelsToDp(rect.left + rect.right);
}
//
private LineDataSet addChart(List<CreditScore> values) {
List<Entry> entryList = new ArrayList<>();
for (int i = 0; i < values.size(); i++) {
CreditScore score = values.get(i);
entryList.add(new Entry(i, score.getBeforeScore() + score.getScore()));
}
LineDataSet dataSet = new LineDataSet(entryList, "");
//线宽度
dataSet.setLineWidth(1.6f);
//不显示圆点
dataSet.setDrawCircles(false);
//线条平滑
// dataSet.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
dataSet.setValueTextSize(18);
dataSet.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
return String.valueOf((int) value);
}
});
return dataSet;
}
//设置图表格式
private void initChart() {
int textSize = 18;
//设置x轴数据格式
XAxis xAxis = lineChart.getXAxis();
//轴刻度最小值
xAxis.setAxisMinimum(1);
//轴刻度间隔最小值
xAxis.setGranularity(1);
//轴文本大小
xAxis.setTextSize(textSize);
//轴位置
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
//不显示网格线
xAxis.setDrawGridLines(false);
//设置Y轴数据格式
initYAxis(lineChart.getAxisLeft(), textSize);
initYAxis(lineChart.getAxisRight(), textSize);
//设置x轴0刻度和最后刻度的左右偏移,保证日期显示完整
float offset = computeTextWidth(getString(R.string.dateformat), textSize) + 3;
//设置轴左侧偏移
lineChart.setExtraLeftOffset(offset);
//设置轴右侧偏移
lineChart.setExtraRightOffset(offset);
// 设置底部偏移
lineChart.setExtraBottomOffset(20);
//隐藏描述信息
lineChart.getDescription().setEnabled(false);
//无数据时显示的文本
lineChart.setNoDataText(getString(R.string.no_score_history));
//图例:得到Lengend
Legend legend = lineChart.getLegend();
//隐藏Lengend
legend.setEnabled(false);
}
private void initYAxis(YAxis yAxis, int textSize) {
//轴细分粒度最小单位长度
yAxis.setAxisMinimum(1);
//轴间隔最小单位长度
yAxis.setGranularity(2);
//轴文本大小
yAxis.setTextSize(textSize);
//
yAxis.setXOffset(20);
}
//加载图表数据 //加载图表数据
private void loadChart(List<Integer> values) { private void loadChart(final List<CreditScore> values) {
LineData lineData; LineData lineData;
ILineDataSet lineDataSet;
if (lineChart.getData() == null) { if (lineChart.getData() == null) {
lineData = new LineData(); lineData = new LineData();
lineChart.setData(lineData); lineChart.setData(lineData);
List<Entry> entries = new ArrayList<>();
for (int i = 0; i < values.size(); i++) {
entries.add(new Entry(i, values.get(i)));
}
lineDataSet = new LineDataSet(entries, getString(R.string.active_history));
lineData.addDataSet(lineDataSet);
} else { } else {
lineData = lineChart.getData(); lineData = lineChart.getData();
lineDataSet = lineData.getDataSetByIndex(0); lineData.removeDataSet(0);
lineDataSet.clear();
for (int i = 0; i < values.size(); i++) {
lineDataSet.addEntry(new Entry(i, values.get(i)));
} }
lineData.addDataSet(addChart(values));
//设置x轴数据格式
XAxis xAxis = lineChart.getXAxis();
//格式化日期数据
xAxis.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
int index = (int) (value - 1);
return formatUnix(values.get(index).getTime());
} }
});
lineChart.getDescription().setEnabled(false); //设置标记
// lineChart.setMarker(new MyChartView(getContext(),R.layout.chart,values));
//刷新图表
lineChart.notifyDataSetChanged(); lineChart.notifyDataSetChanged();
lineChart.invalidate(); lineChart.invalidate();
} }

@ -0,0 +1,35 @@
package com.community.pocket.ui.main.ui.info;
import android.content.Context;
import android.widget.TextView;
import androidx.annotation.LayoutRes;
import com.community.pocket.R;
import com.community.pocket.data.model.CreditScore;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import java.util.List;
public class MyChartView extends MarkerView {
private TextView score;
private TextView notes;
private final List<CreditScore> creditScoreList;
public MyChartView(Context context, @LayoutRes int layoutResource, List<CreditScore> creditScoreList) {
super(context, layoutResource);
this.creditScoreList = creditScoreList;
score = findViewById(R.id.score);
notes = findViewById(R.id.notes);
}
@Override
public void refreshContent(Entry e, Highlight highlight) {
CreditScore creditScore = creditScoreList.get(((int) e.getX()) - 1);
score.setText(getContext().getString(R.string.change_score, creditScore.getScore()));
notes.setText(getContext().getString(R.string.score_notes, creditScore.getNotes()));
super.refreshContent(e, highlight);
}
}

@ -70,7 +70,7 @@ public abstract class VisitorMyVisitor extends BaseFragment {
gridLayout.removeAllViews(); gridLayout.removeAllViews();
for (final Visitor visitor : visitors) { for (final Visitor visitor : visitors) {
createTextView(visitor.getName()); createTextView(visitor.getName());
createTextView(DateFormat.format(getString(R.string.dateformat), visitor.getTime())); createTextView(formatUnix(visitor.getTime()));
createTextView(visitor.getNotes().length() <= maxLength ? visitor.getNotes() : visitor.getNotes().substring(0, maxLength)); createTextView(visitor.getNotes().length() <= maxLength ? visitor.getNotes() : visitor.getNotes().substring(0, maxLength));
switch (visitor.getStatus()) { switch (visitor.getStatus()) {

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/notes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="false"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/score" />
</androidx.constraintlayout.widget.ConstraintLayout>

@ -124,11 +124,14 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="@dimen/size_30" android:layout_marginTop="@dimen/size_30"
android:gravity="center" android:gravity="center"
android:text="@string/active_history" /> android:text="@string/active_history"
android:textSize="24sp"
android:textStyle="bold" />
<HorizontalScrollView <HorizontalScrollView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent"
android:layout_marginTop="@dimen/size_30">
<LinearLayout <LinearLayout
android:layout_width="@dimen/chart_height" android:layout_width="@dimen/chart_height"
@ -146,7 +149,7 @@
android:id="@+id/open_modify_password" android:id="@+id/open_modify_password"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="@dimen/size_8" android:layout_marginTop="@dimen/size_30"
android:text="@string/modify_password" /> android:text="@string/modify_password" />
<Button <Button

@ -217,4 +217,7 @@
<string name="forum_show_complain">complain:%1s</string> <string name="forum_show_complain">complain:%1s</string>
<string name="password_diff">新密码不能与原密码相同</string> <string name="password_diff">新密码不能与原密码相同</string>
<string name="dateformat_visitor">yyyy-MM-dd HH:mm</string> <string name="dateformat_visitor">yyyy-MM-dd HH:mm</string>
<string name="no_score_history">no data</string>
<string name="change_score">score%1d</string>
<string name="score_notes">notes:\n%1s</string>
</resources> </resources>

@ -217,4 +217,7 @@
<string name="forum_show_complain">投诉人:%1s</string> <string name="forum_show_complain">投诉人:%1s</string>
<string name="password_diff">新密码不能与原密码相同</string> <string name="password_diff">新密码不能与原密码相同</string>
<string name="dateformat_visitor">yyyy-MM-dd HH:mm</string> <string name="dateformat_visitor">yyyy-MM-dd HH:mm</string>
<string name="no_score_history">暂无历史信用分记录</string>
<string name="change_score">信用分%1d</string>
<string name="score_notes">备注信息:\n%1s</string>
</resources> </resources>

@ -218,6 +218,9 @@
<string name="forum_show_complain">complain:%1s</string> <string name="forum_show_complain">complain:%1s</string>
<string name="password_diff">新密码不能与原密码相同</string> <string name="password_diff">新密码不能与原密码相同</string>
<string name="dateformat_visitor">yyyy-MM-dd HH:mm</string> <string name="dateformat_visitor">yyyy-MM-dd HH:mm</string>
<string name="no_score_history">no data</string>
<string name="change_score">score%1d</string>
<string name="score_notes">notes:\n%1s</string>
<!-- Strings used for fragments for navigation --> <!-- Strings used for fragments for navigation -->
<!-- Strings used for fragments for navigation --> <!-- Strings used for fragments for navigation -->

@ -8,7 +8,7 @@ buildscript {
jcenter() jcenter()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:3.6.2' classpath 'com.android.tools.build:gradle:3.6.3'
classpath 'com.github.jokar:multi-languages.plugin:0.0.8' classpath 'com.github.jokar:multi-languages.plugin:0.0.8'
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong

Loading…
Cancel
Save