Thursday, 25 February 2016

Chart in Android

Hi!

To handle the chart in android will differ based on the library. I have developed reusable .AAR file to handle your chart on existing available Github library.

By using this .AAR, you can reduce your code and RD timing. simply pass the parameters to display on chart. It will give you a result on Pie,Bar & Line chart.

Download sample APK to test the output.

Steps To Do:

  • Download this .aar file,
  • Import as .aar file into your workspace,
  • Add this .aar file into your project and synchronize your project,
  • Reuse the available classes on any of your projects.


To call the Pie-Chart

public class Pi_Crt extends ActionBarActivity {

// call the xml chart 
PieChart xml_chart;
// call / create object for Pie-Chart
Pie_Chart P_Chart; // Array list for values ArrayList<Entry> dataSets_2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getSupportActionBar().hide(); setContentView(R.layout.activity_pi__crt);
        xml_chart = (PieChart) findViewById(R.id.xml_pie_chart);
        P_Chart = new Pie_Chart();
// Array List for values
        dataSets_2 = new ArrayList<>();
// Label to display
        P_Chart.Top_Title.add("NSE");
        P_Chart.Top_Title.add("BSE");
        P_Chart.Top_Title.add("NFT");
// Value to show on position
        dataSets_2.add(new Entry(Float.valueOf("100"), 0));
        dataSets_2.add(new Entry(Float.valueOf("380"), 1));
        dataSets_2.add(new Entry(Float.valueOf("625"), 2));
// Fix on chart
        P_Chart.set_Pie(xml_chart, dataSets_2);
    }
}

To call the Bar-Chart (Fallow the same Steps of Pie-Chart)

public class Bar_Crt extends ActionBarActivity {

    BarChart xml_chart;
    Bar_Chart l_Chart;

    ArrayList<Customer_Data_B> main_list;


    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_bar__crt);
        xml_chart = (BarChart) findViewById(R.id.xml_bar_chart);
        l_Chart = new Bar_Chart();

        main_list = new ArrayList<>();

        l_Chart.Top_Title.add("NSE");
        l_Chart.Top_Title.add("BSE");
        l_Chart.Top_Title.add("NFT");

        main_list.add(new Customer_Data_B("200", "350", "480"));

        ArrayList<BarEntry> valueSet1;
        ArrayList<BarEntry> valueSet2;
        ArrayList<BarEntry> valueSet3;

        valueSet1 = new ArrayList<>();
        valueSet2 = new ArrayList<>();
        valueSet3 = new ArrayList<>();


        BarEntry v_Set_1 = new BarEntry(Float.valueOf(main_list.get(0).getMarker_Rate()), 0);
        BarEntry v_Set_2 = new BarEntry(Float.valueOf(main_list.get(0).getActual_Rate()), 1);
        BarEntry v_Set_3 = new BarEntry(Float.valueOf(main_list.get(0).getExpected_Rate()), 2);

        valueSet1.add(v_Set_1);
        valueSet2.add(v_Set_2);
        valueSet3.add(v_Set_3);

        BarDataSet B1 = new BarDataSet(valueSet1, "Expected Value");
        B1.setColor(Color.rgb(255, 102, 0));
        BarDataSet B2  = new BarDataSet(valueSet2, "Actual Value");
        B2.setColor(Color.rgb(106, 150, 31));
        BarDataSet B3  = new BarDataSet(valueSet3, "Market Value");
        B3.setColor(Color.rgb(215, 12, 100));

        l_Chart.dataSets.add(B1);
        l_Chart.dataSets.add(B2);
        l_Chart.dataSets.add(B3);
        l_Chart.set_Bar(xml_chart);
    }
}

To call the Line-Chart (Fallow the same Steps of Pie-Chart)

public class Line_Crt extends ActionBarActivity {

    LineChart xml_chart;
    Line_Chart l_Chart;

    ArrayList<Sales_Revenue_B> main_list;


    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_line__crt);
        xml_chart = (LineChart) findViewById(R.id.xml_line_chart);
        l_Chart = new Line_Chart();

        main_list = new ArrayList<>();

        l_Chart.Top_Title.add("3 Months");
        l_Chart.Top_Title.add("6 Months");
        l_Chart.Top_Title.add("9 Months");
        l_Chart.Top_Title.add("1 Year");
        l_Chart.Top_Title.add("2 Years");

        l_Chart.Lower_Title.add("NSE");
        l_Chart.Lower_Title.add("BSE");
        l_Chart.Lower_Title.add("NFT");

        main_list.add(new Sales_Revenue_B("100", "200", "350", "480", "850"));
        main_list.add(new Sales_Revenue_B("50", "125", "480", "520", "700"));
        main_list.add(new Sales_Revenue_B("150", "600", "800", "920", "1000"));


        ArrayList<Entry> Entry_List = null;

        for (int i = 0; i < main_list.size(); i++) {

            Entry_List = new ArrayList<>();
            BarEntry ThreeMonthRevenue_val = new BarEntry(Float.valueOf(main_list.get(i).getThreeMonthRevenue()), 0);
            BarEntry SixMonthRevenue_val = new BarEntry(Float.valueOf(main_list.get(i).getSixMonthRevenue()), 1);
            BarEntry NineMonthRevenue_val = new BarEntry(Float.valueOf(main_list.get(i).getNineMonthRevenue()), 2);
            BarEntry OneYearRevenue_val = new BarEntry(Float.valueOf(main_list.get(i).getOneYearRevenue()), 3);
            BarEntry TwoYearsRevenue_val = new BarEntry(Float.valueOf(main_list.get(i).getTwoYearsRevenue()), 4);

            Entry_List.add(ThreeMonthRevenue_val);
            Entry_List.add(SixMonthRevenue_val);
            Entry_List.add(NineMonthRevenue_val);
            Entry_List.add(OneYearRevenue_val);
            Entry_List.add(TwoYearsRevenue_val);

            l_Chart.Bar_Set_ = new LineDataSet(Entry_List, l_Chart.set_indicator(i));
            l_Chart.set_Range(i);
            l_Chart.dataSets.add(l_Chart.Bar_Set_);
        }
        l_Chart.set_line(xml_chart);

    }
}


On your XML


To call Pie-Chart

<com.github.mikephil.charting.charts.PieChart    android:id="@+id/xml_pie_chart"    android:layout_width="match_parent"    android:layout_height="match_parent" />


To call Bar-Chart

<com.github.mikephil.charting.charts.BarChart    android:id="@+id/xml_bar_chart"    android:layout_width="match_parent"    android:layout_height="match_parent" />


To call Line-Chart

<com.github.mikephil.charting.charts.LineChart    android:id="@+id/xml_line_chart"    android:layout_width="match_parent"    android:layout_height="match_parent" />


Thats All!

Enjoy Your Code..,

Please Leave Your Comment..,



Have A Happy Day..,