首页 > 解决方案 > 使用 MPChart 库的折线图

问题描述

图表图像

在此处输入图像描述

我在 android 中使用折线图,使用 MPChart 库可以正常工作。我得到的只有一个问题是 x 轴标签的边距。我想在 x 轴标签中从左侧添加一些边距(在附图中按箭头显示)。我尝试了很多东西,但没有得到任何解决方案。

        xAxis.setSpaceMax(20);
        xAxis.setSpaceMin(20);

我也尝试了这个空间最小值和最大值,但使用它没有任何效果。我怎样才能做到这一点?

我的完整代码

ChartActivity 扩展了 Activity {

private LineChart lineChart;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_realm_wiki);
    lineChart = (LineChart) findViewById(R.id.lineChart);
    setup(lineChart);

    // creating list of entry
    ArrayList<Entry> entries = new ArrayList<>();
    entries.add(new Entry(2015, 50));

    entries.add(new Entry(2016, 70));
    entries.add(new Entry(2017, 75));
    entries.add(new Entry(2018, 80));
    entries.add(new Entry(2019, 90));


    ArrayList<LineDataSet> lines = new ArrayList<LineDataSet>();
    LineDataSet lDataSet1 = new LineDataSet(entries, "DataSet1");
    lDataSet1.setColor(Color.BLACK);
    lDataSet1.setCircleColor(Color.BLACK);
    lines.add(lDataSet1);
    lineChart.getXAxis().setXOffset(1000);


    lineChart.setData(new LineData(lDataSet1));


  }


protected void setup(Chart<?> chart) {

    Typeface mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
    // no description text
    chart.getDescription().setEnabled(false);
    // enable touch gestures
    chart.setTouchEnabled(true);
    LineChart lineChart = (LineChart) chart;
    lineChart.setPinchZoom(false);
    lineChart.setOnTouchListener(null);


    if (chart instanceof BarLineChartBase) {
        BarLineChartBase mChart = (BarLineChartBase) chart;
        mChart.setDrawGridBackground(false);
        // enable scaling and dragging
        mChart.setDragEnabled(true);
        mChart.setScaleEnabled(true);
        // if disabled, scaling can be done on x- and y-axis separately
        mChart.setPinchZoom(false);
        mChart.setDoubleTapToZoomEnabled(false);

        YAxis leftAxis = mChart.getAxisLeft();
        leftAxis.removeAllLimitLines(); // reset all limit lines to avoid overlapping lines
        leftAxis.setTypeface(mTf);
        leftAxis.setTextSize(8f);
        leftAxis.setTextColor(Color.DKGRAY);
        leftAxis.setValueFormatter(new PercentFormatter());
        leftAxis.setLabelCount(8);
        leftAxis.setAxisMinValue(50);
        leftAxis.setAxisMaxValue(120);
        leftAxis.setGranularity(1f);
        leftAxis.setSpaceBottom(5);
        leftAxis.setSpaceTop(5);
        leftAxis.setDrawGridLines(false);

        XAxis xAxis = mChart.getXAxis();
        xAxis.setTypeface(mTf);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setTextSize(8f);
        xAxis.setTextColor(Color.DKGRAY);
        xAxis.setDrawGridLines(false);
        xAxis.setLabelCount(4, true);
        xAxis.setAxisMinValue(2015);
        xAxis.setAxisMaxValue(2018);
        mChart.getAxisRight().setEnabled(false);

    }
}

protected void styleData(ChartData data) {
    data.setValueTextSize(8f);
    data.setValueTextColor(Color.DKGRAY);
    data.setValueFormatter(new PercentFormatter());
}

标签: android

解决方案


推荐阅读