Merge "Improve X axis labels in battery chart (1)"

This commit is contained in:
TreeHugger Robot
2022-07-31 02:52:19 +00:00
committed by Android (Google) Code Review
5 changed files with 74 additions and 23 deletions

View File

@@ -279,15 +279,17 @@ public class BatteryChartPreferenceControllerV2 extends AbstractPreferenceContro
batteryLevelData.getDailyBatteryLevels().getLevels(),
generateTimestampDayOfWeekTexts(
mContext, batteryLevelData.getDailyBatteryLevels().getTimestamps()),
mDailyChartIndex);
mDailyChartIndex,
BatteryChartViewModel.AxisLabelPosition.CENTER_OF_TRAPEZOIDS);
mHourlyViewModels = new ArrayList<>();
for (BatteryLevelData.PeriodBatteryLevelData perDayData :
for (BatteryLevelData.PeriodBatteryLevelData hourlyBatteryLevelsPerDay :
batteryLevelData.getHourlyBatteryLevelsPerDay()) {
mHourlyViewModels.add(new BatteryChartViewModel(
perDayData.getLevels(),
hourlyBatteryLevelsPerDay.getLevels(),
generateTimestampHourTexts(
mContext, perDayData.getTimestamps()),
mHourlyChartIndex));
mContext, hourlyBatteryLevelsPerDay.getTimestamps()),
mHourlyChartIndex,
BatteryChartViewModel.AxisLabelPosition.BETWEEN_TRAPEZOIDS));
}
refreshUi();
// TODO: Loads item icon and label and build mBatteryIndexedMap.

View File

@@ -33,23 +33,31 @@ class BatteryChartViewModel {
// We need at least 2 levels to draw a trapezoid.
private static final int MIN_LEVELS_DATA_SIZE = 2;
enum AxisLabelPosition {
BETWEEN_TRAPEZOIDS,
CENTER_OF_TRAPEZOIDS,
}
private final List<Integer> mLevels;
private final List<String> mTexts;
private final AxisLabelPosition mAxisLabelPosition;
private int mSelectedIndex;
BatteryChartViewModel(
@NonNull List<Integer> levels, @NonNull List<String> texts, int selectedIndex) {
@NonNull List<Integer> levels, @NonNull List<String> texts, int selectedIndex,
@NonNull AxisLabelPosition axisLabelPosition) {
Preconditions.checkArgument(
levels.size() == texts.size()
&& levels.size() >= MIN_LEVELS_DATA_SIZE
&& selectedIndex >= SELECTED_INDEX_ALL
&& selectedIndex < levels.size(),
String.format(Locale.getDefault(), "Invalid BatteryChartViewModel"
String.format(Locale.ENGLISH, "Invalid BatteryChartViewModel"
+ " levels.size: %d\ntexts.size: %d\nselectedIndex: %d.",
levels.size(), texts.size(), selectedIndex));
mLevels = levels;
mTexts = texts;
mSelectedIndex = selectedIndex;
mAxisLabelPosition = axisLabelPosition;
}
public int size() {
@@ -72,9 +80,13 @@ class BatteryChartViewModel {
mSelectedIndex = index;
}
public AxisLabelPosition axisLabelPosition() {
return mAxisLabelPosition;
}
@Override
public int hashCode() {
return Objects.hash(mLevels, mTexts, mSelectedIndex);
return Objects.hash(mLevels, mTexts, mSelectedIndex, mAxisLabelPosition);
}
@Override
@@ -87,12 +99,15 @@ class BatteryChartViewModel {
final BatteryChartViewModel batteryChartViewModel = (BatteryChartViewModel) other;
return Objects.equals(mLevels, batteryChartViewModel.mLevels)
&& Objects.equals(mTexts, batteryChartViewModel.mTexts)
&& mSelectedIndex == batteryChartViewModel.mSelectedIndex;
&& mSelectedIndex == batteryChartViewModel.mSelectedIndex
&& mAxisLabelPosition == batteryChartViewModel.mAxisLabelPosition;
}
@Override
public String toString() {
return String.format(Locale.getDefault(), "levels: %s\ntexts: %s\nselectedIndex: %d",
Objects.toString(mLevels), Objects.toString(mTexts), mSelectedIndex);
return String.format(Locale.ENGLISH,
"levels: %s\ntexts: %s\nselectedIndex: %d, axisLabelPosition: %s",
Objects.toString(mLevels), Objects.toString(mTexts), mSelectedIndex,
mAxisLabelPosition);
}
}

View File

@@ -441,11 +441,19 @@ public class BatteryChartViewV2 extends AppCompatImageView implements View.OnCli
for (int index = 0; index < DEFAULT_AXIS_LABEL_COUNT; index++) {
xOffsets[index] = baselineX + index * offsetX * slotBarOffset;
}
drawAxisLabel(canvas, xOffsets);
switch (mViewModel.axisLabelPosition()) {
case CENTER_OF_TRAPEZOIDS:
drawAxisLabelsCenterOfTrapezoids(canvas, xOffsets, unitWidth);
break;
case BETWEEN_TRAPEZOIDS:
default:
drawAxisLabelsBetweenTrapezoids(canvas, xOffsets);
break;
}
}
}
private void drawAxisLabel(Canvas canvas, float[] xOffsets) {
private void drawAxisLabelsBetweenTrapezoids(Canvas canvas, float[] xOffsets) {
// Draws the 1st axis label info.
canvas.drawText(
mAxisLabels[0], xOffsets[0] - mAxisLabelsBounds[0].left, getAxisLabelY(0),
@@ -471,6 +479,18 @@ public class BatteryChartViewV2 extends AppCompatImageView implements View.OnCli
}
}
private void drawAxisLabelsCenterOfTrapezoids(
Canvas canvas, float[] xOffsets, float unitWidth) {
for (int index = 0; index < DEFAULT_AXIS_LABEL_COUNT - 1; index++) {
canvas.drawText(
mAxisLabels[index],
xOffsets[index] + (unitWidth - (mAxisLabelsBounds[index].width()
- mAxisLabelsBounds[index].left)) * .5f,
getAxisLabelY(index),
mTextPaint);
}
}
private int getAxisLabelY(int index) {
return getHeight()
- mAxisLabelsBounds[index].height()