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
Related
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
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();
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);
I am trying to customize legend but not able to do so.My purpose is to give different legend labels.I ma using MPChart library to do so.
ArrayList<BarEntry> entries = new ArrayList<>();
entries.add(new BarEntry(4f, 0));
entries.add(new BarEntry(8f, 1));
entries.add(new BarEntry(6f, 2));
entries.add(new BarEntry(12f, 3));
entries.add(new BarEntry(18f, 4));
mColors.add(R.color.red);
mColors.add(R.color.text_color_gray);
mColors.add(R.color.text_color_blue);
mColors.add(R.color.green);
mColors.add(R.color.black);
BarDataSet dataset = new BarDataSet(entries, null);
ArrayList<String> labels = new ArrayList<String>();
labels.add("05");
labels.add("06");
labels.add("07");
labels.add("08");
labels.add("09");
BarData data = new BarData(labels, dataset);
Legend legend = mChart.getLegend();
legend.setEnabled(true);
legend.setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);
legend.setForm(Legend.LegendForm.SQUARE);
legend.setColors(mColors);
legend.setLabels(mLabels);
mChart.setData(data);
mChart.animateY(2000);
LimitLine line = new LimitLine(10f);
YAxis yAxis = mChart.getAxisLeft();
yAxis.addLimitLine(line);
yAxis.setDrawAxisLine(true);
mChart.setDrawValueAboveBar(true);
mChart.setDrawBarShadow(false);
mChart.setVisibleXRange(4);
mChart.moveViewToX(2);
mChart.setDrawValueAboveBar(false);
mChart.invalidate();
Please let me know any solution for this.
you can custom your legend like this
LegendEntry legendEntryA = new LegendEntry();
legendEntryA.label = "a";
legendEntryA.formColor = Color.GREEN;
And add them to legend of the chart
legend.setCustom(Arrays.asList(legendEntryA, legendEntryB, legendEntryC, legendEntryD));
If you want to have 4 legend labels, you need 4 BarDataSet objects.
Having different colours will only group the different colours on the one legend that will be generated.
And you need to pass the colors to the DataSet and it will be mapped with the Legend.
Finally, your DataSets need a label which will be used for the legend. You can specify the label as second parameter in the constructor.