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);
Related
I want to create below image like combine graph, I have refered https://github.com/PhilJay/MPAndroidChart for it.
The code which they mention for it is not able to generate this graph?
Can somebody please help me with it?
Follow below example:
CombinedData data = new CombinedData();
data.setData(barData());
data.setData(lineData());
combinedChart.setData(data);
combinedChart.getDescription().setText("");
Legend legend = combinedChart.getLegend();
legend.setStackSpace(5);
// xAxis customization
XAxis xAxis = combinedChart.getXAxis();
xAxis.setGranularity(1f);
xAxis.setGranularityEnabled(true);
xAxis.setCenterAxisLabels(false);
xAxis.setDrawGridLines(false);
xAxis.setAxisMaximum(barData().getXMax() + 1);
xAxis.setAxisMinimum(barData().getXMin() - 1);
xAxis.setPosition(XAxis.XAxisPosition.BOTH_SIDED);
xAxis.setValueFormatter(new IndexAxisValueFormatter(getXAxisValues()));
YAxis leftAxis = combinedChart.getAxisLeft();
YAxis rightAxis = combinedChart.getAxisRight();
combinedChart.animateY(1000);
legend.setTextColor(Color.BLACK);
xAxis.setTextColor(Color.BLACK);
leftAxis.setTextColor(Color.BLACK);
rightAxis.setTextColor(Color.BLACK);
combinedChart.getDescription().setText("Last 6 Months Data");
combinedChart.getDescription().setTextSize(12);
combinedChart.setDrawMarkers(true);
combinedChart.setMarker(markerView(context));
combinedChart.getAxisLeft().addLimitLine(lowerLimitLine(2,"Lower Limit",2,12,getColor("defaultOrange"),getColor("defaultOrange")));
combinedChart.getAxisLeft().addLimitLine(upperLimitLine(5,"Upper Limit",2,12,getColor("defaultGreen"),getColor("defaultGreen")));
combinedChart.getXAxis().setGranularityEnabled(true);
combinedChart.getXAxis().setGranularity(1.0f);
combinedChart.getXAxis().setLabelCount(data.getEntryCount());
private BarData barData()
{
ArrayList<BarEntry> group1 = new ArrayList<BarEntry>();
group1.add(new BarEntry(0, 1));
group1.add(new BarEntry(1, 2));
group1.add(new BarEntry(2, 3));
group1.add(new BarEntry(3, 4));
group1.add(new BarEntry(4, 3));
group1.add(new BarEntry(5, 6));
BarDataSet barDataSet = new BarDataSet(group1, "Bars");
barDataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
barDataSet.setValueTextSize(10);
BarData barData = new BarData(barDataSet);
barData.setBarWidth(0.9f);
barDataSet.setValueTextColor(Color.BLACK);
return barData;
}
private LineData lineData()
{
ArrayList<Entry> line = new ArrayList<Entry> ();
line.add(new Entry(0, 4));
line.add(new Entry(1, 5));
line.add(new Entry(2, 2));
line.add(new Entry(3, 3));
line.add(new Entry(4, 1));
line.add(new Entry(5, 2));
LineDataSet lineDataSet = new LineDataSet(line, "Line");
lineDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
lineDataSet.setLineWidth(2);
lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
lineDataSet.setHighlightEnabled(true); // allow highlighting for DataSet
// set this to false to disable the drawing of highlight indicator (lines)
lineDataSet.setDrawHighlightIndicators(true);
lineDataSet.setHighLightColor(Color.RED);
lineDataSet.setValueTextSize(10);
lineDataSet.setCircleRadius(6);
lineDataSet.setCircleHoleRadius(3);
LineData lineData = new LineData(lineDataSet);
lineDataSet.setColors(Color.DKGRAY);
lineDataSet.setValueTextColor(Color.BLACK);
lineDataSet.setCircleColorHole(Color.DKGRAY);
lineDataSet.setCircleColor(Color.CYAN);
return lineData;
}
where combinedChart is your chart view.
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 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
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.