我正在使用mpandroidchart库处理分组Barchart:
public void drawing_length(String length)
{
ArrayList<String> xx = new ArrayList<String>();
ArrayList<String> yy = new ArrayList<String>();
ArrayList<String> zz = new ArrayList<String>();
int L = 7;
if (length.equals("7days"))
{
L = 7;
}
if (length.equals("14days"))
{
L = 14;
}
if (length.equals("30days"))
{
L = 30;
}
if (L > x.size())
{
L = x.size();
}
for (int g=0; g<L; g++)
{
xx.add(x.get(g)); // newest first, oldest last
yy.add(y.get(g));
zz.add(z.get(g));
}
Collections.reverse(xx); //oldest first, newest last, i.e. sequential
Collections.reverse(yy);
Collections.reverse(zz);
setupGrouppedChart(xx,yy,zz);
}
public void setupGrouppedChart(ArrayList<String> x, ArrayList<String> y1, ArrayList<String> y2)
{
float barWidth;
float barSpace;
float groupSpace;
barWidth = 0.3f;
barSpace = 0f;
groupSpace = 0.4f;
ll_combined_chart.setVisibility(View.GONE);
ll_scatteredchart.setVisibility(View.GONE);
ll_groupped_barchart.setVisibility(View.VISIBLE);
chart_grouppedBar = (BarChart) findViewById(R.id.chart_grouppedBar);
chart_grouppedBar.getDescription().setEnabled(false);
chart_grouppedBar.setBackgroundColor(Color.TRANSPARENT);
chart_grouppedBar.setDrawGridBackground(false);
chart_grouppedBar.setPinchZoom(false);
chart_grouppedBar.setScaleEnabled(false);
chart_grouppedBar.setDrawBarShadow(false);
chart_grouppedBar.setDrawValueAboveBar(false);
chart_grouppedBar.setHighlightFullBarEnabled(false);
chart_grouppedBar.setHighlightPerDragEnabled(false);
chart_grouppedBar.setHighlightPerTapEnabled(false);
chart_grouppedBar.setDoubleTapToZoomEnabled(false);
chart_grouppedBar.getXAxis().setDrawGridLines(false);
chart_grouppedBar.getAxisLeft().setDrawGridLines(false);
chart_grouppedBar.getAxisRight().setDrawGridLines(false);
chart_grouppedBar.getAxisRight().setEnabled(false);
chart_grouppedBar.animateXY(800, 800);
chart_grouppedBar.getDescription().setText("This is testing Description");
chart_grouppedBar.setVisibleXRangeMaximum(14f);
Utilities.custom_toast(Stat.this, "Xsize=" + x.size(), "gone", "short");
final String[] x_name = new String[x.size()];
for (int j = 0; j <x.size(); j++)
{
String[] temp = x.get(j).split("-");
x_name[j] = temp[0] + "/" + temp[1];
}
XAxis xLabels = chart_grouppedBar.getXAxis();
xLabels.setPosition(XAxis.XAxisPosition.BOTTOM);
xLabels.setGranularity(1f);
xLabels.setValueFormatter(new IAxisValueFormatter()
{
@Override
public String getFormattedValue(float value, AxisBase axis)
{
return x_name[(int) value % x_name.length];
}
});
BarDataSet set1, set2;
ArrayList<BarEntry> valueSet1 = new ArrayList<>();
ArrayList<BarEntry> valueSet2 = new ArrayList<>();
for (int i = 0; i < x_name.length; i++)
{
float val1 = Float.parseFloat(y1.get(i));
BarEntry v1e1 = new BarEntry(i, val1);
valueSet1.add(v1e1);
float val2 = Float.parseFloat(y2.get(i));
BarEntry v1e2 = new BarEntry(i, val2);
valueSet2.add(v1e2);
}
if (chart_grouppedBar.getData() != null && chart_grouppedBar.getData().getDataSetCount() > 0)
{
set1 = (BarDataSet) chart_grouppedBar.getData().getDataSetByIndex(0);
set2 = (BarDataSet) chart_grouppedBar.getData().getDataSetByIndex(1);
set1.setValues(valueSet1);
set2.setValues(valueSet2);
chart_grouppedBar.getData().setHighlightEnabled(false);
chart_grouppedBar.groupBars(0, groupSpace, barSpace);
chart_grouppedBar.getData().notifyDataChanged();
chart_grouppedBar.notifyDataSetChanged();
}
else
{
set1 = new BarDataSet(valueSet1, "A");
set1.setColor(getResources().getColor(R.color.pink_light));
set2 = new BarDataSet(valueSet2, "B");
set2.setColor(getResources().getColor(R.color.purple1));
BarData data = new BarData(set1, set2);
//data.setValueFormatter(new LargeValueFormatter());
//data.setValueTypeface(mTfLight);
data.setValueFormatter(new MyValueFormatter());
chart_grouppedBar.setData(data);
chart_grouppedBar.getBarData().setBarWidth(barWidth);
chart_grouppedBar.getXAxis().setAxisMaximum(0 + chart_grouppedBar.getBarData().getGroupWidth(groupSpace, barSpace) * x_name.length);
chart_grouppedBar.getAxisLeft().setAxisMinimum(0);
chart_grouppedBar.getAxisLeft().setValueFormatter(new MyYAxisValueFormatter());
chart_grouppedBar.getData().setHighlightEnabled(false);
chart_grouppedBar.groupBars(0, groupSpace, barSpace);
chart_grouppedBar.invalidate(); // refresh
}
}
谢谢。
答案 0 :(得分:2)
硬编码条宽是问题。找到条宽度动态如下
((1.00f- 0.20f) / barEntryCount) - 0.05f
其中
0.05f -> Bar Spacing;
0.20f -> Group Spacing;
关于X Axis Labels
(问题1)的对齐
喜欢以下
if (dataSets.size() > 1) {
barChart.getXAxis().setCenterAxisLabels(true);
else
barChart.getXAxis().setCenterAxisLabels(false);
关于第二个问题
请验证您的数据集计数是否为9.如果您在数据集中给出了9个元素,则会进行绘图。请检查其他视图属性,包括视图裁剪。
答案 1 :(得分:1)
第二个问题
图表怎么可以滚动?
chart_grouppedBar.setDragEnabled(true);
第三个问题
Y轴也是错误的。条的高度与条形不匹配 Y轴。
尝试设置Min&amp;轴和标签计数的最大值。例如:
chart_grouppedBar.getAxisLeft().setAxisMaxValue(MAX VALUE FROM YOUR DATASET);
chart_grouppedBar.getAxisLeft().setLabelCount(8, true);