spookymodem
spookymodem

Reputation: 425

MPAndroidChart: Chart Not Displaying

I added a BarChart into my project XML (snippet of layout below):

<RelativeLayout
    android:id="@+id/relative_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

 <com.github.mikephil.charting.charts.BarChart
    android:id="@+id/chart"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<TextView
    android:id="@+id/stepsTitle_textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:textSize="28sp"
    android:layout_below="@+id/chart"
    android:text="STEPS TODAY"
    android:layout_marginTop="8dp" />

And I added data to chart in my onCreate() method:

    chart = (BarChart) findViewById(R.id.chart);
    chart.setLogEnabled(true);

    chart.setDrawBarShadow(false);
    chart.setDrawValueAboveBar(true);

    chart.setDescription("");

    // if more than 60 entries are displayed in the chart, no values will be
    // drawn
     chart.setMaxVisibleValueCount(60);

    List<String> usernamesArray = new ArrayList<String>();
    //ArrayList<Long> stepsArray = new ArrayList<Long>();
    List<BarEntry> entryArray = new ArrayList<BarEntry>();

    usernamesArray.add("a");
    usernamesArray.add("b");
    usernamesArray.add("c");

    BarEntry entry1 = new BarEntry(500, 0);
    BarEntry entry2 = new BarEntry(500, 1);
    BarEntry entry3 = new BarEntry(500, 2);
    entryArray.add(entry1);
    entryArray.add(entry2);
    entryArray.add(entry3);

    BarDataSet dataSet = new BarDataSet(entryArray, "Steps");
    dataSet.setColor(Color.rgb(104, 241, 175));
    BarData data = new BarData(usernamesArray, dataSet);
    //graphRow.addView(chart);
    //leaderboard_tableLayout.addView(graphRow);
    //chart = new BarChart(this);
    chart.setData(data);
    chart.fitScreen();
    chart.setBackgroundColor(Color.WHITE);
    chart.invalidate();

However, the chart object is not displaying; in fact, I do not see anything at all in the place where the chart should be.

Any thoughts on what I may be missing? I can certainly provide more details if necessary.

Thanks!

Upvotes: 2

Views: 4282

Answers (2)

Ajay B
Ajay B

Reputation: 786

You can use the following code to set the width according to the device wether it is mobile or a tablet. I have added only few parameters you can add widtth & height as well to make your code smarter.

Code-

 /* Start of setMyDevice() */
    public static void setMyDevice(int screen_density, XAxis xa) {

        Log.e("Logger:Utility", "setMyDevice() Called");
        // int screen_density = (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK);

        if (screen_density == Configuration.SCREENLAYOUT_SIZE_LARGE || screen_density == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
            //The device is a tablet or at least has the screen density of a tablet
            xa.setLabelRotationAngle(-35);
            xa.setDrawLabels(true);
            xa.setTextSize(14);
            Log.e("Logger:Utility", "setMyDevice-IT IS A TABLET");
        } else {
            xa.setLabelRotationAngle(-55);
            xa.setTextSize(10);
            Log.e("Logger:Utility", "setMyDevice-IT IS A MOBILE");
        }

    }
    /* End  of setMyDevice() */

Upvotes: 0

spookymodem
spookymodem

Reputation: 425

So it turns out that the width and height were being set to 0, so I changed them both from "match_parent" to an explicit setting in units of dp's (400dp and 200dp, respecitively), and that resolved the problem.

Upvotes: 2

Related Questions