Android Open Source - StockTicker Graph Activity






From Project

Back to project page StockTicker.

License

The source code is released under:

MIT License

If you think the Android project StockTicker listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.github.premnirmal.ticker.ui;
//  w ww .j  av a  2 s.  co m
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

import com.github.premnirmal.ticker.BaseActivity;
import com.github.premnirmal.ticker.StocksApp;
import com.github.premnirmal.ticker.Tools;
import com.github.premnirmal.ticker.model.IHistoryProvider;
import com.github.premnirmal.ticker.network.Stock;
import com.github.premnirmal.tickerwidget.R;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GridLabelRenderer;
import com.jjoe64.graphview.Viewport;
import com.jjoe64.graphview.helper.DateAsXAxisLabelFormatter;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.DataPointInterface;
import com.jjoe64.graphview.series.LineGraphSeries;
import com.jjoe64.graphview.series.OnDataPointTapListener;
import com.jjoe64.graphview.series.PointsGraphSeries;
import com.jjoe64.graphview.series.Series;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import javax.inject.Inject;

import rx.Subscriber;

/**
 * Created by premnirmal on 12/30/14.
 */
public class GraphActivity extends BaseActivity {

    public static final String GRAPH_DATA = "GRAPH_DATA";

    private final DateTimeFormatter formatter = DateTimeFormat.forPattern("MM/dd/YYYY");

    private Stock ticker;
    private DataPoint[] dataPoints;

    @Inject
    IHistoryProvider historyProvider;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ((StocksApp) getApplicationContext()).inject(this);
        setContentView(R.layout.progress);
        ticker = (Stock) getIntent().getSerializableExtra(GRAPH_DATA);
        getSupportActionBar().hide();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (dataPoints == null) {
            if (Tools.isNetworkOnline(this)) {
                historyProvider.getDataPoints(ticker.symbol)
                        .subscribe(new Subscriber<DataPoint[]>() {
                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {
                                showDialog("Error loading datapoints", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        finish();
                                    }
                                });
                            }

                            @Override
                            public void onNext(DataPoint[] data) {
                                dataPoints = data;
                                loadGraph();
                            }
                        });
            } else {
                showDialog(getString(R.string.no_network_message), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                });
            }
        } else {
            loadGraph();
        }
    }

    private void loadGraph() {
        setContentView(R.layout.activity_graph);
        final GraphView graphView = (GraphView) findViewById(R.id.graph);
        final TextView tickerName = (TextView) findViewById(R.id.ticker);
        final TextView desc = (TextView) findViewById(R.id.desc);
        tickerName.setText(ticker.symbol);
        desc.setText(ticker.Name);
        final TextView dataPointValue = (TextView) findViewById(R.id.dataPointValue);
        final LineGraphSeries<DataPoint> series = new LineGraphSeries(dataPoints);
        graphView.addSeries(series);

        final PointsGraphSeries disposableSeries = new PointsGraphSeries(new DataPointInterface[]{dataPoints[dataPoints.length - 1]});
        graphView.addSeries(disposableSeries);
        disposableSeries.setColor(Color.GREEN);
        disposableSeries.setShape(PointsGraphSeries.Shape.POINT);
        disposableSeries.setSize(10f);

        series.setDrawBackground(true);
        series.setBackgroundColor(getResources().getColor(R.color.maroon));
        series.setOnDataPointTapListener(new OnDataPointTapListener() {
            @Override
            public void onTap(Series series, DataPointInterface dataPointInterface) {
                final StringBuilder dataPointText = new StringBuilder();
                final DateTime dateTime = new DateTime((long) dataPointInterface.getX());
                dataPointText.append(formatter.print(dateTime));
                dataPointText.append(" // ");
                dataPointText.append("$");
                dataPointText.append(dataPointInterface.getY());
                dataPointValue.setText(dataPointText.toString());
                disposableSeries.resetData(new DataPointInterface[]{dataPointInterface});
            }
        });
        final GridLabelRenderer gridLabelRenderer = graphView.getGridLabelRenderer();
        gridLabelRenderer.setLabelFormatter(new DateAsXAxisLabelFormatter(this));
        gridLabelRenderer.setNumHorizontalLabels(5);
        final Viewport viewport = graphView.getViewport();
        viewport.setXAxisBoundsManual(true);
        viewport.setMinX(dataPoints[0].getX());
        viewport.setMaxX(dataPoints[dataPoints.length - 1].getX());
    }
}




Java Source Code List

com.github.premnirmal.ticker.AppModule.java
com.github.premnirmal.ticker.ApplicationTest.java
com.github.premnirmal.ticker.BaseActivity.java
com.github.premnirmal.ticker.StocksApp.java
com.github.premnirmal.ticker.Tools.java
com.github.premnirmal.ticker.UpdateReceiver.java
com.github.premnirmal.ticker.events.NoNetworkEvent.java
com.github.premnirmal.ticker.events.StockUpdatedEvent.java
com.github.premnirmal.ticker.model.HistoryProvider.java
com.github.premnirmal.ticker.model.IHistoryProvider.java
com.github.premnirmal.ticker.model.IStocksProvider.java
com.github.premnirmal.ticker.model.StocksProvider.java
com.github.premnirmal.ticker.model.StocksStorage.java
com.github.premnirmal.ticker.network.ApiModule.java
com.github.premnirmal.ticker.network.QueryCreator.java
com.github.premnirmal.ticker.network.QueryResults.java
com.github.premnirmal.ticker.network.Query.java
com.github.premnirmal.ticker.network.Results.java
com.github.premnirmal.ticker.network.StockQuery.java
com.github.premnirmal.ticker.network.Stock.java
com.github.premnirmal.ticker.network.StocksApi.java
com.github.premnirmal.ticker.network.StupidYahooWrapConverter.java
com.github.premnirmal.ticker.network.SuggestionApi.java
com.github.premnirmal.ticker.network.Suggestion.java
com.github.premnirmal.ticker.network.Suggestions.java
com.github.premnirmal.ticker.network.historicaldata.HistoricalData.java
com.github.premnirmal.ticker.network.historicaldata.History.java
com.github.premnirmal.ticker.network.historicaldata.Query.java
com.github.premnirmal.ticker.network.historicaldata.Quote.java
com.github.premnirmal.ticker.settings.FileExportTask.java
com.github.premnirmal.ticker.settings.FileImportTask.java
com.github.premnirmal.ticker.settings.SettingsActivity.java
com.github.premnirmal.ticker.ui.GraphActivity.java
com.github.premnirmal.ticker.ui.ParanormalActivity.java
com.github.premnirmal.ticker.ui.StocksAdapter.java
com.github.premnirmal.ticker.ui.SuggestionsAdapter.java
com.github.premnirmal.ticker.ui.TickerSelectorActivity.java
com.github.premnirmal.ticker.widget.RemoteStockProviderService.java
com.github.premnirmal.ticker.widget.RemoteStockViewAdapter.java
com.github.premnirmal.ticker.widget.StockWidget.java
com.terlici.dragndroplist.DragNDropAdapter.java
com.terlici.dragndroplist.DragNDropCursorAdapter.java
com.terlici.dragndroplist.DragNDropListView.java
com.terlici.dragndroplist.DragNDropSimpleAdapter.java