Newer
Older
MpAndroidChart / MPChartExample / src / com / xxmassdeveloper / mpchartexample / MyHorizontalBarActivity.java
Bello on 6 Mar 2018 8 KB 横柱图
package com.xxmassdeveloper.mpchartexample;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;

import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.xxmassdeveloper.mpchartexample.view.HorizontalChartView;

import java.util.ArrayList;
import java.util.List;

/**
 * @Info
 * @Auth Bello
 * @Time 18-3-5 下午2:16
 * @Ver
 */

public class MyHorizontalBarActivity extends FragmentActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_horizontal_multi);

        HorizontalChartView scoreList = (HorizontalChartView) findViewById(R.id.my_chart);
        List<HorizontalChartView.Bar> barList = new ArrayList<>();
        barList.add(new HorizontalChartView.Bar(18, 25, "听力"));
        barList.add(new HorizontalChartView.Bar(15, 19, "语法"));
        barList.add(new HorizontalChartView.Bar(17, 29, "词法"));
        barList.add(new HorizontalChartView.Bar(20, 80, "阅读理解"));
        barList.add(new HorizontalChartView.Bar(10, 40, "说"));
        barList.add(new HorizontalChartView.Bar(18, 1, "听力"));

        scoreList.setBarList(barList);

        /*
        ArrayList<BarEntry> valueSet1 = new ArrayList<>();
        BarEntry v1e1 = new BarEntry(110.000f, 0); // Jan
        valueSet1.add(v1e1);
        BarEntry v1e2 = new BarEntry(40.000f, 1); // Feb
        valueSet1.add(v1e2);
        BarEntry v1e3 = new BarEntry(60.000f, 2); // Mar
        valueSet1.add(v1e3);
        BarEntry v1e4 = new BarEntry(30.000f, 3); // Apr
        valueSet1.add(v1e4);
        BarEntry v1e5 = new BarEntry(90.000f, 4); // May
        valueSet1.add(v1e5);
        BarEntry v1e6 = new BarEntry(100.000f, 5); // Jun
        valueSet1.add(v1e6);


        HorizontalBarChart chart = findViewById(R.id.chart1);

        ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
        final ArrayList<String> xVals = new ArrayList<String>();
        xVals.add("Jan");
        xVals.add("Feb");
        xVals.add("Mar");
        xVals.add("Apr");
        xVals.add("May");
        xVals.add("Jun");
        *//*for (int i = 0; i < listChart.size(); i++){
            BarEntry newBEntry = new BarEntry(i,listChart.get(i).getAmount());
            xVals.add(listChart.get(i).getAltName());
            yVals1.add(newBEntry);
        }*//*

        BarDataSet set1;
        set1 = new BarDataSet(valueSet1, "");
        set1.setColors(new int[] {Color.BLUE,Color.GREEN});
        ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
        dataSets.add(set1);
        BarData data = new BarData(dataSets);
        data.setValueTextSize(10f);
        chart.setData(data);

        chart.setDrawBarShadow(false);
        chart.setDrawValueAboveBar(true);

        chart.getDescription().setEnabled(false);
        chart.setMaxVisibleValueCount(60);
        // scaling can now only be done on x- and y-axis separately
        chart.setPinchZoom(true);
        chart.setDrawGridBackground(false);
        chart.setHighlightFullBarEnabled(false);
        chart.setHighlightPerDragEnabled(false);

        XAxis xAxis = chart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setGranularity(1f);
        *//*xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                if(Math.round(value) >= xVals.size()) {
                    return null;
                } else {
                    return xVals.get(Math.round(value));
                }
            }
        });*//*


        YAxis leftAxis = chart.getAxisLeft();
        leftAxis.setDrawGridLines(false);
        leftAxis.setLabelCount(8, false);
        leftAxis.setSpaceTop(15f);
        leftAxis.setDrawLabels(false);

        YAxis rightAxis = chart.getAxisRight();
        rightAxis.setLabelCount(8, false);
        rightAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);

        Legend l = chart.getLegend();
        l.setEnabled(false);

        chart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
            @Override
            public void onValueSelected(Entry entry, Highlight highlight) {

            }

            @Override
            public void onNothingSelected() {

            }
        });
*/

/*
        HorizontalBarChart chart = findViewById(R.id.chart1);
        BarData data = new BarData(getXAxisValues(), getDataSet());
        chart.setData(data);
        chart.setDescription("My Chart");
        chart.animateXY(2000, 2000);
        chart.invalidate();
*/



/*


        HorizontalBarChart creditDebitMonthlyBarChart  = findViewById(R.id.chart1);

//        creditDebitMonthlyBarChart.setDescription("desc");

        // if more than 60 entries are displayed in the chart, no values will be
        // drawn
        creditDebitMonthlyBarChart.setMaxVisibleValueCount(8);

        // scaling can now only be done on x- and y-axis separately
        creditDebitMonthlyBarChart.setPinchZoom(false);

        creditDebitMonthlyBarChart.setDrawBarShadow(false);
        creditDebitMonthlyBarChart.setDrawGridBackground(false);

        creditDebitMonthlyBarChart.getAxisLeft().setEnabled(false);
        creditDebitMonthlyBarChart.getAxisRight().setEnabled(false);

        creditDebitMonthlyBarChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);

        // add a nice and smooth animation
        creditDebitMonthlyBarChart.animateY(2000);
        creditDebitMonthlyBarChart.getLegend().setEnabled(false);

        ArrayList<BarEntry> barChartentriesCredit = new ArrayList<>();
        ArrayList<BarEntry> barChartentriesDebit = new ArrayList<>();

        //add some data into arraly list
        for (int i = 0; i < 10; i++) {
            barChartentriesDebit.add(new BarEntry(i+1, i));
            barChartentriesCredit.add(new BarEntry(i+2, i));
        }

        BarDataSet dataset1 = new BarDataSet(barChartentriesDebit, "data1");
        BarDataSet dataset2 = new BarDataSet(barChartentriesCredit, "data2");

        dataset1.setColors(new int[]{Color.rgb(56, 142, 60)});
        dataset2.setColors(new int[]{Color.rgb(211, 47, 47)});


        ArrayList<IBarDataSet> dataSets = new ArrayList<>();
        dataSets.add(dataset1);
        dataSets.add(dataset2);

        creditDebitMonthlyBarChart.setVisibility(View.VISIBLE);

        BarData data = new BarData(dataSets);
        data.setDrawValues(true);
        data.setValueTextSize(11f);
        data.setValueTextColor(Color.BLACK);

        creditDebitMonthlyBarChart.setData(data);
*/

    }

    private ArrayList<BarDataSet> getDataSet() {
        ArrayList<BarDataSet> dataSets = null;

        ArrayList<BarEntry> valueSet1 = new ArrayList<>();
        BarEntry v1e1 = new BarEntry(110.000f, 0); // Jan
        valueSet1.add(v1e1);
        BarEntry v1e2 = new BarEntry(40.000f, 1); // Feb
        valueSet1.add(v1e2);
        BarEntry v1e3 = new BarEntry(60.000f, 2); // Mar
        valueSet1.add(v1e3);
        BarEntry v1e4 = new BarEntry(30.000f, 3); // Apr
        valueSet1.add(v1e4);
        BarEntry v1e5 = new BarEntry(90.000f, 4); // May
        valueSet1.add(v1e5);
        BarEntry v1e6 = new BarEntry(100.000f, 5); // Jun
        valueSet1.add(v1e6);

        ArrayList<BarEntry> valueSet2 = new ArrayList<>();
        BarEntry v2e1 = new BarEntry(150.000f, 0); // Jan
        valueSet2.add(v2e1);
        BarEntry v2e2 = new BarEntry(90.000f, 1); // Feb
        valueSet2.add(v2e2);
        BarEntry v2e3 = new BarEntry(120.000f, 2); // Mar
        valueSet2.add(v2e3);
        BarEntry v2e4 = new BarEntry(60.000f, 3); // Apr
        valueSet2.add(v2e4);
        BarEntry v2e5 = new BarEntry(20.000f, 4); // May
        valueSet2.add(v2e5);
        BarEntry v2e6 = new BarEntry(80.000f, 5); // Jun
        valueSet2.add(v2e6);

        BarDataSet barDataSet1 = new BarDataSet(valueSet1, "Brand 1");
        barDataSet1.setColor(Color.rgb(0, 155, 0));
        BarDataSet barDataSet2 = new BarDataSet(valueSet2, "Brand 2");
        barDataSet2.setColors(ColorTemplate.COLORFUL_COLORS);

        dataSets = new ArrayList<>();
        dataSets.add(barDataSet1);
        dataSets.add(barDataSet2);
        return dataSets;
    }

    private ArrayList<String> getXAxisValues() {
        ArrayList<String> xAxis = new ArrayList<>();
        xAxis.add("JAN");
        xAxis.add("FEB");
        xAxis.add("MAR");
        xAxis.add("APR");
        xAxis.add("MAY");
        xAxis.add("JUN");
        return xAxis;
    }
}