简体   繁体   中英

Getting repeated x-axis labels in barchart

How to set x-axis labels from web service? Getting repeated labels in x-axis..

在此处输入图片说明

Below is my code;

  final ArrayList xAxisValuesforHourly = new ArrayList();
                            for (int i = 0; i < jsonResult.getJSONObject("Data").getJSONArray("HourlyOffer").length(); i++) {
                                xAxisValuesforHourly.add(jsonResult.getJSONObject("Data").getJSONArray("HourlyOffer").getJSONObject(i).getString("Percentage"));
                            }

                            XAxis xAxis1 = half_hourly_bar_chart.getXAxis();
                            xAxis1.setGranularityEnabled(false);
                            xAxis1.setValueFormatter(new IAxisValueFormatter() {
                            @Override
                            public String getFormattedValue(float value, AxisBase axis) {
                                return String.valueOf(xAxisValuesforHourly.get((int) value));
                            }
                        });

Here I got my answer with dynamic data;

  ArrayList<String> horlyXaxis = new ArrayList<>();
  for (int i = 0; i < jsonResult.getJSONObject("Data").getJSONArray("HourlyOffer").length(); i++) {
 horlyXaxis.add(jsonResult.getJSONObject("Data").getJSONArray("HourlyOffer").getJSONObject(i).getString("Percentage"));
  }


  ArrayList<BarEntry> horlyYaxisUsers = new ArrayList<>();
  ArrayList<BarEntry> horlyYaxisRevenue = new ArrayList<>();

  for (int i = 0; i < jsonResult.getJSONObject("Data").getJSONArray("HourlyOffer").length(); i++) {
      horlyYaxisUsers.add(new BarEntry(Integer.parseInt(jsonResult.getJSONObject("Data").getJSONArray("HourlyOffer").getJSONObject(i).getString("UserCount")),
      (float) Integer.parseInt(jsonResult.getJSONObject("Data").getJSONArray("HourlyOffer").getJSONObject(i).getString("UserCount"))));
  }
  for (int i = 0; i < jsonResult.getJSONObject("Data").getJSONArray("HourlyOffer").length(); i++) {
      horlyYaxisRevenue.add(new BarEntry(Integer.parseInt(jsonResult.getJSONObject("Data").getJSONArray("HourlyOffer").getJSONObject(i).getString("OrderAmount")),
      (float) Integer.parseInt(jsonResult.getJSONObject("Data").getJSONArray("HourlyOffer").getJSONObject(i).getString("OrderAmount"))));
  }


 BarDataSet barDataSet1, barDataSet2;
 barDataSet1 = new BarDataSet(horlyYaxisUsers, "No of Users");
 barDataSet1.setColor(Color.RED);

 barDataSet2 = new BarDataSet(horlyYaxisRevenue, "Total Revenues($)");
 barDataSet2.setColor(Color.BLUE);
 BarData barData1 = new BarData(barDataSet1, barDataSet2);



 barWidth = 0.5f;
 barSpace = 0.00f;
 groupSpace = 0.0f;

 barData1.setValueFormatter(new LargeValueFormatter());
 half_hourly_bar_chart.setData(barData1);
 half_hourly_bar_chart.getBarData().setBarWidth(barWidth);
 half_hourly_bar_chart.getXAxis().setAxisMinimum(0);
 half_hourly_bar_chart.getXAxis().setAxisMaximum(horlyXaxis.size());
 half_hourly_bar_chart.groupBars(0, groupSpace, barSpace);

 half_hourly_bar_chart.getData().setHighlightEnabled(false);
 half_hourly_bar_chart.invalidate();

 Legend legend = half_hourly_bar_chart.getLegend();

 legend.setPosition(Legend.LegendPosition.BELOW_CHART_RIGHT);
 legend.setWordWrapEnabled(true);

 //X-axis
 XAxis xAxis1 = half_hourly_bar_chart.getXAxis();
 xAxis1.setGranularity(1f);
 xAxis1.setGranularityEnabled(true);
 xAxis1.setCenterAxisLabels(true);
 xAxis1.setLabelRotationAngle(0);
 xAxis1.setPosition(XAxis.XAxisPosition.BOTTOM);
 xAxis1.setValueFormatter(new IndexAxisValueFormatter(horlyXaxis));

 //Y-axis
 half_hourly_bar_chart.getAxisRight().setEnabled(false);
 YAxis axisLeft = half_hourly_bar_chart.getAxisLeft();
 axisLeft.setValueFormatter(new LargeValueFormatter());
 axisLeft.setDrawGridLines(false);
 axisLeft.setSpaceTop(35f);
 axisLeft.setAxisMinimum(0f);
 axisLeft.setGranularity(1.0f);
 axisLeft.setGranularityEnabled(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM