【问题标题】:MPAndroidChart line graph remove only top border androidMPAndroidChart 折线图仅删除顶部边框 android
【发布时间】:2017-12-06 05:10:12
【问题描述】:

我将 MPAndroidChart 用于折线图。我大概有5分。下面是我的代码。

 LineChart lineChart = (LineChart) findViewById(R.id.chart);
    lineChart.setDrawBorders(true);
    lineChart.getDescription().setEnabled(false);
    lineChart.fitScreen();
    lineChart.setPadding(0,0,0,0);
    lineChart.getLegend().setEnabled(false);
    lineChart.setDoubleTapToZoomEnabled(false);

    lineChart.getAxisLeft().setEnabled(false);
    lineChart.getAxisRight().setEnabled(true);
    lineChart.getAxisLeft().setStartAtZero(true);

    lineChart.getAxisRight().setDrawAxisLine(true);
    lineChart.getAxisRight().setDrawLabels(true);
    lineChart.getAxisRight().setDrawGridLines(false);

    lineChart.getXAxis().setEnabled(true);
    lineChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);

    lineChart.getXAxis().setDrawAxisLine(true);
    lineChart.getXAxis().setDrawGridLines(true);
    lineChart.setScaleMinima(3f, 0f);
    lineChart.setBackgroundColor(Color.TRANSPARENT); //set whatever color you prefer
    lineChart.setDrawGridBackground(false);
    lineChart.setTouchEnabled(true);
    lineChart.setDragEnabled(true);
    lineChart.setScaleEnabled(true);
    lineChart.setPinchZoom(false);
    Legend l = lineChart.getLegend();
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
    l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
    l.setDrawInside(true);
    ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();

    ArrayList<Entry> values = new ArrayList<Entry>();
    values.add(new Entry(Float.parseFloat("1"), 5));
    values.add(new Entry(Float.parseFloat("2"), 2));
    values.add(new Entry(Float.parseFloat("3"), 6));
    values.add(new Entry(Float.parseFloat("4"), 8));
    values.add(new Entry(Float.parseFloat("5"), 2));

    LineDataSet d = new LineDataSet(values, "Actual kWh ");
    d.setMode(LineDataSet.Mode.CUBIC_BEZIER);
    d.setLineWidth(2.5f);
    d.setCircleRadius(3f);
    d.setCircleColorHole(Color.BLACK);
    d.setValueTextSize(10f);
    d.setValueTextColor(Color.WHITE);
    dataSets.add(d);
    LineData data = new LineData(dataSets);
    lineChart.setData(data);
    lineChart.invalidate();

我能够完成大部分的定制。我的问题是如何只删除 topborder ?同样在 X 轴上,我得到像 1、1.2、1.3 这样的十进制值,它还在继续。我希望 X 轴具有 1、2、3、4、5 之类的值。我该怎么做?

【问题讨论】:

    标签: android mpandroidchart linegraph


    【解决方案1】:
     LineChart lineChart = (LineChart) findViewById(R.id.chart);
            lineChart.setDrawBorders(false);
            lineChart.getDescription().setEnabled(false);
            lineChart.fitScreen();
            lineChart.setPadding(0, 0, 0, 0);
            lineChart.getLegend().setEnabled(false);
            lineChart.setDoubleTapToZoomEnabled(false);
    
            lineChart.getAxisLeft().setEnabled(true);
            lineChart.getAxisRight().setEnabled(true);
            lineChart.getAxisLeft().setStartAtZero(false);
    
            lineChart.getAxisRight().setDrawAxisLine(true);
            lineChart.getAxisRight().setDrawLabels(true);
            lineChart.getAxisRight().setDrawGridLines(false);
    
            lineChart.getXAxis().setEnabled(true);
            lineChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
    
            XAxis xLabels = lineChart.getXAxis();
            xLabels.setGranularity(1f);
    
            lineChart.getXAxis().setDrawAxisLine(true);
            lineChart.getXAxis().setDrawGridLines(true);
            lineChart.setScaleMinima(3f, 0f);
            lineChart.setBackgroundColor(Color.TRANSPARENT); //set whatever color you prefer
            lineChart.setDrawGridBackground(false);
            lineChart.setTouchEnabled(false);
            lineChart.setDragEnabled(false);
            lineChart.setScaleEnabled(false);
            lineChart.setPinchZoom(false);
            Legend l = lineChart.getLegend();
            l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
            l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
            l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
            l.setDrawInside(true);
            ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
    
            ArrayList<Entry> values = new ArrayList<Entry>();
            values.add(new Entry(Float.parseFloat("1"), 5));
            values.add(new Entry(Float.parseFloat("2"), 2));
            values.add(new Entry(Float.parseFloat("3"), 6));
            values.add(new Entry(Float.parseFloat("4"), 8));
            values.add(new Entry(Float.parseFloat("5"), 2));
    
            LineDataSet d = new LineDataSet(values, "Actual kWh ");
            d.setMode(LineDataSet.Mode.CUBIC_BEZIER);
            d.setLineWidth(2.5f);
            d.setCircleRadius(3f);
            d.setCircleColorHole(Color.BLACK);
            d.setValueTextSize(10f);
            d.setValueTextColor(Color.WHITE);
            dataSets.add(d);
            LineData data = new LineData(dataSets);
            lineChart.setData(data);
            lineChart.invalidate();
    

    更新

     //to hide x-axis line 
     xLabels.setAxisLineColor(Color.TRANSPARENT);
    

    【讨论】:

    • 这对我有用。非常感谢。但是1和2之间的距离太远了。我怎样才能减少呢?我们也可以在折线图下方画线而不是填充颜色吗?
    • 我也想删除底部边框。我该怎么做?
    • @madhuri H R 添加这个 xLabels.setEnabled(false);
    • 我试过这个。但它也会删除垂直线和标签
    • @madhuriHR 检查我的更新答案,如果有帮助,请投票
    【解决方案2】:

    试试这个

    这个显示垂直线

    LineChart lineChart = (LineChart) findViewById(R.id.chart1);
        lineChart.setDrawBorders(false);
        lineChart.getDescription().setEnabled(false);
        lineChart.fitScreen();
        lineChart.setPadding(0,0,0,0);
        lineChart.getLegend().setEnabled(false);
        lineChart.setDoubleTapToZoomEnabled(false);
    
        lineChart.getAxisLeft().setEnabled(true);
        lineChart.getAxisRight().setEnabled(true);
        lineChart.getAxisLeft().setStartAtZero(false);
    
        lineChart.getAxisRight().setDrawAxisLine(false);
        lineChart.getAxisRight().setDrawLabels(true);
        lineChart.getAxisRight().setDrawGridLines(false);
    
        lineChart.getXAxis().setEnabled(true);
        lineChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        lineChart.getAxisLeft().setDrawGridLines(false);
        lineChart.getAxisRight().setDrawGridLines(false);
        lineChart.getXAxis().setDrawAxisLine(true);
        lineChart.getXAxis().setDrawGridLines(true);
        lineChart.setScaleMinima(3f, 0f);
        lineChart.setBackgroundColor(Color.TRANSPARENT); //set whatever color you prefer
        lineChart.setDrawGridBackground(false);
        lineChart.setTouchEnabled(false);
        lineChart.setDragEnabled(false);
        lineChart.setScaleEnabled(false);
        lineChart.setPinchZoom(false);
        Legend l = lineChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        l.setDrawInside(true);
        ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
    
        ArrayList<Entry> values = new ArrayList<Entry>();
        values.add(new Entry(Float.parseFloat("1"), 5));
        values.add(new Entry(Float.parseFloat("2"), 2));
        values.add(new Entry(Float.parseFloat("3"), 6));
        values.add(new Entry(Float.parseFloat("4"), 8));
        values.add(new Entry(Float.parseFloat("5"), 2));
    
        LineDataSet d = new LineDataSet(values, "Actual kWh ");
        d.setMode(LineDataSet.Mode.CUBIC_BEZIER);
        d.setLineWidth(2.5f);
        d.setCircleRadius(3f);
        d.setCircleColorHole(Color.BLACK);
        d.setValueTextSize(10f);
        d.setValueTextColor(Color.WHITE);
        dataSets.add(d);
        LineData data = new LineData(dataSets);
        lineChart.setData(data);
        lineChart.invalidate();
    

    如果你不需要任何网格线

    使用

    lineChart.getAxisLeft().setDrawGridLines(false);
            lineChart.getAxisRight().setDrawGridLines(false);
            lineChart.getXAxis().setDrawAxisLine(true);
            lineChart.getXAxis().setDrawGridLines(true);
    

    【讨论】:

    • 但这给出了水平线。
    • 所以你只需要垂直线好吗?
    • 我只需要左边框、右边框和下边框。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多