首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MPAndroidChart折线图小尺寸

MPAndroidChart折线图小尺寸
EN

Stack Overflow用户
提问于 2015-09-01 10:10:39
回答 4查看 5K关注 0票数 2

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

代码语言:javascript
复制
    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);
} 
EN

回答 4

Stack Overflow用户

发布于 2015-09-03 04:32:42

我也有同样的问题。已尝试以编程方式将LineChart的LayoutParams设置为match_parent,但不起作用。在我的示例中,我从布局xml中删除了LineChart视图,并将其替换为以下代码:

代码语言:javascript
复制
LineChart chart = new LineChart(context);
setContentView(chart);

在您的情况下,您应该删除:

代码语言:javascript
复制
myLayout = (RelativeLayout) findViewById(R.id.myLayout);

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

EDIT:如果您正在使用片段,则在按back时将收到一个错误。

片段的临时修复:

代码语言:javascript
复制
@Override
public void onStop() {
    super.onStop();
    getActivity().setContentView(R.layout.activity_main);
}
票数 2
EN

Stack Overflow用户

发布于 2015-09-01 12:42:41

你可以尝试很多方法:

在添加linearChart之前检查

  1. 是否可以设置Viewgroup.LayoutParams。在这里你可以设置linearChart的宽度和高度,这个方法应该调用setLayoutParams必须或者所有的Android视图都有它。

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

票数 0
EN

Stack Overflow用户

发布于 2015-09-23 06:14:58

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

代码语言:javascript
复制
mainLayouts.addView(mChart)

通过

代码语言:javascript
复制
setContentView(mChart);

一定要试一试。这对我很管用。祝你编码愉快!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32322511

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档