MPAndroidChart - 如何动态设置BarEntry中的值?

时间:2017-04-27 12:17:34

标签: android mpandroidchart

我正在使用MPAndroidChart。我可以使用静态内容。但是当我尝试使用API​​集成时。

  1. ArrayIndexOutOfBoundException错误 - 如何解决此问题,
  2.   

    ATAL EXCEPTION:主要                                                                     处理:com.eengage.toit,PID:15219                                                                     java.lang.ArrayIndexOutOfBoundsException:length = 0;索引= 0                                                                         在com.github.mikephil.charting.buffer.BarBuffer.addBar(BarBuffer.java:37)                                                                         在com.github.mikephil.charting.buffer.Horizo​​ntalBarBuffer.feed(Horizo​​ntalBarBuffer.java:52)                                                                         at com.github.mikephil.charting.renderer.Horizo​​ntalBarChartRenderer.drawDataSet(Horizo​​ntalBarChartRenderer.java:66)                                                                         at com.github.mikephil.charting.renderer.BarChartRenderer.drawData(BarChartRenderer.java:74)                                                                         at com.github.mikephil.charting.charts.BarLineChartBase.onDraw(BarLineChartBase.java:247)                                                                         在android.view.View.draw(View.java:14508)                                                                         在android.view.View.getDisplayList(View.java:13405)                                                                         在android.view.View.getDisplayList(View.java:13447)                                                                         在android.view.View.draw(View.java:14225)                                                                         在android.view.ViewGroup.drawChild(ViewGroup.java:3105)                                                                         在android.view.ViewGroup.dispatchDraw(ViewGroup.java:2942)                                                                         在android.view.View.getDisplayList(View.java:13400)                                                                         在android.view.View.getDisplayList(View.java:13447)                                                                         在android.view.View.draw(View.java:14225)                                                                         在android.view.ViewGroup.drawChild(ViewGroup.java:3105)                                                                         在android.view.ViewGroup.dispatchDraw(ViewGroup.java:2942)                                                                         在android.view.View.getDisplayList(View.java:13400)                                                                         在android.view.View.getDisplayList(View.java:13447)                                                                         在android.view.View.draw(View.java:14225)                                                                         在android.view.ViewGroup.drawChild(ViewGroup.java:3105)                                                                         在android.view.ViewGroup.dispatchDraw(ViewGroup.java:2942)                                                                         在android.view.View.getDisplayList(View.java:13400)                                                                         在android.view.View.getDisplayList(View.java:13447)                                                                         在android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3079)                                                                         在android.view.View.getDisplayList(View.java:13338)                                                                         在android.view.View.getDisplayList(View.java:13447)                                                                         在android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3079)                                                                         在android.view.View.getDisplayList(View.java:13338)                                                                         在android.view.View.getDisplayList(View.java:13447)                                                                         在android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3079)                                                                         在android.view.View.getDisplayList(View.java:13338)                                                                         在android.view.View.getDisplayList(View.java:13447)                                                                         在android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3079)                                                                         在android.view.View.getDisplayList(View.java:13338)                                                                         在android.view.View.getDisplayList(View.java:13447)                                                                         在android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3079)                                                                         在android.view.View.getDisplayList(View.java:13338)                                                                         在android.view.View.getDisplayList(View.java:13447)                                                                         在android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3079)                                                                         在android.view.View.getDisplayList(View.java:13338)                                                                         在android.view.View.getDisplayList(View.java:13447)                                                                         在android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3079)                                                                         在android.view.View.getDisplayList(View.java:13338)                                                                         在android.view.View.getDisplayList(View.java:13447)                                                                         在android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3079)                                                                         在android.view.View.getDisplayList(View.java:13338)                                                                         在android.view.View.getDisplayList(View.java:13447)                                                                         在android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3079)                                                                         在android.view.View.getDisplayList(View.java:13338)                                                                         在android.view.View.getDisplayList(View.java:13447)                                                                         在android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3079)                                                                         在android.view.View.getDisplayList(View.java:13338)                                                                         在android.view.View.getDisplayList(View.java:13447)                                                                         在android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3079)                                                                         在android.view.View.getDisplayList(View.java:13338)                                                                         在android.view.View.getDisplayList(View.java:13447)                                                                         在android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3079)                                                                         在android.view.View.getDisplayList(View.java:13338)                                                                         在android.view.View.getDisplayList(View.java:13447)                                                                         在android.view.HardwareRenderer $ GlRenderer.buildDisplayList(HardwareRenderer.java:1577)                                                                         在android.view.HardwareRenderer $ GlRenderer.draw(HardwareRenderer.java:1449)                                                                         在android.view.ViewRootImpl.draw(ViewRootImpl.java:2407)                                                                         在android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2279)                                                                       在android.view.ViewRootImpl.performTraversals(ViewRootImpl。

    1. API集成详细信息 - 将值添加到列表中是否有任何问题,

      class Top10UsersChartDetails extends AsyncTask<String, Void, String> {
      
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
      }
      
      @Override
      protected String doInBackground(String... params) {
          String response = null;
          try {
              response = apiDetails.Top10UsersChartFunc(params[0], params[1]);
          } catch (Exception e) {
      
          }
          return response;
      }
      
      @Override
      protected void onPostExecute(String s) {
          super.onPostExecute(s);
      
          try {
              JSONObject resObj = new JSONObject(s);
              String status = resObj.getString("status");
              String Month = resObj.getString("Month");
      
      
                  JSONArray userdataDetailsArray = resObj.getJSONArray("userdata");
      
                  for (int j = 0; j < userdataDetailsArray.length(); j++) {
                      JSONObject CampaignDetailsArrayObject = userdataDetailsArray.getJSONObject(j);
                      String userId = CampaignDetailsArrayObject.getString("userId");
                      String UserName = CampaignDetailsArrayObject.getString("UserName");
                      String CountRunning = CampaignDetailsArrayObject.getString("Count");
                      String position=CampaignDetailsArrayObject.getString("position");
                      float fp1 = Float.parseFloat(CountRunning);
      
                      BARENTRY1.add(new BarEntry(fp1,j));
                      BarEntryLabels1.add("User#"+position);
                  }
                  horizontalBarChart.notifyDataSetChanged();
                  horizontalBarChart.invalidate();
              } else {
              }
          } catch (Exception e) {
          }
      
      }
      

      }

      1. 如何设置x轴和y轴值。

        BARENTRY1.add(new BarEntry(fp1,j));                 BarEntryLabels1.add(&#34;用户#&#34 +位);

    2. 这里设置了值,但是我的x轴和y轴总是这样显示并且fp1值没有设置。请找到屏幕截图enter image description here

      注意:

      • 如何设置x轴和y轴值?
      • 如何设置BarEntry值?
      • 如何更改横向图表Bardataset订单以进行反转?

2 个答案:

答案 0 :(得分:0)

首先尝试格式化x轴和y轴,然后将值提供给条形图。以下代码可能有助于您理解。

这是轴格式化类。

/**
 * Created by aghatiki on 8/25/2016.
 */
public class MyAxisValueFormatter implements AxisValueFormatter {
    private String[] mValues;
    public MyAxisValueFormatter(String[] values)
    {
        this.mValues = values;
    }
    @Override
    public String getFormattedValue(float value, AxisBase axis) {
        return mValues[(int) value];
    }

    @Override
    public int getDecimalDigits() {
        return 0;
    }

这是将值添加到BarEntry的类。请看下面的内容:

public class MainActivity extends AppCompatActivity {

    BarChart barChart;
    String[] values = new String[]{"June","July","August","September","October","Novenber"};
    final String[] quarters = new String[] { "Q1", "Q2", "Q3", "Q4" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        barChart = (BarChart) findViewById(R.id.bargraph);

        ArrayList<BarEntry> barEntries = new ArrayList<>();
        barEntries.add(new BarEntry(0f,30f));
        barEntries.add(new BarEntry(1f,80f));
        barEntries.add(new BarEntry(2f,60f));
        barEntries.add(new BarEntry(3f,50f));
        barEntries.add(new BarEntry(4f,70f));
        barEntries.add(new BarEntry(5f,60f));
        BarDataSet barDataSet = new BarDataSet(barEntries,"Dates");

        ArrayList<String> theDates = new ArrayList<>();
        theDates.add("April");
        theDates.add("MAy");
        theDates.add("June");
        theDates.add("July");
        theDates.add("August");
        theDates.add("September");

        BarData theData = new BarData(barDataSet);
        theData.setBarWidth(0.9f);
        barChart.setData(theData);

        barChart.setTouchEnabled(true);
        barChart.setDragEnabled(true);
        barChart.setScaleEnabled(true);
        barChart.setFitBars(true);
        barChart.setDescription("Test data on the bar");

        XAxis xAxis = barChart.getXAxis();
        xAxis.setValueFormatter(new MyAxisValueFormatter(values));
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);

    }
}

答案 1 :(得分:0)

您需要在

下面执行以下代码
horizontalBarChart.notifyDataSetChanged();
        horizontalBarChart.invalidate();

Bardataset = new BarDataSet(BARENTRY1, "Projects");
                BARDATA = new BarData(BarEntryLabels1, Bardataset);
                Bardataset.setColors(new int[]{Color.parseColor("#701112")});
                horizontalBarChart.setData(BARDATA);
                horizontalBarChart.setDrawBarShadow(false);
                horizontalBarChart.setDrawValueAboveBar(true);
                horizontalBarChart.setPinchZoom(false);
                horizontalBarChart.setDrawGridBackground(false);
                horizontalBarChart.setDescription("");
                Bardataset.setBarSpacePercent(10f);
                Legend legend = horizontalBarChart.getLegend();
                legend.setEnabled(false);
                horizontalBarChart.setTouchEnabled(false);
                XAxis xAxis1 = horizontalBarChart.getXAxis();
                xAxis1.setPosition(XAxis.XAxisPosition.BOTTOM);
                xAxis1.setTextSize(8);
                xAxis1.setSpaceBetweenLabels(8);
                xAxis1.setTextColor(Color.parseColor("#701112"));
                xAxis1.setTypeface(tf);
                YAxis leftAxis = barChart.getAxisLeft();
                leftAxis.setEnabled(false);