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.
Related
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
In the following image I've plotted a day's worth of prices and the trading volume per hour.
To achieve this I'm using a CombinedChart type. You can immediately see that the volume data distracts the user from the price data because both charts fill the screen.
QUESTION:
Is there a way to reserve 25% of the bottom of the chart for volume and 75% of the top of the chart for prices ?
Code is as follows:
private void setupChart(){
chart.setAutoScaleMinMaxEnabled(true);
chart.setBackgroundColor(Color.WHITE);
chart.getDescription().setEnabled(false);
chart.setPinchZoom(false);
chart.setDrawGridBackground(false);
chart.getLegend().setEnabled(true);
chart.setDrawOrder(new CombinedChart.DrawOrder[]{ CombinedChart.DrawOrder.BAR, CombinedChart.DrawOrder.CANDLE });// draw bars behind candles
// right side is for the volume
YAxis rightAxis = chart.getAxisRight();
rightAxis.setDrawGridLines(false);
rightAxis.setAxisMinimum(0f);
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
chart.resetTracking();
ArrayList<HistoricPrice> historicPrices = presenter.getHistoricalDataGranular(DEFAULT_GRANULARITY_MINS);
ArrayList<CandleEntry> valuesCandles = new ArrayList<>();
ArrayList<BarEntry> valuesBars = new ArrayList<>();
for (int i = progress; (i < progress+MAX_CANDLES_COUNT) && (i < historicPrices.size()); i++) {
HistoricPrice historicPrice = historicPrices.get(i);
CandleEntry candleEntry = new CandleEntry(i, historicPrice.high.floatValue(), historicPrice.low.floatValue(), historicPrice.open.floatValue(), historicPrice.close.floatValue());
valuesCandles.add(candleEntry);
BarEntry barEntry = new BarEntry(i, Float.valueOf(historicPrice.volume.toPlainString()));
valuesBars.add(barEntry);
}
CandleDataSet set1 = new CandleDataSet(valuesCandles, "Prices");
set1.setDrawIcons(false);
set1.setAxisDependency(YAxis.AxisDependency.LEFT);
set1.setShadowColor(Color.DKGRAY);
set1.setShadowWidth(0.7f);
set1.setDecreasingColor(Color.RED);
set1.setDecreasingPaintStyle(Paint.Style.FILL);
set1.setIncreasingColor(Color.rgb(122, 242, 84));
set1.setIncreasingPaintStyle(Paint.Style.FILL);
set1.setNeutralColor(Color.BLUE);
set1.setDrawValues(true);
set1.setShowCandleBar(true);
CandleData candleData = new CandleData(set1);
BarDataSet set2 = new BarDataSet(valuesBars, "Volume");
set2.setColor(ContextCompat.getColor(this, R.color.blue_grey));
set2.setDrawValues(true);
set2.setAxisDependency(YAxis.AxisDependency.RIGHT);
BarData barData = new BarData(set2);
CombinedData data = new CombinedData();
data.setData(candleData);
data.setData(barData);
chart.setData(data);
chart.invalidate();
}
you can set the wether the data set is dependant on leftAxis or rightAxis. So you set set1 dependant on leftAxis and set2 on RightAxis. After doing thing this call set1.getAxisLeft().setAxisMinimum(minValue) from where the graph will start.
have a look at the below snippet
CandleDataSet set1 = new CandleDataSet(valuesCandles, "Prices");
set1.setAxisDependency(YAxis.AxisDependency.LEFT);
set1.getAxisLeft().setAxisMinimum(minValue)
BarDataSet set2 = new BarDataSet(valuesBars, "Volume");
set2.setAxisDependency(YAxis.AxisDependency.RIGHT);
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();
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 ...
I want to create a bar chart with YAxis contains floats values and xAxis contains String values,
My problem is that i want to change displayed data in YAxis and make it String, but the value still float
this is my chart
mChart = (BarChart) findViewById(R.id.chart1);
mChart.setDescription("");
// if more than 60 entries are displayed in the chart, no values will be
// scaling can now only be done on x- and y-axis separately
mChart.setPinchZoom(false);
mChart.setDrawBarShadow(false);
mChart.setDrawGridBackground(false);
XAxis xAxis = mChart.getXAxis();
xAxis.setPosition(XAxisPosition.BOTTOM);
xAxis.setSpaceBetweenLabels(0);
xAxis.setTextSize(10);
xAxis.setDrawGridLines(false);
mChart.getAxisRight().setDrawGridLines(false);
mChart.getAxisLeft().setDrawGridLines(false);
mChart.getAxisLeft().setDrawLabels(true);
mChart.getXAxis().setXOffset(10);
YAxis rightAxis = mChart.getAxisRight();
rightAxis.setDrawAxisLine(false);
rightAxis.setTextColor(Color.WHITE);
rightAxis.setDrawGridLines(false);
// add a nice and smooth animation
mChart.animateY(2500);
mChart.getLegend().setEnabled(true);
and this is how i call the bar data set
xVals.add(" ");
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
for (int i = 0; i < yvalues.size(); i++) {
BarEntry b=new BarEntry((int)Float.parseFloat(yvalues.get(i)), i);
//here is the BarEntry values but i cant change the displayed value its still
//showing float
yVals1.add(b);
}
BarDataSet set1 = new BarDataSet(yVals1, "duration min");
set1.setColor(Color.GREEN);
set1.setDrawValues(false);
set1.setLabel("duration min");
ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
dataSets.add(set1);
BarData data = new BarData(xVals, dataSets);
mChart.setData(data);
mChart.invalidate();
If you do not want to display the floating point value, try fetching the rounded value.
BarEntry b=new BarEntry(Math.round(yvalues.get(i)), i);