@Test @JSTranslation(ignore = true) public void testJavaDates() { long feb_1_1970 = 60907593600000L; Field f = Data.makeColumnField( "test", "dates", new Object[] { new Date(feb_1_1970), new Date(feb_1_1970 + 1000 * 60 * 60 * 24), new Date(feb_1_1970 + 2 * 1000 * 60 * 60 * 24), new Date(feb_1_1970 + 3 * 1000 * 60 * 60 * 24), new Date(feb_1_1970 + 4 * 1000 * 60 * 60 * 24), new Date(feb_1_1970 + 5 * 1000 * 60 * 60 * 24), new Date(feb_1_1970 + 6 * 1000 * 60 * 60 * 24), new Date(feb_1_1970 + 7 * 1000 * 60 * 60 * 24), new Date(feb_1_1970 + 8 * 1000 * 60 * 60 * 24), new Date(feb_1_1970 + 9 * 1000 * 60 * 60 * 24), new Date(feb_1_1970 + 10 * 1000 * 60 * 60 * 24), new Date(feb_1_1970 + 20 * 1000 * 60 * 60 * 24) }); f.set("numeric", true); f.set("date", true); assertTrue(f.isDate()); assertEquals("Feb 1 00:00", f.valueFormatted(0)); assertEquals("Feb 2 00:00", f.valueFormatted(1)); assertEquals("Feb 3 00:00", f.valueFormatted(2)); assertEquals("Feb 11 00:00", f.valueFormatted(10)); assertEquals("Feb 21 00:00", f.valueFormatted(11)); }
@Test public void testProperties() { Field f = Data.makeColumnField("a", "label", new Object[0]); assertEquals(null, f.property("xyz")); f.set("xyz", "12"); assertEquals("12", f.stringProperty("xyz")); assertEquals(12.00001, f.numericProperty("xyz"), 0.001); f.set("abc", false); assertEquals(Boolean.FALSE, f.property("abc")); f.set("abc", true); assertEquals(Boolean.TRUE, f.property("abc")); }
private void setProperties(String f, Field to, Field src) { // Nothing to set for a list if (f.equals("list")) { // Need to keep the date format to.copyProperties(src, "dateFormat"); to.set("list", true); } else if (f.equals("count") || f.equals("percent") || f.equals("valid") || f.equals("unique")) // These are numeric, but do not preserve properties to.setNumeric(); else // All properties copy over Fields.copyBaseProperties(src, to); }
@Test public void testPadding() { Field a = Data.toNumeric(Data.makeColumnField("a", "label", new Object[] {100, 200, 120, 200, 3100})); a.set("transform", "linear"); Assert.assertEquals( "linear : 70 3130 : |500|1000|1500|2000|2500|3000", asString( NumericScale.makeLinearScale(a, false, 0.0, new double[] {0.01, 0.01}, 5, false), a)); // Does not pad past zero Assert.assertEquals( "linear : -200 3400 : |0|1000|2000|3000", asString( NumericScale.makeLinearScale(a, false, 0.0, new double[] {0.1, 0.1}, 5, false), a)); }
@Test public void testLinearScale() { double[] pad = {0, 0}; Field a = Data.makeColumnField("a", "label", new Object[] {2, 4, 7, 120, 45, 120, 200, 3345}); a = Data.toNumeric(a); a.set("transform", "linear"); Assert.assertEquals( "linear : 2 3345 : |500|1000|1500|2000|2500|3000", asString(NumericScale.makeLinearScale(a, false, 0.0, pad, 5, false), a)); Assert.assertEquals( "linear : 2 3345 : |200|400|600|800|1000|1200|1400|1600|1800|2000|2200|2400|2600|2800|3000|3200", asString(NumericScale.makeLinearScale(a, false, 0.0, pad, 15, false), a)); Assert.assertEquals( "linear : 2 3345 : |1000|2000|3000", asString(NumericScale.makeLinearScale(a, false, 0.0, pad, 2, false), a)); Assert.assertEquals( "linear : 2 3345 : |2000", asString(NumericScale.makeLinearScale(a, false, 0.0, pad, 1, false), a)); Assert.assertEquals( "linear : 0 3345 : |0|500|1000|1500|2000|2500|3000", asString(NumericScale.makeLinearScale(a, false, 0.01, pad, 7, false), a)); Assert.assertEquals( "linear : 0 3500 : |0|500|1000|1500|2000|2500|3000|3500", asString(NumericScale.makeLinearScale(a, true, 0.0, pad, 7, false), a)); a = Data.makeColumnField("a", "label", new Object[] {-22.2, -22.201, -22.9, -22.7}); a = Data.toNumeric(a); Assert.assertEquals( "linear : -23 -22 : |-23|-22.8|-22.6|-22.4|-22.2|-22", asString(Auto.makeNumericScale(a, true, pad, 0.0, 5, false), a)); Assert.assertEquals( "linear : -22.9 -22.2 : |-22.8|-22.6|-22.4|-22.2", asString(NumericScale.makeLinearScale(a, false, 0.0, pad, 4, false), a)); Assert.assertEquals( "linear : -23 -22 : |-23|-22.5|-22", asString(Auto.makeNumericScale(a, true, pad, 0.0, 4, false), a)); Assert.assertEquals( "linear : -23 -22 : |-23|-22.5|-22", asString(Auto.makeNumericScale(a, true, pad, 0.95, 4, false), a)); Assert.assertEquals( "linear : -30 0 : |-30|-20|-10|0", asString(NumericScale.makeLinearScale(a, true, 0.99, pad, 4, false), a)); }
private Field[] make() { // Assemble arrays of fields Field[] dimensionFields = getFields(dimensions); Field[] percentBaseFields = percentBase.toArray(new Field[percentBase.size()]); Field[] measureFields = getFields(measures); // The comparators to let us know when rows differ FieldRowComparison dimComparison = new FieldRowComparison(dimensionFields, null, false); FieldRowComparison percentBaseComparison = new FieldRowComparison(percentBaseFields, null, false); // group[row] gives the index of the summary group for row 'row'; 'groupCount' is the number of // groups int[] group = new int[rowCount]; int groupCount = buildGroups(group, dimComparison); // These are just like the summary groups, but only for the percent bases // The percent groups nest within each base group: rows with the same group have the same // summary group also // we do not create these if they are not needed, for efficiency int[] percentGroup = percentNeeded ? new int[rowCount] : null; int percentGroupCount = percentNeeded ? buildGroups(percentGroup, percentBaseComparison) : 0; // Create the summary values for each group, and percentage sums SummaryValues[] summaries = new SummaryValues[groupCount]; for (int i = 0; i < summaries.length; i++) summaries[i] = new SummaryValues(measureFields, percentBaseFields, dimensionFields); double[][] percentSums = new double[percentGroupCount][measureFields.length]; // Perform the Aggregation for (int row = 0; row < rowCount; row++) { SummaryValues value = summaries[group[row]]; if (percentNeeded) { // If the group has not had percent sums set yet, then set it if (value.percentSums == null) value.percentSums = percentSums[percentGroup[row]]; // Then add the values to the percentage count for (int i = 0; i < measureFields.length; i++) { if (measures.get(i).isPercent()) { Double v = Data.asNumeric(measureFields[i].value(row)); if (v != null) value.percentSums[i] += v; } } } // Add the current row to that group, with the relevant 'sums' for percentages value.rows.add(row); } Object[][] dimData = new Object[dimensions.size()][groupCount]; Object[][] measureData = new Object[measures.size()][groupCount]; for (int g = 0; g < groupCount; g++) { SummaryValues values = summaries[g]; int originalRow = values.firstRow(); // Set the dimension values for (int i = 0; i < dimensions.size(); i++) dimData[i][g] = dimensionFields[i].value(originalRow); // Set the measure values for (int i = 0; i < measures.size(); i++) { MeasureField m = measures.get(i); measureData[i][g] = values.get(i, m); } } // Assemble fields Field[] fields = new Field[dimData.length + measureData.length]; for (int i = 0; i < dimData.length; i++) { DimensionField f = dimensions.get(i); fields[i] = Fields.makeColumnField(f.rename, f.label(), dimData[i]); Fields.copyBaseProperties(f.field, fields[i]); } for (int i = 0; i < measureData.length; i++) { MeasureField m = measures.get(i); Field result = Fields.makeColumnField(m.rename, m.label(), measureData[i]); setProperties(m.method, result, m.field); result.set("summary", m.method); result.set("calculated", true); if (m.field != null) result.set("originalLabel", m.field.label); fields[dimData.length + i] = result; } return fields; }