All bars are showing in the same position in MPAndroidChart - android

I am using MPAndroidChart library.
performance= result.getJSONArray("performance");
for(int i = 0; i<performance.length();i++){
JSONObject jsonChildNode = performance.getJSONObject(i);
Iterator iterator = jsonChildNode.keys();
int j=0;
duration= new ArrayList<>();
barEntries= new ArrayList<>();
while(iterator.hasNext()){
String key = (String)iterator.next();
String issue = jsonChildNode.optString(key) ;
Float val=parseFloat(issue);
duration.add(key);
barEntries.add(new BarEntry(val,j));
}
j++;
barDataSet= new BarDataSet(barEntries,"Qty");
barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
barData= new BarData(duration,barDataSet);
barChart.setData(barData);
barChart.animateY(3000);
output as like bellow
enter image description here

Move j++ inside while loop like this,
while(iterator.hasNext()){
String key = (String)iterator.next();
String issue = jsonChildNode.optString(key) ;
Float val=parseFloat(issue);
duration.add(key);
barEntries.add(new BarEntry(val,j));
j++;
}

Related

For loop last item issue

I am using a for loop in another for loop, Problem is I want Left options, Right Options and answers, but only getting last item of each. how will I get that? Please help me out
for (int i = 0; i < jsonArray.length(); i++) {
DataModel model = new DataModel();
jsonObject = jsonArray.getJSONObject(i);
LeftOpts = jsonObject.getString("LQUESTION");
RightOpts = jsonObject.getString("RQUESTION");
Ans = jsonObject.getString("TRUE_ANS");
String[] leftItem = LeftOpts.split("#");
String[] rightItem = RightOpts.split("#");
String[] ansItem = Ans.split("#");
for (int j = 0; j < leftItem.length; j++) {
item1 = leftItem[j];
Log.v("Left", item1);
model.setLOpt1(item1);
item2 = rightItem[j];
model.setROpt1(item2);
item3 = ansItem[j];
model.setAns1(item3);
myList.add(model);
}
You could try this:
for (int j = 0; j < leftItem.length; j++) {
DataModel model = new DataModel(); //move to here
item1 = leftItem[j];
Log.v("Left", item1);
model.setLOpt1(item1);
item2 = rightItem[j];
model.setROpt1(item2);
item3 = ansItem[j];
model.setAns1(item3);
myList.add(model);
}

PieDataSet cannot be applied to String in MPAndroidChart

i want make PieChart with library MPAndroidChart from github and i follow instruction on google or youtube, but i'm stuck in this code
private void addData(){
ArrayList<Entry> yVals1 = new ArrayList<Entry>();
for (int i = 0; i < yData.length; i++)
yVals1.add(new Entry(yData[i], i));
ArrayList<String> xVals = new ArrayList<String>();
for (int i = 0; i < xData.length; i++)
xVals.add(xData[i]);
PieDataSet dataSet = new PieDataSet(yVals1, "Market Share");
dataSet.setSliceSpace(3);
dataSet.setSelectionShift(5);
}
in
PieDataSet dataSet = new PieDataSet(yVals1, "Market Share");
I got warn, PieDataSet Cannot Applied to String. I don't know whats wrong because in tutorial is same with me
for pie chart with MPCharts, I've used this snippet
protected PieData generatePieData() {
int count = 10;
ArrayList<PieEntry> entries1 = new ArrayList<PieEntry>();
for(int i = 0; i < count; i++) {
entries1.add(new PieEntry( (float) salary[i], months[i]));
}
PieDataSet ds1 = new PieDataSet(entries1, "Monthly Salary 2017");
ds1.setColors(ColorTemplate.JOYFUL_COLORS);
ds1.setSliceSpace(2f);
ds1.setValueTextColor(Color.WHITE);
ds1.setValueTextSize(10f);
ds1.setSliceSpace(5f);
return new PieData(ds1);
}
where salary, and months are array
I think you need to specify the ArrayList as PieEntry?
ArrayList<PieEntry> yVals1 = new ArrayList<PieEntry>();
EDIT:
I think this is what you might need?;
ArrayList<String> xVals = new ArrayList<String>();
for (int i = 0; i < xData.length; i++){
xVals.add(xData[i]);
}
ArrayList<PieEntry> yVals1 = new ArrayList<PieEntry>();
for (int i = 0; i < yData.length; i++){
yVals1.add(new Entry(yData[i], xData[i]));
}

Create a arraylist from output of a for loop

I am trying to get a data set as [{a,b,c,d} {e,f,g,h}] but I'm getting it as [a,b,c,d,e,f,g,h] using below mentioned code. What should I do to achieve that. any help will be highly appreciated. I'm new to android so please be kind enough to answer me. thanks in advance
List<String> CartItemEntityList = new ArrayList<String>();
for (int i = 0; i < mCartList.size(); i++) {
if (!mCartList.isEmpty()) {
Product product = mCartList.get(i);
int quantity = product.quantity;
double subTotal = product.subTotal;
CartItemEntity = product.items;
for (int j = 0; j < CartItemEntity.size(); j++) {
Item item = CartItemEntity.get(j);
subMenuCode = item.subMenuCode;
mainMenuCode = item.mainMenuCode;
price = item.price;
CartItemEntityList.add(mainMenuCode);
CartItemEntityList.add(String.valueOf(price));
CartItemEntityList.add(String.valueOf(quantity));
CartItemEntityList.add(subMenuCode);
}
}
System.out.println("CartItemEntityList " + CartItemEntityList);
}
I was able to achieve this using json serialization and this is my answer
JSONObject jsonObj = new JSONObject();
jsonObj.put("MainMenuCode",
item.getMainMenuCode());
jsonObj.put("Price", item.getPrice());
jsonObj.put("Quantity", product.getQuantity());
jsonObj.put("SubMenuCode",
item.getSubMenuCode());
String a = jsonObj.toString();
CartIddtemEntityList.add(a);
you have to create array of arraylist.
Try something like below:
ArrayList<ArrayList<String>> group = new ArrayList<ArrayList<String>>(mCartList.size());
for (int i = 0; i < mCartList.size(); i++) {
List<String> CartItemEntityList = new ArrayList<String>();
if (!mCartList.isEmpty()) {
Product product = mCartList.get(i);
int quantity = product.quantity;
double subTotal = product.subTotal;
CartItemEntity = product.items;
for (int j = 0; j < CartItemEntity.size(); j++) {
Item item = CartItemEntity.get(j);
subMenuCode = item.subMenuCode;
mainMenuCode = item.mainMenuCode;
price = item.price;
CartItemEntityList.add(mainMenuCode);
CartItemEntityList.add(String.valueOf(price));
CartItemEntityList.add(String.valueOf(quantity));
CartItemEntityList.add(subMenuCode);
}
}
System.out.println("CartItemEntityList " + CartItemEntityList);
group.add(CartItemEntityList);
}

Android get elements from json array

I'm trying to get elements from my json array.
this is my json response:
{"IDs":["635426812801493839","635429094450867472","635433640807558204"]}
This is what I've tried so far:
itemList = new ArrayList<HashMap<String, String>>();
JSONArray a = jsonObj.getJSONArray(Constants.IDS);
int arrSize = a.length();
ArrayList<String> stringArray = new ArrayList<String>();
for (int i = 0; i < arrSize; ++i) {
JSONObject obj = a.getJSONObject(i);
stringArray.add(obj.toString());
item = new HashMap<String, String>();
item.put(Constants.ID, obj.toString());
itemList.add(item);
}
Log.e("ARR COUNT", "" + stringArray.size());
But I'm getting empty list. What is wrong with my code? Any help will be truly appreciated. Thanks.
The for loop should be
for (int i = 0; i < arrSize; ++i) {
stringArray.add(a.getString(i));
your the JSONArray contains already string
JSONObject obj = a.getJSONObject(i);
replace with
String str = a.getString(i);
Use this
itemList = new ArrayList<HashMap<String, String>>();
JSONArray a = jsonObj.getJSONArray(Constants.IDS);
int arrSize = a.length();
ArrayList<String> stringArray = new ArrayList<String>();
for (int i = 0; i < arrSize; ++i) {
stringArray.add(a.getString(i));
item = new HashMap<String, String>();
item.put(Constants.ID, obj.toString());
itemList.add(item);
}
Log.e("ARR COUNT", "" + stringArray.size());

MPAndroidChart BarChart Grouped DataSets

I am using the MPAndroidChart library release v2. I'm trying to show 3 bars, with data taken from the database.
Unfortunately, instead of seeing 3 bars with their data, viewing them with the same result. See the screenshot. Thanks for your help.
int [] x = {1,2,3};
Cursor c = db.rawQuery(sql, null);
int count = c.getCount();
float value1 ;
float value2 ;
float value3 ;
String[] mesi = new String[count];
for(int n=0; n<count; n++) {
c.moveToNext();
mesi[n]= c.getString(0);
value1 = c.getFloat(1);
value2 = c.getFloat(2);
value3 = c.getFloat(3);
ArrayList<String> xVals = new ArrayList<String>();
for (int i = 0; i <x.length; i++) {
xVals.add(x.length + " " + mChart.getUnit());
}
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
ArrayList<BarEntry> yVals2 = new ArrayList<BarEntry>();
ArrayList<BarEntry> yVals3 = new ArrayList<BarEntry>();
for (int i = 0; i < x.length; i++) {
yVals1.add(new BarEntry(value1, i));
}
for (int i = 0; i < x.length; i++) {
yVals2.add(new BarEntry(value2, i));
}
for (int i = 0; i < x.length; i++) {
yVals3.add(new BarEntry(value3, i));
}
// create 3 datasets with different types
BarDataSet set1 = new BarDataSet(yVals1, "Company A");
set1.setColor(Color.rgb(104, 241, 175));
BarDataSet set2 = new BarDataSet(yVals2, "Company B");
set2.setColor(Color.rgb(164, 228, 251));
BarDataSet set3 = new BarDataSet(yVals3, "Company C");
set3.setColor(Color.rgb(242, 247, 158));
ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
dataSets.add(set1);
dataSets.add(set2);
dataSets.add(set3);
BarData data = new BarData(xVals, dataSets);
// add space between the dataset groups in percent of bar-width
data.setGroupSpace(0);
mChart.setData(data);
mChart.invalidate();
}
c.close();
db.close();
}
the result
Did you make a logcat output of the values the chart should display?
Maybe they are not stored correctly in the database.
Your code seems correct to me.
You could also test it by simply providing a predefined value like e.g. 50 for all bars and see if it is plotted correctly.
To change the color call:
BarDataSet.setColor(...);
UPDATE: There is now a very detailed tutorial on how to create grouped BarCharts available on the official GitHub page of the library, based on release v3.0.0: Grouped BarChart tutorial

Categories

Resources