【问题标题】:MPAndroidChart Line chart small sizeMPAndroidChart 折线图小尺寸
【发布时间】:2015-09-01 02:10:38
【问题描述】:

这是我运行它时的家庭活动代码,它只是一个小尺寸的箱形图,所有内容都压缩在一个非常小的图表中。请帮助我如何让我的图表更大

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

【问题讨论】:

标签: android linechart mpandroidchart


【解决方案1】:

我遇到了同样的问题。尝试以编程方式将 LineChart 的 LayoutParams 设置为 match_parent,但它不起作用。就我而言,我从布局 xml 中删除了 LineChart 视图,并将其替换为以下代码:

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

【讨论】:

    【解决方案2】:

    你可以尝试很多事情:

    1. 在添加线性图表之前,请检查您是否可以设置 Viewgroup.LayoutParams。那里可以设置linearChart的宽高,这个方法必须调用setLayoutParams或者Android中的所有view都有。

    2. 设置好后添加linearChart,我的意思是放这行代码:> mChart = new LineChart(this);在 y12.setEnabled(false) 之后。

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

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,但我通过替换解决了它

      mainLayouts.addView(mChart)
      

      setContentView(mChart);
      

      试试吧。它对我有用。编码愉快!

      【讨论】:

        【解决方案4】:

        我也遇到了这个问题,我用

        替换 myLayout.addView(mChart); 解决了它
        myLayout.addView(mChart, new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.MATCH_PARENT));`
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-12-05
          • 1970-01-01
          相关资源
          最近更新 更多