I want to display barchart in full screen after clicking on a button but I got an error when passing BarEntry class between activities.
android.os.ParcelFormatException: Cannot parcel an Entry with non-parcelable data
my code :
ArrayList<BarEntry> barEntries = new ArrayList<>();
barEntries.clear();
barEntries.add(new BarEntry(0, new float[]{8f, 9f, 10f}, "Jan"));
barEntries.add(new BarEntry(1, new float[]{11f, 9f, 5f}, "Feb"));
barEntries.add(new BarEntry(2, new float[]{8f, 3f, 4f}, "Mar"));
barEntries.add(new BarEntry(3, new float[]{5f, 14f, 2f}, "Apr"));
barEntries.add(new BarEntry(4, new float[]{5f, 2f, 3f}, "May"));
barEntries.add(new BarEntry(5, new float[]{2f, 15f, 6f}, "Jun"));
barEntries.add(new BarEntry(6, new float[]{15f, 2f, 8f}, "Jul"));
barEntries.add(new BarEntry(7, new float[]{4f, 19f, 4f}, "Aug"));
barEntries.add(new BarEntry(8, new float[]{5f, 5f, 5f}, "Sept"));
barEntries.add(new BarEntry(9, new float[]{6f, 6f, 6f}, "Oct"));
barEntries.add(new BarEntry(10, new float[]{8f, 9f, 10f}, "Nov"));
barEntries.add(new BarEntry(11, new float[]{2f, 7f, 7f}, "Dec"));
barEntries.add(new BarEntry(12, new float[]{6f, 2f, 8f}, "Jan"));
Intent bar = new Intent(ChartRevenueActivity.this, FullscreenActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("barEntries", barEntries);
bar.putExtra("type", "barChart");
bar.putExtras(bundle);
startActivity(bar);
Add this lib to build.gradle,
compile 'com.google.code.gson:gson:2.8.1'
convert your Arraylist to json using Gson and pass the json string to new activity.
Intent bar = new Intent(ChartRevenueActivity.this, FullscreenActivity.class);
bar.putExtra("type", "barChart");
bar.putExtra("barentry",new Gson().toJson(barEntries));
startActivity(bar);
retrieve the Arraylist in new activity like this,
ArrayList<BarEntry> barEntries =
new Gson().fromJson(getIntent().getStringExtra("barentry"), new TypeToken<ArrayList<BarEntry>>(){}.getType());
Related
private void displayGraph(){
barChart.setDrawBarShadow(false);
barChart.setDrawValueAboveBar(true);
barChart.setMaxVisibleValueCount(50);
barChart.setPinchZoom(false);
barChart.setDrawGridBackground(false);
barChart.getDescription().setEnabled(false);
Legend l = barChart.getLegend();
l.setEnabled(false);
ArrayList<BarEntry> barEntries = new ArrayList<>();
ArrayList<BarEntry> barEntries1 = new ArrayList<>();
barEntries.add(new BarEntry(1,50f));
barEntries.add(new BarEntry(2,42f));
barEntries.add(new BarEntry(3,34f));
barEntries.add(new BarEntry(4,24f));
barEntries.add(new BarEntry(1,50f));
barEntries.add(new BarEntry(2,42f));
barEntries1.add(new BarEntry(1,17f));
barEntries1.add(new BarEntry(2,19f));
barEntries1.add(new BarEntry(3,19f));
barEntries1.add(new BarEntry(4,18f));
barEntries1.add(new BarEntry(1,17f));
barEntries1.add(new BarEntry(2,19f));
BarDataSet barDataSet = new BarDataSet(barEntries,"");
barDataSet.setColor(Color.parseColor("#F44336"));
barDataSet.setDrawValues(false);
BarDataSet barDataSet1 = new BarDataSet(barEntries1,"");
barDataSet1.setColors(Color.parseColor("#9C27B0"));
barDataSet1.setDrawValues(false);
String[] months = new String[] {"TYPE 1", "TYPE 2", "TYPE 3", "TYPE 4","TYPE 5","TYPE 6"};
BarData data = new BarData(barDataSet,barDataSet1);
barChart.setData(data);
XAxis xAxis = barChart.getXAxis();
xAxis.setValueFormatter(new IndexAxisValueFormatter(months));
barChart.getAxisLeft().setAxisMinimum(0);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setGranularity(1);
xAxis.setGranularityEnabled(true);
xAxis.setLabelCount(5);
xAxis.setDrawGridLines(false);
xAxis.setAxisMaximum(6);
xAxis.setAvoidFirstLastClipping(true);
float barSpace = 0.02f;
float groupSpace = 0.3f;
int groupCount = 6;
data.setBarWidth(0.15f);
barChart.getXAxis().setAxisMinimum(0);
barChart.getXAxis().setAxisMaximum(0 + barChart.getBarData().getGroupWidth(groupSpace, barSpace) * groupCount);
barChart.groupBars(0, groupSpace, barSpace); // perform the "explicit" grouping
}
This is my code i am trying plot graph with 2 group item with 6 bar and i have defined 6 type of {"TYPE 1", "TYPE 2", "TYPE 3", "TYPE 4","TYPE 5","TYPE 6"} value but when i try run and seeing all x-axis value is not printing and also its not displaying with aligned with group item .
below is output of my graph using given code :
while i want to display all x-axis value aligned with group bar please help me what i am doing wrong in code .
its because of the bar space and group space, needed to change the width correctly and set setVisibleXRangeMaximum to 6.
You can try this
String[] days = new String[]{"TYPE 1", "TYPE 2", "TYPE 3", "TYPE 4", "TYPE 5", "TYPE 6"};
ArrayList<BarEntry> barEntries = new ArrayList<>();
ArrayList<BarEntry> barEntries2 = new ArrayList<>();
barEntries.add(new BarEntry(1,50f));
barEntries.add(new BarEntry(2,42f));
barEntries.add(new BarEntry(3,34f));
barEntries.add(new BarEntry(4,24f));
barEntries.add(new BarEntry(5,50f));
barEntries.add(new BarEntry(6,42f));
barEntries2.add(new BarEntry(1,17f));
barEntries2.add(new BarEntry(2,19f));
barEntries2.add(new BarEntry(3,19f));
barEntries2.add(new BarEntry(4,18f));
barEntries2.add(new BarEntry(5,17f));
barEntries2.add(new BarEntry(6,19f));
BarDataSet barDataSet = new BarDataSet(barEntries,"");
barDataSet.setColor(Color.parseColor("#F44336"));
BarDataSet barDataSet1 = new BarDataSet(barEntries2,"");
barDataSet1.setColors(Color.parseColor("#9C27B0"));
BarData data = new BarData(barDataSet,barDataSet1);
chart7.setData(data);
chart7.setDrawBarShadow(false);
chart7.setDrawValueAboveBar(true);
chart7.setMaxVisibleValueCount(50);
chart7.setPinchZoom(false);
chart7.setDrawGridBackground(false);
chart7.getDescription().setEnabled(false);
Legend l = chart7.getLegend();
l.setEnabled(false);
XAxis xAxis = chart7.getXAxis();
xAxis.setValueFormatter(new IndexAxisValueFormatter(days));
xAxis.setCenterAxisLabels(true);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setGranularity(1);
xAxis.setGranularityEnabled(true);
chart7.setDragEnabled(true);
chart7.setVisibleXRangeMaximum(6);
float barSpace = 0.02f;
float groupSpace = 0.55f;
data.setBarWidth(0.20f);
chart7.getXAxis().setAxisMinimum(0);
chart7.getXAxis().setAxisMaximum(6f);
chart7.groupBars(0, groupSpace, barSpace);
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 am making a barchart as shown in the code below. However the x labels "12:00", "12:10", ... are not showing up on the graph. Only the first one shows ("12:00").
I am facing another issue. I have two charts on the same page and I can't scroll down the page to see the bottom chart which is only partially shown.
BarChart bchart = view.findViewById(R.id.barChart);
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
yVals1.add(new BarEntry(0, 75));
yVals1.add(new BarEntry(10, 74));
yVals1.add(new BarEntry(20, 15));
yVals1.add(new BarEntry(30, 28));
yVals1.add(new BarEntry(40, 59));
yVals1.add(new BarEntry(50, 78));
yVals1.add(new BarEntry(60, 68));
yVals1.add(new BarEntry(70, 81));
yVals1.add(new BarEntry(80, 98));
yVals1.add(new BarEntry(90, 42));
yVals1.add(new BarEntry(100, 100));
BarDataSet set1;
//setting color
set1 = new BarDataSet(yVals1, "Satisfaction %");
set1.setColors(ColorTemplate.MATERIAL_COLORS);
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(set1);
String [] values = {"12:00", "12:10", "12:20", "12:30", "12:40", "12:50", "13:00"};
bchart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(values));
//Removing description
Description description = new Description();
description.setText("");
bchart.setDescription(description);
//Removing right y axis labels
YAxis rightYAxis = bchart.getAxisRight();
rightYAxis.setEnabled(false);
//Removing grid background
bchart.setDrawGridBackground(false);
BarData data = new BarData(dataSets);
data.setValueTextSize(10f);
data.setBarWidth(5f);
bchart.setTouchEnabled(false);
bchart.getAxisRight().setDrawLabels(false);
bchart.getXAxis().setDrawLabels(true);
bchart.getLegend().setEnabled(false);
bchart.setData(data);
For 1st problem follow following code:
ArrayList<String> labels = new ArrayList<String> ();
labels.add( "12:00");
labels.add( "12:10");
labels.add( "12:20");
labels.add( "12:30");
labels.add( "12:40");
labels.add( "12:50");
XAxis xAxis = chart.getXAxis();
xAxis.setValueFormatter(new IndexAxisValueFormatter(labels));
For 2nd problem you need to implement scrollview in your view.xml as parent view of your layout where your are creating your chartviews.
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!
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);