Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to update chart in androidplot

I made a graph from my database with androidplot, but when i reset the database with by button, the graph dosnt update, how do i update the graph without recreating the activity?

public class Overview extends Activity {
Number[] seriesOfNumbers;
private XYPlot mySimpleXYPlot;
// double totalprofitd;
private BetsDbAdapter dbHelper;
TextView NBtext;
TextView TOtext;
TextView TPtext;
TextView ROItext;

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

    dbHelper = new BetsDbAdapter(this);
    TOtext = (TextView) findViewById(R.id.turnover);
    TPtext = (TextView) findViewById(R.id.totalprofit);
    NBtext = (TextView) findViewById(R.id.numberOfBet);
    ROItext = (TextView) findViewById(R.id.roi);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.android_tab_layout, menu);
    return true;
}

public void resetClick(View v) {
    dbHelper.open();
    dbHelper.deleteDatabase();
    dbHelper.close();
    CalculateProfit();
    graph();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    CalculateProfit();
    graph();
}

public void CalculateProfit() {
    dbHelper.open();
    List<String> tip = dbHelper.getTip();
    List<String> betamount = dbHelper.getBetAmount();
    ArrayList<String> sbodds = dbHelper.getSBodds();
    dbHelper.close();

    int NumberOfBets = tip.size();
    double turnover = 0;
    double ROI = 0;
    double totalprofit = 0;
    double profit = 0;
    seriesOfNumbers = new Number[NumberOfBets];
    for (int i = 0; i < NumberOfBets; i++) {
        String WLV = tip.get(i);
        String arrpct[] = WLV.split(" ", 2);
        String WLV1 = arrpct[0];

        if (WLV1.equals("W")) {
            profit = Double.parseDouble(betamount.get(i).replaceAll(",",
                    "."))
                    * (Double.parseDouble(sbodds.get(i)) - 1);
        }

        else if (WLV1.equals("L")) {
            profit = -Double.parseDouble(betamount.get(i).replaceAll(",",
                    "."));
        } else {
            profit = 0;
        }
        //Add total turnover
        turnover += Double.parseDouble(betamount.get(i).replaceAll(",", "."));
        //Add total profit
        totalprofit += profit;
        //For the graph
        seriesOfNumbers[i] = totalprofit;
    }
    ROI = ((turnover + totalprofit) / turnover) * 100;
    TPtext.setText(new DecimalFormat("##.##").format(totalprofit));
    TOtext.setText(new DecimalFormat("##.##").format(turnover));
    NBtext.setText(Integer.toString(NumberOfBets));
    ROItext.setText(new DecimalFormat("##.##").format(ROI));
}

public void graph() {
    dbHelper.open();
    // initialize our XYPlot reference:
    mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot);

    // Turn the above arrays into XYSeries':
    XYSeries series1 = new SimpleXYSeries(Arrays.asList(seriesOfNumbers),
    // SimpleXYSeries takes a List so turn our array into a list
            SimpleXYSeries.ArrayFormat.Y_VALS_ONLY,
            // Y_VALS_ONLY means use the element index as the x value
            "Series1"); // Set the display title of the series

    // Create a formatter to use for drawing a series using
    // LineAndPointRenderer:
    LineAndPointFormatter series1Format = new LineAndPointFormatter(
            Color.rgb(0, 200, 0), // line color
            null, // point color
            Color.rgb(0, 200, 0)); // fill color (none)

    // add a new series' to the xyplot:
    mySimpleXYPlot.addSeries(series1, series1Format);

    // reduce the number of range labels
    mySimpleXYPlot.setTicksPerRangeLabel(3);

    // by default, AndroidPlot displays developer guides to aid in laying
    // out your plot.
    // To get rid of them call disableAllMarkup():
    mySimpleXYPlot.disableAllMarkup();
    dbHelper.close();
}

}

Dont know if its important, but here is my:

c_overview.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/Profit_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="6"
                android:text="Profit:" />

            <TextView
                android:id="@+id/ROI_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="6"
                android:text="ROI:" />

            <TextView
                android:id="@+id/Bet_Counter_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="6"
                android:text="No. of Bets:" />

            <TextView
                android:id="@+id/Turnover_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="6"
                android:text="Turnover:" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.75"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/totalprofit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:text="total profit" />

            <TextView
                android:id="@+id/roi"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:text="Return Of Interest" />

            <TextView
                android:id="@+id/numberOfBet"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="number of bets" />

            <TextView
                android:id="@+id/turnover"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:text="Turnover" />
        </LinearLayout>

        <Button
            android:id="@+id/reset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="resetClick"
            android:text="Reset" />

    </LinearLayout>

    <com.androidplot.xy.XYPlot
            android:id="@+id/mySimpleXYPlot"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginTop="10px"
            android:layout_marginLeft="10px"
            android:layout_marginRight="10px"
            title="A Simple XYPlot Example"/>



</LinearLayout>
like image 839
Yoshidk Avatar asked Jun 05 '13 18:06

Yoshidk


1 Answers

I found 2 usable functions: clear and redraw, so now its working:

CalculateProfit();
mySimpleXYPlot.clear();
graph();        
mySimpleXYPlot.redraw();
like image 101
Yoshidk Avatar answered Oct 21 '22 04:10

Yoshidk