如何在android中使用BarChart

时间:2018-06-02 11:33:22

标签: android

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view = inflater.inflate (R.layout.fragment_navhoo__earning__chart, container, false);
    chart = (BarChart) view.findViewById (R.id.barChart);
    chart.setDescription (null);
    chart.setPinchZoom (false);
    chart.setScaleEnabled (true);
    chart.setDrawBarShadow (false);
    chart.setDrawGridBackground (false);
    chart.setFitBars (true);
    //  int groupCount = 6;

    ArrayList xVals = new ArrayList();
    xVals.add ("");
    xVals.add("Sun");
    xVals.add("Mon");
    xVals.add("Tue");
    xVals.add("Wed");
    xVals.add("Thu");
    xVals.add("Fri");
    xVals.add("Sat");

    ArrayList yVals1 = new ArrayList();
    // ArrayList yVals2 = new ArrayList();

    yVals1.add(new BarEntry(1, (float) 1));
    // yVals2.add(new BarEntry(1, (float) 2));
    yVals1.add(new BarEntry(2, (float) 3));
    // yVals2.add(new BarEntry(2, (float) 4));
    yVals1.add(new BarEntry(3, (float) 5));
    // yVals2.add(new BarEntry(3, (float) 6));
    yVals1.add(new BarEntry(4, (float) 7));
    // yVals2.add(new BarEntry(4, (float) 8));
    yVals1.add(new BarEntry(5, (float) 9));
    // yVals2.add(new BarEntry(5, (float) 10));
    yVals1.add(new BarEntry(6, (float) 11));
    yVals1.add(new BarEntry(7, (float) 11));

    // yVals2.add(new BarEntry(6, (float) 12));

    BarDataSet set1;
    set1 = new BarDataSet(yVals1, "Navhoo Report");
    set1.setColor(Color.GREEN);
    //set2 = new BarDataSet(yVals2, "B");
    //set2.setColor(Color.BLUE);
    BarData data = new BarData(set1);
    data.setValueFormatter(new LargeValueFormatter ());
    chart.setData(data);
    chart.setFitBars (true);
    chart.getBarData().setBarWidth(barWidth);
    chart.getXAxis().setAxisMinimum(0);
    //chart.getXAxis().setAxisMaximum(0 + chart.getBarData().getIndexOfDataSet (set1));
    //chart.groupBars(0, groupSpace, barSpace);
    chart.getData().setHighlightEnabled(false);
    chart.invalidate();
    Legend l = chart.getLegend();
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
    l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
    l.setDrawInside(true);
    l.setYOffset(20f);
    l.setXOffset(10f);
    l.setYEntrySpace(0f);
    l.setTextSize(3f);
    XAxis xAxis = chart.getXAxis();
    xAxis.setGranularity(1f);
    xAxis.setGranularityEnabled(true);
    xAxis.setCenterAxisLabels(false);
    xAxis.setDrawGridLines(false);
    chart.setFitBars (true);
    xAxis.setAxisMaximum(8);
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setValueFormatter(new IndexAxisValueFormatter (xVals));
    //Y-axis
    chart.getAxisRight().setEnabled(false);
    YAxis leftAxis = chart.getAxisLeft();
    leftAxis.setValueFormatter(new LargeValueFormatter());
    leftAxis.setDrawGridLines(true);
    leftAxis.setSpaceTop(35f);
    leftAxis.setAxisMinimum(0f);

    return view;
}

}

0 个答案:

没有答案