Debian Codegraph @

src/dr/app/gui/chart/DiscreteAxis.java

raw symbols SCIP
1 /*
2 * DiscreteAxis.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.app.gui.chart;
27
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
33 public class DiscreteAxis extends Axis.AbstractAxis {
34
35 private final boolean originBetweenCategories;
36 private final boolean showEveryCategory;
37 private final Map<Integer, String> categoryLabelMap;
38
39 public DiscreteAxis(boolean originBetweenCategories, boolean showEveryCategory) {
40 this(null,originBetweenCategories, showEveryCategory);
41 }
42
43 public DiscreteAxis(Map<Integer, String> categoryLabelMap, boolean originBetweenCategories, boolean showEveryCategory) {
44 super(AT_MAJOR_TICK, AT_MAJOR_TICK, true);
45
46 this.originBetweenCategories = originBetweenCategories;
47 this.showEveryCategory = showEveryCategory;
48
49 this.categoryLabelMap = categoryLabelMap;
50
51 prefMajorTickCount = 20;
52 }
53
54 /**
55 * Transform a value
56 */
57 public double transform(double value) {
58 return value; // a linear transform !
59 }
60
61 /**
62 * Untransform a value
63 */
64 public double untransform(double value) {
65 return value; // a linear transform !
66 }
67
68 @Override
69 public String format(double value) {
70 if (categoryLabelMap != null) {
71 String label = categoryLabelMap.get((int)value);
72 if (label == null) {
73 return "Missing";
74 }
75 return label;
76 }
77 return super.format(value);
78 }
79
80 public void calibrate() {
81 majorTick=1;
82 minorTick=1;
83
84 minTick=minData;
85 maxTick=maxData;
86 majorTickCount = (int)((maxTick-minTick)/majorTick)+1;
87 minorTickCount = 0;
88
89 if (!showEveryCategory) {
90 while (majorTickCount > prefMajorTickCount) {
91 majorTickCount=(int)((maxTick-minTick)/(majorTick*2))+1;
92 if (majorTickCount <= prefMajorTickCount) {
93 majorTick*=2;
94 break;
95 }
96 majorTickCount=(int)((maxTick-minTick)/(majorTick*4))+1;
97 if (majorTickCount <= prefMajorTickCount) {
98 majorTick*=4;
99 break;
100 }
101 majorTickCount=(int)((maxTick-minTick)/(majorTick*5))+1;
102 if (majorTickCount <= prefMajorTickCount) {
103 majorTick*=5;
104 break;
105 }
106
107 majorTick*=10;
108 majorTickCount=(int)((maxTick-minTick)/majorTick)+1;
109 }
110 }
111
112 minorTickCount=(int)(majorTick)-1;
113
114 minAxis=minTick;
115 maxAxis=maxTick;
116
117 // handleAxisFlags();
118
119 if (originBetweenCategories) {
120 minAxis-=0.5;
121 maxAxis+=0.5;
122 }
123 }
124 }