I want to get xAxis from database which is result of distinct query so my chart can display data dynamically.
for static mpchart i have no problem with that. I called this function in onCreate.
private void drawChart(){ getChartData(); BarDataSet barDataSet1 = new BarDataSet(sehat(), "Sehat"); BarDataSet barDataSet2 = new BarDataSet(sakit(), "Sakit"); barDataSet1.setColor(Color.BLUE); barDataSet2.setColor(Color.RED); chart.getXAxis().setDrawGridLines(false); chart.getAxisLeft().setDrawGridLines(false); chart.getDescription().setEnabled(false); BarData barData = new BarData(barDataSet1, barDataSet2); chart.setData(barData); String[] area = new String[]{"UT PBRK", "POOL SLI", "UT TJR", "UT TRD", "UTR BATAKAN", "UT PBLP", "UT PBPN"}; XAxis xAxis = chart.getXAxis(); xAxis.setValueFormatter(new IndexAxisValueFormatter(area)); xAxis.setCenterAxisLabels(true); xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); xAxis.setGranularity(1); xAxis.setGranularityEnabled(true); chart.setDragEnabled(true); chart.setVisibleXRangeMaximum(3); float barSpace = 0.08f; float groupSpace = 0.54f; barData.setBarWidth(0.15f); chart.getXAxis().setAxisMinimum(0); chart.getXAxis().setAxisMaximum(0+chart.getBarData().getGroupWidth(groupSpace, barSpace)*7); chart.getAxisLeft().setAxisMinimum(0); chart.groupBars(0, groupSpace, barSpace); chart.invalidate(); } private ArrayList<BarEntry> sakit() { ArrayList<BarEntry> dataEntries = new ArrayList<>(); dataEntries.add(new BarEntry(1, 2)); dataEntries.add(new BarEntry(2, 1)); dataEntries.add(new BarEntry(3, 1)); dataEntries.add(new BarEntry(4, 4)); dataEntries.add(new BarEntry(5, 3)); dataEntries.add(new BarEntry(6, 7)); dataEntries.add(new BarEntry(7, 7)); return dataEntries; } private ArrayList<BarEntry> sehat() { ArrayList<BarEntry> dataEntries = new ArrayList<>(); dataEntries.add(new BarEntry(1, 10)); dataEntries.add(new BarEntry(2, 6)); dataEntries.add(new BarEntry(3, 8)); dataEntries.add(new BarEntry(4, 5)); dataEntries.add(new BarEntry(5, 3)); dataEntries.add(new BarEntry(6, 2)); dataEntries.add(new BarEntry(7, 2)); return dataEntries; }
this line which i to make dynamic.
String[] area = new String[]{"UT PBRK", "POOL SLI", "UT TJR", "UT TRD", "UTR BATAKAN", "UT PBLP", "UT PBPN"};
so far i’ve tried some experiment from what i found in google. this one is closer. Log.d display the result data. but when i use to String[] it doesn’t show in my chart.
public void getChartData(){ Call<List<Data>> call = userService.getDataArea(); call.enqueue(new Callback<List<Data>>() { @Override public void onResponse(Call<List<Data>> call, Response<List<Data>> response) { if(response.isSuccessful()){ List<Data> data = response.body(); for(Data get : data){ Log.d("testing", get.getArea()); area = new String[]{get.getArea()}; } } } @Override public void onFailure(Call<List<Data>> call, Throwable t) { } }); }
this is my json
[{ "area": "BP BLP / BPN" }, { "area": "AMK CPBP" }, { "area": "BP CPBP" }, { "area": "BP TJR" }, { "area": "UT PBIN" }, { "area": "UT PBIB" }, { "area": "UT PBLN" }, { "area": "UT PBPN" }, { "area": "UT HUB BPN" }, { "area": "UT PLTI" }, { "area": "SELOG BPN OFFICE" }, { "area": "UTR SUDIRMAN" }, { "area": "RBPC" }, { "area": "RBPF" }, { "area": "RBPM" }, { "area": "RBPS" }, { "area": "SAMBARATA" }, { "area": "TJR DEPO" }, { "area": "TJR SELF" }, { "area": "TRANSPORT" }, { "area": "UT BLP -SELF" }, { "area": "UT BRK" }, { "area": "DEPO TAM" }, { "area": "POLL SLI" }, { "area": "KRA MP" }, { "area": "UT PTRD" }, { "area": "UT PSBT" }, { "area": "KRA BP" }, { "area": "UTR BATAKAN" }, { "area": "UT PBLP" }, { "area": "Administrator" }]
I appreciate any help
Advertisement
Answer
See the below code carefully
for(Data get : data){ Log.d("testing", get.getArea()); area = new String[]{get.getArea()}; }
area = new String[]{get.getArea()}; method in for loop so every time it’s update area variable right so you final got only one value inside the area variable so please make changes as mentioned below in your code
Int i = 0; String[] area = new String[data.size()]; for(Data get : data){ Log.d("testing", get.getArea()); area[i] = get.getArea(); i++; }
I hope it’s helps you thanks