Java tutorial
/** * This file is licensed under MIT * * The MIT License (MIT) * * Copyright (c) 2014 Jonas Gehring * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.mehdi.graphview.custom; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Typeface; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.jjoe64.graphview.GraphView; import com.jjoe64.graphview.GridLabelRenderer; import com.jjoe64.graphview.helper.StaticLabelsFormatter; import com.jjoe64.graphview.series.DataPoint; import com.jjoe64.graphview.series.LineGraphSeries; import com.mehdi.graphview.R; import com.mehdi.graphview.background.GetGraphData; import com.mehdi.graphview.background.GraphDataWatcher; import com.mehdi.graphview.dao.TickerList; import com.mehdi.graphview.dao.Tickers; import com.mehdi.graphview.utils.DateFormatter; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * Created by Mehdi on 09.05.16 */ public class SimpleLineGraph extends Fragment implements GraphDataWatcher { View rootView; private final String labelTypeFace = ""; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { rootView = inflater.inflate(R.layout.fragment_main, container, false); new GetGraphData(this); return rootView; } @Override public void onFinished(TickerList tickerList) { if (null == tickerList) { return; } GraphView graphView = (GraphView) rootView.findViewById(R.id.graph); LineGraphSeries<DataPoint> series = new LineGraphSeries<>(); ArrayList<Tickers> tickers = tickerList.tickers; ArrayList<DataPoint> dataPointArrayList = new ArrayList<>(); for (int i = 0; i < tickers.get(0).historical.size(); i++) { dataPointArrayList.add(new DataPoint(i, //Double.parseDouble(String.valueOf(tickers.get(0).historical.get(i).get(0))), Double.parseDouble(String.valueOf(tickers.get(0).historical.get(i).get(1))))); } DataPoint[] dataPoints = dataPointArrayList.toArray(new DataPoint[dataPointArrayList.size()]); series.resetData(dataPoints); graphView.addSeries(series); // set manual X bounds graphView.getViewport().setXAxisBoundsManual(true); graphView.getViewport().setMinX(0); graphView.getViewport().setMaxX(series.getHighestValueX()); // set manual Y bounds graphView.getViewport().setYAxisBoundsManual(true); graphView.getViewport().setMinY(series.getLowestValueY()); graphView.getViewport().setMaxY(series.getHighestValueY()); graphView.getViewport().setScrollable(false); StaticLabelsFormatter staticLabelsFormatter = new StaticLabelsFormatter(graphView); // set xLabels values int middleIndex = tickers.get(0).historical.size() / 2; staticLabelsFormatter.setHorizontalLabels(new String[] { getHourMinute(String.valueOf(tickers.get(0).historical.get(0).get(0))), "", getHourMinute(String.valueOf(tickers.get(0).historical.get(middleIndex).get(0))), "", getHourMinute(String .valueOf(tickers.get(0).historical.get(tickers.get(0).historical.size() - 1).get(0))) }); staticLabelsFormatter.setVerticalLabels(new String[] { "", "", "" }); graphView.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter); // graphView.getGridLabelRenderer().setTextSize(35); // graphView.getGridLabelRenderer().setTextSize(25); float spToFloat = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 25, graphView.getContext().getResources().getDisplayMetrics()); graphView.getGridLabelRenderer().setTextSize(spToFloat); // setting label typeface if (null != getContext()) { Typeface plain = Typeface.createFromAsset(getContext().getAssets(), "SouthernAire_Personal_Use_Only.ttf"); Typeface typeface = Typeface.create(plain, Typeface.BOLD_ITALIC); graphView.getGridLabelRenderer().setLabelTypeface(typeface); } // set second scale. This one is used to show the vertical labels // on the right side as per UI Spec. graphView.getSecondScale().setMinY(getMinimumValue(tickers.get(0).historical)); graphView.getSecondScale().setMaxY(getMaximumValue(tickers.get(0).historical)); graphView.getGridLabelRenderer().setVerticalLabelsSecondScaleAlign(Paint.Align.CENTER); if (null != getContext()) { int colorStockLabelGray = Color.GRAY; graphView.getGridLabelRenderer().setVerticalLabelsSecondScaleColor(colorStockLabelGray); graphView.getGridLabelRenderer().setHorizontalLabelsColor(colorStockLabelGray); graphView.getGridLabelRenderer().setGridColor(colorStockLabelGray); } graphView.getGridLabelRenderer().setHighlightZeroLines(false); graphView.getGridLabelRenderer().setGridStyle(GridLabelRenderer.GridStyle.VERTICAL); graphView.getGridLabelRenderer().reloadStyles(); //series.setColor(Color.GREEN); series.setColor(Color.WHITE); } private String getHourMinute(String timeStampString) { //convert unix epoch timestamp (seconds) to milliseconds long timestamp = Long.parseLong(timeStampString) * 1000; Date newDate = (new Date(timestamp)); return DateFormatter.formatToHourMinute(newDate); } private Double getMinimumValue(List<List<BigDecimal>> historical) { Double min = Double.parseDouble(String.valueOf(historical.get(0).get(1))); for (int i = 0; i < historical.size(); i++) { if (Double.parseDouble(String.valueOf(historical.get(i).get(1))) < min) { min = Double.parseDouble(String.valueOf(historical.get(i).get(1))); } } return min; } private Double getMaximumValue(List<List<BigDecimal>> historical) { Double max = Double.parseDouble(String.valueOf(historical.get(0).get(1))); for (int i = 0; i < historical.size(); i++) { if (Double.parseDouble(String.valueOf(historical.get(i).get(1))) > max) { max = Double.parseDouble(String.valueOf(historical.get(i).get(1))); } } return max; } }