How can I make linechart values on every grid line - android

I'm using MPAndroidChart lib with v3.0.1.
And I have 7 values in every week day to show on LineChart with below image.
How can I set every value on xAxis grid line?
every labels values in IAxisValueFormatter are :
0.0 Thu
1.1666666 Fri
2.3333333 Sat
3.5 Sun
4.6666665 Mon
5.833333 Tue
6.9999995 Wed
my chart setting as below:
chart.setDrawGridBackground(true);
chart.setPinchZoom(false);
chart.setDescription(null);
chart.setTouchEnabled(false);
chart.getAxisRight().setEnabled(false);
leftAxis = chart.getAxisLeft();
leftAxis.setDrawGridLines(true);
leftAxis.setAxisMinimum(0);
xaxis = chart.getXAxis();
xaxis.setCenterAxisLabels(false);
xaxis.setDrawGridLines(true);
xaxis.setAxisMinimum(0);
xaxis.setPosition(XAxis.XAxisPosition.BOTTOM);
Legend legend = chart.getLegend();
legend.setEnabled(true);
legend.setForm(Legend.LegendForm.LINE);
every data set as below:
for (int i = 0; i <= days; i++) {
float usageTime = 0;
entries.add(new Entry(i, usageTime));
}
LineDataSet dataSet = new LineDataSet(entries, p.getName()); // add
// entries to dataset
dataSet.setDrawCircles(false);
dataSet.setDrawValues(false);
dataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
dataSet.setLineWidth(2);
dataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
dataSet.setColor(ColorTemplate.rgb(hexColor[colorPosition]));
colorPosition += 1;
dataSets.add(dataSet);
show data as below:
LineData lineData = new LineData(dataSets);
chart.setData(lineData);
chart.animateX(500);
chart.invalidate();
But if I use points more than grid lines, it's draw what I except.

Edit method public void renderGridLines(Canvas c) in XAxisRenderer.java. Put value you want into float[] positions.
position[i] is x-value, position[i+1] is y-value
Make positions is same data set array. Referring snippet drawing chart's value
LineData lineData = mChart.getLineData();
for (ILineDataSet set : lineData.getDataSets()) {
if (set.isVisible())
drawDataSet(c, set);
}

Related

MPAndroidChart - how to reduce the amount of entries to display

I am using MPAndroidChart to display Linecharts in my Android Application. Some of my Linecharts have a hugh amount of data which I add to a LineDataSet. When I display
the data on the screen, it becomes quite confusing for the user because the dots are so close to each other. So I am looking for a possiblity to display e.g. only every
fith data dot on the chart.
LineDataSet dataSet = new LineDataSet(entries, mViewModel.getMeterUnit().getValue());
dataSet.setColor(Color.RED);
dataSet.setDrawValues(false);
dataSet.setCircleColor(Color.BLACK);
dataSet.setCircleHoleColor(Color.BLACK);
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setValueFormatter(new LineBarMeasurementFragment.LineChartXAxisValueFormatter());
xAxis.setLabelCount(0);
for (LimitLine l1 : limitLines) {
xAxis.addLimitLine(l1);
}
MinMax mx = getXMinMax(measurementList) ;
xAxis.setAxisMaximum(mx.getxMax());
xAxis.setAxisMinimum(mx.getxMin());
chart.getAxisLeft().setAxisMinimum(getYAxisMin(measurementList));
chart.getAxisRight().setEnabled(false);
chart.getDescription().setText(mViewModel.getMeterIdentifier().getValue());
LineData lineData = new LineData(dataSet);
chart.setData(lineData);
// something like chart.displayOnly(5)
chart.invalidate();

Android MPChart Not Showing Any Dat

I'm trying to show a Horizontal Bar Graph in my Android app. I'm using MPChart library to do so.
However, I can't see graph being displayed. There is just the X axis and Y axis drawn but no actual graph.
Here's how I'm displaying it:
private void renderDataMPChart(ArrayList<String> nameList, ArrayList<Integer> countList){
ArrayList<BarEntry> values = new ArrayList<>();
for(int i = 0; i<nameList.size(); i++){
values.add(new BarEntry(countList.get(i), (float) i++));
}
BarDataSet barDataSet = new BarDataSet(values, type);
barDataSet.setBarBorderWidth(0.9f);
barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
BarData barData = new BarData(barDataSet);
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
IndexAxisValueFormatter formatter = new IndexAxisValueFormatter(nameList);
xAxis.setGranularity(1f);
xAxis.setValueFormatter(formatter);
chart.setData(barData);
chart.setFitBars(true);
chart.animateXY(5000, 5000);
chart.invalidate();
}
The nameList is the X Axis values and the countList contains the value to display against those name. Eg: Apple-5, Banana-10

MPAndroid Linechart with single data entry

I'm using MPAndroidChart-v2.1.6 and I'm facing the following problem.
When I have a single data entry, it starts displaying the value from 0 index at X axis. This is how it looks like.
I want it to aligned in center, when i have only single data entry, like this..
I have tried setMinimum() and setMaximum() property for that particular condition but nothing positive happened to me.when I have more than one entry, it works well.
Here is my code,
ArrayList<LineDataSet> lines = new ArrayList<>();
LineDataSet linedataset1 = new LineDataSet(group1, "Text1");
linedataset1.setDrawFilled(false);
linedataset1.setValueFormatter(new MyDataSetFormatter());
linedataset1.setFillAlpha(110);
linedataset1.setLineWidth(1f);
linedataset1.setColor(Color.rgb(67, 91, 153));
linedataset1.setCircleColor(Color.rgb(67, 91, 153));
LineDataSet linedataset2 = new LineDataSet(group2, "Text2");
linedataset2.setDrawFilled(false);
linedataset2.setValueFormatter(new MyDataSetFormatter());
linedataset2.setFillAlpha(110);
linedataset2.setLineWidth(1f);
linedataset2.setColor(Color.rgb(254, 252, 59));
linedataset2.setCircleColor(Color.rgb(254, 252, 59));
LineDataSet linedataset3 = new LineDataSet(group3, "Text3");
linedataset3.setDrawFilled(false);
linedataset3.setValueFormatter(new MyDataSetFormatter());
linedataset3.setFillAlpha(110);
linedataset3.setLineWidth(1f);
linedataset3.setColor(Color.rgb(68, 185, 102));
linedataset3.setCircleColor(Color.rgb(68, 185, 102));
LineDataSet linedataset4 = new LineDataSet(group4, "text4");
linedataset4.setDrawFilled(false);
linedataset4.setValueFormatter(new MyDataSetFormatter());
linedataset4.setFillAlpha(110);
linedataset4.setLineWidth(1f);
linedataset4.setColor(Color.rgb(145, 92, 96));
linedataset4.setCircleColor(Color.rgb(145, 92, 96));
lines.add(linedataset1);
lines.add(linedataset2);
lines.add(linedataset3);
lines.add(linedataset4);
leftYAxis = lineChart.getAxisLeft();
rightYAxis = lineChart.getAxisRight();
rightYAxis.setDrawLabels(false);
rightYAxis.setAxisMaxValue(105);
leftYAxis.setAxisMaxValue(105);
lineChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
lineChart.getXAxis().setLabelRotationAngle(-70);
lineChart.getXAxis().setDrawGridLines(false);
lineChart.getAxisLeft().setDrawGridLines(false);
lineChart.getAxisRight().setDrawGridLines(false);
lineChart.getAxisRight().setEnabled(false);
lineChart.setBackgroundColor(Color.WHITE);
lineChart.setDrawGridBackground(false);
lineChart.setData(new LineData(newLabels, lines));
lineChart.setPinchZoom(false);
lineChart.animateY(1000);
lineChart.setDescription(null);
XAxis xAxis = lineChart.getXAxis();
xAxis.setAvoidFirstLastClipping(true);
//lineChart.setBackgroundColor(Color.rgb(255, 255, 255));
lineChart.setTouchEnabled(false);
lineChart.invalidate();
Thanks in advance,
Declare line chart entries separately outside the class
private ArrayList<Entry> dataValuesDaily(){
ArrayList<Entry> dataVals = new ArrayList<>();
dataVals.add(new Entry(1,10));
dataVals.add(new Entry(11,2));
dataVals.add(new Entry(21,16));
dataVals.add(new Entry(30,4));
return dataVals;
}
And then in the class
//TODO:: LINE GRAPH
LineDataSet lineDataSet1 = new LineDataSet(dataValuesDaily(), "Data Set 1");
lineDataSet1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
ArrayList<ILineDataSet> dataSets = new ArrayList<>();
dataSets.add(lineDataSet1);
//customization
mLineGraph.setTouchEnabled(true);
mLineGraph.setDragEnabled(true);
mLineGraph.setScaleEnabled(false);
mLineGraph.setPinchZoom(false);
mLineGraph.setDrawGridBackground(false);
mLineGraph.setExtraLeftOffset(15);
mLineGraph.setExtraRightOffset(15);
//to hide background lines
mLineGraph.getXAxis().setDrawGridLines(false);
mLineGraph.getAxisLeft().setDrawGridLines(false);
mLineGraph.getAxisRight().setDrawGridLines(false);
//to hide right Y and top X border
YAxis rightYAxis = mLineGraph.getAxisRight();
rightYAxis.setEnabled(false);
XAxis topXAxis = mLineGraph.getXAxis();
topXAxis.setEnabled(false);
XAxis xAxis = mLineGraph.getXAxis();
xAxis.setEnabled(true);
xAxis.setDrawGridLines(false);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
lineDataSet1.setLineWidth(4f);
lineDataSet1.setCircleRadius(3f);
lineDataSet1.setDrawValues(false);
lineDataSet1.setCircleHoleColor(getResources().getColor(R.color.pie_color_4));
lineDataSet1.setCircleColor(getResources().getColor(R.color.pie_color_4));
LineData data = new LineData(dataSets);
mLineGraph.setData(data);
mLineGraph.animateX(2000);
mLineGraph.invalidate();
mLineGraph.getLegend().setEnabled(false);
mLineGraph.getDescription().setEnabled(false);
To set the X axis limits to specific values you can call setAxisMinimum and setAxisMaximum. For example:
XAxis xAxis = lineChart.getXAxis();
xAxis.setAxisMinimum(10);
xAxis.setAxisMaximum(30);
If you want to do this automatically using your LineDataSet you can query the min and max values of the X data
float xMin = dataSet.getXMin() - 5;
float xMax = dataSet.getXMax() + 5;
xAxis.setAxisMinimum(xMin);
xAxis.setAxisMaximum(xMax);
which will center the data even when there is only one point.

MPAndroidChart : Only alternate labels are shown in x axis when more entries comes

In my android application i have a horizontal bar chart using MPAndroidChart.My problem is there are 12 number of bars in my bar chart each represents month from April to March,but i can see only alternate months label in x-axis.If there are small number of bars then i can see all the labels in x-axis.I didn't set any label count for x-axis using
xAxix.setLabelCount()
method.Then why i can't see all the labels? If I zoomed then i can see labels for each bar.I am using MPAndroidChart v3.0.1.Attached is the screenshot of the above.See here I can see only 'Apr,Jun,Aug,Oct,Dec,Feb' and all the other months are not displayed.How can I see all the other months also.
Below is my code.
yVals1 = new ArrayList<BarEntry>();
xVals = new ArrayList<String>();
for (int i = 0; i < listChart.size(); i++){
BarEntry newBEntry = new BarEntry(i,listChart.get(i).getAmount());
xVals.add(listChart.get(i).getAltName());
yVals1.add(newBEntry);
}
BarDataSet set1;
set1 = new BarDataSet(yVals1, "");
set1.setColors(new int[] {Color.BLUE,Color.GREEN});
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
data.setValueTextSize(10f);
barChartIncExp.setData(data);
barChartIncExp.setDrawBarShadow(false);
barChartIncExp.setDrawValueAboveBar(true);
barChartIncExp.getDescription().setEnabled(false);
barChartIncExp.setMaxVisibleValueCount(60);
// scaling can now only be done on x- and y-axis separately
barChartIncExp.setPinchZoom(true);
barChartIncExp.setDrawGridBackground(false);
barChartIncExp.setHighlightFullBarEnabled(false);
barChartIncExp.setHighlightPerDragEnabled(false);
XAxis xAxis = barChartIncExp.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);
xAxis.setGranularity(1f);
xAxis.setValueFormatter(new IAxisValueFormatter() {
#Override
public String getFormattedValue(float value, AxisBase axis) {
if(Math.round(value) >= xVals.size()) {
return null;
} else {
return xVals.get(Math.round(value));
}
}
});
YAxis leftAxis = barChartIncExp.getAxisLeft();
leftAxis.setDrawGridLines(false);
leftAxis.setLabelCount(8, false);
leftAxis.setSpaceTop(15f);
leftAxis.setDrawLabels(false);
YAxis rightAxis = barChartIncExp.getAxisRight();
rightAxis.setLabelCount(8, false);
rightAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
Legend l = barChartIncExp.getLegend();
l.setEnabled(false);
barChartIncExp.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
#Override
public void onValueSelected(Entry entry, Highlight highlight) {
}
#Override
public void onNothingSelected() {
}
});
You can change the number of labels you want to show in the y-axis.
XAxis xAxis=lineChart.getXAxis();
xAxis.setLabelCount(4,true); //4 is the number of values to be shown.
Try This
// where 12 is the number of bars in your chart.
xAxis.setLabelCount(12)
XAxis xAxis = mChart.getXAxis();
xAxis.setLabelCount(label.size()+1, true);
Set the label count to the size of the label + 1 in the xAxis.setLabelCount function.
It worked for me!
XAxis xAxis=lineChart.getXAxis();
xAxis.setLabelCount(4,true);
xAxis.setGranularity(1f);

Android MPChart how to change text value of YAxis

I want to create a bar chart with YAxis contains floats values and xAxis contains String values,
My problem is that i want to change displayed data in YAxis and make it String, but the value still float
this is my chart
mChart = (BarChart) findViewById(R.id.chart1);
mChart.setDescription("");
// if more than 60 entries are displayed in the chart, no values will be
// scaling can now only be done on x- and y-axis separately
mChart.setPinchZoom(false);
mChart.setDrawBarShadow(false);
mChart.setDrawGridBackground(false);
XAxis xAxis = mChart.getXAxis();
xAxis.setPosition(XAxisPosition.BOTTOM);
xAxis.setSpaceBetweenLabels(0);
xAxis.setTextSize(10);
xAxis.setDrawGridLines(false);
mChart.getAxisRight().setDrawGridLines(false);
mChart.getAxisLeft().setDrawGridLines(false);
mChart.getAxisLeft().setDrawLabels(true);
mChart.getXAxis().setXOffset(10);
YAxis rightAxis = mChart.getAxisRight();
rightAxis.setDrawAxisLine(false);
rightAxis.setTextColor(Color.WHITE);
rightAxis.setDrawGridLines(false);
// add a nice and smooth animation
mChart.animateY(2500);
mChart.getLegend().setEnabled(true);
and this is how i call the bar data set
xVals.add(" ");
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
for (int i = 0; i < yvalues.size(); i++) {
BarEntry b=new BarEntry((int)Float.parseFloat(yvalues.get(i)), i);
//here is the BarEntry values but i cant change the displayed value its still
//showing float
yVals1.add(b);
}
BarDataSet set1 = new BarDataSet(yVals1, "duration min");
set1.setColor(Color.GREEN);
set1.setDrawValues(false);
set1.setLabel("duration min");
ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
dataSets.add(set1);
BarData data = new BarData(xVals, dataSets);
mChart.setData(data);
mChart.invalidate();
If you do not want to display the floating point value, try fetching the rounded value.
BarEntry b=new BarEntry(Math.round(yvalues.get(i)), i);

Categories

Resources