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 ...
Related
I'm using BarCharts for a project in Android Studio
I created a Barchart with 2 vertical bars for each group. I also changed the x axis by adding a list of names (Strings), but it doesn't look well. I want the x axis to always be between the 2 vertical bars from 1 group (because those 2 bars are correspondent for 1 name from the x axis). Is this possible? I need the x axis to always be between the blue bar and the yellow bar. To have 1 x axis label for 1 bar group.
Here is how my chart looks right now:
My chart at the moment
Here is my code:
ArrayList<BarEntry> entries = new ArrayList<>();
ArrayList<BarEntry> firstEntries = new ArrayList<>();
for(int i=0;i<scorelist.size();i++)
{
BarEntry barEntry = new BarEntry(i, scorelist.get(i));
entries.add(barEntry);
}
for(int i=0;i<first3Scorelist.size();i++)
{
BarEntry barEntry = new BarEntry(i, first3Scorelist.get(i));
firstEntries.add(barEntry);
}
BarDataSet barDataSet2 = new BarDataSet(entries, "Last Games Score");
barDataSet2.setColors(Color.YELLOW);
BarDataSet barDataSet1 = new BarDataSet(firstEntries, "First Games Score");
barDataSet1.setColors(Color.CYAN);
BarData barData = new BarData(barDataSet1,barDataSet2);
barChart.setData(barData);
barChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(nameList));
barChart.getXAxis().setTextColor(Color.MAGENTA);
barChart.getXAxis().setTextSize(10);
barChart.setVisibleXRangeMaximum(7);
barData.setBarWidth(0.3f);
float barSpace = 0.01f;
float groupSpace = 0.3f;
barChart.animateY(3000);
barChart.animate();
barChart.groupBars((float) -0.2, groupSpace, barSpace);
barChart.getDescription().setEnabled(false);
barChart.invalidate();
}
Thank you so much!
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
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 :)
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.
I am new to MP Android Chart, and I am not able to figure out, how to fill specific Y Range with different color. Highlighted in screenshot as point 2.
Also after reading function documentation I am trying to change line color highlighted in point 1, and fill area under graph with some color point 3. But point 1 and point 3 are not working, and I couldnt figure out how point 2 can be done.Screenshot and code attached.
public class LineChartActivity extends AppCompatActivity {
private static final String TAG = "Test";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_line_chart);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Log.i(TAG, "Line Chart Activity Created");
LineChart chart = (LineChart) findViewById(R.id.chart);
LineData data = new LineData(getXAxisValues(), getDataSet());
chart.setData(data);
setXAxis(chart);
chart.getAxisRight().setDrawLabels(false);
//chart.setAutoScaleMinMaxEnabled(true);
chart.setGridBackgroundColor(128);
chart.setBorderColor(255);
chart.setDrawGridBackground(false);
//chart.setBackgroundColor(0);
chart.getLegend().setEnabled(false);
chart.setPinchZoom(true);
chart.setDescription("");
chart.setTouchEnabled(true);
chart.setDoubleTapToZoomEnabled(true);
chart.animateXY(2000, 2000);
chart.invalidate();
}
private ArrayList<ILineDataSet> getDataSet() {
ArrayList<ILineDataSet> dataSets = null;
ArrayList<Entry> valueSet1 = new ArrayList<>();
Entry v1e1 = new Entry(110.000f, 0);
valueSet1.add(v1e1);
Entry v1e2 = new Entry(40.000f, 4);
valueSet1.add(v1e2);
Entry v1e3 = new Entry(60.000f, 5);
valueSet1.add(v1e3);
Entry v1e4 = new Entry(30.000f, 6);
valueSet1.add(v1e4);
Entry v1e5 = new Entry(90.000f, 7);
valueSet1.add(v1e5);
Entry v1e6 = new Entry(100.000f, 8);
valueSet1.add(v1e6);
LineDataSet lineDataSet1 = new LineDataSet(valueSet1, "Brand 1");
int[] colors = new int[] {R.color.red ,R.color.red, R.color.orange ,R.color.green, R.color.orange, R.color.red };
lineDataSet1.setCircleColors(colors, this);
lineDataSet1.setCircleRadius(8f);
lineDataSet1.setLineWidth(3f);
lineDataSet1.setValueTextSize(20f);
lineDataSet1.enableDashedLine(6f, 18f, 0);
dataSets = new ArrayList<>();
dataSets.add(lineDataSet1);
return dataSets;
}
private ArrayList<String> getXAxisValues() {
ArrayList<String> xAxis = new ArrayList<>();
xAxis.add("JAN");
xAxis.add("FEB");
xAxis.add("MAR");
xAxis.add("APR");
xAxis.add("MAY");
xAxis.add("JUN");
xAxis.add("JUL");
xAxis.add("AUG");
xAxis.add("SEPT");
xAxis.add("OCT");
xAxis.add("NOV");
xAxis.add("DEC");
return xAxis;
}
private void setXAxis(LineChart chart){
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTextSize(10f);
xAxis.setTextColor(Color.BLUE);
//xAxis.setDrawGridLines(true);
xAxis.setDrawAxisLine(true);
}
}
Many Thanks !
Ankit
if you are trying to change the color of the line that is connecting the plot points, you can try this:
lineDataSet1.setColor(Color.BLACK) //or any other color you want
For filling the area under the graph, try using:
lineDataSet1.setFillColor(Color.GREEN);
lineDataSet1.setFillAlpha(10); //setting alpha is optional, use if needed.
As far as your point 2 is concerned, I think using LimitLines could be the starting as they can be plotted parallel to any axis. Filling them in will need some looking into. I will definitely update the answer if I find something.
Hope this helps!