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.
Related
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();
A problem occured while building a chart. Studio doesn't accept data in this
form. Actually problem is only with month's names. Without them code works fine.
MPAndroidChart version 3. In official examples i understood nothing). Studio says:Error:(46, 24) error: constructor BarData in class BarData cannot be applied to given types; required: IBarDataSet[] found: ArrayList,BarDataSet reason: varargs mismatch; ArrayList cannot be converted to IBarDataSet. Please tell me how to add strings with months. Thank you.
barChart= (BarChart) findViewById(R.id.bargraph);
List<BarEntry> calls = new ArrayList<>();
calls.add(new BarEntry(0, 9f));
calls.add(new BarEntry(1, 3f));
calls.add(new BarEntry(2, 5f));
calls.add(new BarEntry(3, 2f));
calls.add(new BarEntry(4, 6f));
calls.add(new BarEntry(5, 12f));
BarDataSet barDataSet = new BarDataSet(calls,"num");
ArrayList<String> months = new ArrayList<>();
months.add("Jan");
months.add("Feb");
months.add("Mar");
months.add("Apr");
months.add("May");
months.add("June");
BarData data;
data = new BarData(months,barDataSet);
data.setBarWidth(0.9f);
barChart.setData(data);
barChart.setFitBars(true);
barChart.invalidate();
Buddy use version 3.0.4 and follow example below:
ArrayList<BarEntry>() barEntries = new ArrayList<BarEntry>();
barEntries.add(new BarEntry(0, 1));
barEntries.add(new BarEntry(1, 2));
barEntries.add(new BarEntry(2, 4));
barEntries.add(new BarEntry(3, 6));
barEntries.add(new BarEntry(4, 5));
barEntries.add(new BarEntry(5, 7));
barDataSet = new BarDataSet(barEntries, "Contracts");
barDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
// barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
barDataSet.setColor(getColor("defaultYellow"));
barDataSet.setHighlightEnabled(true);
barDataSet.setHighLightColor(Color.RED);
barDataSet.setValueTextSize(defaultValueTextSize);
barDataSet.setValueTextColor(getColor("primaryDark"));
BarData barData = new BarData(barDataSet);
barChart.getDescription().setText("No. of Contracts signed in 6 months");
barChart.getDescription().setTextSize(12);
barChart.setDrawMarkers(true);
barChart.setMarker(markerView(context));
barChart.getAxisLeft().addLimitLine(lowerLimitLine(2,"Minimum",2,12,getColor("defaultOrange"),getColor("defaultOrange")));
barChart.getAxisLeft().addLimitLine(upperLimitLine(5,"Target",2,12,getColor("defaultGreen"),getColor("defaultGreen")));
barChart.getAxisLeft().setAxisMinimum(0);
barChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTH_SIDED);
ArrayList<String> labels = new ArrayList<String> ();
labels.add( "JAN");
labels.add( "FEB");
labels.add( "MAR");
labels.add( "APR");
labels.add( "MAY");
labels.add( "JUN");
barChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(labels));
barChart.animateY(1000);
barChart.getXAxis().setGranularityEnabled(true);
barChart.getXAxis().setGranularity(1.0f);
barChart.getXAxis().setLabelCount(barDataSet.getEntryCount());
barChart.setData(barData);
one days ago ,i have same question
ArrayList<String> months = new ArrayList<>();
months.add("Jan");
months.add("Feb");
months.add("Mar");
months.add("Apr");
months.add("May");
months.add("June");
change by:
final HashMap<Integer,String>nuMap = new HashMap<>();
nuMap.put(0,"Jan");
nuMap.put(1,"Feb");
nuMap.put(2,"Mar");
nuMap.put(3,"Apr");
nuMap.put(4,"May");
nuMap.put(5,"June");
and now setValueFormatter
XAxis xAxis = yourchart.getXAxis();
xAxis.setValueFormatter(new IAxisValueFormatter() {
#Override
public String getFormattedValue(float value, AxisBase axis) {
return nuMap.get((int)value);
}
});
new it's OK
you can plot the XAxis Label's word!
Is there anyway to set dynamically values to Yaxis using MPAndroidChart. I searched SO question but here I didn't get any answer. I have to set run-time values to Yaxis. I have also tried setGranularity() but this method is showing me error like can't resolve method setGranularity(float).Please help me.
you can try like this
private ArrayList<Entry> setYAxisValues(){
ArrayList<Entry> yVals = new ArrayList<Entry>();
//Change to your values
yVals.add(new Entry(0, 6));
yVals.add(new Entry(1, 4));
yVals.add(new Entry(2, 5));
yVals.add(new Entry(3, 4));
yVals.add(new Entry(4, 3));
yVals.add(new Entry(5, 2));
yVals.add(new Entry(6, 3));
yVals.add(new Entry(7, 4));
yVals.add(new Entry(8, 5));
yVals.add(new Entry(9, 3));
yVals.add(new Entry(10, 2));
yVals.add(new Entry(11,4));
return yVals;
}
private void setData() {
//call this method to set data
ArrayList<Entry> yVals = setYAxisValues();
LineDataSet set1;
// create a dataset and give it a type
set1 = new LineDataSet(yVals, "DataSet 1");
set1.setFillAlpha(110);
set1.setColor(Color.WHITE);
set1.setCircleColor(Color.WHITE);
set1.setLineWidth(1f);
set1.setCircleRadius(3f);
set1.setDrawCircleHole(false);
set1.setValueTextSize(9f);
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
dataSets.add(set1); // add the datasets
// create a data object with the datasets
LineData data = new LineData(dataSets);
// set data to LineChart
mChart.setData(data);
mChart.setVisibleXRangeMaximum(4); // allow 20 values to be displayed at once on the x-axis, not more
mChart.moveViewToX(10);
}
I'm trying to have a grouped bar chart using MPAndroidChart. The last bar at 5 in the screenshot below gets cut off.
What could I have done wrong?
I am using v3.0.1. I have read this official doc at https://github.com/PhilJay/MPAndroidChart/wiki/Setting-Data#grouped-barchart.
private void createBarChart(BarChart barChart) {
barChart.getDescription().setEnabled(false);
barChart.getAxisRight().setEnabled(false);
barChart.getAxisLeft().setEnabled(true);
barChart.setDrawGridBackground(true);
barChart.setDrawValueAboveBar(false);
barChart.setDrawBarShadow(false);
barChart.setEnabled(true);
barChart.setTouchEnabled(false);
barChart.setPinchZoom(false);
barChart.setDoubleTapToZoomEnabled(false);
barChart.getLegend().setEnabled(false);
XAxis xAxis = barChart.getXAxis();
xAxis.setDrawLabels(true);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(true);
xAxis.setCenterAxisLabels(true);
xAxis.setAxisMinimum(0F);
xAxis.setGranularity(1F);
YAxis leftAxis = barChart.getAxisLeft();
leftAxis.setAxisMinimum(0F);
leftAxis.setAxisMaximum(50F); // 100F(100%) max
barChart.setData(createBarChartData());
barChart.groupBars(0F, 0.06F, 0.02F);
barChart.invalidate();
}
private BarData createBarChartData() {
ArrayList<BarEntry> valuesA = new ArrayList<>();
valuesA.add(new BarEntry(0, 20));
valuesA.add(new BarEntry(1, 40));
valuesA.add(new BarEntry(2, 20));
valuesA.add(new BarEntry(3, 10));
valuesA.add(new BarEntry(4, 5));
valuesA.add(new BarEntry(5, 5));
BarDataSet valuesADataSet = new BarDataSet(valuesA, "A");
valuesADataSet.setColor(ColorTemplate.COLORFUL_COLORS[3]);
ArrayList<BarEntry> valuesB = new ArrayList<>();
valuesB.add(new BarEntry(0, 35));
valuesB.add(new BarEntry(1, 20));
valuesB.add(new BarEntry(2, 15));
valuesB.add(new BarEntry(3, 10));
valuesB.add(new BarEntry(4, 10));
valuesB.add(new BarEntry(5, 10));
BarDataSet valuesBDataSet = new BarDataSet(valuesB, "B");
valuesBDataSet.setColor(ColorTemplate.COLORFUL_COLORS[4]);
BarData barData = new BarData(valuesADataSet, valuesBDataSet);
barData.setDrawValues(false);
barData.setBarWidth(0.45F);
return barData;
}
I was able to accomplish it by calling xAxis.setAxisMaximum(6F);