From 9e55bfde46ff51886ef1d33ec0456a8ea598466c Mon Sep 17 00:00:00 2001 From: stockiNail Date: Sat, 20 Jan 2024 17:49:50 +0100 Subject: [PATCH] Fix meter and gauge label --- .../client/impl/charts/BaseMeterChart.java | 35 ++---- .../impl/charts/BaseMeterController.java | 100 ++++++++++-------- 2 files changed, 64 insertions(+), 71 deletions(-) diff --git a/src/org/pepstock/charba/client/impl/charts/BaseMeterChart.java b/src/org/pepstock/charba/client/impl/charts/BaseMeterChart.java index 59f6e4477a3..e69ec08ab36 100644 --- a/src/org/pepstock/charba/client/impl/charts/BaseMeterChart.java +++ b/src/org/pepstock/charba/client/impl/charts/BaseMeterChart.java @@ -26,7 +26,6 @@ Licensed to the Apache Software Foundation (ASF) under one import org.pepstock.charba.client.commons.Checker; import org.pepstock.charba.client.controllers.ControllerType; import org.pepstock.charba.client.data.Dataset; -import org.pepstock.charba.client.dom.elements.ImageData; import org.pepstock.charba.client.enums.BorderAlign; import org.pepstock.charba.client.enums.DefaultTransitionMode; import org.pepstock.charba.client.items.FontItem; @@ -42,19 +41,19 @@ Licensed to the Apache Software Foundation (ASF) under one abstract class BaseMeterChart extends AbstractChart implements IsDatasetCreator { /** - * Default of maximum value of data in the a dataset (percentage based), {@value DEFAULT_MAX}. + * Default of maximum value of data in the a dataset (percentage based), + * {@value DEFAULT_MAX}. */ public static final double DEFAULT_MAX = 100D; // maximum amount of datasets private static final int MAXIMUM_DATASETS_COUNT = 1; // controller instance private BaseMeterController meterController = null; - // stores image data to apply on label square - private ImageData imageData = null; /** * Builds the chart.
- * This is must be extended for controller which are based on this type of chart. + * This is must be extended for controller which are based on this type of + * chart. * * @param type type of chart */ @@ -78,7 +77,8 @@ abstract class BaseMeterChart extends AbstractChart impl abstract ControllerType getControllerType(); /** - * Returns the controller instance or null if chart not initialized. + * Returns the controller instance or null if chart not + * initialized. * * @return the controller instance */ @@ -86,24 +86,6 @@ final BaseMeterController getController() { return meterController; } - /** - * Returns the image data, of chart label, previously stored, or null. - * - * @return the image data, of chart label, previously stored, or null - */ - final ImageData getImageData() { - return imageData; - } - - /** - * Sets the image data, of chart label, previously stored, or null. - * - * @param imageData the image data, of chart label, previously stored, or null - */ - final void setImageData(ImageData imageData) { - this.imageData = imageData; - } - /* * (non-Javadoc) * @@ -124,7 +106,8 @@ protected final void applyConfiguration() { // then gets it Controller controllerInstance = Defaults.get().getControllers().getController(type); // checks if controller is a base meter controller - Checker.assertCheck(controllerInstance instanceof BaseMeterController, "Controller stored for " + getControllerType().value() + " is not a BaseMeterController"); + Checker.assertCheck(controllerInstance instanceof BaseMeterController, + "Controller stored for " + getControllerType().value() + " is not a BaseMeterController"); // casts to meter controller meterController = (BaseMeterController) controllerInstance; } @@ -150,7 +133,7 @@ protected final void applyConfiguration() { options.getElements().getArc().setBackgroundColor(MeterDataset.DEFAULT_EMPTY_VALUE_COLOR); options.getElements().getArc().setHoverBackgroundColor(MeterDataset.DEFAULT_EMPTY_VALUE_COLOR); // resets image data - setImageData(null); + // setImageData(null); // creates a new mode every time // because once it has been added to the options // it could be changed by user diff --git a/src/org/pepstock/charba/client/impl/charts/BaseMeterController.java b/src/org/pepstock/charba/client/impl/charts/BaseMeterController.java index 993662002b8..e1f08cb813e 100644 --- a/src/org/pepstock/charba/client/impl/charts/BaseMeterController.java +++ b/src/org/pepstock/charba/client/impl/charts/BaseMeterController.java @@ -34,7 +34,6 @@ Licensed to the Apache Software Foundation (ASF) under one import org.pepstock.charba.client.controllers.ControllerType; import org.pepstock.charba.client.data.Dataset; import org.pepstock.charba.client.dom.elements.Context2dItem; -import org.pepstock.charba.client.dom.elements.ImageData; import org.pepstock.charba.client.dom.elements.TextMetricsItem; import org.pepstock.charba.client.dom.enums.TextBaseline; import org.pepstock.charba.client.enums.TextAlign; @@ -50,7 +49,8 @@ Licensed to the Apache Software Foundation (ASF) under one import org.pepstock.charba.client.utils.Utilities; /** - * Controller implementation to create charts like meter of gauges, extending doughnut chart. + * Controller implementation to create charts like meter of gauges, extending + * doughnut chart. * * @author Andrea "Stock" Stocchero */ @@ -81,12 +81,15 @@ private BaseMeterController(ControllerType type) { /* * (non-Javadoc) * - * @see org.pepstock.charba.client.Controller#onBeforeInitialize(org.pepstock.charba.client.controllers.ControllerContext, org.pepstock.charba.client.IsChart) + * @see + * org.pepstock.charba.client.Controller#onBeforeInitialize(org.pepstock.charba. + * client.controllers.ControllerContext, org.pepstock.charba.client.IsChart) */ @Override public void onBeforeInitialize(ControllerContext context, IsChart chart) { // checks if arguments are consistent - Checker.assertCheck(Controller.isConsistent(this, context, chart), "Initialize method arguments are not consistent"); + Checker.assertCheck(Controller.isConsistent(this, context, chart), + "Initialize method arguments are not consistent"); // gets the data set at index Dataset dataset = chart.getData().getDatasets().get(context.getIndex()); // casts to meter data set @@ -103,13 +106,16 @@ public void onBeforeInitialize(ControllerContext context, IsChart chart) { /* * (non-Javadoc) * - * @see org.pepstock.charba.client.Controller#onBeforeUpdate(org.pepstock.charba.client.controllers.ControllerContext, org.pepstock.charba.client.IsChart, + * @see + * org.pepstock.charba.client.Controller#onBeforeUpdate(org.pepstock.charba. + * client.controllers.ControllerContext, org.pepstock.charba.client.IsChart, * org.pepstock.charba.client.options.TransitionMode) */ @Override public void onBeforeUpdate(ControllerContext context, IsChart chart, TransitionMode mode) { // checks if arguments are consistent - Checker.assertCheck(Controller.isConsistent(this, context, chart), "Before updating method arguments are not consistent"); + Checker.assertCheck(Controller.isConsistent(this, context, chart), + "Before updating method arguments are not consistent"); // gets options reference MeterOptions options = null; // checks if meter chart @@ -128,20 +134,25 @@ public void onBeforeUpdate(ControllerContext context, IsChart chart, TransitionM /* * (non-Javadoc) * - * @see org.pepstock.charba.client.Controller#onBeforeDraw(org.pepstock.charba.client.controllers.ControllerContext, org.pepstock.charba.client.IsChart) + * @see + * org.pepstock.charba.client.Controller#onBeforeDraw(org.pepstock.charba.client + * .controllers.ControllerContext, org.pepstock.charba.client.IsChart) */ @Override public void onBeforeDraw(ControllerContext context, IsChart chart) { // checks if arguments are consistent // checks if the index is 0 because // only the data set 0 contains my value - Checker.assertCheck(Controller.isConsistent(this, context, chart) && context.getIndex() == 0, "Draw method arguments are not consistent"); + Checker.assertCheck(Controller.isConsistent(this, context, chart) && context.getIndex() == 0, + "Draw method arguments are not consistent"); } /* * (non-Javadoc) * - * @see org.pepstock.charba.client.Controller#onAfterDraw(org.pepstock.charba.client.controllers.ControllerContext, org.pepstock.charba.client.IsChart) + * @see + * org.pepstock.charba.client.Controller#onAfterDraw(org.pepstock.charba.client. + * controllers.ControllerContext, org.pepstock.charba.client.IsChart) */ @Override public void onAfterDraw(ControllerContext context, IsChart chart) { @@ -155,7 +166,7 @@ public void onAfterDraw(ControllerContext context, IsChart chart) { * Draws the labels in the center of doughnut chart. * * @param chart chart instance - * @param node native chart as chart node + * @param node native chart as chart node */ void drawLabels(IsChart chart, ChartNode node) { // gets the list of data sets @@ -183,7 +194,7 @@ void drawLabels(IsChart chart, ChartNode node) { /** * Calculates the easing based on circumference of the data set element. * - * @param chart chart instance to use for calculation + * @param chart chart instance to use for calculation * @param dataset meter data set to get the ratio between value and maximum * @return easing of drawing (between 0 and 1) for animation */ @@ -219,13 +230,15 @@ private double calculateEase(IsChart chart, MeterDataset dataset) { /** * Draws the value inside the inner radius of doughnut. * - * @param chart chart instance - * @param item chart item with CHART.JS properties needed to calculate the area where to drawn the value + * @param chart chart instance + * @param item chart item with CHART.JS properties needed to calculate the + * area where to drawn the value * @param dataset the data set instance * @param options the chart options - * @param ease easing of drawing (between 0 and 1) for animation + * @param ease easing of drawing (between 0 and 1) for animation */ - private void execute(BaseMeterChart chart, ChartNode item, MeterDataset dataset, MeterOptions options, double ease) { + private void execute(BaseMeterChart chart, ChartNode item, MeterDataset dataset, MeterOptions options, + double ease) { // gets elements ValueLabel valueLabel = dataset.getValueLabel(); DescriptionLabel descriptionLabel = dataset.getDescriptionLabel(); @@ -238,8 +251,7 @@ private void execute(BaseMeterChart chart, ChartNode item, MeterDataset datas // it can not be null DatasetItem datasetMetaItem = chart.getDatasetItem(0); // calculate the side of the square where to draw the value - // minus 1 because of the image data to put and the round management - final double sideOfSquare = Math.floor((datasetMetaItem.getController().getInnerRadius() * 2) / SQRT_2) - 1; + final double sideOfSquare = Math.floor((datasetMetaItem.getController().getInnerRadius() * 2) / SQRT_2); // gets canvas context 2d Context2dItem ctx = chart.getCanvas().getContext2d(); // gets the chart area of CHART.JS @@ -252,7 +264,8 @@ private void execute(BaseMeterChart chart, ChartNode item, MeterDataset datas // gets max value final double maxValue = valueLabel.isPercentage() ? MAX_PERCENTAGE : dataset.getMax(); // gets value - final double valueToCalculate = valueLabel.isPercentage() ? dataset.getValue() / dataset.getMax() : dataset.getValue(); + final double valueToCalculate = valueLabel.isPercentage() ? dataset.getValue() / dataset.getMax() + : dataset.getValue(); // here is calculating the value to showed // based on easing of drawing final double value = valueLabel.isAnimated() ? valueToCalculate * ease : valueToCalculate; @@ -300,18 +313,6 @@ private void execute(BaseMeterChart chart, ChartNode item, MeterDataset datas ctx.rect(x, y, sideOfSquare, sideOfSquare); // clip area ctx.clip(); - // checks if image data must be get - if (chart.getImageData() == null) { - // gets image data in the center, 1 pixel more - // because of rounded of dimensions - ImageData data = ctx.getImageData(x, y, sideOfSquare + 1, sideOfSquare + 1); - // stores the image data - chart.setImageData(data); - } - // clears the previous label - ctx.clearRect(x, y, sideOfSquare, sideOfSquare); - // sets stored image data to canvas - ctx.putImageData(chart.getImageData(), x, y); // checks if auto font size is set if (valueLabel.isAutoFontSize()) { // calculates the font size @@ -371,12 +372,13 @@ private void execute(BaseMeterChart chart, ChartNode item, MeterDataset datas } /** - * Calculates the font size based on available space in the square in the doughnut inner radius. + * Calculates the font size based on available space in the square in the + * doughnut inner radius. * - * @param ctx canvas context + * @param ctx canvas context * @param sideOfSquare side of square - * @param value value to display - * @param font font instance + * @param value value to display + * @param font font instance */ private void calculateFontSize(Context2dItem ctx, double sideOfSquare, String value, IsFont font) { // half of side of square @@ -405,15 +407,17 @@ private void calculateFontSize(Context2dItem ctx, double sideOfSquare, String va /** * Returns the color to apply to rendered label, invoking the callback if set. * - * @param options options of the element - * @param defaultValue default color to use if the callback returns an unconsistent value - * @param context scriptable context of meter + * @param options options of the element + * @param defaultValue default color to use if the callback returns an + * unconsistent value + * @param context scriptable context of meter * @return the font color to apply to rendered label */ private String getColor(AbstractMeterElement options, String defaultValue, MeterContext context) { // checks if the options font color is set as callback if (options.getColorCallback() != null) { - Object result = ScriptableUtil.getOptionValueAsColor(context, options.getColorCallback(), defaultValue, false); + Object result = ScriptableUtil.getOptionValueAsColor(context, options.getColorCallback(), defaultValue, + false); // checks the result if (result instanceof IsColor) { // casts to color @@ -444,7 +448,8 @@ private String getColor(AbstractMeterElement options, String defaultValue, Meter private FontItem getFont(AbstractMeterElement options, MeterContext context) { // checks if the options font is set as callback if (options.getFontCallback() != null) { - FontItem result = ScriptableUtil.getOptionValueAsFont(context, options.getFontCallback(), options.getFontItem()); + FontItem result = ScriptableUtil.getOptionValueAsFont(context, options.getFontCallback(), + options.getFontItem()); // checks the result if (result != null) { return result; @@ -457,7 +462,8 @@ private FontItem getFont(AbstractMeterElement options, MeterContext context) { } /** - * Returns a formatted value of the chart applying the precision or invoking the value callback. + * Returns a formatted value of the chart applying the precision or invoking the + * value callback. * * @param options options of the element * @param context scriptable context of meter @@ -509,10 +515,11 @@ private String getContent(DescriptionLabel options, MeterContext context) { } /** - * Calculates the cutout of the doughnut chart,using the thickness passed by user in the options. + * Calculates the cutout of the doughnut chart,using the thickness passed by + * user in the options. * * @param options chart options instance - * @param item dataset item + * @param item dataset item */ private void applyCutoutPercentage(MeterOptions options, DatasetItem item) { // checks if options is consistent @@ -524,13 +531,15 @@ private void applyCutoutPercentage(MeterOptions options, DatasetItem item) { // checks if thickness is defined if (Undefined.isNot(thickness) && Checker.isPositive(radius) && Double.compare(thickness, radius) < 0) { // calculates and sets the percentage - options.setInternalCutoutPercentage(Utilities.getAsPercentage((radius - thickness) / radius, MeterOptions.DEFAULT_CUTOUT_PERCENTAGE)); + options.setInternalCutoutPercentage(Utilities.getAsPercentage((radius - thickness) / radius, + MeterOptions.DEFAULT_CUTOUT_PERCENTAGE)); } } } /** - * Inner class which is implementing a {@link ControllerProvider} to create a base meter controller. + * Inner class which is implementing a {@link ControllerProvider} to create a + * base meter controller. * * @author Andrea "Stock" Stocchero * @@ -540,7 +549,8 @@ private static class BaseMeterControllerProvier implements ControllerProvider { /* * (non-Javadoc) * - * @see org.pepstock.charba.client.controllers.ControllerProvider#provide(org.pepstock.charba.client.controllers.ControllerType) + * @see org.pepstock.charba.client.controllers.ControllerProvider#provide(org. + * pepstock.charba.client.controllers.ControllerType) */ @Override public Controller provide(ControllerType controllerType) {