Android - MPAndroidChart - LineChart没有显示行

时间:2016-08-10 16:42:20

标签: java android charts mpandroidchart

我的图表上有一个普拉姆。图表本身正在显示,但显示值的行没有显示。我从DB获得了值。我在下面添加了一些图片和我的活动代码:

我看到的第一件事 enter image description here

过了几秒钟...... enter image description here

我的代码:

public class SensorDataActivity extends AppCompatActivity {

RelativeLayout mainLayout;

SensorDataRequest_Temperature sensorDataRequest_temperature;
ArrayList<Entry> temperature_data_entry;
ArrayList<String> temperature_dates_entry;
XAxis xAxis;
YAxis leftAxis;
LineChart mChart;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sensor_data_activity_layout);

    mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);

    mChart =(LineChart) findViewById(R.id.mChart);

    if (mChart != null) {
        mChart.setNoDataTextDescription("press anywhere to refresh or wait");
    mChart.setHighlightPerTapEnabled(true);
    mChart.setTouchEnabled(false);
    mChart.setDrawGridBackground(false);
    mChart.setDrawBorders(false);
    mChart.setBackgroundColor(Color.WHITE);
    mChart.setHighlightPerTapEnabled(true);
    // disable the right axis's
    mChart.getAxisRight().setEnabled(false);
}

addResponseListener_temperature();
queueInit();
}

/*
    Response Listener for the Temperature table Request.
    used in case "Month" or "Week" was clicked in spinner.
*/
private void addResponseListener_temperature() {
    Response.Listener<String> responseListener = new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        Log.d("Response",response);
        JSONArray json;
        try {
            json = new JSONArray(response);
            Log.i("log_json",json.get(1).toString());

            temperature_data_entry = new ArrayList<>();
            temperature_dates_entry = new ArrayList<>();

            for (int i=0; i<json.length(); i++)
            {
                JSONArray tempJ = new JSONArray(json.get(i).toString());
                //Log.i("tempJ",tempJ.toString());

                // get the temperature values.
                temperature_data_entry.add(new Entry( Integer.valueOf(tempJ.getString(1)) ,i));
                // get the temperature timestamps.
                temperature_dates_entry.add(tempJ.getString(0));
            }

            xAxis = mChart.getXAxis();
            xAxis.setTextSize(12f);
            xAxis.setDrawGridLines(false);
            xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
            xAxis.setEnabled(true);
            xAxis.setAxisMinValue(0);
            xAxis.setValueFormatter(new AxisValueFormatter() {

                private SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                @Override
                public String getFormattedValue(float value, AxisBase axis) {
                    String timeStamp = "none";
                    try {
                        Date date = mFormat.parse(temperature_dates_entry.get((int)value));
                        timeStamp = mFormat.format(new Date(date.getTime()));

                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    return timeStamp;
                }

                @Override
                public int getDecimalDigits() {
                    return 0;
                }
            });

            leftAxis = mChart.getAxisLeft();
            leftAxis.setAxisMaxValue(60f);
            leftAxis.setTextSize(20f);
            leftAxis.setDrawAxisLine(false);
            leftAxis.setEnabled(true);

            LineDataSet set1 = new LineDataSet(temperature_data_entry,"temperature data");
            set1.setAxisDependency(YAxis.AxisDependency.LEFT);
            set1.setColor(Color.BLUE);
            set1.setDrawCircles(false);
            set1.setDrawValues(true);
            set1.setLineWidth(3f);
            set1.setValues(temperature_data_entry);
            set1.setValueTextColor(Color.RED);
            set1.setVisible(true);


            LineData data = new LineData(set1);
            mChart.setData(data); // set the data and list of lables into chart
            data.setValueTextColor(Color.RED);
            data.setDrawValues(true);

            mChart.invalidate();
            mChart.notifyDataSetChanged();

        }catch (JSONException je)
        {
            Log.e("JSONException",je.getMessage());
        }

    }
};
// initiating the SensorDataRequest object with the appropriate parameters.
sensorDataRequest_temperature = new SensorDataRequest_Temperature(
        //userDetails.get("raspberry_product_key"),
        TEMP_RASP_ID,
        responseListener);
}

/*
    initiates the RequestQueue object with the activity context and
    adds all the table's request's to the queue.
 */
private void queueInit() {
    // creating a RequestQueue object and providing it with the context of this activity.
    RequestQueue queue = Volley.newRequestQueue(SensorDataActivity.this);
    // adding our SensorDataRequest object to our RequestQueue object.
    queue.add(sensorDataRequest_temperature);

}


@Override
protected void onResume() {
    mChart.invalidate();
    mChart.notifyDataSetChanged();
    super.onResume();
}

2 个答案:

答案 0 :(得分:1)

根据您提供的代码判断,您使用的是MPAndroidChart库的最新3.0.0-beta1版本。那里引入了许多重大变化。

在您的情况下,可能的问题是您的参赛作品没有正确构建。条目构造函数已更改,现在为Entry(float x, float y),因此您需要先提供x值,然后再提供y值。将代码更改为此应该可以工作(只需切换值):

// Get the temperature values.
temperature_data_entry.add(new Entry(i, Integer.valueOf(tempJ.getString(1))));

答案 1 :(得分:0)

我认为问题是因为这段代码:

@Override
protected void onResume() {
mChart.invalidate();
mChart.notifyDataSetChanged();
super.onResume();
}

只有在更改/添加数据集后才能执行mChart.invalidate();。我有同样的问题,我在添加数据集之前使用了invalidate。所以我可以看到数据,但它从未填充过。当我在添加数据集后添加此行时,它完美地运行。