MPAndroidChart - First and last bars not rendering correctly

匿名 (未验证) 提交于 2019-12-03 02:24:01

问题:

  1. In the bar chart I am creating the first and last rows are consistently being cut in half (even if I add additional bars). This also causes the values above the bars to be out of place. I am inflating this within a fragment.

  2. The axis are also only increasing by 0.9 instead of 1. To fix this do I need to implement the AxisValueFormatter interface?

Image:

Code: .java

chart = (BarChart) view.findViewById(R.id.chart1);  // Chart settings chart.setDrawGridBackground(false); chart.setHighlightFullBarEnabled(true); chart.setDrawBarShadow(false); chart.setDrawValueAboveBar(true); chart.setDescription("");  // Settings for X-Axis XAxis xAxis = chart.getXAxis(); xAxis.setDrawGridLines(false); xAxis.setEnabled(true); xAxis.setDrawLabels(true); xAxis.setPosition(XAxisPosition.BOTTOM);  // Settings for Y-Axis YAxis leftAxis = chart.getAxisLeft(); YAxis rightAxis = chart.getAxisRight();  leftAxis.setAxisMinValue(0f); rightAxis.setAxisMinValue(0f);  BARENTRY = new ArrayList<>();  BarEntryLabels = new ArrayList<String>();  BARENTRY.add(new BarEntry(2f, 1)); BARENTRY.add(new BarEntry(3f, 2)); BARENTRY.add(new BarEntry(4f, 3)); BARENTRY.add(new BarEntry(5f, 4)); BARENTRY.add(new BarEntry(6f, 5));  Bardataset = new BarDataSet(BARENTRY, "Projects");  Bardataset.setColors(ColorTemplate.COLORFUL_COLORS);  BARDATA = new BarData(Bardataset);  chart.setData(BARDATA); chart.invalidate(); // refresh  chart.animateY(1500);  return view; 

回答1:

To answer your questions:

  1. In order to properly show your bars you need to fit the bars, like this (don't ask me why it's not enabled by default):

    chart.setFitBars(true) 

    You can find more information in the BarChart javadocs:

    setFitBars(boolean enabled): Adds half of the bar width to each side of the x-axis range in order to allow the bars of the barchart to be fully displayed.

    If you have a CombinedChart you could use setSpaceMin() and setSpaceMax() to add additional spacing on both ends of the axis:

    XAxis xAxis = chart.getXAxis(); xAxis.setSpaceMin(barData.getBarWidth() / 2f); xAxis.setSpaceMax(barData.getBarWidth() / 2f); 
  2. It is currently impossible to infuence the value positions rendered on the axis. Creating a ValueFormatter only changes the displayed text, not the actual position of the label.



回答2:

Please use setAxisMinimum and setAxisMaximum, it will work perfectly!

After your BarData is instantiated, you just need

chart.getXAxis().setAxisMinimum(-data.getBarWidth() / 2); chart.getXAxis().setAxisMaximum(count-data.getBarWidth() / 2);` 

There is no need for setFitBars, and the count is BARENTRY size.



回答3:

If the problem is with the first bar only, you can use negative values for the axis minimum:

xAxis.setAxisMinimum(-0.5f); 

Like in the answer to this question here:



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!