我正在使用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。我该怎么做?
答案 0 :(得分:0)
试试这个
此显示垂直线
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);
答案 1 :(得分:0)
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);