MPAndroidChart vertical values - android

I want to delimit on vertical values between 13 and 14 not this is how it looks my linear chart, always starts on 0 as the starting limit, I want to be something around 13. Please see the image on the next link to see what I am talking about, I am attaching the piece of code for it enter image description here
LineChart lineChart = (LineChart) rootView.findViewById(R.id.chart);
LineData data = new LineData(labels, dataset);
dataset.setColors(ColorTemplate.COLORFUL_COLORS); //
dataset.setDrawCubic(true);
dataset.setDrawFilled(true);
lineChart.setData(data);
lineChart.animateY(5000);

Finally I got the answer about this, you can check it here
LineChart lineChart = (LineChart) rootView.findViewById(R.id.chart);
LineData data = new LineData(labels, dataset);
lineChart.getAxisLeft().setLabelCount(2, true);
lineChart.getAxisRight().setEnabled(false);
lineChart.getAxisLeft().setStartAtZero(false);
lineChart.setAutoScaleMinMaxEnabled(false);
lineChart.getAxisLeft().setAxisMaxValue(maxYval + 1);
lineChart.getAxisLeft().setAxisMinValue(minYval - 1);
dataset.setColors(ColorTemplate.COLORFUL_COLORS); //
dataset.setValueTextSize(10);
dataset.setDrawCubic(true);
dataset.setDrawFilled(true);
lineChart.setData(data);
lineChart.animateY(10);

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

MPAndroidChart - How to draw bar at bottom of line chart

I want to draw stock's history price chart with volume, similar to this:
With MPAndroidChart, I managed to get this result using CombinedChart:
Here's my code snippet:
CombinedChart mChart = (CombinedChart) findViewById(R.id.chart);
final ArrayList<BarEntry> barList = new ArrayList<>();
final ArrayList<Entry> lineList = new ArrayList<>();
// add Bar & Line data entries
.....
// bar depends on LEFT y-axis
BarDataSet barSet = new BarDataSet(barList, "");
barSet.setAxisDependency(YAxis.AxisDependency.LEFT);
BarData barData = new BarData(barSet);
// line depends on RIGHT y-axis
LineDataSet lineSet = new LineDataSet(lineList, "");
lineSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
LineData lineData = new LineData(lineSet);
// set data to chart
....
float barYMax = barData.getYMax();
float lineYMin = lineData.getYMin();
// to make bar appears at bottom
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setAxisMaximum(barYMax * 10);
// to make line appears at top
YAxis rightAxis = mChart.getAxisRight();
rightAxis.setAxisMinimum(lineYMin * 0.5f);
In order to make "Line Chart" appears above "Bar Chart":
Bar chart depends on Left y-axis. To make bar appears at bottom, I make it's scale larger, with leftAxis.setAxisMaximum(barYMax * 10).
Line chart depends on Right y-axis. To shift line up, I do rightAxis.setAxisMinimum(lineYMin * 0.5f).
I wonder is there a better recommended way to achieve this effect?
With my solution, line chart may still overlap with bar partially. Ideally I wish to achieve no overlapping at all, with line appears completely on top of bar.

MPAndroidChart bar chart issues in android

I am using MPAndroidChart.
How to change the BarEntryLabels in to left in HorizontalBarChart?vHow to remove that projects from its bottom?
How to disable zoom ?
While clicking on to particular bar colour is changing how to disable it?
IN BarChart there i have added 6 months but EntryLabels is not properly aligned to corresponding bar.
Code:
BARENTRY = new ArrayList<>();
BarEntryLabels = new ArrayList<String>();
AddValuesToBARENTRY();
AddValuesToBarEntryLabels();
Bardataset = new BarDataSet(BARENTRY, "Projects");
BARDATA = new BarData(BarEntryLabels, Bardataset);
Bardataset.setColors(new int[]{Color.parseColor("#701112")});
horizontalBarChart.setData(BARDATA);
horizontalBarChart.setDrawBarShadow(false);
horizontalBarChart.setDrawValueAboveBar(true);
// if more than 60 entries are displayed in the chart, no values will be
// drawn
horizontalBarChart.setMaxVisibleValueCount(60);
// scaling can now only be done on x- and y-axis separately
horizontalBarChart.setPinchZoom(false);
// draw shadows for each bar that show the maximum value
// mChart.setDrawBarShadow(true);
horizontalBarChart.setDrawGridBackground(false);
horizontalBarChart.setDescription("");
Bardataset.setBarSpacePercent(10f);
barChart.setData(BARDATA);
barChart.setDrawBarShadow(false);
barChart.setDrawValueAboveBar(true);
// if more than 60 entries are displayed in the chart, no values will be
// drawn
barChart.setMaxVisibleValueCount(60);
// scaling can now only be done on x- and y-axis separately
barChart.setPinchZoom(false);
// draw shadows for each bar that show the maximum value
// mChart.setDrawBarShadow(true);
barChart.setDrawGridBackground(false);
barChart.setDescription("");
For the "project" legend part try this
Legend legend = mBarChart.getLegend();
legend.setEnabled(false);
To make touch disable set setTouchEnabled(boolean enabled) on your bar chart object like this
mBarChart.setTouchEnabled(false);
and if you want to show all label you should try
axis.setLabelCount(...)
mBarChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(labels));
and also try official documents
BARENTRY1 = new ArrayList<>();
BarEntryLabels1 = new ArrayList<String>();
AddValuesToHorizontalBARENTRY();
AddValuesToHorizontalBarEntryLabels();
Bardataset = new BarDataSet(BARENTRY1, "Projects");
BARDATA = new BarData(BarEntryLabels1, Bardataset);
Bardataset.setColors(new int[]{Color.parseColor("#701112")});
horizontalBarChart.setData(BARDATA);
horizontalBarChart.setDrawBarShadow(false);
horizontalBarChart.setDrawValueAboveBar(true);
horizontalBarChart.setPinchZoom(false);
horizontalBarChart.setDrawGridBackground(false);
horizontalBarChart.setDescription("");
Bardataset.setBarSpacePercent(10f);
Legend legend = horizontalBarChart.getLegend();
legend.setEnabled(false);
horizontalBarChart.setTouchEnabled(false);
XAxis xAxis1 = horizontalBarChart.getXAxis();
xAxis1.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis1.setTextSize(8);
xAxis1.setSpaceBetweenLabels(8);
xAxis1.setTextColor(Color.parseColor("#701112"));
xAxis1.setTypeface(tf);
YAxis leftAxis = barChart.getAxisLeft();
leftAxis.setEnabled(false);
Do like this.The following issue will be fix.

Android MPAndroidCharts LineChart skipping values

I´m working with MPAndroid charts on my app to draw a chart from my sqlite database using "weight" values for the Y axes and "date" values for the X axes.
The problem is that just some rows of my database have that weight value. When all the rows have the weight layout my code works like a charm, but when there is a row that doesn´t have weight value, and the next one contains that weight value, it leaves a space on the YAxes of the chart, altought the date value is set on XAxes. As a result of that, a value is missed on Y Axes for every row without the weight value, because the value that comes after the weight row is moved one point forward. For example:
I have this code for setting up the chart:
public void setUpCharts(){
chart = (LineChart) rootView.findViewById(R.id.weightChart);
chart.setNoDataTextDescription("Nothing");
chart.setDrawBorders(false);
chart.setDescription("");
chart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
chart.getAxisRight().setEnabled(false);
chart.getAxisLeft().setEnabled(false);
chart.getAxisLeft().setDrawGridLines(false);
chart.getAxisRight().setDrawGridLines(false);
chart.getXAxis().setDrawGridLines(false);
chart.getXAxis().setDrawAxisLine(false);
chart.getLegend().setEnabled(false);
chart.setScaleEnabled(false);
chart.setPinchZoom(false);
chart.setDoubleTapToZoomEnabled(false);
weights = new ArrayList<>(); //Pesos
dates = new ArrayList<String>(); //Días del mes
getChartData();
LineDataSet dataSet = new LineDataSet(weights, "Weights");
dataSet.setDrawCubic(false);
dataSet.setCircleRadius(4);
dataSet.setLineWidth(3);
dataSet.setDrawValues(true);
dataSet.setColor(getContext().getResources().getColor(R.color.colorWeightChart));
dataSet.setCircleColor(getContext().getResources().getColor(R.color.colorWeightChart));
dataSet.setCircleColorHole(getContext().getResources().getColor(R.color.colorWeightChart));
LineData data = new LineData(dates, dataSet);
chart.setData(data);
//LimitLine
String weight_get = PreferenceManager.getDefaultSharedPreferences(getContext()).getString("Goal_weight", null);
if(!weight_get.equals(null) && !weight_get.equals("")){
float weight_goal = Float.valueOf(weight_get);
YAxis leftAxis = chart.getAxisLeft();
LimitLine ll = new LimitLine(weight_goal);
ll.setLineColor(getContext().getResources().getColor(R.color.weightTabFAB));
ll.setLineWidth(1f);
ll.enableDashedLine(10f, 10f, 0f);
leftAxis.addLimitLine(ll);
float YMax= dataSet.getYMax();
if(weight_goal > YMax){
float difference = weight_goal - YMax;
chart.getAxisRight().setAxisMaxValue(weight_goal + difference); //So the limit line will be always visible
chart.getAxisLeft().setAxisMaxValue(weight_goal + difference);
}
}
chart.setVisibleXRangeMaximum(5f);
}
And I use getChartData to set up all the chart data getting the values from my sqlite database. It´s supposed to add just the values of rows in which weight exists, and in fact the log shows that it works correctly, so I don´t know how can I fix it.
protected void getChartData(){
IDs = new ArrayList<Integer>();
Cursor data = dataSource.getAllItems();
if(data!=null) {
while(data.moveToNext()){
int id = data.getInt(data.getColumnIndex(RecordsDataSource.ColumnRecords.ID_RECORDS));
String weight= data.getString(data.getColumnIndex(RecordsDataSource.ColumnRecords.WEIGHTS_RECORDS));
if(weight!=null && !weight.equals("")){
long milisDate =data.getLong(data.getColumnIndex(RecordsDataSource.ColumnRecords.DATES_RECORDS));
String date = Utils.milisToDate(milisDate, "dd/MM");
IDs.add(id);
weights.add(new Entry(Float.valueOf(weight), id -1));
dates.add(date);
Log.i("ID "+ String.valueOf(id),"Not null, "+ weight);
}else{
Log.i("ID "+ String.valueOf(id), "Null");
}
}
data.close();
}
}
I have been all the day trying to solve this issue, but nothing has worked.
Could somebody help me please?
By the way, sorry for my bad English.
The problem is in your id in getChartData() method. The id is skipping your chart Entry position.
Instead of this
weights.add(new Entry(Float.valueOf(weight), -1));
Add a variable for entry index and increment it on each iteration
index++;
weights.add(new Entry(Float.valueOf(weight), index));
It should work.

How to hide legends and axis in MPAndroidChart?

Is their any possibility to hide all rounded items from this picture.
I have used the following code,
public void setDataList(List<HorizontalBarChartData> dataList, Resources resources) {
ArrayList<String> categories = new ArrayList<String>();
ArrayList<BarEntry> values = new ArrayList<BarEntry>();
ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
BarDataSet set1;
for (int i = 0; i < dataList.size(); i++) {
categories.add(dataList.get(i).getName());
values.add(new BarEntry(dataList.get(i).getValue(), i));
}
/*set1 = new BarDataSet(values, "Income, Expense, Disposable Income");*/
set1 = new BarDataSet(values, "Category 1, Category 2, Category 3");
set1.setBarSpacePercent(35f);
set1.setColors(new int[]{resources.getColor(R.color.cyan_blue), resources.getColor(R.color.vermilion_tint), resources.getColor(R.color.sea_green)});
dataSets.add(set1);
BarData data = new BarData(categories, dataSets);
data.setValueTextSize(10f);
horizontalBarChart.setData(data);
}
Update
How to hide rounded part from this image?
Yes, is possible, just using following code:
mChart.setDescription(""); // Hide the description
mChart.getAxisLeft().setDrawLabels(false);
mChart.getAxisRight().setDrawLabels(false);
mChart.getXAxis().setDrawLabels(false);
mChart.getLegend().setEnabled(false); // Hide the legend
As per this answer
mChart.getXAxis().setDrawLabels(false); will hide the entire X-Axis(as required for this question).
For positioning the X-Axis, following code works.
XAxis xAxis = mChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
Position can be set to
BOTTOM
BOTH_SIDED
BOTTOM_INSIDE
TOP
TOP_INSIDE
This helps if you are trying to hide only the particular side axis instead of hiding the entire axis.
It appears mChart.setDescription() no longer accepts a String.
The method now accepts an instance of the Description Class like this:
mChart.setDescription(Description description)
So to modify or delete the Chart Description you may do it like below
Description description = new Description();
description.setText("");
mChart.setDescription(description);
Following code work for all chart
Legend l = mchart.getLegend();
l.setEnabled(false);.
To hide description, use this
mChart.getDescription().setEnabled(false)
Below code works for PieChart. Try to get same method for your Chart.
Legend l = mChart.getLegend();
l.setPosition(LegendPosition.NONE);
chart=(LineChart) findViewById(R.id.Chart);
chart.getLegend().setEnabled(false); // for hiding square on below graph
Kotlin solution for MPAndroidChart:v3.1.0
chart.description.isEnabled = false // hide the description
chart.legend.isEnabled = false // hide the legend
chart.xAxis.setDrawLabels(false) // hide bottom label
chart.axisLeft.setDrawLabels(false) // hide left label
chart.axisRight.setDrawLabels(false) // hide right label
I had the problem, that the bottom xAxis Labels are cutted off when I used mChart.getLegend().setEnabled(false)
Now I use chart.getLegend().setForm(Legend.LegendForm.NONE); instead and the labels are not cutted of anymore

Categories

Resources