如何为MPAndroidChart添加数据

时间:2015-06-01 11:17:12

标签: java android database charts mpandroidchart

这是我第一次尝试制作图表而且我很困难。我尝试了我能在网上找到的所有东西,但没有任我知道我做错了什么,但我似乎不知道是什么。它创建了图表但没有数据。

这是我的代码:

package com.danynuria.fmp;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.RelativeLayout;
import android.widget.Toast;

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.utils.ColorTemplate;

import java.security.KeyStore;
import java.util.ArrayList;

/**
 * Created by Dany&Nuria on 28/05/2015.
 */
public class PersonalInfo extends Activity {

private RelativeLayout mainLayout;
private LineChart mChart;
ArrayList<Entry> valsCO2 = new ArrayList<Entry>();
OverallDatabaseOperations myODB = new OverallDatabaseOperations(this);
int userId;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.personal_info_activity);
    userId = getIntent().getIntExtra("USER_ID", 0);

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

    // Create line chart
    mChart = new LineChart(this);

    //Add it to layout
    mainLayout.addView(mChart);

    //customize line chart
    mChart.setDescription("");
    mChart.setNoDataTextDescription("No data for the moment");

    //enable value highlighting
    mChart.setHighlightEnabled(true);

    //enable toutch gestures
    mChart.setTouchEnabled(true);

    //enable scaling and draging
    mChart.setDragEnabled(true);
    mChart.setScaleEnabled(true);
    mChart.setDrawGridBackground(false);

    //enable pinch zooming
    mChart.setPinchZoom(true);

    //alternative background colour
    mChart.setBackgroundColor(Color.LTGRAY);

    //Adding Data
    LineData data = new LineData();
    data.setValueTextColor(Color.WHITE);

    //adding data to line chart
    mChart.setData(data);

    // get legend object
    Legend l =mChart.getLegend();

    //customize legend
    l.setForm(Legend.LegendForm.LINE);
    l.setTextColor(Color.WHITE);

    XAxis xl = mChart.getXAxis();
    xl.setTextColor(Color.WHITE);
    xl.setDrawGridLines(false);
    xl.setAvoidFirstLastClipping(true);

    YAxis yl = mChart.getAxisLeft();
    yl.setTextColor(Color.WHITE);
    yl.setAxisMaxValue(1000f);
    yl.setDrawGridLines(true);


    YAxis yl2 = mChart.getAxisRight();
    yl2.setEnabled(false);




}


private void addEntry(){
    LineData data =mChart.getData();

    if(data != null){
        LineDataSet set = new LineDataSet(getItems(),"CO2");

        if(set==null){

            set = createSet();
            data.addDataSet(set);
        }


        //notify chart data has changed
        mChart.notifyDataSetChanged();

        //limit the number of visible entries
        mChart.setVisibleXRange(6);

        // scroll to the last entry
        mChart.moveViewToX(data.getXValCount() - 7);


    }
}

//method to create set
private LineDataSet createSet(){

    LineDataSet set = new LineDataSet(null,"gCO2 saved");
    set.setDrawCubic(true);
    set.setCubicIntensity(0.2f);
    set.setAxisDependency(YAxis.AxisDependency.LEFT);
    set.setColor(ColorTemplate.getHoloBlue());
    set.setCircleColor(ColorTemplate.getHoloBlue());
    set.setLineWidth(2f);
    set.setCircleSize(4f);
    set.setFillAlpha(65);
    set.setFillColor(ColorTemplate.getHoloBlue());
    set.setHighLightColor(Color.rgb(244, 117, 177));
    set.setValueTextColor(Color.WHITE);
    set.setValueTextSize(10f);


    return set;
}


public ArrayList<Entry> getItems(){

    ArrayList<Entry> list = new ArrayList<>();

    Cursor CR = myODB.getSpecificCO2(myODB);
    CR.moveToFirst();
    int i=0;
    while (!CR.isAfterLast()) {
        if(userId == CR.getInt(0)) {
            Toast.makeText(this,getString(CR.getInt(1)),Toast.LENGTH_SHORT).show();
            Entry c1 = new Entry (CR.getInt(1),i);
            list.add(c1);
            i=i+1;
            CR.moveToNext();
        }
    }


    return list;
}




}

我正在尝试从数据库中获取数据:

 public Cursor getSpecificCO2 (OverallDatabaseOperations odb){

    SQLiteDatabase OSQ = odb.getReadableDatabase();
    String[] cloums = {OverallTableinfo.USER_ID,OverallTableinfo.CO2_SAVED};
    Cursor OCR =    OSQ.query(OverallTableinfo.OVERALL_TABLE_NAME,cloums,null,null,null,null,null);
    return OCR;
    }

有人可以帮助我,我对此有点绝望。

1 个答案:

答案 0 :(得分:2)

example project显示了如何将数据添加到图表中的大量用例,您可能需要查看它。

此外,该文档提供了广泛的example on how to set data