Home Articles

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

Asked
Viewed 1925 times
-1

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

  • ArrayIndexOutOfBoundException错误 - 如何解决这个问题,

ATAL EXCEPTION:main进程:com.eengage.toit,PID:15219 java.lang.ArrayIndexOutOfBoundsException:length = 0;在com.github.mikephil.charting.buffer.BarBuffer.addBar(BarBuffer.java:37)的com.github上的com.github.mikephil.charting.buffer.HorizontalBarBuffer.feed(HorizontalBarBuffer.java:52)中索引= 0 . mikephil.charting.renderer.HorizontalBarChartRenderer.drawDataSet(HorizontalBarChartRenderer.java:66)at com.github.mikephil.charting.renderer.BarChartRenderer.drawData(BarChartRenderer.java:74)at com.github.mikephil.charting.charts.BarLineChartBase . 在Android.view.View.get.DisplayList(View.java)上的android.view.View.View.Start(View.java:13405)的android.view.View.draw(View.java:14508)上的onDraw(BarLineChartBase.java:247) :13447)在android.view.View.draw(View.java:14225)android.view.ViewGroup.drawChild(ViewGroup.java:3105)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2942)at android位于android.view.View.View(View.java:14225)的android.view.View.View(View.java:14225)上的android.view.View.View.View.View.View.Starts上的.view.View.getDisplayList(View.java:13400) . 在Android上的drawChild(ViewGroup.java:3105)在android.view.View.View.View.View.View.View.View.View.View上的android.view.View.getDisplayList(View.java:13447)的android.view.View.getDisplayList(View.java:13400)上的.view.ViewGroup.dispatchDraw(ViewGroup.java:2942) . 在Android.view.View.DisplayList(View.java)的android.view.ViewGroup.dispatchDraw(ViewGroup.java:2942)的android.view.ViewGroup.drawChild(ViewGroup.java:3105)上绘制(View.java:14225) :13400)在android.view.View.getDisplayList(View.java:13447)的android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3079)at android.view.View.getDisplayList(View.java:13338)at android .view.View.getDisplayList(View.java:13447)位于android.view.View.View.View.View.View.View上的android.view.View.get.getDisplayList(View.java:13338)上的android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3079) . getDisplayList(View.java:13447)位于android.view.View.get.目录中的android.view.View.get.getDisplayList(View.java:13338)上的android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3079) :13447)在android.view.ViewGroup.dispatchGetDisplayList(V iewGroup.java:3079)在android.view.View.DisplayList(View.java:13448)的android.view.View.DisplayList(View.java:13448)处于android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3079) )在android.view.View.get.DisplayList(View.java:13447)的android.view.View.get.DisplayList(View.java:13448)的android.view.View.View.DisplayList(ViewGroup.java:794) .View.getDisplayList(View.java:13338)位于android.view.View.get.目录中的android.view.View.View.View.View.View.DisplayList(View.java:13447) View.java:13338)在android.view.View.get.目录(View.java:13447)的android.view.View.View.View.Eava播放(View.java:13447) )在Android.view.View.View.DisplayList(View.java:13338)上的android.view.View.get.DisplayList(ViewGroup.java:30797)上的android.view.View.getDisplayList(View.java:13447) .View.getDisplayList(View.java:1 3447)在Android.view.View.get.getDisplayList(View.java:13447)的android.view.View.get.DisplayList(View.java:13338)上的android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3079) . 位于android.view.View.DisplayList(View.java:13338)的android.view.View.DisplayList(View.java:13448)上的view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3079),位于android.view.ViewGroup.dispatchGetDisplayList (ViewGroup.java:3079)位于android.view.HardwareRenderer的android.view.View.getDisplayList(View.java:13447)上的android.view.View.getDisplayList(View.java:13338)$(PriceRaderer) 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 .

  • 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) {
    }

}

}

  • 如何设置x轴和y轴值 .

BARENTRY1.add(new BarEntry(fp1,j)); BarEntryLabels1.add(“用户#”位置);

我在这里设定值,但我的x轴和y轴总是显示如下,并且fp1值没有设置 . 请找到截图
enter image description here

注意:

  • 如何设置x轴和y轴值?

  • 如何设置BarEntry值?

  • 如何将水平图表Bardataset顺序更改为反转?

2 Answers

  • 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);
    
        }
    }
    
  • 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);
    

Related