MPAndroid chart need to add values using array list - android

I have an arraylist of object, I need to create a line chart using this array of object, I have the minimum y value and maximum y value for y axis, and also I have string array for x axis. This 2 arrays doesn't have the same length.
My problem is in MPAndroidChart library I can create a line chart using just static values. But here I need to create it using dynamic values.
for (int i=0;i<deals.size();i++) {
float y = (float)deals.get(i).getPrice();
values.add(new Entry(y, y));
}
// create a dataset and give it a type
LineDataSet set1 = new LineDataSet(values, "DataSet 1");
set1.setAxisDependency(YAxis.AxisDependency.LEFT);
set1.setColor(ColorTemplate.getHoloBlue());
set1.setValueTextColor(ColorTemplate.getHoloBlue());
set1.setLineWidth(1.5f);
set1.setDrawCircles(false);
set1.setDrawValues(false);
set1.setFillAlpha(65);
set1.setFillColor(ColorTemplate.getHoloBlue());
set1.setHighLightColor(Color.rgb(244, 117, 117));
set1.setDrawCircleHole(false);
// create a data object with the datasets
LineData data = new LineData(set1);
data.setValueTextColor(Color.WHITE);
data.setValueTextSize(9f);
// set data
mChart.setData(data);

First you have to create ArrayList values , which i think you created upon adding values.add(new Entry(y, y)); you have to give index in second argument you are giving value in both so you should do something like values.add(new Entry(y,i));
Hope it helps, Do let me know if it works. I used Mpcharts library alot in case of further queries you can contact me. Best of luck !

You need to create two data set
1. For Y axis data
2. For X axis label
you can simply skip X value if the corresponding index has not value to display.
ArrayList<YourData> data= new ArrayList<>();
data.addAll(getDataFromDataSource());
ArrayList<String> xVals = new ArrayList<String>();
ArrayList<Entry> yVals = new ArrayList<>();
for (int i = 0; i <= data.size() - 1; i++) {
if(data.getName().equals(null)){
xVals.add(i,"");
}
xVals.add(i, data.getName());
yVals.add(new Entry(data.getValue(), i));
}
After creating data source, create LineDataSet object to apply Y axis line color, line width, etc.
LineDataSet set1 = new LineDataSet(yVals, "");
set1.setAxisDependency(YAxis.AxisDependency.LEFT);
set1.setColor(ColorTemplate.getHoloBlue());
set1.setValueTextColor(ColorTemplate.getHoloBlue());
set1.setLineWidth(1.5f);
set1.setDrawCircles(false);
set1.setDrawValues(false);
set1.setFillAlpha(65);
set1.setFillColor(ColorTemplate.getHoloBlue());
set1.setHighLightColor(Color.rgb(244, 117, 117));
set1.setDrawCircleHole(false);
Create LineData object for X axis labels
LineData data = new LineData(xVals, set1);
data.setValueTextColor(Color.WHITE);
data.setValueTextSize(9f);
And finally pass LineData object to LineChart
mChart.setData(data);
Your final code should look like this
ArrayList<YourData> data= new ArrayList<>();
data.addAll(getDataFromDataSource());
ArrayList<String> xVals = new ArrayList<String>();
ArrayList<Entry> yVals = new ArrayList<>();
for (int i = 0; i <= data.size() - 1; i++) {
if(data.getName().equals(null)){
xVals.add(i,"");
}
xVals.add(i, data.getName());
yVals.add(new Entry(data.getValue(), i));
}
LineDataSet set1 = new LineDataSet(yVals, "");
set1.setAxisDependency(YAxis.AxisDependency.LEFT);
set1.setColor(ColorTemplate.getHoloBlue());
set1.setValueTextColor(ColorTemplate.getHoloBlue());
set1.setLineWidth(1.5f);
set1.setDrawCircles(false);
set1.setDrawValues(false);
set1.setFillAlpha(65);
set1.setFillColor(ColorTemplate.getHoloBlue());
set1.setHighLightColor(Color.rgb(244, 117, 117));
set1.setDrawCircleHole(false);
LineData data = new LineData(xVals, set1);
data.setValueTextColor(Color.WHITE);
data.setValueTextSize(9f);
mChart.setData(data);
Hope it works :)

Related

Android MPChart Not Showing Any Dat

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

MPAndroid Linechart with single data entry

I'm using MPAndroidChart-v2.1.6 and I'm facing the following problem.
When I have a single data entry, it starts displaying the value from 0 index at X axis. This is how it looks like.
I want it to aligned in center, when i have only single data entry, like this..
I have tried setMinimum() and setMaximum() property for that particular condition but nothing positive happened to me.when I have more than one entry, it works well.
Here is my code,
ArrayList<LineDataSet> lines = new ArrayList<>();
LineDataSet linedataset1 = new LineDataSet(group1, "Text1");
linedataset1.setDrawFilled(false);
linedataset1.setValueFormatter(new MyDataSetFormatter());
linedataset1.setFillAlpha(110);
linedataset1.setLineWidth(1f);
linedataset1.setColor(Color.rgb(67, 91, 153));
linedataset1.setCircleColor(Color.rgb(67, 91, 153));
LineDataSet linedataset2 = new LineDataSet(group2, "Text2");
linedataset2.setDrawFilled(false);
linedataset2.setValueFormatter(new MyDataSetFormatter());
linedataset2.setFillAlpha(110);
linedataset2.setLineWidth(1f);
linedataset2.setColor(Color.rgb(254, 252, 59));
linedataset2.setCircleColor(Color.rgb(254, 252, 59));
LineDataSet linedataset3 = new LineDataSet(group3, "Text3");
linedataset3.setDrawFilled(false);
linedataset3.setValueFormatter(new MyDataSetFormatter());
linedataset3.setFillAlpha(110);
linedataset3.setLineWidth(1f);
linedataset3.setColor(Color.rgb(68, 185, 102));
linedataset3.setCircleColor(Color.rgb(68, 185, 102));
LineDataSet linedataset4 = new LineDataSet(group4, "text4");
linedataset4.setDrawFilled(false);
linedataset4.setValueFormatter(new MyDataSetFormatter());
linedataset4.setFillAlpha(110);
linedataset4.setLineWidth(1f);
linedataset4.setColor(Color.rgb(145, 92, 96));
linedataset4.setCircleColor(Color.rgb(145, 92, 96));
lines.add(linedataset1);
lines.add(linedataset2);
lines.add(linedataset3);
lines.add(linedataset4);
leftYAxis = lineChart.getAxisLeft();
rightYAxis = lineChart.getAxisRight();
rightYAxis.setDrawLabels(false);
rightYAxis.setAxisMaxValue(105);
leftYAxis.setAxisMaxValue(105);
lineChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
lineChart.getXAxis().setLabelRotationAngle(-70);
lineChart.getXAxis().setDrawGridLines(false);
lineChart.getAxisLeft().setDrawGridLines(false);
lineChart.getAxisRight().setDrawGridLines(false);
lineChart.getAxisRight().setEnabled(false);
lineChart.setBackgroundColor(Color.WHITE);
lineChart.setDrawGridBackground(false);
lineChart.setData(new LineData(newLabels, lines));
lineChart.setPinchZoom(false);
lineChart.animateY(1000);
lineChart.setDescription(null);
XAxis xAxis = lineChart.getXAxis();
xAxis.setAvoidFirstLastClipping(true);
//lineChart.setBackgroundColor(Color.rgb(255, 255, 255));
lineChart.setTouchEnabled(false);
lineChart.invalidate();
Thanks in advance,
Declare line chart entries separately outside the class
private ArrayList<Entry> dataValuesDaily(){
ArrayList<Entry> dataVals = new ArrayList<>();
dataVals.add(new Entry(1,10));
dataVals.add(new Entry(11,2));
dataVals.add(new Entry(21,16));
dataVals.add(new Entry(30,4));
return dataVals;
}
And then in the class
//TODO:: LINE GRAPH
LineDataSet lineDataSet1 = new LineDataSet(dataValuesDaily(), "Data Set 1");
lineDataSet1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
ArrayList<ILineDataSet> dataSets = new ArrayList<>();
dataSets.add(lineDataSet1);
//customization
mLineGraph.setTouchEnabled(true);
mLineGraph.setDragEnabled(true);
mLineGraph.setScaleEnabled(false);
mLineGraph.setPinchZoom(false);
mLineGraph.setDrawGridBackground(false);
mLineGraph.setExtraLeftOffset(15);
mLineGraph.setExtraRightOffset(15);
//to hide background lines
mLineGraph.getXAxis().setDrawGridLines(false);
mLineGraph.getAxisLeft().setDrawGridLines(false);
mLineGraph.getAxisRight().setDrawGridLines(false);
//to hide right Y and top X border
YAxis rightYAxis = mLineGraph.getAxisRight();
rightYAxis.setEnabled(false);
XAxis topXAxis = mLineGraph.getXAxis();
topXAxis.setEnabled(false);
XAxis xAxis = mLineGraph.getXAxis();
xAxis.setEnabled(true);
xAxis.setDrawGridLines(false);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
lineDataSet1.setLineWidth(4f);
lineDataSet1.setCircleRadius(3f);
lineDataSet1.setDrawValues(false);
lineDataSet1.setCircleHoleColor(getResources().getColor(R.color.pie_color_4));
lineDataSet1.setCircleColor(getResources().getColor(R.color.pie_color_4));
LineData data = new LineData(dataSets);
mLineGraph.setData(data);
mLineGraph.animateX(2000);
mLineGraph.invalidate();
mLineGraph.getLegend().setEnabled(false);
mLineGraph.getDescription().setEnabled(false);
To set the X axis limits to specific values you can call setAxisMinimum and setAxisMaximum. For example:
XAxis xAxis = lineChart.getXAxis();
xAxis.setAxisMinimum(10);
xAxis.setAxisMaximum(30);
If you want to do this automatically using your LineDataSet you can query the min and max values of the X data
float xMin = dataSet.getXMin() - 5;
float xMax = dataSet.getXMax() + 5;
xAxis.setAxisMinimum(xMin);
xAxis.setAxisMaximum(xMax);
which will center the data even when there is only one point.

MPAndroidChart grouped bar doesn't begin from left side

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();

How to remove Legend of BarChart

How to remove Legend from BarChat.
I have some code.
float[] yData = { (float)Math.abs(item._Lent),(float) Math.abs( item._Barrow )};
String[] xData = { "salary", "Spends" };
ArrayList<BarEntry> entries = new ArrayList<>();
for (int i = 0; i < yData.length; i++)
entries.add(new BarEntry(yData[i], i));
BarDataSet dataset = new BarDataSet(entries, "");
ArrayList<String> labels = new ArrayList<String>();
for (int i = 0; i < xData.length; i++)
labels.add(xData[i]);
BarData data = new BarData(labels, dataset);
mChart.setData(data); // set the data and list of lables into chart
BarChart barchart = mChart;
Legend legend = barchart.getLegend();
legend.setEnabled(false);
YAxis topAxis = barchart.getAxisLeft();
topAxis.setDrawLabels(false);
YAxis bottomAxis = barchart.getAxisRight();
bottomAxis.setDrawLabels(false);
XAxis rightAxis = barchart.getXAxis();
rightAxis.setDrawLabels(false);
bottomAxis.setDrawLabels(false);
`
This code not working properly. I gives me following out put.
output
Hide the description with setDescription("") :
barchart.setDescription("");
You need to set legend visible to false, like this: barchart.setLegendVisible(false); - so your code will look something like this:
//rest of your code
....
BarData data = new BarData(labels, dataset);
mChart.setData(data); // set the data and list of lables into chart
BarChart barchart = mChart;
barchart.setLegendVisible(false);
...
//more of your code...
Give it a try and let me know if it helps. You can also look at the selected answer to this closely related question here.

set labels for Xaxis in MPandroid Chart (Bar Chart)

I have been trying to figure out and have read many post. but i am unable to set labels to xaxis in Bar Chart of MPAndroid. currently it is showing the position of the bar.
It is difficult to answer without your work samples , try this, it may help you
// Plotting Data
ArrayList<BarEntry> XValues = new ArrayList<>();
BarEntry v1e1 = new BarEntry(110.000f, 0);
valueSet1.add(v1e1);
// Setting X label this way
BarDataSet set = new BarDataSet(XValues, "Age Distribution");
Step 1 : Initialize x axis like this = in global = ArrayList xaxis0
Initialize xaxis0 = new ArrayList<>();
Step 2 : After that if you have arraylist strings of data.Then start loop
add all strings into the x axis values like below code
for (int i = 0; i < xdata.size(); i++)
{
* xaxis0.add(i, xdata.get(i).get("date"));
int data222 = Integer.parseInt(str);
BarEntry v1e11 = new BarEntry(data222, i);
}
BarDataSet barDataSet1 = new BarDataSet(valueSet1, "Top 5 deals");
barDataSet1.setColors(whitecolors);
barDataSet1.setHighLightColor(Color.GREEN);
barDataSet1.setBarSpacePercent(60f);
barDataSet1.setValueTextColor(Color.WHITE);
dataSets = new ArrayList<>();
dataSets.add(barDataSet1);
*BarData data11 = new BarData(xaxis0, dataSets);
data11.setValueFormatter(new LargeValueFormatter());
data11.setGroupSpace(100f);
holder.chart.setData(data11);
That's its ...

Categories

Resources