Reputation: 719
I am using MPAndroidChart for line graph. I have some 5 points. Below is my code.
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();
I am able to do most of the customization. My question is how to remove only the topborder ? Also in the X Axis I am getting decimal values like 1, 1.2, 1.3 and it goes on. I want the X Axis to have values like 1, 2, 3, 4 , 5. How can I do this ?
Upvotes: 0
Views: 2797
Reputation: 1646
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();
Update
//to hide x-axis line
xLabels.setAxisLineColor(Color.TRANSPARENT);
Upvotes: 1
Reputation: 1378
Try this
This Display Vertical lines
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();
If You don't need any grid lines
use
lineChart.getAxisLeft().setDrawGridLines(false);
lineChart.getAxisRight().setDrawGridLines(false);
lineChart.getXAxis().setDrawAxisLine(true);
lineChart.getXAxis().setDrawGridLines(true);
Upvotes: 0