0

This is my first time trying to make a chart and I am so stuck. I tried everything I could find online but nothing works. I know I am doing something wrong but I don´t seem to figure out what. It creates the chart but without data.

This is my code:

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




}

I am trying to get the data out of the database:

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

Can someone help me I am getting a bit desperate with this.

1 Answer 1

2

The example project shows numerous use cases of how data can be added to the charts, you might wanna check it out.

Also, the documentation provides an extensive example on how to set data.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.