How to set dynamically values to Yaxis using MPAndroidChart? - android

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

Related

How to generate combine graph with single bar char & single line

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.

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

MPCharts Bar chart X label and can't scroll

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.

MPAndroid chart need to add values using array list

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 set legend labels MPChart

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.

Categories

Resources