public class PieChartView extends LinearLayout {

//Who ever wrote this, thanks for the laugh!

//while (true) {copy+paste}

//#lowerthanjuniorlevelskillz

private TextView mTVHeader ;

private PieChart mPieChart ;

private PieChartKeyListViewAdapter adapter ;

private final List < PieChartItem > itemsForList = new ArrayList < PieChartItem > ( 0 ) ;

private final List < PieChartItem > mParentPie = new ArrayList < PieChartItem > ( 0 ) ;

private final List < List < PieChartItem >> mPieChartList = new ArrayList < List < PieChartItem >> ( 0 ) ;

private String keyValue ;

/**

*

* @param context

* @param attrs

*/

public PieChartView ( Context context, AttributeSet attrs ) {

super ( context, attrs ) ;

final LayoutInflater inflater = ( LayoutInflater ) context. getSystemService ( Context . LAYOUT_INFLATER_SERVICE ) ;

final View v = inflater. inflate ( R. layout . layout_compound_pie_chart , this , true ) ;

// inflate ui components

mPieChart = ( PieChart ) v. findViewById ( R. id . pie_chart ) ;

mTVHeader = ( TextView ) v. findViewById ( R. id . pie_chart_header ) ;

// mTVKeyHeader = (TextView) v.findViewById(R.id.tv_key_header);

// mTVKeyValue = (TextView) v.findViewById(R.id.tv_key_value);

final ListView mKeyListView = ( ListView ) v. findViewById ( R. id . lv_key ) ;

adapter = new PieChartKeyListViewAdapter ( context, itemsForList ) ;

mKeyListView. setAdapter ( adapter ) ;

TypedArray a = context. getTheme ( ) . obtainStyledAttributes ( attrs, R. styleable . PieChartLayout , 0 , 0 ) ;

try {

// Retrieve the values from the TypedArray and store into

// fields of this class.

//

// The R.styleable.PieChart_* constants represent the index for

// each custom attribute in the R.styleable.PieChart array.

boolean isHeader = a. getBoolean ( R. styleable . PieChartLayout_showHeaderText , true ) ;

boolean isKeyChartCentered = a. getBoolean ( R. styleable . PieChartLayout_isKeyChartCentered , true ) ;

if ( ! isHeader ) {

mTVHeader. setVisibility ( View . GONE ) ;

} else {

mTVHeader. setVisibility ( View . VISIBLE ) ;

}

if ( isKeyChartCentered ) {

LinearLayout. LayoutParams params = new LinearLayout. LayoutParams ( 0 , LinearLayout. LayoutParams . WRAP_CONTENT ) ;

params. weight = 3 ;

params. gravity = Gravity. CENTER_VERTICAL ;

mKeyListView. setLayoutParams ( params ) ;

} else {

LinearLayout. LayoutParams params = new LinearLayout. LayoutParams ( 0 , LinearLayout. LayoutParams . WRAP_CONTENT ) ;

params. weight = 3 ;

params. gravity = Gravity. TOP ;

mKeyListView. setLayoutParams ( params ) ;

}

} finally {

// release the TypedArray so that it can be reused.

a. recycle ( ) ;

}

}

/**

* @return the mPieChart

*/

public PieChart getmPieChart ( ) {

return mPieChart ;

}

/**

*

* @param balanceData

*/

public void populatePieChart ( List < BalanceData > balanceData ) {

Resources res = getResources ( ) ;

// build pie slices

BigDecimal fTotal = BigDecimal . ZERO ;

PieChartItem item = new PieChartItem ( ) ;

for ( int index = 0 ; index < balanceData. size ( ) ; index ++ ) {

item = new PieChartItem ( ) ;

// get data object

BalanceData data = balanceData. get ( index ) ;

item. mLabel = data. getPortfolio ( ) ;

BigDecimal total = data. getCashBalance ( ) . add ( data. getInvestmentBalance ( ) ) ;

for ( ExchangeRate rate : < hidden > . getExchangeRates ( ) ) {

if ( rate. getCurrency ( ) . equals ( data. getCashBalanceCurrency ( ) ) ) {

total = total. multiply ( rate. getRate ( ) ) ;

}

}

// convert to current exchange rate

if ( < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) . compareTo ( new BigDecimal ( "0.0000" ) ) == 1 ) {

total = total. divide ( < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) , BigDecimal . ROUND_UP ) ;

}

item. mSliceValue = total ;

item. mValue = total. floatValue ( ) ;

Logger. debug ( "PCS" , "mValue - " + item. mValue + " floatValue() - " + total. floatValue ( ) ) ;

switch ( index ) {

case 0 :

item. mColor = res. getColor ( R. color . pcs_teal ) ;

mParentPie. add ( item ) ;

break ;

case 1 :

item. mColor = res. getColor ( R. color . pcs_air_force_blue ) ;

mParentPie. add ( item ) ;

break ;

case 2 :

item. mColor = res. getColor ( R. color . pcs_biscuit ) ;

mParentPie. add ( item ) ;

break ;

case 3 :

item. mColor = res. getColor ( R. color . pcs_pecan ) ;

mParentPie. add ( item ) ;

break ;

default :

fTotal = fTotal. add ( total ) ;

item. mLabel = "Other" ;

item. mColor = res. getColor ( R. color . pcs_summer_sky ) ;

break ;

}

}

if ( fTotal. compareTo ( BigDecimal . ZERO ) == 1 ) {

item. mSliceValue = fTotal ;

item. mValue = fTotal. floatValue ( ) ;

mParentPie. add ( item ) ;

}

}

/**

*

* @param balanceData

*/

public void populatePieChartWithCashVSInvestment ( List < BalanceData > balanceData ) {

Resources res = getResources ( ) ;

BigDecimal cashTotal = BigDecimal . ZERO ;

BigDecimal investmentTotal = BigDecimal . ZERO ;

for ( BalanceData data : balanceData ) {

BigDecimal cash = BigDecimal . ZERO ;

BigDecimal investment = BigDecimal . ZERO ;

// extract cash value

cash = data. getCashBalance ( ) ;

// investment value

investment = data. getInvestmentBalance ( ) ;

for ( ExchangeRate rate : < hidden > . getExchangeRates ( ) ) {

if ( rate. getCurrency ( ) . equals ( data. getCashBalanceCurrency ( ) ) ) {

cash = cash. multiply ( rate. getRate ( ) ) ;

investment = investment. multiply ( rate. getRate ( ) ) ;

}

}

if ( < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) . compareTo ( new BigDecimal ( "0.0000" ) ) == 1 ) {

// convert to current rate

cash = cash. divide ( < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) , BigDecimal . ROUND_UP ) ;

investment = investment. divide ( < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) , BigDecimal . ROUND_UP ) ;

}

cashTotal = cashTotal. add ( cash ) ;

investmentTotal = investmentTotal. add ( investment ) ;

}

// investment slice

PieChartItem item = new PieChartItem ( ) ;

item. mLabel = "Investment Value" ;

item. mSliceValue = investmentTotal ;

item. mValue = investmentTotal. floatValue ( ) ;

item. mColor = res. getColor ( R. color . pcs_teal ) ;

mParentPie. add ( item ) ;

// cash slice

item = new PieChartItem ( ) ;

item. mLabel = "Cash Value" ;

item. mSliceValue = cashTotal ;

item. mValue = cashTotal. floatValue ( ) ;

item. mColor = res. getColor ( R. color . pcs_biscuit ) ;

mParentPie. add ( item ) ;

}

/**

*

* @param balanceData

*/

public void populatePieChartWithLocalVSOffshore ( List < BalanceData > balanceData ) {

Resources res = getResources ( ) ;

BigDecimal localTotal = BigDecimal . ZERO ;

BigDecimal offshoreTotal = BigDecimal . ZERO ;

for ( BalanceData data : balanceData ) {

BigDecimal local = BigDecimal . ZERO ;

BigDecimal offshore = BigDecimal . ZERO ;

if ( data. getIsOffshore ( ) ) {

offshore = data. getCashBalance ( ) . add ( data. getInvestmentBalance ( ) ) ;

for ( ExchangeRate rate : < hidden > . getExchangeRates ( ) ) {

if ( rate. getCurrency ( ) . equals ( data. getCashBalanceCurrency ( ) ) ) {

offshore = offshore. multiply ( rate. getRate ( ) ) ;

}

}

} else if ( ! data. getIsOffshore ( ) ) {

local = data. getCashBalance ( ) . add ( data. getInvestmentBalance ( ) ) ;

for ( ExchangeRate rate : < hidden > . getExchangeRates ( ) ) {

if ( rate. getCurrency ( ) . equals ( data. getCashBalanceCurrency ( ) ) ) {

local = local. multiply ( rate. getRate ( ) ) ;

}

}

}

if ( < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) . compareTo ( new BigDecimal ( "0.0000" ) ) == 1 ) {

// convert offshore to current exchange rate

offshore = offshore. divide ( < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) , BigDecimal . ROUND_UP ) ;

// convert local to current exchange rate

local = local. divide ( < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) , BigDecimal . ROUND_UP ) ;

}

localTotal = localTotal. add ( local ) ;

offshoreTotal = offshoreTotal. add ( offshore ) ;

}

// investment slice

PieChartItem item = new PieChartItem ( ) ;

item. mLabel = "Offshore" ;

item. mSliceValue = offshoreTotal ;

item. mValue = offshoreTotal. floatValue ( ) ;

item. mColor = res. getColor ( R. color . pcs_teal ) ;

mParentPie. add ( item ) ;

// cash slice

item = new PieChartItem ( ) ;

item. mLabel = "Local" ;

item. mSliceValue = localTotal ;

item. mValue = localTotal. floatValue ( ) ;

item. mColor = res. getColor ( R. color . pcs_biscuit ) ;

mParentPie. add ( item ) ;

}

/**

*

* @param balanceData

*/

public void populatePieChartWithLocalPortfolios ( List < BalanceData > balanceData ) {

Resources res = getResources ( ) ;

// build pie slices

BigDecimal fTotal = BigDecimal . ZERO ;

PieChartItem item = new PieChartItem ( ) ;

for ( int index = 0 ; index < balanceData. size ( ) ; index ++ ) {

// get data object

BalanceData data = balanceData. get ( index ) ;

if ( ! data. getIsOffshore ( ) ) {

item = new PieChartItem ( ) ;

item. mLabel = data. getPortfolio ( ) ;

BigDecimal total = new BigDecimal ( 0.0 ) ;

total = data. getCashBalance ( ) . add ( data. getInvestmentBalance ( ) ) ;

for ( ExchangeRate rate : < hidden > . getExchangeRates ( ) ) {

if ( rate. getCurrency ( ) . equals ( data. getCashBalanceCurrency ( ) ) ) {

total = total. multiply ( rate. getRate ( ) ) ;

}

}

// convert to current rate

if ( < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) . compareTo ( new BigDecimal ( "0.0000" ) ) == 1 ) {

total = total. divide ( < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) , BigDecimal . ROUND_UP ) ;

}

item. mSliceValue = total ;

item. mValue = total. floatValue ( ) ;

switch ( index ) {

case 0 :

item. mColor = res. getColor ( R. color . pcs_teal ) ;

mParentPie. add ( item ) ;

break ;

case 1 :

item. mColor = res. getColor ( R. color . pcs_air_force_blue ) ;

mParentPie. add ( item ) ;

break ;

case 2 :

item. mColor = res. getColor ( R. color . pcs_biscuit ) ;

mParentPie. add ( item ) ;

break ;

case 3 :

item. mColor = res. getColor ( R. color . pcs_pecan ) ;

mParentPie. add ( item ) ;

break ;

default :

fTotal = fTotal. add ( total ) ;

item. mLabel = "Other" ;

item. mColor = res. getColor ( R. color . pcs_summer_sky ) ;

break ;

}

}

}

if ( fTotal. compareTo ( BigDecimal . ZERO ) == 1 ) {

item. mSliceValue = fTotal ;

item. mValue = fTotal. floatValue ( ) ;

mParentPie. add ( item ) ;

}

}

/**

*

* @param balanceData

*/

public void populatePieChartWithOffshorePortfolios ( List < BalanceData > balanceData ) {

Resources res = getResources ( ) ;

// build pie slices

BigDecimal fTotal = BigDecimal . ZERO ;

PieChartItem item = new PieChartItem ( ) ;

for ( int index = 0 ; index < balanceData. size ( ) ; index ++ ) {

// get data object

BalanceData data = balanceData. get ( index ) ;

if ( data. getIsOffshore ( ) ) {

item = new PieChartItem ( ) ;

item. mLabel = data. getPortfolio ( ) ;

BigDecimal total = new BigDecimal ( 0.0 ) ;

total = data. getCashBalance ( ) . add ( data. getInvestmentBalance ( ) ) ;

for ( ExchangeRate rate : < hidden > . getExchangeRates ( ) ) {

if ( rate. getCurrency ( ) . equals ( data. getCashBalanceCurrency ( ) ) ) {

total = total. multiply ( rate. getRate ( ) ) ;

}

}

if ( < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) . compareTo ( new BigDecimal ( "0.0000" ) ) == 1 ) {

total = total. divide ( < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) , BigDecimal . ROUND_UP ) ;

}

item. mSliceValue = total ;

item. mValue = total. floatValue ( ) ;

switch ( index ) {

case 0 :

item. mColor = res. getColor ( R. color . pcs_teal ) ;

mParentPie. add ( item ) ;

break ;

case 1 :

item. mColor = res. getColor ( R. color . pcs_air_force_blue ) ;

mParentPie. add ( item ) ;

break ;

case 2 :

item. mColor = res. getColor ( R. color . pcs_biscuit ) ;

mParentPie. add ( item ) ;

break ;

case 3 :

item. mColor = res. getColor ( R. color . pcs_pecan ) ;

mParentPie. add ( item ) ;

break ;

default :

fTotal = fTotal. add ( total ) ;

item. mLabel = "Other" ;

item. mColor = res. getColor ( R. color . pcs_summer_sky ) ;

break ;

}

}

}

if ( fTotal. compareTo ( BigDecimal . ZERO ) == 1 ) {

item. mSliceValue = fTotal ;

item. mValue = fTotal. floatValue ( ) ;

mParentPie. add ( item ) ;

}

}

/**

*

* @param balanceData

*/

public void populatePieChartWithCurrencies ( List < BalanceData > balanceData ) {

Resources res = getResources ( ) ;

boolean isAdded = false ;

// build pie slices

BigDecimal fTotal = BigDecimal . ZERO ;

PieChartItem item = new PieChartItem ( ) ;

int count = 0 ;

for ( int index = 0 ; index < < hidden > . getExchangeRates ( ) . size ( ) ; index ++ ) {

ExchangeRate rate = < hidden > . getExchangeRates ( ) . get ( index ) ;

BigDecimal total = BigDecimal . ZERO ;

for ( BalanceData data : balanceData ) {

if ( data. getCashBalanceCurrency ( ) . equals ( rate. getCurrency ( ) ) ) {

isAdded = true ;

total = total. add ( data. getCashBalance ( ) . add ( data. getInvestmentBalance ( ) ) ) ;

}

}

if ( isAdded ) {

total = total. multiply ( rate. getRate ( ) ) ;

if ( < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) . compareTo ( new BigDecimal ( "0.0000" ) ) == 1 ) {

total = total. divide ( < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) , BigDecimal . ROUND_UP ) ;

}

item = new PieChartItem ( ) ;

item. mLabel = rate. getCurrency ( ) ;

item. mSliceValue = total ;

item. mValue = total. floatValue ( ) ;

switch ( count ) {

case 0 :

item. mColor = res. getColor ( R. color . pcs_teal ) ;

mParentPie. add ( item ) ;

break ;

case 1 :

item. mColor = res. getColor ( R. color . pcs_air_force_blue ) ;

mParentPie. add ( item ) ;

break ;

case 2 :

item. mColor = res. getColor ( R. color . pcs_biscuit ) ;

mParentPie. add ( item ) ;

break ;

case 3 :

item. mColor = res. getColor ( R. color . pcs_pecan ) ;

mParentPie. add ( item ) ;

break ;

default :

fTotal = fTotal. add ( total ) ;

item. mLabel = "Other" ;

item. mColor = res. getColor ( R. color . pcs_summer_sky ) ;

break ;

}

count ++;

isAdded = false ;

}

}

if ( fTotal. compareTo ( BigDecimal . ZERO ) == 1 ) {

item. mSliceValue = fTotal ;

item. mValue = fTotal. floatValue ( ) ;

mParentPie. add ( item ) ;

}

}

/**

*

* @param balanceData

*/

public void populateCashAndInvestments ( List < BalanceData > balanceData ) {

PieChartItem item ;

Resources res = getResources ( ) ;

for ( BalanceData data : balanceData ) {

BigDecimal cash = BigDecimal . ZERO ;

BigDecimal investment = BigDecimal . ZERO ;

List < PieChartItem > slices = new ArrayList < PieChartItem > ( 0 ) ;

if ( data. getIsOffshore ( ) ) { // offshore

cash = data. getCashBalance ( ) ;

investment = data. getInvestmentBalance ( ) ;

for ( ExchangeRate rate : < hidden > . getExchangeRates ( ) ) {

if ( rate. getCurrency ( ) . equals ( data. getCashBalanceCurrency ( ) ) ) {

cash = cash. multiply ( rate. getRate ( ) ) ;

investment = investment. multiply ( rate. getRate ( ) ) ;

}

}

} else if ( ! data. getIsOffshore ( ) ) { // local

cash = data. getCashBalance ( ) ;

investment = data. getInvestmentBalance ( ) ;

}

if ( < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) . compareTo ( new BigDecimal ( "0.0000" ) ) == 1 ) {

// convert to current rate

cash = cash. divide ( < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) , BigDecimal . ROUND_UP ) ;

investment = investment. divide ( < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) , BigDecimal . ROUND_UP ) ;

}

// investment slice

item = new PieChartItem ( ) ;

item. mLabel = res. getString ( R. string . pcs_investment ) ;

item. mSliceValue = investment ;

item. mValue = investment. floatValue ( ) ;

item. mColor = res. getColor ( R. color . pcs_teal ) ;

slices. add ( item ) ;

// cash slice

item = new PieChartItem ( ) ;

item. mLabel = res. getString ( R. string . pcs_cash ) ;

item. mValue = cash. floatValue ( ) ;

item. mSliceValue = cash ;

item. mColor = res. getColor ( R. color . pcs_biscuit ) ;

slices. add ( item ) ;

mPieChartList. add ( slices ) ;

}

}

/**

*

* @param balanceData

*/

public void populatePieChartWithCashPortfolios ( List < BalanceData > balanceData ) {

Resources res = getResources ( ) ;

// build pie slices

BigDecimal fTotal = BigDecimal . ZERO ;

PieChartItem item = new PieChartItem ( ) ;

for ( int index = 0 ; index < balanceData. size ( ) ; index ++ ) {

// get data object

BalanceData data = balanceData. get ( index ) ;

item = new PieChartItem ( ) ;

item. mLabel = data. getPortfolio ( ) ;

BigDecimal total = new BigDecimal ( 0.0 ) ;

total = data. getCashBalance ( ) ;

for ( ExchangeRate rate : < hidden > . getExchangeRates ( ) ) {

if ( rate. getCurrency ( ) . equals ( data. getCashBalanceCurrency ( ) ) ) {

total = total. multiply ( rate. getRate ( ) ) ;

}

}

// convert to current rate

if ( < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) . compareTo ( new BigDecimal ( "0.0000" ) ) == 1 ) {

total = total. divide ( < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) , BigDecimal . ROUND_UP ) ;

}

item. mSliceValue = total ;

item. mValue = total. floatValue ( ) ;

switch ( index ) {

case 0 :

item. mColor = res. getColor ( R. color . pcs_teal ) ;

mParentPie. add ( item ) ;

break ;

case 1 :

item. mColor = res. getColor ( R. color . pcs_air_force_blue ) ;

mParentPie. add ( item ) ;

break ;

case 2 :

item. mColor = res. getColor ( R. color . pcs_biscuit ) ;

mParentPie. add ( item ) ;

break ;

case 3 :

item. mColor = res. getColor ( R. color . pcs_pecan ) ;

mParentPie. add ( item ) ;

break ;

default :

fTotal = fTotal. add ( total ) ;

item. mLabel = "Other" ;

item. mColor = res. getColor ( R. color . pcs_summer_sky ) ;

break ;

}

System . out . println ( "[ITEM VALUE] - " + item. mValue ) ;

}

if ( fTotal. compareTo ( BigDecimal . ZERO ) == 1 ) {

item. mSliceValue = fTotal ;

item. mValue = fTotal. floatValue ( ) ;

mParentPie. add ( item ) ;

System . out . println ( "[OTHER TOTAL] - " + fTotal ) ;

}

}

/**

*

* @param balanceData

*/

public void populatePieChartWithInvestmentPortfolios ( List < BalanceData > balanceData ) {

Resources res = getResources ( ) ;

// build pie slices

BigDecimal fTotal = BigDecimal . ZERO ;

PieChartItem item = new PieChartItem ( ) ;

for ( int index = 0 ; index < balanceData. size ( ) ; index ++ ) {

// get data object

BalanceData data = balanceData. get ( index ) ;

item = new PieChartItem ( ) ;

item. mLabel = data. getPortfolio ( ) ;

BigDecimal total = new BigDecimal ( 0.0 ) ;

total = data. getInvestmentBalance ( ) ;

for ( ExchangeRate rate : < hidden > . getExchangeRates ( ) ) {

if ( rate. getCurrency ( ) . equals ( data. getInvestmentBalanceCurrency ( ) ) ) {

total = total. multiply ( rate. getRate ( ) ) ;

}

}

// convert to current rate

Logger. debug ( "PCS" , "PCS RATE - " + < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) ) ;

Logger. debug ( "PCS" , "PCS TOTAL - " + total ) ;

if ( < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) . compareTo ( new BigDecimal ( "0.0000" ) ) == 1 ) {

total = total. divide ( < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) , BigDecimal . ROUND_UP ) ;

}

item. mSliceValue = total ;

item. mValue = total. floatValue ( ) ;

switch ( index ) {

case 0 :

item. mColor = res. getColor ( R. color . pcs_teal ) ;

mParentPie. add ( item ) ;

break ;

case 1 :

item. mColor = res. getColor ( R. color . pcs_air_force_blue ) ;

mParentPie. add ( item ) ;

break ;

case 2 :

item. mColor = res. getColor ( R. color . pcs_biscuit ) ;

mParentPie. add ( item ) ;

break ;

case 3 :

item. mColor = res. getColor ( R. color . pcs_pecan ) ;

mParentPie. add ( item ) ;

break ;

default :

fTotal = fTotal. add ( total ) ;

item. mLabel = "Other" ;

item. mColor = res. getColor ( R. color . pcs_summer_sky ) ;

break ;

}

System . out . println ( "[ITEM VALUE] - " + item. mValue ) ;

}

if ( fTotal. compareTo ( BigDecimal . ZERO ) == 1 ) {

item. mSliceValue = fTotal ;

item. mValue = fTotal. floatValue ( ) ;

mParentPie. add ( item ) ;

System . out . println ( "[OTHER TOTAL] - " + fTotal ) ;

}

}

/**

*

* @param balanceData

*/

public void populatePieChartWithAssociatedCurrencyPortfolio ( List < BalanceData > balanceData, final String selectedCurrency ) {

Resources res = getResources ( ) ;

// build pie slices

BigDecimal fTotal = BigDecimal . ZERO ;

PieChartItem item = new PieChartItem ( ) ;

for ( int index = 0 ; index < balanceData. size ( ) ; index ++ ) {

item = new PieChartItem ( ) ;

// get data object

BalanceData data = balanceData. get ( index ) ;

// if associated currency then do

if ( data. getCashBalanceCurrency ( ) . equals ( selectedCurrency ) ) {

item. mLabel = data. getPortfolio ( ) ;

BigDecimal total = new BigDecimal ( 0.0 ) ;

total = data. getCashBalance ( ) . add ( data. getInvestmentBalance ( ) ) ;

for ( ExchangeRate rate : < hidden > . getExchangeRates ( ) ) {

if ( rate. getCurrency ( ) . equals ( data. getCashBalanceCurrency ( ) ) ) {

total = total. multiply ( rate. getRate ( ) ) ;

}

}

// convert to current rate

Logger. debug ( "PCS" , "PCS RATE - " + < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) ) ;

Logger. debug ( "PCS" , "PCS TOTAL - " + total ) ;

if ( < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) . compareTo ( new BigDecimal ( "0.0000" ) ) == 1 ) {

total = total. divide ( < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) , BigDecimal . ROUND_UP ) ;

}

item. mSliceValue = total ;

item. mValue = total. floatValue ( ) ;

switch ( index ) {

case 0 :

item. mColor = res. getColor ( R. color . pcs_teal ) ;

mParentPie. add ( item ) ;

break ;

case 1 :

item. mColor = res. getColor ( R. color . pcs_air_force_blue ) ;

mParentPie. add ( item ) ;

break ;

case 2 :

item. mColor = res. getColor ( R. color . pcs_biscuit ) ;

mParentPie. add ( item ) ;

break ;

case 3 :

item. mColor = res. getColor ( R. color . pcs_pecan ) ;

mParentPie. add ( item ) ;

break ;

default :

fTotal = fTotal. add ( total ) ;

item. mLabel = "Other" ;

item. mColor = res. getColor ( R. color . pcs_summer_sky ) ;

break ;

}

}

}

if ( fTotal. compareTo ( BigDecimal . ZERO ) == 1 ) {

item. mSliceValue = fTotal ;

item. mValue = fTotal. floatValue ( ) ;

mParentPie. add ( item ) ;

}

}

/**

*

* @param balanceData

*/

public void populateCashAndInvestmentsForCurrency ( List < BalanceData > balanceData, String currency ) {

PieChartItem item = new PieChartItem ( ) ;

Resources res = getResources ( ) ;

for ( BalanceData data : balanceData ) {

// if associated currency then do

if ( data. getCashBalanceCurrency ( ) . equals ( currency ) ) {

BigDecimal cash = BigDecimal . ZERO ;

BigDecimal investment = BigDecimal . ZERO ;

List < PieChartItem > slices = new ArrayList < PieChartItem > ( ) ;

if ( data. getIsOffshore ( ) ) { // offshore

cash = data. getCashBalance ( ) ;

investment = data. getInvestmentBalance ( ) ;

for ( ExchangeRate rate : < hidden > . getExchangeRates ( ) ) {

if ( rate. getCurrency ( ) . equals ( data. getCashBalanceCurrency ( ) ) ) {

cash = cash. multiply ( rate. getRate ( ) ) ;

investment = investment. multiply ( rate. getRate ( ) ) ;

}

}

} else if ( ! data. getIsOffshore ( ) ) { // local

cash = data. getCashBalance ( ) ;

investment = data. getInvestmentBalance ( ) ;

}

if ( < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) . compareTo ( new BigDecimal ( "0.0000" ) ) == 1 ) {

// convert to current rate

cash = cash. divide ( < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) , BigDecimal . ROUND_UP ) ;

investment = investment. divide ( < hidden > . getmCurrentExchangeRate ( ) . getRate ( ) , BigDecimal . ROUND_UP ) ;

}

// investment slice

item = new PieChartItem ( ) ;

item. mLabel = res. getString ( R. string . pcs_investment ) ;

item. mSliceValue = investment ;

item. mValue = investment. floatValue ( ) ;

item. mColor = res. getColor ( R. color . pcs_teal ) ;

slices. add ( item ) ;

// cash slice

item = new PieChartItem ( ) ;

item. mLabel = res. getString ( R. string . pcs_cash ) ;

item. mSliceValue = cash ;

item. mValue = cash. floatValue ( ) ;

item. mColor = res. getColor ( R. color . pcs_biscuit ) ;

slices. add ( item ) ;

mPieChartList. add ( slices ) ;

}

}

}

/**

* Sets header for key

*

* @param header

*/

public void setKeyHeader ( String header ) {

if ( itemsForList. size ( ) > 0 ) {

PieChartItem headerItem = itemsForList. get ( 0 ) ;

if ( headerItem instanceof PieChartLegendHeaderItem ) {

( ( PieChartLegendHeaderItem ) headerItem ) . setKeyHeader ( header ) ;

adapter. notifyDataSetChanged ( ) ;

}

}

}

/**

* Sets key value

*

* @param value

*/

public void setKeyValue ( String value ) {

this . keyValue = value ;

}

/**

* Draws a pie

*

* @param header

*/

public void drawPieChart ( String header, boolean isListHeader ) {

if ( ! mParentPie. isEmpty ( ) ) {

mPieChart. getmData ( ) . clear ( ) ;

mPieChart. setmTotal ( 0.0f ) ;

mTVHeader. setText ( header ) ;

// set pie visible

mPieChart. setVisibility ( View . VISIBLE ) ;

for ( PieChartItem item : mParentPie ) {

mPieChart. addItem ( item ) ;

}

itemsForList. clear ( ) ;

if ( isListHeader ) {

PieChartLegendHeaderItem headerItem = new PieChartLegendHeaderItem ( ) ;

headerItem. setKeyHeader ( header ) ;

headerItem. setKeyValue ( keyValue ) ;

itemsForList. add ( headerItem ) ;

}

itemsForList. addAll ( mParentPie ) ;

adapter. setfTotal ( mPieChart. getmTotal ( ) ) ;

adapter. notifyDataSetChanged ( ) ;

} else {

mPieChart. setVisibility ( View . GONE ) ;

}

}

/**

* Draws a pie

*

* @param header

*/

public void drawPieChart ( String header, final int index ) {

if ( ! mPieChartList. isEmpty ( ) ) {

mPieChart. getmData ( ) . clear ( ) ;

mPieChart. setmTotal ( 0.0f ) ;

mTVHeader. setText ( header ) ;

List < PieChartItem > slices = mPieChartList. get ( index ) ;

// set pie visible

mPieChart. setVisibility ( View . VISIBLE ) ;

for ( PieChartItem item : slices ) {

mPieChart. addItem ( item ) ;

}

System . out . println ( "[PIE TOTAL] - " + mPieChart. getmTotal ( ) ) ;

itemsForList. clear ( ) ;

// PieChartLegendHeaderItem headerItem = new PieChartLegendHeaderItem();

// headerItem.setKeyHeader(header);

// headerItem.setKeyValue(keyValue);

//

// itemsForList.add(headerItem);

itemsForList. addAll ( slices ) ;

adapter. setfTotal ( mPieChart. getmTotal ( ) ) ;

adapter. notifyDataSetChanged ( ) ;

} else {

mPieChart. setVisibility ( View . GONE ) ;

}

}