MPAndroidChart折线图小尺寸

MPAndroidChart折线图小尺寸,android,linechart,mpandroidchart,Android,Linechart,Mpandroidchart,这是我运行时的家庭活动代码,它只是一个小尺寸的方框图,所有内容都压缩在一个小图表中。请帮我把图表放大 package com.example.frendzy.iassist; import android.graphics.Color; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.support.v7.widget.To

这是我运行时的家庭活动代码,它只是一个小尺寸的方框图,所有内容都压缩在一个小图表中。请帮我把图表放大

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

您可以尝试很多东西:

  • 添加linearChart之前,请检查是否可以设置Viewgroup.LayoutParams。您可以在那里设置linearChart的宽度和高度,该方法应该调用setLayoutParams-must,或者Android中的所有视图都有它

  • 设置后添加linearChart,我的意思是放置这行代码:>mChart=新的折线图(此);y12之后。setEnabled(错误)


  • 很抱歉,如果您看到我在猜测,但我现在没有合适的笔记本电脑来测试您的代码:(

    我也有同样的问题。尝试通过编程将线形图的LayoutParams设置为与父级匹配,但不起作用。在我的情况下,我从布局xml中删除了线形图视图,并将其替换为以下代码:

    LineChart chart = new LineChart(context);
    setContentView(chart);
    
    在您的情况下,您应该删除:

    myLayout = (RelativeLayout) findViewById(R.id.myLayout);
    
    //add to mylayout
    myLayout.addView(mChart);
    
    编辑:如果您使用的是片段,则按下后退键时将收到一个错误

    碎片的临时修复程序:

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

    我也有同样的问题,但我通过更换

    mainLayouts.addView(mChart)
    


    一定要试试。它对我很有效。编码快乐!

    我也有这个问题,我用替换myLayout.addView(mChart);解决了它

    myLayout.addView(mChart, new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.MATCH_PARENT));`
    
    看看这里,我已经发布了一个答案。
    myLayout.addView(mChart, new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.MATCH_PARENT));`