JaRMoS  1.1
Java Reduced Model Simulations
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
SingleLabelChart.java
Go to the documentation of this file.
1 // rbAPPmit: An Android front-end for the Certified Reduced Basis Method
2 // Copyright (C) 2010 David J. Knezevic and Phuong Huynh
3 //
4 // This file is part of rbAPPmit
5 //
6 // @ref rbappmit is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // @ref rbappmit is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with rbAPPmit. If not, see <http://www.gnu.org/licenses/>.
18 
19 package jarmos.app.misc.rb;
20 
21 import java.text.DecimalFormat;
22 
23 import org.achartengine.chart.LineChart;
24 import org.achartengine.model.XYMultipleSeriesDataset;
25 import org.achartengine.model.XYSeries;
26 import org.achartengine.renderer.XYMultipleSeriesRenderer;
27 
38 public class SingleLabelChart extends LineChart {
39 
43  private static final long serialVersionUID = 5178056223442588108L;
44 
45  // which series to show labels for
46  private int seriesNum;
47  // indicates which data point in the series should be labeled
48  private int pointInSeries;
49  // whether to show a progress label
50  private boolean progressLabel;
51 
52  public SingleLabelChart(XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, int s, int p, boolean pr) {
53  super(dataset, renderer);
54  seriesNum = s;
55  pointInSeries = p;
56  progressLabel = pr;
57  }
58 
59  @Override
60  public void drawChartValuesText(android.graphics.Canvas canvas, XYSeries series, android.graphics.Paint paint,
61  float[] points, int seriesIndex) // seriesIndex is 0 (mod 3) for output, 1 for LB, 2
62  // for UB
63  {
64 
65  // showing numbered labels case
66  if (!progressLabel) {
67  if ((pointInSeries >= 0) && (pointInSeries < points.length)) {
68 
69  double accuracyPlace = (series.getMaxY() - series.getMinY()) / 100;
70  // need to get multiple of ten
71  double decimalPlace = 0.000001;
72  while (accuracyPlace >= decimalPlace * 10)
73  decimalPlace *= 10;
74  accuracyPlace = decimalPlace;
75 
76  float verticalAdjustment;
77  switch (seriesIndex % 3) {
78  default:
79  verticalAdjustment = 0;
80  break;
81  case 1:
82  verticalAdjustment = 8f;
83  break;
84  case 2:
85  verticalAdjustment = -8f;
86  break;
87  }
88 
89  // this is essentially identical to LineChart's
90  // drawChartValuesText, but it rounds the data
91  // points depending on the range of values present, and only
92  // plots for one series and one
93  // point in that series
94  for (int k = 0; k < points.length; k += 2) {
95  if (k == pointInSeries && seriesIndex / 3 == seriesNum)
96  super.drawText(canvas, roundToNthPlace(series.getY(k / 2), accuracyPlace), points[k],
97  (points[k + 1] - 3.5f + verticalAdjustment), paint, 0);
98  }
99  }
100  }
101  // showing progress label case
102  else {
103  for (int k = 0; k < points.length; k += 2) {
104  if (k > 3 && seriesIndex == seriesNum)
105  super.drawText(canvas, "working", points[k] + 1f, points[k + 1] - 1f, paint, 0);
106  }
107  }
108  }
109 
110  // rounds toRound to an arbitrary place, using the multiple of ten accPlace
111  // (e.g. 0.01 to round to the hundredths place)
112  public static String roundToNthPlace(double toRound, double accPlace) {
113  // find leftmost place in double
114  double count = 1;
115  while (count <= Math.abs(toRound))
116  count *= 10;
117  int placesBeforeDecimal = (int) Math.log10(count);
118 
119  int placesAfterDecimal = -(int) Math.log10(accPlace);
120 
121  String forDecimalFormat = "";
122  // if accPlace is greater than or equal to 1, round to whole number
123  if (placesAfterDecimal <= 0)
124  placesAfterDecimal = 0;
125 
126  if (toRound < 0)
127  forDecimalFormat += "-";
128  for (int i = 0; i < placesBeforeDecimal; i++)
129  forDecimalFormat += "0";
130  if (placesAfterDecimal > 0)
131  forDecimalFormat += ".";
132  for (int i = 0; i < placesAfterDecimal; i++)
133  forDecimalFormat += "0";
134 
135  DecimalFormat decimal_format = new DecimalFormat(forDecimalFormat);
136  return decimal_format.format(toRound);
137 
138  }
139 
140 }
void drawChartValuesText(android.graphics.Canvas canvas, XYSeries series, android.graphics.Paint paint, float[] points, int seriesIndex)
static String roundToNthPlace(double toRound, double accPlace)
SingleLabelChart(XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer, int s, int p, boolean pr)
A line chart implementation with a single label.