Kharlo Ridado
Kharlo Ridado

Reputation: 21

MPAndroidChart Line chart small size

This is my home activity code when I run it, it's just a small size box chart all the contents are compressed in a ver small chart. Help me please on how to get my chart bigger

    package com.example.frendzy.iassist;

    import android.graphics.Color;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.support.v7.widget.Toolbar;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.RelativeLayout;

    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.LineData;

public class homeActivity extends ActionBarActivity {

private RelativeLayout myLayout;
private LineChart mChart;

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


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

    //create line chart
    mChart = new LineChart(this);
    //add to mylayout
    myLayout.addView(mChart);

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

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

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

    //we want also enable scaling and dragging
    mChart.setDragEnabled(true);
    mChart.setScaleEnabled(true);
    mChart.setDrawGridBackground(false);

    //enable pinch zoom to avoid scaling x and y axis separately
    mChart.setPinchZoom(true);

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

    //now we work on data
    LineData data=new LineData();
    data.setValueTextColor(Color.WHITE);

    //add 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 x1 = mChart.getXAxis();
    x1.setTextColor(Color.WHITE);
    x1.setDrawGridLines(false);
    x1.setAvoidFirstLastClipping(true);

    YAxis y1 = mChart.getAxisLeft();
    y1.setTextColor(Color.WHITE);
    y1.setAxisMaxValue(120f);
    y1.setDrawGridLines(true);

    YAxis y12 = mChart.getAxisRight();
    y12.setEnabled(false);
} 

Upvotes: 2

Views: 5706

Answers (4)

Michele
Michele

Reputation: 326

I also had this problem and I solved it replacing myLayout.addView(mChart); with

myLayout.addView(mChart, new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.MATCH_PARENT));`

Upvotes: 0

user3863537
user3863537

Reputation: 111

I had the same problem but I solved it by replacing

mainLayouts.addView(mChart)

by

setContentView(mChart);

Do try it. It worked for me. Happy coding!

Upvotes: 0

Danielius Ašmontas
Danielius Ašmontas

Reputation: 67

I had the same problem. Tried setting the LayoutParams of the LineChart to match_parent programatically but it did not work. In my case I removed the LineChart view from my layout xml and replaced it with the following code:

LineChart chart = new LineChart(context);
setContentView(chart);

In your case you should remove:

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

//add to mylayout
myLayout.addView(mChart);

EDIT: if you are using fragments you will receive an error while pressing back.

Temporary fix for fragments:

@Override
public void onStop() {
    super.onStop();
    getActivity().setContentView(R.layout.activity_main);
}

Upvotes: 2

Otejada92
Otejada92

Reputation: 26

There are many things that you could try:

  1. Before add the linearChart check if you could set the Viewgroup.LayoutParams. there you can set the width and height of the linearChart, the method should call setLayoutParams must or all the views in Android has it.

  2. Add the linearChart after setting it, I mean put this line of code :> mChart = new LineChart(this); after y12.setEnabled(false).

Sorry if you see that I'm guessing but I don't have a properly laptop right now for test your code :(

Upvotes: 0

Related Questions