I am having problem creating LineChart it's just showing the last Line when I need to show all Lines can you please help? here's the code:
Collections.addAll(labels,` column);
dataSets = new ArrayList<ILineDataSet>();
for (monthlysales company : companieslist) {
entries.clear();
for (int j = 0; j < listofcompanies.Total.size(); j++) {
entries.add(new Entry(Float.parseFloat(listofcompanies.Total.get(j)), j));
}
setComp1 = new LineDataSet(entries, company.StoreName);
setComp1.setAxisDependency(YAxis.AxisDependency.LEFT);
setComp1.setColor(Color.BLUE);
dataSets.add(setComp1);
}
LineData data = new LineData(column,dataSets);
linechart.setData(data);
linechart.setDescription("Sales");
linechart.animateXY(5000,5000);
linechart.setPinchZoom(true);
linechart.setDoubleTapToZoomEnabled(true);
linechart.setDragDecelerationEnabled(true);
linechart.notifyDataSetChanged();
linechart.invalidate();
}
Thank you
This actually makes sense. You are adding data to the entries list, and then add it to the DataSet correctly. The problem is, you are clearing the entries list every time after you add it. you should use a separate list for each dataset.
replace the line:
entries.clear();
with
List<Entry> entries = new ArrayList<>();
it's now solved by creating a Function and call it as below :
**dataSets.add(createLineChart(company,company.StoreName,company.Total));**
data = new LineData(column,dataSets);
linechart.setData(data);
linechart.invalidate();
linechart.setDescription("Sales");
and this is the function:
private LineDataSet createLineChart(monthlysales company,String storeName,List<String> listofcompanies){
// LineData data=new LineData();
ArrayList<Entry> entries= new ArrayList<Entry>();
for (int j = 0; j < listofcompanies.size(); j++) {
entries.add(new Entry(Float.parseFloat(listofcompanies.get(j)),j));
linechart.notifyDataSetChanged();
}
Random rd = new Random();
setComp1 = new LineDataSet(entries,storeName);
setComp1.setColor(Color.argb(255,rd.nextInt(256),rd.nextInt(256),rd.nextInt(256)));
// LineData data =new LineData(labels,dataset);
return setComp1;
}
it seems that the LineDataSet was used the last time it was called displaying just one line.
Related
I am trying to use arraylist.clear and adapter.notifyDataSetChanged() to clear an arraylist the list is cleared successfully but when i try to reload the list again with some data the list is not showing despite the arraylist.size showing the total number of items which is greater than 0. Here is the method to create the initial list
private void getTH(int ID) {
url = "url";
mainAdapterClasses = new ArrayList<>();
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, response -> {
try {
JSONObject jsonObject0 = new JSONObject(response);
String code = jsonObject0.getString("code");;
if (code.matches("200")) {
JSONArray jsonArray = jsonObject1.getJSONArray("items");
if(jsonArray.length() == 0){
txtEmpty.setText(String.format("%s %s", getString(R.string.no), getString(R.string.empty)));
} else {
for (int k = 0; k < jsonArray.length(); k++) {
JSONObject jsonObject = jsonArray.optJSONObject(k);
String na = jsonObject.optString("na", "NA");
String aT = jsonObject.optString("Type", "NA")
MainAdapterClass mainAdapterClass = new MainAdapterClass();
mainAdapterClass.setName(na);
mainAdapterClass.setType(aT);
mainAdapterClasses.add(mainAdapterClass);
}
mainAdapter = new TAdapter(WalletHome.this, mainAdapterClasses);
listView.setAdapter(mainAdapter);
mainAdapter.notifyDataSetChanged();
txtEmpty.setText("Data Is Here"+mainAdapterClasses.size());
}
}
} catch (JSONException e) {
Toast.makeText(this, ""+getString(R.string.unknown_err), Toast.LENGTH_SHORT).show();
}
})
requestQueue.add(stringRequest);
}
and here is the code that is supposes to reload the list
#Override
public void getRadioButtonListener1(int position) {
WClass wClass = stateClassArrayList.get(position);
wId = wClass.getWId();
if(mainAdapter.getCount() > 0){
mainAdapterClasses.clear();
mainAdapter.notifyDataSetChanged();
getTH(id);
}
}
I tried to log the issue here the method is getting the current total of items everytime i run the method the only issue is that the list is not getting populated with the new data once it is initially cleared.
txtEmpty.setText("Data Is Here"+mainAdapterClasses.size());
Any help will be greatly appreciated thanks
At first glance it looks like you are creating and populating the "MainAdapterClass" with the "na" and "aT" variables but not using them anywhere, so you are not really adding items to mainAdapterClasses array.
Second of all, in the for loop you are recreating and setting the adapter each time. I would advise creating and setting the adapter outside of the foor loop once you have your array (mainAdapterClasses) populated with the requested items.
for (int k = 0; k < jsonArray.length(); k++) {
JSONObject jsonObject = jsonArray.optJSONObject(k);
String na = jsonObject.optString("na", "NA");
String aT = jsonObject.optString("Type", "NA")
MainAdapterClass mainAdapterClass = new MainAdapterClass();
mainAdapterClass.setName(na);
mainAdapterClass.setType(aT);
mainAdapterClasses.add(mainAdapterClass);
} //for
mainAdapter = new TAdapter(WalletHome.this, mainAdapterClasses);
listView.setAdapter(mainAdapter);
mainAdapter.notifyDataSetChanged();
txtEmpty.setText("Data Is Here"+mainAdapterClasses.size());
You forgot to add mainAdapterClass to the ArrayList.
for (int k = 0; k < jsonArray.length(); k++) {
JSONObject jsonObject = jsonArray.optJSONObject(k);
String na = jsonObject.optString("na", "NA");
String aT = jsonObject.optString("Type", "NA")
MainAdapterClass mainAdapterClass = new MainAdapterClass();
mainAdapterClass.setName(na);
mainAdapterClass.setType(aT);
mainAdapterClasses.add(mainAdapterClass); // Add this line.
}
Don't setAdapter inside the loop. Just add items and then set the adapter outside the loop.
Also calling notifyDataSetChanged() after setting adapter doesn't make sence.
for (int i = 0; i < jsonArray.length(); i++) {
// ...
}
listview.setAdapter(mainAdapter);
I am looking to separate a single array into separate arrays based on gaps in the key. For example take this scenario:
I'm attempting to create separate datasets (arrays) for consecutive days of the month. If a day is missed a new dataset needs to be created starting with the next day that has a value.
The data is retrieved in one array like so:
[1:10, 2:8, 4:5, 5:12, 8:6, 9:10, 10:5, 11:4, 13:6, 14:5]
I would like to output:
[1:10, 2:8], [4:5, 5:12], [8:6, 9:10, 10:5, 11:4], [13:6, 14:5]
How would I achieve this?
I currently have this:
ArrayList<Entry> allValues = new ArrayList<>();
// Data Retrieval from the Server is Here (hidden for privacy)
// Each data entry contains key and value
// This is converted into a data model "Entry" which is essentially an x & y coordinate ( Entry(x,y) )
// and then added to the allValues List
List<ArrayList<Entry>> rawDataSets = new ArrayList<>();
ArrayList<Entry> tempDataSet = new ArrayList<>();
for(int i = 0; i < allValues.size(); i++){
Entry tempEntry = allValues.get(i);
if(i == tempEntry.getX()){
tempDataSet.add(tempEntry);
}else{
if(tempDataSet.size() > 0) {
rawDataSets.add(tempDataSet);
tempDataSet.clear();
}
}
}
Something like this should do trick:
ArrayList<Entry> allValues = new ArrayList<>();
// Assuming at this point that `allValues` is sorted in ascending order by X values.
// If necessary, it can be sorted with
//
// Collections.sort(allValues, Comparator.comparing(Entry::getX));
//
List<ArrayList<Entry>> rawDataSets = new ArrayList<>();
ArrayList<Entry> tempDataSet = new ArrayList<>();
for (Entry tempEntry : allValues){
if (!tempDataSet.isEmpty() &&
tempEntry.getX() != tempDataSet.get(tempDataSet.size()-1).getX() + 1)
{
// tempDataSet is not empty, and tempEntry's X is not
// consecutive with the X of tempDataSet's last entry, so it's
// it's time finish with the current tempDataSet and start fresh
// with a new one.
rawDataSets.add(tempDataSet);
tempDataSet = new ArrayList<>();
}
// Regardless of what happened, or didn't happen, with tempDataSet above,
// the current allValues entry now belongs with the current tempDataSet
tempDataSet.add(tempEntry);
}
// Now add any final non-empty tempDataSet (there will always be one if
// allValues wasn't empty) onto rawDataSets
if (!tempDataSet.isEmpty()) {
rawDataSets.add(tempDataSet);
}
After a number of attempts I think I found the solution, although I'm not sure if this is the most effective:
List<ArrayList<Entry>> rawDataSets = new ArrayList<>();
ArrayList<Entry> tempDataSet = new ArrayList<>();
for(int i = 0; i <= allValues.get(allValues.size() - 1).getX(); i++){
int matchedIndex = -1;
for(int j = 0; j < allValues.size(); j++){
if(allValues.get(j).getX() == i){
matchedIndex = j;
}
}
if(matchedIndex != -1) {
Entry tempEntry = allValues.get(matchedIndex);
tempDataSet.add(tempEntry);
} else {
if (tempDataSet.size() > 0) {
rawDataSets.add(tempDataSet);
tempDataSet = new ArrayList<>();
}
}
}
I want to generate some test data using this Java code:
#GetMapping("/volumes")
public ResponseEntity<List<DashboardDTO>> getProcessingVolumes() {
return ResponseEntity.ok(testDate());
}
public List<DashboardDTO> testDate() {
List<DashboardDTO> list = null;
for (int i = 0; i <= 10; i++) {
list = new ArrayList<>();
DashboardDTO obj = new DashboardDTO();
obj.setAmount(ThreadLocalRandom.current().nextInt(20, 500 + 1));
LocalDate localDate = LocalDate.now().minus(Period.ofDays((new Random().nextInt(365 * 70))));
Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
obj.setDate(date);
obj.setNumber_of_transactions(ThreadLocalRandom.current().nextInt(300, 5000 + 1));
list.add(obj);
}
return list;
}
But when the code is run only one object is generated. Do you know where I'm wrong? I want to generate 10 test objects.
Here:
for (int i = 0; i <= 10; i++) {
list = new ArrayList<>();
You create a new result list during each loop. So the last loop creates another list for the last entry!
Simply move that line list = new ArrayList<>(); in front of the loop, so that it gets executed just once.
Your code creates 11 new lists, each one with one entry, and you return that last list object. Instead: create one list and add your 11 elements and then return that single list.
for (int i = 0; i <= 10; i++) {
list = new ArrayList<>(); //(Fix here)--> resetting your list everytime causing only single object to return.
Try to initialize only single time.
List<DashboardDTO> list = new ArrayList<>();
for (int i = 0; i <= 10; i++) {
DashboardDTO obj = new DashboardDTO();
obj.setAmount(ThreadLocalRandom.current().nextInt(20, 500 + 1));
LocalDate localDate = LocalDate.now().minus(Period.ofDays((new Random().nextInt(365 * 70))));
Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
obj.setDate(date);
obj.setNumber_of_transactions(ThreadLocalRandom.current().nextInt(300, 5000 + 1));
list.add(obj);
}
return list;
I am using an HorizontalBarChart I want to draw a Label (the name of the vendor) inside or in the middle of the Bar, in the BarDaset there is something called setLabel but it's not working.
Here is my code:
private BarDataSet createLineChart(String storeName, List<String> listofcompanies){
ArrayList<BarEntry> entries= new ArrayList<BarEntry>();
for (int j = 0; j < listofcompanies.size(); j++) {
entries.add(new BarEntry(Float.parseFloat(listofcompanies.get(j)),j));
}
Random rd = new Random();
setComp1 = new BarDataSet(entries,storeName);
setComp1.setColor(Color.argb(255,rd.nextInt(256),rd.nextInt(256),rd.nextInt(256)));
setComp1.setDrawValues(true);
setComp1.setLabel(storeName);
setComp1.setHighlightEnabled(true);
setComp1.setDrawValues(true);
// LineData data =new LineData(labels,dataset);
return setComp1;
}
Try to use:
chart.getXAxis().setPosition(XAxisPosition.BOTTOM_INSIDE);
Or
chart.getXAxis().setPosition(XAxisPosition.TOP_INSIDE);
This will solve your problem.
I am very new to java and i am developing an inventory management system(Its my first project ).I want to add the column values sententiously when a new row is created. please any one help me how to do so..
i want to sum the line total and show it as the arrow is located..here is my code for table data.
int quantity, price;
Product p = new Product();
String[] result = new String[8];
String data[] = new String[7];
int i = 0;
result=p.getInfo(this.addItemField.getText());
for (String s : result) {
data[i] = s;
i += 1;
}
data[0] = "1";
quantity = Integer.parseInt(data[0]);
price = Integer.parseInt(data[5]);
int tPrice = price*quantity;
data[6] = Integer.toString(tPrice); //this is the field which i want to add for all row.
table.addRow(data);
this.addItemField.grabFocus();
addItemField.setText("");
Add a TableModelListener to the TableModel. When a TableModel event is generated you can loop through all the rows in the model and calculate a new total and then update your label.