如何制作存储在firebase数据库中的数据图?

时间:2017-06-18 13:24:46

标签: java android android-studio graph mpandroidchart

我正在尝试使用以下代码将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();
}

1 个答案:

答案 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