I've been reading through the documentation for MPAndroidChart (using v3.0.1) and I can't seem to find a way to remove the labels on the bars
I would like to hide the values that are shown on top of each bar, OR change the values of them to something I can set manually. Either option would work well for my implementation.
I'm not sure if it is even possible to do either. I'm very new to android development any help would be great.
my code is:
public class MainActivity extends AppCompatActivity {
protected BarChart chart;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chart = (BarChart) findViewById(R.id.chart1);
Description desc ;
Legend L;
L = chart.getLegend();
desc = chart.getDescription();
desc.setText(""); // this is the weirdest way to clear something!!
L.setEnabled(false);
YAxis leftAxis = chart.getAxisLeft();
YAxis rightAxis = chart.getAxisRight();
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxisPosition.BOTTOM);
xAxis.setTextSize(10f);
xAxis.setDrawAxisLine(true);
xAxis.setDrawGridLines(false);
leftAxis.setTextSize(10f);
leftAxis.setDrawLabels(false);
leftAxis.setDrawAxisLine(true);
leftAxis.setDrawGridLines(false);
rightAxis.setDrawAxisLine(false);
rightAxis.setDrawGridLines(false);
rightAxis.setDrawLabels(false);
BarData data = new BarData( setData());
data.setBarWidth(0.9f); // set custom bar width
chart.setData(data);
chart.setFitBars(true); // make the x-axis fit exactly all bars
chart.invalidate(); // refresh
chart.setScaleEnabled(false);
chart.setDoubleTapToZoomEnabled(false);
chart.setBackgroundColor(Color.rgb(255, 255, 255));
chart.animateXY(2000, 2000);
chart.setDrawBorders(false);
chart.setDescription(desc);
chart.setDrawValueAboveBar(true);
}
private BarDataSet setData() {
ArrayList<BarEntry> entries = new ArrayList<>();
entries.add(new BarEntry(0f, 30f));
entries.add(new BarEntry(1f, 80f));
entries.add(new BarEntry(2f, 60f));
entries.add(new BarEntry(3f, 50f));
entries.add(new BarEntry(4f, 70f));
entries.add(new BarEntry(5f, 60f));
BarDataSet set = new BarDataSet(entries, "");
set.setColor(Color.rgb(155, 155, 155));
set.setValueTextColor(Color.rgb(155,155,155));
return set;
}
}
Has anyone had any experience with this?
Github link for the library is here: https://github.com/PhilJay/MPAndroidChart
Simply create a new class and let it implement the IValueFormatter and return whatever you want to be displayed from the getFormattedValue(...) method like below.
public class MyValueFormatter implements IValueFormatter
{
#Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler)
{
return "";
}
}
BarDataSet set = new BarDataSet(entries, "");
set.setValueFormatter(new MyValueFormatter());
set.setColor(Color.rgb(155, 155, 155));
set.setValueTextColor(Color.rgb(155, 155, 155));
You can find out more here.
For others coming through here looking for solutions, try setDrawValues on the data set instead.
BarDataSet dataSet = new BarDataSet(entries, "");
dataSet.setDrawValues(false);
Related
In my application, I need to display bar graphs. For that I am using MPAndroidChart library. Displayed the graph very well by using the following code.
This is the line added in gradle file
implementation 'com.github.PhilJay:MPAndroidChart:v2.2.4'
This is the activity
public class MainActivity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
BarChart barChart = (BarChart) findViewById(R.id.barchart);
ArrayList<BarEntry> entries = new ArrayList<>();
entries.add(new BarEntry(38f, 0));
entries.add(new BarEntry(52f, 1));
entries.add(new BarEntry(65f, 2));
entries.add(new BarEntry(30f, 3));
entries.add(new BarEntry(85f, 4));
entries.add(new BarEntry(19f, 5));
entries.add(new BarEntry(75f, 6));
BarDataSet bardataset = new BarDataSet(entries, " ");
ArrayList<String> labels = new ArrayList<String>();
labels.add("Mon");
labels.add("Tue");
labels.add("Wed");
labels.add("Thus");
labels.add("Fri");
labels.add("Sat");
labels.add("Sun");
BarData data = new BarData(labels, bardataset);
barChart.setData(data); // set the data and list of lables into chart
bardataset.setValueFormatter(new MyValueFormatter());
bardataset.setColor(Color.rgb(102, 178, 255));
}
public class MyValueFormatter implements ValueFormatter {
#Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
return "";
}
}
}
I am getting the bellow out put.
But in my application, I need to display Y-Axis values are like this 5, 10, 15, 20, 25, 30 ,35 instaed of 10, 20, 30, 40, 50 and X-axis values (Mon, Tue, Wed, ....) should display bottom of the screen.
How to do these two changes.
Thanks... In advance
For displaying X Axis values in bottom.
barChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
So I am looking for a solution still on how to get any text on the left side of the bar chart.
The libary I am using is the MpAndroidChart.
XML:
<com.github.mikephil.charting.charts.HorizontalBarChart
android:id="#+id/barchart"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.github.mikephil.charting.charts.HorizontalBarChart>
Java:
BarDataSet barDataSet = new BarDataSet(data(),null);
barDataSet.setColor(getResources().getColor(bars));
BarData barData = new BarData( barDataSet);
YAxis yAxisRight = barChart.getAxisRight();
yAxisRight.setEnabled(false);
barChart.getLegend().setEnabled(false);
barChart.animateY(1000);
barChart.invalidate();
barChart.getDescription().setEnabled(false);
XAxis xAxis = barChart.getXAxis();
xAxis.setEnabled(false);
barChart.setData(barData);
private ArrayList<BarEntry> data(){
ArrayList<BarEntry> entries = new ArrayList<>();
entries.add(new BarEntry(7,7));
entries.add(new BarEntry(6,6));
entries.add(new BarEntry(5,5));
entries.add(new BarEntry(4,4));
entries.add(new BarEntry(3,3));
entries.add(new BarEntry(2,2));
entries.add(new BarEntry(1,1));
return entries;
}
Right now it looks like this:
but I want on the left of every bar a label or text.
thanks ;)
For anyone wondering the answer is actually pretty simple:
XAxis xAxis = barChart.getXAxis();
//xAxis.setEnabled(false);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
final String[] week = {"S","S","F","T","W","T","M"};
IndexAxisValueFormatter formatter = new IndexAxisValueFormatter(week);
xAxis.setGranularity(1f);
xAxis.setValueFormatter(formatter);
I'm using latest version of MPAndroidChart. I'm trying to generate a grouped bar chart, following the example on this link, but without success. The problem is the xAxis. Altought I set fromX = 0 , bars don't begin fit to left axis. It has an space between the left vertical line and the beginig of the first bar.
Here is my code:
BarChart barChart = (BarChart) view.findViewById(R.id.chartDesgFam);
ArrayList<BarEntry> entries = new ArrayList<>();
entries.add(new BarEntry(0,4f));
entries.add(new BarEntry(1,8f));
entries.add(new BarEntry(2,6f));
entries.add(new BarEntry(3,12f));
entries.add(new BarEntry(4,18f));
entries.add(new BarEntry(5,9f));
ArrayList<BarEntry> entries2 = new ArrayList<>();
entries2.add(new BarEntry(0,5f));
entries2.add(new BarEntry(1,6f));
entries2.add(new BarEntry(2,12f));
entries2.add(new BarEntry(3,5f));
entries2.add(new BarEntry(4,14f));
entries2.add(new BarEntry(5,3f));
BarDataSet dataset = new BarDataSet(entries, "Serie 1");
dataset.setColor(Color.rgb(0, 0, 200));
BarDataSet dataset2 = new BarDataSet(entries2, "Serie 2");
dataset2.setColor(Color.rgb(200, 0, 0));
ArrayList<String> labels = new ArrayList<String>();
labels.add("January");
labels.add("February");
labels.add("March");
labels.add("April");
labels.add("May");
labels.add("June");
XAxis xAxis = barChart.getXAxis();
xAxis.setSpaceMin(0f);
xAxis.setDrawGridLines(false);
xAxis.setDrawLabels(true);
xAxis.setCenterAxisLabels(true);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setLabelCount(labels.size());
xAxis.setValueFormatter(new IAxisValueFormatter() {
#Override
public String getFormattedValue(float value, AxisBase axis) {
if (value>=0) {
if (value < labels.size() ) return labels.get((int) value);
else return "";
}
return "";
}
});
ArrayList<IBarDataSet> dataSets = new ArrayList<>();
dataSets.add(dataset);
dataSets.add(dataset2);
BarData data = new BarData(dataSets);
float groupSpace = 0.06f;
float barSpace = 0.02f; // x2 dataset
float barWidth = 0.45f; // x2 dataset
data.setBarWidth(barWidth);
barChart.setData(data);
barChart.groupBars(0, groupSpace, barSpace);
barChart.setFitBars(true);
barChart.invalidate();
In order to adjust the first bar to the left side, I need to put -0.2f in fromX parameter. I think It isn't for that, but I don't know why It doesn't begin from the left side. This is the result:
Please follow code below to get it fixed:
chart.getXAxis().setAxisMinimum(0);
chart.getXAxis().setAxisMaximum(0 + chart.getBarData().getGroupWidth(groupSpace, barSpace) * groupCount);
where groupCount is total number of entries you are having in your dataset. You can get that as:
data.getDataSetCount();
Chart Image
i am using line chart in android its working fine with using MPChart library. i am getting only one issue is that margin of x axis labels. i want to add some margin from left in x Axis labels (Show in attached image by arrow). I tried many things but not getting any solution.
xAxis.setSpaceMax(20);
xAxis.setSpaceMin(20);
i also try this space min and max but not getting any effect using this. How can i achieve this?
my compelete code
ChartActivity extends Activity {
private LineChart lineChart;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_realm_wiki);
lineChart = (LineChart) findViewById(R.id.lineChart);
setup(lineChart);
// creating list of entry
ArrayList<Entry> entries = new ArrayList<>();
entries.add(new Entry(2015, 50));
entries.add(new Entry(2016, 70));
entries.add(new Entry(2017, 75));
entries.add(new Entry(2018, 80));
entries.add(new Entry(2019, 90));
ArrayList<LineDataSet> lines = new ArrayList<LineDataSet>();
LineDataSet lDataSet1 = new LineDataSet(entries, "DataSet1");
lDataSet1.setColor(Color.BLACK);
lDataSet1.setCircleColor(Color.BLACK);
lines.add(lDataSet1);
lineChart.getXAxis().setXOffset(1000);
lineChart.setData(new LineData(lDataSet1));
}
protected void setup(Chart<?> chart) {
Typeface mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
// no description text
chart.getDescription().setEnabled(false);
// enable touch gestures
chart.setTouchEnabled(true);
LineChart lineChart = (LineChart) chart;
lineChart.setPinchZoom(false);
lineChart.setOnTouchListener(null);
if (chart instanceof BarLineChartBase) {
BarLineChartBase mChart = (BarLineChartBase) chart;
mChart.setDrawGridBackground(false);
// enable scaling and dragging
mChart.setDragEnabled(true);
mChart.setScaleEnabled(true);
// if disabled, scaling can be done on x- and y-axis separately
mChart.setPinchZoom(false);
mChart.setDoubleTapToZoomEnabled(false);
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.removeAllLimitLines(); // reset all limit lines to avoid overlapping lines
leftAxis.setTypeface(mTf);
leftAxis.setTextSize(8f);
leftAxis.setTextColor(Color.DKGRAY);
leftAxis.setValueFormatter(new PercentFormatter());
leftAxis.setLabelCount(8);
leftAxis.setAxisMinValue(50);
leftAxis.setAxisMaxValue(120);
leftAxis.setGranularity(1f);
leftAxis.setSpaceBottom(5);
leftAxis.setSpaceTop(5);
leftAxis.setDrawGridLines(false);
XAxis xAxis = mChart.getXAxis();
xAxis.setTypeface(mTf);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTextSize(8f);
xAxis.setTextColor(Color.DKGRAY);
xAxis.setDrawGridLines(false);
xAxis.setLabelCount(4, true);
xAxis.setAxisMinValue(2015);
xAxis.setAxisMaxValue(2018);
mChart.getAxisRight().setEnabled(false);
}
}
protected void styleData(ChartData data) {
data.setValueTextSize(8f);
data.setValueTextColor(Color.DKGRAY);
data.setValueFormatter(new PercentFormatter());
}
I am working on MPChart graph library where I am showing bar graph, with following code I am displaying a graph, but I want titles like: "Jan", "Feb" etc with them. How can I make a graph with title values?
List<BarEntry> entries = new ArrayList<>();
entries.add(new BarEntry(0f, 30f));
entries.add(new BarEntry(1f, 80f));
entries.add(new BarEntry(2f, 60f));
entries.add(new BarEntry(3f, 50f));
BarDataSet set = new BarDataSet(entries, "BarDataSet");
BarData data = new BarData(set);
data.setBarWidth(0.9f); // set custom bar width
chart.setData(data);
chart.setFitBars(true); // make the x-axis fit exactly all bars
chart.invalidate(); // refresh
This library I am using right now for this:
implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
First of all do this:
ArrayList<String> xVals = new ArrayList<String>();
xVals.add("Jan");
xVals.add("Feb");
xVals.add("Mar");
xVals.add("Apr");
xVals.add("May");
xVals.add("Jun");
Then customize your Xaxis as below:
XAxis xAxis = chart.getXAxis();
xAxis.setGranularity(1f);
xAxis.setGranularityEnabled(true);
xAxis.setCenterAxisLabels(true);
xAxis.setDrawGridLines(false);
xAxis.setAxisMaximum(6);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
// Below line will add labels on your xAxis:
xAxis.setValueFormatter(new IndexAxisValueFormatter(xVals));
Use this:
BarDataSet set = new BarDataSet(entries, "BarDataSet");
ArrayList<IBarDataSet> dataSets = new ArrayList<>();
dataSets.add(set);
BarData data = new BarData(dataSets);
data.setBarWidth(0.9f); // set custom bar width
chart.setData(data);
chart.setFitBars(true); // make the x-axis fit exactly all bars
chart.invalidate(); // refres