1 /* 2 * Copyright 2012 AndroidPlot.com 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.androidplot.demos; 18 19 import android.app.Activity; 20 import android.graphics.*; 21 import android.os.Bundle; 22 import com.androidplot.xy.SimpleXYSeries; 23 import com.androidplot.xy.XYSeries; 24 import com.androidplot.xy.*; 25 26 import java.text.*; 27 import java.util.Arrays; 28 import java.util.Date; 29 30 public class TimeSeriesActivity extends Activity 31 { 32 33 private XYPlot plot1; 34 35 @Override 36 public void onCreate(Bundle savedInstanceState) 37 { 38 super.onCreate(savedInstanceState); 39 setContentView(R.layout.time_series_example); 40 41 plot1 = (XYPlot) findViewById(R.id.plot1); 42 Number[] numSightings = {5, 8, 9, 2, 5}; 43 44 // an array of years in milliseconds: 45 Number[] years = { 46 978307200, // 2001 47 1009843200, // 2002 48 1041379200, // 2003 49 1072915200, // 2004 50 1104537600 // 2005 51 }; 52 // create our series from our array of nums: 53 XYSeries series2 = new SimpleXYSeries( 54 Arrays.asList(years), 55 Arrays.asList(numSightings), 56 "Sightings in USA"); 57 58 plot1.getGraphWidget().getGridBackgroundPaint().setColor(Color.WHITE); 59 plot1.getGraphWidget().getDomainGridLinePaint().setColor(Color.BLACK); 60 plot1.getGraphWidget().getDomainGridLinePaint(). 61 setPathEffect(new DashPathEffect(new float[]{1, 1}, 1)); 62 plot1.getGraphWidget().getRangeGridLinePaint().setColor(Color.BLACK); 63 plot1.getGraphWidget().getRangeGridLinePaint(). 64 setPathEffect(new DashPathEffect(new float[]{1, 1}, 1)); 65 plot1.getGraphWidget().getDomainOriginLinePaint().setColor(Color.BLACK); 66 plot1.getGraphWidget().getRangeOriginLinePaint().setColor(Color.BLACK); 67 68 // Create a formatter to use for drawing a series using LineAndPointRenderer: 69 LineAndPointFormatter series1Format = new LineAndPointFormatter( 70 Color.rgb(0, 100, 0), // line color 71 Color.rgb(0, 100, 0), // point color 72 Color.rgb(100, 200, 0), null); // fill color 73 74 75 // setup our line fill paint to be a slightly transparent gradient: 76 Paint lineFill = new Paint(); 77 lineFill.setAlpha(200); 78 79 // ugly usage of LinearGradient. unfortunately there's no way to determine the actual size of 80 // a View from within onCreate. one alternative is to specify a dimension in resources 81 // and use that accordingly. at least then the values can be customized for the device type and orientation. 82 lineFill.setShader(new LinearGradient(0, 0, 200, 200, Color.WHITE, Color.GREEN, Shader.TileMode.CLAMP)); 83 84 LineAndPointFormatter formatter = 85 new LineAndPointFormatter(Color.rgb(0, 0,0), Color.BLUE, Color.RED, null); 86 formatter.setFillPaint(lineFill); 87 plot1.getGraphWidget().setPaddingRight(2); 88 plot1.addSeries(series2, formatter); 89 90 // draw a domain tick for each year: 91 plot1.setDomainStep(XYStepMode.SUBDIVIDE, years.length); 92 93 // customize our domain/range labels 94 plot1.setDomainLabel("Year"); 95 plot1.setRangeLabel("# of Sightings"); 96 97 // get rid of decimal points in our range labels: 98 plot1.setRangeValueFormat(new DecimalFormat("0")); 99 100 plot1.setDomainValueFormat(new Format() { 101 102 // create a simple date format that draws on the year portion of our timestamp. 103 // see http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html 104 // for a full description of SimpleDateFormat. 105 private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy"); 106 107 @Override 108 public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) { 109 110 // because our timestamps are in seconds and SimpleDateFormat expects milliseconds 111 // we multiply our timestamp by 1000: 112 long timestamp = ((Number) obj).longValue() * 1000; 113 Date date = new Date(timestamp); 114 return dateFormat.format(date, toAppendTo, pos); 115 } 116 117 @Override 118 public Object parseObject(String source, ParsePosition pos) { 119 return null; 120 121 } 122 }); 123 124 // by default, AndroidPlot displays developer guides to aid in laying out your plot. 125 // To get rid of them call disableAllMarkup(): 126 //plot1.disableAllMarkup(); 127 } 128 }