使用MPChart库的折线图

时间:2018-05-10 08:45:31

标签: android

图表图片

enter image description here

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

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

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

我的竞争代码

ChartActivity扩展了活动{

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

0 个答案:

没有答案