我正在尝试使用以下代码将MPAndroidChart添加到我的应用中,但我收到错误
BarData barData = new BarData(xData,barDataSet); ------此行
,错误是
BarData in BarData cannot be applied to (java.util.ArrayList <java.lang.String>,
com.github.mikephil.charting.data.BarDataSet)
如何解决此错误,请帮助
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab1, container, false);
TextView myTextView = (TextView)rootView.findViewById(R.id.textView5);
Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/slabo.ttf");
myTextView.setTypeface(typeface);
barChart = (BarChart)rootView.findViewById(R.id.chart);
float yValues [] = {10, 20, 30, 40, 50};
String xValues [] = {"first", "second", "third", "four", "five"};
drawBarGraph(yValues,xValues);
return rootView;
}
private void drawBarGraph(float yValues[], String []xValues){
ArrayList<BarEntry> yData = new ArrayList<>();
for (int i = 0; i < yValues.length; i++){
yData.add(new BarEntry(yValues[i],i));
}
ArrayList<String> xData = new ArrayList<>();
for(int i = 0; i < xValues.length; i++){
xData.add(xValues[i]);
}
BarDataSet barDataSet = new BarDataSet(yData, "Cells");
barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
BarData barData = new BarData(xData, barDataSet);
barData.setValueTextSize(13f);
barData.setValueTextColor(Color.BLACK);
barChart.setData(barData);
barChart.invalidate();
}
答案 0 :(得分:0)
1)我认为你反过来传递(x,y)
数据。它应该是Data.add(new BarEntry(i, yValues[i]));
2)您传递了BarData
的两个参数。它只能是一个。
barData = new BarData(barDataSet);
3)对于标签使用IAxisValueFormatter
并将标签之间的粒度设置为1f,这是设置{{时使用的X轴值(0,1,2,3,4)之间的差异1}} S上。
这是基于您的代码,MPCharts GitHub中的示例和this question on setting the labels for BarChart
的示例代码BarEntry