android - MP图表未在布局内显示:Android
问题描述
我正在尝试将 MP 折线图放入我的相对布局中。但问题是当我在手机或模拟器上运行它时没有内容(图表未显示)我试图解决这个问题但没有运气。请帮助我,我真的需要你的帮助。谢谢。
在这个应用程序中,我试图伪造一组点,以使图表看起来真实。
下面是我的主要活动,它是 xml
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.github.mikephil.charting.charts.LineChart;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
ChartHelper mChart;
LineChart chart;
@Override
public void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chart = findViewById(R.id.graph);
mChart = new ChartHelper(chart);
for (int i = 0; i < 10000; i++) {
for (int j = 0; j < 5; j++) {
TimeUnit.SECONDS.sleep(1);
mChart.addEntry(Float.valueOf(26));
}
for (int j = 0; j < 5; j++) {
TimeUnit.SECONDS.sleep(1);
mChart.addEntry(Float.valueOf(27));
}
TimeUnit.SECONDS.sleep(1);
mChart.addEntry(Float.valueOf(25));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
下面是我的xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layoutmain"
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/graph"
android:layout_width="600dp"
android:layout_height="300dp"
android:visibility="visible" />
</RelativeLayout>
下面是我的 ChartHelper 类
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;
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.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
public class ChartHelper implements OnChartValueSelectedListener {
private LineChart mChart;
public ChartHelper(LineChart chart) {
mChart = chart;
mChart.setOnChartValueSelectedListener(this);
// no description text
mChart.setNoDataText("You need to provide data for the chart.");
// enable touch gestures
mChart.setTouchEnabled(true);
// enable scaling and dragging
mChart.setDragEnabled(true);
mChart.setScaleEnabled(true);
mChart.setDrawGridBackground(false);
// if disabled, scaling can be done on x- and y-axis separately
mChart.setPinchZoom(true);
// set an alternative background color
mChart.setBackgroundColor(Color.WHITE);
mChart.setBorderColor(Color.rgb(67,164,34));
LineData data = new LineData();
data.setValueTextColor(Color.WHITE);
// add empty data
mChart.setData(data);
// get the legend (only possible after setting data)
Legend l = mChart.getLegend();
// modify the legend ...
// l.setPosition(LegendPosition.LEFT_OF_CHART);
l.setForm(Legend.LegendForm.LINE);
l.setTypeface(Typeface.MONOSPACE);
l.setTextColor(Color.rgb(67, 164, 34));
XAxis xl = mChart.getXAxis();
xl.setTypeface(Typeface.MONOSPACE);
xl.setTextColor(Color.rgb(67, 164, 34));
xl.setDrawGridLines(false);
xl.setAvoidFirstLastClipping(true);
xl.setEnabled(true);
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setTypeface(Typeface.MONOSPACE);
leftAxis.setTextColor(Color.rgb(67, 164, 34));
leftAxis.setDrawGridLines(true);
YAxis rightAxis = mChart.getAxisRight();
rightAxis.setEnabled(false);
}
public void setChart(LineChart chart){ this.mChart = chart; }
public void addEntry(float value) {
LineData data = mChart.getData();
if (data != null){
ILineDataSet set = data.getDataSetByIndex(0);
// set.addEntry(...); // can be called as well
if (set == null) {
set = createSet();
data.addDataSet(set);
}
data.addEntry(new Entry(set.getEntryCount(),value),0);
Log.w("anjing", set.getEntryForIndex(set.getEntryCount()-1).toString());
data.notifyDataChanged();
// let the chart know it's data has changed
mChart.notifyDataSetChanged();
// limit the number of visible entries
mChart.setVisibleXRangeMaximum(10);
// mChart.setVisibleYRange(30, AxisDependency.LEFT);
// move to the latest entry
mChart.moveViewTo(set.getEntryCount()-1, data.getYMax(), YAxis.AxisDependency.LEFT);
// this automatically refreshes the chart (calls invalidate())
// mChart.moveViewTo(data.getXValCount()-7, 55f,
// AxisDependency.LEFT);
}
}
private LineDataSet createSet() {
LineDataSet set = new LineDataSet(null, "Data");
set.setAxisDependency(YAxis.AxisDependency.LEFT);
set.setColor(Color.rgb(67, 164, 34));
//set.setCircleColor(Color.WHITE);
set.setLineWidth(2f);
//set.setCircleRadius(4f);
set.setFillAlpha(65);
set.setFillColor(Color.rgb(67, 164, 34));
set.setHighLightColor(Color.rgb(67, 164, 34));
set.setValueTextColor(Color.rgb(67, 164, 34));
set.setValueTextSize(9f);
set.setDrawValues(false);
return set;
}
@Override
public void onValueSelected(Entry e, Highlight h) {
Log.i("Entry selected", e.toString());
}
@Override
public void onNothingSelected(){
Log.i("Nothing selected", "Nothing selected.");
}
}
解决方案
查看您的代码后,我发现您Main Thread
通过调用TimeUnit.SECONDS.sleep(1);
. onCreate()
所以,我把阻塞调用放在一个 newThread
中,这样它就不会阻塞Main Thread
. 请查看我的以下解决方法,
主要活动
public class MainActivity extends AppCompatActivity {
ChartHelper mChart;
LineChart chart;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chart = findViewById(R.id.graph);
mChart = new ChartHelper(chart);
new Thread(new Runnable() {
@Override
public void run() {
try {
for (int i = 0; i < 10000; i++) {
for (int j = 0; j < 5; j++) {
TimeUnit.SECONDS.sleep(1);
mChart.addEntry(Float.valueOf(26));
}
for (int j = 0; j < 5; j++) {
TimeUnit.SECONDS.sleep(1);
mChart.addEntry(Float.valueOf(27));
}
TimeUnit.SECONDS.sleep(1);
mChart.addEntry(Float.valueOf(25));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
经过上述修改后,我发现您的代码可以正常工作并绘制图表。
推荐阅读
- ios - 我如何阅读 iOS 中 ptr_refs 给出的引用?
- class - 根据外部设置创建子类
- c# - 为大数字 C# 设置用户输入限制
- vsto - VSTO excel AddIn 自动更新
- reactjs - redux表单中的动态formName不起作用
- hdf5 - 无法使用 h5py.file() 创建 hdf5 文件
- ios - 在“配置默认 Firebase 应用”后,iOS 模拟器上的 Flutter 崩溃并出现错误
- android-recyclerview - 单击时如何更改recyclerview项目和子项目,并从片段中获取单击位置的数据?
- c# - 为什么 C# 编译器使用隐式运算符返回的值的父类型来调用重载运算符
- angular - 如何在 Kendo Angular multiselectBox 中实现选择和取消选择事件