【问题标题】:MP Chart not displaying inside Layout :AndroidMP图表未在布局内显示:Android
【发布时间】:2019-09-24 12:51:08
【问题描述】:

我正在尝试将 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.");
    }

}

【问题讨论】:

    标签: android charts linechart


    【解决方案1】:

    查看您的代码后,我发现您通过在onCreate() 中调用TimeUnit.SECONDS.sleep(1); 来阻止Main Thread。所以,我将阻塞调用放在一个新的Thread 中,这样它就不会阻塞Main Thread。请查看我的以下解决方法,

    MainActivity

    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();
    
        }
    
    }
    

    经过上述修改后,我发现您的代码可以正常工作并绘制图表。

    【讨论】:

    【解决方案2】:

    您的 for 循环内部存在问题。 试试这个是否有效。

     for (int i = 0; i < 10; i++) {
                mChart.addEntry(i);
            }
    

    【讨论】:

    • 手机是空白的,我觉得是ui有问题
    • 我使用的 API 级别也是 28,而我使用的 MP Chart 版本是 com.github.PhilJay:MPAndroidChart:v3.1.0
    • 你有没有试过我的代码,因为你的代码导致了ui阻塞,而MP聊天版本很好。
    • 是的。仍然是相同的空白屏幕。
    【解决方案3】:

    你不见了

    chart.invalidate()

    ,并删除 TimeUnit.SECONDS.sleep(1) 阻塞主 UI 线程的原因尝试

      try {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            chart = findViewById(R.id.graph);
            mChart = new ChartHelper(chart);
    
            for (int i = 0; i < 10; i++) {
                for (int j = 0; j < 5; j++) {
                    mChart.addEntry(Float.valueOf(26));
                }
                for (int j = 0; j < 5; j++) {
                    mChart.addEntry(Float.valueOf(27));
                }
                mChart.addEntry(Float.valueOf(25));
            }
            chart.invalidate();
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    

    【讨论】:

    • 是UI相关的问题
    • 它是否会抛出任何异常,因为上面的代码在我这边工作
    • 我是否应该在 pom.xml 文件中包含依赖项。我看到了一个需求,但找不到那个文件,因为项目不是 maven
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    相关资源
    最近更新 更多