| /* |
| * Copyright (C) 2014 Google, Inc. |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package com.google.araploxio; |
| |
| import org.achartengine.ChartFactory; |
| import org.achartengine.GraphicalView; |
| import org.achartengine.chart.PointStyle; |
| import org.achartengine.model.XYMultipleSeriesDataset; |
| import org.achartengine.model.XYSeries; |
| import org.achartengine.renderer.XYMultipleSeriesRenderer; |
| import org.achartengine.renderer.XYSeriesRenderer; |
| |
| import android.app.Activity; |
| import android.graphics.Color; |
| import android.graphics.Paint.Align; |
| import android.os.Bundle; |
| import android.os.Handler; |
| import android.os.Message; |
| import android.util.Log; |
| import android.view.Window; |
| import android.view.WindowManager; |
| import android.view.ViewGroup.LayoutParams; |
| import android.widget.LinearLayout; |
| import android.widget.TextView; |
| |
| public class AraPloxActivity extends Activity { |
| private static final String TAG = "AraPlox/Activity"; |
| private static final int SCROLLBACK_SIZE = 200; |
| |
| private static TextView bpmTextView; |
| private static Handler mHandler = null; |
| private static AFE4400Thread afe4400Thread = null; |
| private static int sampleCount = 0; |
| |
| private AraPloxChart irPloxChart = new AraPloxChart(R.id.irChart); |
| private AraPloxChart redPloxChart = new AraPloxChart(R.id.redChart); |
| |
| private class AraPloxChart { |
| int id; |
| private XYMultipleSeriesRenderer renderer; |
| private XYMultipleSeriesDataset dataSet; |
| private XYSeriesRenderer xySeriesRenderer; |
| private XYSeries xySeries; |
| private GraphicalView chartView; |
| |
| public AraPloxChart(int id) { |
| this.id = id; |
| } |
| |
| public void makeChart(int pointColor) { |
| dataSet = new XYMultipleSeriesDataset(); |
| xySeries = new XYSeries(""); |
| dataSet.addSeries(xySeries); |
| |
| int[] colors = new int[] {pointColor}; |
| PointStyle[] styles = new PointStyle[] {PointStyle.CIRCLE}; |
| |
| renderer = new XYMultipleSeriesRenderer(); |
| |
| int length = colors.length; |
| for (int i = 0; i < length; i++) { |
| xySeriesRenderer = new XYSeriesRenderer(); |
| xySeriesRenderer.setFillPoints(true); |
| xySeriesRenderer.setLineWidth(10.0f); |
| xySeriesRenderer.setPointStrokeWidth(1); |
| xySeriesRenderer.setColor(colors[i]); |
| xySeriesRenderer.setPointStyle(styles[i]); |
| renderer.addSeriesRenderer(xySeriesRenderer); |
| } |
| |
| renderer.setChartTitleTextSize(20); |
| renderer.setLabelsTextSize(15); |
| renderer.setLegendTextSize(15); |
| renderer.setPointSize(5f); |
| renderer.setMargins(new int[] { 50, 50, 50, 50 }); |
| renderer.setXAxisMin(0); |
| renderer.setXAxisMax(10); |
| renderer.setYAxisMin(0); |
| renderer.setYAxisMax(100); |
| renderer.setAxesColor(Color.BLACK); |
| renderer.setLabelsColor(Color.BLACK); |
| renderer.setShowLegend(false); |
| renderer.setXLabels(10); |
| renderer.setYLabels(0); |
| renderer.setShowGrid(false); |
| renderer.setXLabelsAlign(Align.CENTER); |
| renderer.setApplyBackgroundColor(true); |
| renderer.setBackgroundColor(Color.WHITE); |
| renderer.setMarginsColor(Color.WHITE); |
| |
| chartView = ChartFactory.getCubeLineChartView(getApplicationContext(), dataSet, renderer, 1.0f); |
| |
| LinearLayout layout = (LinearLayout)findViewById(id); |
| layout.addView(chartView, new LayoutParams(LayoutParams.FILL_PARENT, |
| LayoutParams.FILL_PARENT)); |
| chartView.repaint(); |
| } |
| |
| public void removeChart() { |
| LinearLayout layout = (LinearLayout)findViewById(id); |
| layout.removeView(chartView); |
| } |
| |
| public void handleNewSample(double x, double y) { |
| xySeries.add(x, y); |
| |
| // Scroll continuously after SCROLLBACK_SIZE points |
| if (x > SCROLLBACK_SIZE) { |
| xySeries.remove(0); |
| } |
| |
| // Autoscale |
| double maxX = xySeries.getMaxX(); |
| double minX = xySeries.getMinX(); |
| double maxY = xySeries.getMaxY(); |
| double minY = xySeries.getMinY(); |
| |
| maxY *= 1.01; |
| minY *= .99; |
| |
| renderer.setXAxisMax(maxX); |
| renderer.setXAxisMin(minX); |
| renderer.setYAxisMax(maxY); |
| renderer.setYAxisMin(minY); |
| |
| chartView.repaint(); |
| |
| sampleCount++; |
| } |
| } |
| |
| protected void onCreate(Bundle savedInstanceState) { |
| super.onCreate(savedInstanceState); |
| Log.d(TAG, "onCreate()"); |
| requestWindowFeature(Window.FEATURE_NO_TITLE); |
| setContentView(R.layout.activity_ara_plox); |
| getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
| |
| mHandler = new Handler() { |
| public void handleMessage(Message msg) { |
| double irLedSample = msg.arg1; |
| double redLedSample = msg.arg2; |
| |
| irPloxChart.handleNewSample(sampleCount, irLedSample); |
| redPloxChart.handleNewSample(sampleCount, redLedSample); |
| |
| sampleCount++; |
| } |
| }; |
| } |
| |
| protected void onResume() { |
| super.onResume(); |
| Log.d(TAG, "onResume()"); |
| if (afe4400Thread == null) { |
| afe4400Thread = new AFE4400Thread(getApplicationContext(), mHandler); |
| afe4400Thread.start(); |
| } |
| irPloxChart.makeChart(Color.GREEN); |
| redPloxChart.makeChart(Color.RED); |
| } |
| |
| protected void onPause() { |
| super.onPause(); |
| Log.d(TAG, "onPause()"); |
| if (afe4400Thread != null) { |
| afe4400Thread.requestStop(); |
| afe4400Thread = null; |
| } |
| irPloxChart.removeChart(); |
| redPloxChart.removeChart(); |
| } |
| |
| protected void onStop() { |
| super.onStop(); |
| Log.d(TAG, "onStop()"); |
| } |
| } |