android - MPAndroidChart 折线图小尺寸

标签 android linechart mpandroidchart

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

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

最佳答案

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

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

根据您的情况,您应该删除:

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

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

编辑:如果您使用 fragment ,则按返回时会收到错误消息。

fragment 的临时修复:

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

关于android - MPAndroidChart 折线图小尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32322511/

相关文章:

android - 使用新数据调用invalidate()后,MP Android Chart消失

android - 如何从另一个 Activity 调用 Gridview Activity

Android关闭自定义对话框

javascript - 在混合图表中对齐线条和条形

java - 从轴中删除负值

charts - 使用 Vega 库创建停产折线图

android - Detox 至少 75% 的 View 区域显示给用户

android - 在 Android 上显示点击覆盖,例如基线网格

android - 如何在 MPAndroidChart 中持续更新 Y Axis 值

android - 在 MPAndroidChart 中实现具有可变切片大小的饼图