Prechádzať zdrojové kódy

Updates in vertical markings resolution, reset scroll offset on adapter change

Berislav 9 rokov pred
rodič
commit
ae52ec5430

+ 1 - 1
.idea/misc.xml

@@ -37,7 +37,7 @@
     <ConfirmationsSetting value="0" id="Add" />
     <ConfirmationsSetting value="0" id="Remove" />
   </component>
-  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
     <output url="file://$PROJECT_DIR$/build/classes" />
   </component>
   <component name="ProjectType">

+ 28 - 5
app/src/main/java/hr/tacitus/graphs/BarGraphView.java

@@ -447,6 +447,10 @@ public class BarGraphView extends View {
 
             // max vrijednost unutar Data arraya, max vrijednost podataka koje imamo
             maxDataValue = getDataMaxValue();
+            String maxValueFormatted = adapter.getVerticalMarkingText(maxDataValue);
+            float maxValueTextWidth = verticalAxisRect.getPaintLine().measureText(maxValueFormatted);
+            verticalAxisValues.setMinDistanceBetweenMarkings(maxValueTextWidth);
+
             // max vrijednost grafa, ovisi o max vrijednosti data arraya, ali nije
             // ista (nekad imamo lufta)
             maxGraphValue = getGraphMaxValue(maxDataValue);
@@ -464,6 +468,8 @@ public class BarGraphView extends View {
 
     public void setAdapter(BarGraphAdapter adapter) {
         this.adapter = adapter;
+        // Reset scroll
+        this.offsetX = 0;
 
         adapterChanged();
     }
@@ -608,13 +614,30 @@ public class BarGraphView extends View {
         }
     }
 
+    /**
+     * Best values of vertical markings are satisfying
+     * 1) n = 2^k or
+     * 2) n = 5 * k.
+     * Try to find highest possible value
+     * E.g.
+     * 7 is not good, but could be lowered to 5 (satisfies no. 2).
+     * 8 is ok (no. 1)
+     * 12 -> 10 (no. 2)
+     *
+     * @param distance minimum possible distance
+     * @return number of vertical markings
+     */
     private int calculateMaximumNumberOfVerticalMarkings(float distance) {
-        // na koliko dijelova možemo raspodijeliti
         int discreteY = (int) (getGraphHeight() / distance);
-        // trazimo najmanju nizu potenciju od 2 koja bi odgovorala discreteY
-        int log2 = (int) Math.floor(Math.log(discreteY) / Math.log(2.));
-        int normDiscreteY = (int) Math.pow(2, log2);
-        return normDiscreteY;
+
+        // Find greatest k which satisfies discreteY >= 2^k (no 1)
+        int k = (int) Math.floor(Math.log(discreteY) / Math.log(2.));
+        int powerOfTwoSolution = (int) Math.pow(2, k);
+
+        // Find greatest k which satisfies discrete> >= 5*k
+        int multipleOfFiveSolution = (discreteY / 5) * 5;
+        // Return the greatest value.
+        return Math.max(powerOfTwoSolution, multipleOfFiveSolution);
     }
 
     private double getDataMaxValue() {

+ 3 - 2
graphsapp/src/main/java/hr/tacitus/graphsapp/MainActivity.java

@@ -28,7 +28,8 @@ public class MainActivity extends AppCompatActivity {
     private BarGraphAdapter createSampleAdapter() {
         return new BarGraphAdapter() {
 
-            double[] testValues = { 0.54, 3.77, 2.44, 2.00, 2.5, 5.32, 1., 10, 3.66, 2.11, 4.6, 5.76, 23.23, 7.99, 5.09, 6.7, 14.22, 4.55 };
+            double[] testValues = { 0.54, 3.77, 2.44, 2.00, 2.5, 5.32, 1., 4.55, 3.66, 2.11, 4.6, 5.76, 23.23, 7.99, 5.09, 6.7, 14.22, 4.55 };
+            // double[] testValues = {5500.f, 16000, 17000, 13300, 24000, 18000, 13000};
 
             @Override
             public int getCount() {
@@ -52,7 +53,7 @@ public class MainActivity extends AppCompatActivity {
 
             @Override
             public String getVerticalMarkingText(double markingValue) {
-                return new DecimalFormat("0.0").format(markingValue);
+                return new DecimalFormat("0.00").format(markingValue);
             }
 
         };

+ 2 - 9
graphsapp/src/main/res/layout/activity_main.xml

@@ -25,19 +25,12 @@
         android:id="@+id/bgvGraf"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
-        android:paddingTop="24dp"
+        android:paddingTop="42dp"
         android:paddingLeft="6dp"
         android:paddingBottom="6dp"
         android:paddingRight="6dp"
 
-        app:barTextSize="16sp"
-        app:barPadding="4dp"
-        app:yAxisText="Time"
-        app:xAxisText="Testiram"
-        app:axisValuesTextSize="12sp"
-        app:showAverageLine="false"
-        app:showHorizontalValues="true"
-        app:noDataText="Shame, no data"
+
 
         />