我有这张图表,我用两种方式填充它:
mChart = (BarChart) findViewById(R.id.barChart);
mChart.setOnChartValueSelectedListener(this);
mChart.setDrawBarShadow(false);
mChart.setDrawValueAboveBar(true);
mChart.setDescription("");
mChart.setMaxVisibleValueCount(60);
mChart.setPinchZoom(false);
XAxis xAxis = mChart.getXAxis();
xAxis.setTypeface(mTf);
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setTypeface(mTf);
leftAxis.setLabelCount(8, false);
leftAxis.setPosition(YAxisLabelPosition.OUTSIDE_CHART);
leftAxis.setSpaceTop(15f);
leftAxis.setAxisMinValue(0f);
YAxis rightAxis = mChart.getAxisRight();
rightAxis.setDrawGridLines(false);
rightAxis.setTypeface(mTf);
rightAxis.setLabelCount(8, false);
rightAxis.setSpaceTop(15f);
rightAxis.setAxisMinValue(0f);
mChart.animateY(3500);
mChart.getLegend().setEnabled(false);
setData();
// WITH ONE DATASET
public void setData(){
ArrayList<String> xVals = new ArrayList<String>();
xVals.add("");
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
yVals1.add(new BarEntry(77,0));
yVals1.add(new BarEntry(56,1));
yVals1.add(new BarEntry(0,2));
yVals1.add(new BarEntry(3,3));
yVals1.add(new BarEntry(0,4));
yVals1.add(new BarEntry(18,5));
yVals1.add(new BarEntry(0,6));
BarDataSet set1 = new BarDataSet(yVals1, null);
set1.setColor(Color.rgb(31, 144, 225));
set1.setBarSpacePercent(50f);
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(set1);
BarData data = new BarData(xVals, dataSets);
data.setValueFormatter(new LargeValueFormatter());
data.setGroupSpace(50f);
data.setValueTextSize(10f);
data.setValueTypeface(mTf);
mChart.setData(data);
mChart.invalidate();
}
//WITH MULTIDATASET
public voi setData(){
ArrayList<String> xVals = new ArrayList<String>();
xVals.add("");
xVals.add("");
xVals.add("");
xVals.add("");
xVals.add("");
xVals.add("");
xVals.add("");
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
yVals1.add(new BarEntry(77,0));
ArrayList<BarEntry> yVals2 = new ArrayList<BarEntry>();
yVals2.add(new BarEntry(56,1));
ArrayList<BarEntry> yVals3 = new ArrayList<BarEntry>();
yVals3.add(new BarEntry(0,2));
ArrayList<BarEntry> yVals4 = new ArrayList<BarEntry>();
yVals4.add(new BarEntry(3,3));
ArrayList<BarEntry> yVals5 = new ArrayList<BarEntry>();
yVals5.add(new BarEntry(0,4));
ArrayList<BarEntry> yVals6 = new ArrayList<BarEntry>();
yVals6.add(new BarEntry(18,5));
ArrayList<BarEntry> yVals7 = new ArrayList<BarEntry>();
yVals7.add(new BarEntry(0,6));
BarDataSet set1 = new BarDataSet(yVals1, null);
set1.setColor(Color.rgb(31, 144, 225));
set1.setBarSpacePercent(50f);
BarDataSet set2 = new BarDataSet(yVals2, null);
set2.setColor(Color.rgb(255, 68, 68));
set2.setBarSpacePercent(50f);
BarDataSet set3 = new BarDataSet(yVals3, null);
set3.setColor(Color.rgb(255, 165, 0));
set3.setBarSpacePercent(50f);
BarDataSet set4 = new BarDataSet(yVals4, null);
set4.setColor(Color.rgb(255, 136, 0));
set4.setBarSpacePercent(50f);
BarDataSet set5 = new BarDataSet(yVals5, null);
set5.setColor(Color.rgb(204, 0, 0));
set5.setBarSpacePercent(50f);
BarDataSet set6 = new BarDataSet(yVals6, null);
set6.setColor(Color.rgb(169, 212, 40));
set6.setBarSpacePercent(50f);
BarDataSet set7 = new BarDataSet(yVals7, null);
set7.setColor(Color.rgb(102, 153, 0));
set7.setBarSpacePercent(50f);
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(set1);
dataSets.add(set2);
dataSets.add(set3);
dataSets.add(set4);
dataSets.add(set5);
dataSets.add(set6);
dataSets.add(set7);
BarData data = new BarData(xVals, dataSets);
data.setValueFormatter(new LargeValueFormatter());
data.setGroupSpace(50f);
data.setValueTextSize(10f);
data.setValueTypeface(mTf);
mChart.setData(data);
mChart.invalidate();
}
使用相同的数据值,条的宽度是不同的,如图所示。
如何改变这一点。
“PS:'传奇'由我控制。”
答案 0 :(得分:0)
通常,您可以使用以下方法定义条宽: data.setBarWidth(0.75f);