ChartUtils.java

  1. package com.hack23.cia.web.impl.ui.application.views.common.chartfactory.impl;

  2. import java.util.Optional;

  3. import org.dussan.vaadin.dcharts.DCharts;

  4. import com.hack23.cia.model.internal.application.data.party.impl.ViewRiksdagenParty;
  5. import com.hack23.cia.service.api.ApplicationManager;
  6. import com.hack23.cia.service.api.DataContainer;
  7. import com.hack23.cia.web.impl.ui.application.views.common.sizing.ContentRatio;
  8. import com.vaadin.server.Page;
  9. import com.vaadin.server.Sizeable.Unit;
  10. import com.vaadin.ui.AbstractOrderedLayout;
  11. import com.vaadin.ui.HorizontalLayout;
  12. import com.vaadin.ui.Panel;

  13. /**
  14.  * The Class ChartUtils.
  15.  */
  16. public final class ChartUtils {

  17.     /** The Constant CHART_BOTTOM_MARGIN_SIZE. */
  18.     private static final int CHART_BOTTOM_MARGIN_SIZE = 2;

  19.     /** The Constant CHART_LEFT_MARGIN. */
  20.     private static final int CHART_LEFT_MARGIN = 2;

  21.     /**  The Constant CHART_RIGHT_MARGIN = 2;. */
  22.     private static final int CHART_RIGHT_MARGIN = 2;

  23.     /** The Constant CHART_TOP_MARGIN_SIZE. */
  24.     private static final int CHART_TOP_MARGIN_SIZE = 2;

  25.     /** The Constant CHART_WIDTH_REDUCTION. */
  26.     private static final int CHART_WIDTH_REDUCTION = 50;

  27.     /** The Constant HEIGHT_PERCENTAGE_FULL_PAGE. */
  28.     private static final double HEIGHT_PERCENTAGE_FULL_PAGE = 0.8;

  29.     /** The Constant HEIGHT_PERCETAGE_HALF_PAGE. */
  30.     private static final double HEIGHT_PERCETAGE_HALF_PAGE = 0.5;

  31.     /** The Constant MINIMUM_CHART_HEIGHT_FULL_PAGE. */
  32.     private static final int MINIMUM_CHART_HEIGHT_FULL_PAGE = 400;

  33.     /** The Constant MINIMUM_CHART_WIDTH. */
  34.     private static final int MINIMUM_CHART_WIDTH = 600;

  35.     /** The Constant NINIMUM_CHART_HEIGHT_HALF_PAGE. */
  36.     private static final int NINIMUM_CHART_HEIGHT_HALF_PAGE = 200;

  37.     /**
  38.      * Instantiates a new chart utils.
  39.      */
  40.     private ChartUtils() {
  41.         // Utility class
  42.     }

  43.     /**
  44.      * Gets the chart window height.
  45.      *
  46.      * @param isFullPage the is full page
  47.      * @return the chart window height
  48.      */
  49.     public static int getChartWindowHeight(final boolean isFullPage) {
  50.         if (isFullPage) {
  51.             return Math.max((int) (Page.getCurrent().getBrowserWindowHeight() * HEIGHT_PERCENTAGE_FULL_PAGE), MINIMUM_CHART_HEIGHT_FULL_PAGE);
  52.         } else {
  53.             return Math.max((int) (Page.getCurrent().getBrowserWindowHeight() * HEIGHT_PERCETAGE_HALF_PAGE), NINIMUM_CHART_HEIGHT_HALF_PAGE);
  54.         }
  55.     }

  56.     /**
  57.      * Gets the chart window width.
  58.      *
  59.      * @return the chart window width
  60.      */
  61.     public static int getChartWindowWidth() {
  62.         return Math.max(Page.getCurrent().getBrowserWindowWidth() - CHART_WIDTH_REDUCTION, MINIMUM_CHART_WIDTH);
  63.     }

  64.     /**
  65.      * Adds the chart.
  66.      *
  67.      * @param layout the layout
  68.      * @param caption the caption
  69.      * @param chart the chart
  70.      * @param isFullPage the is full page
  71.      */
  72.     public static void addChart(final AbstractOrderedLayout layout, final String caption, final DCharts chart, final boolean isFullPage) {
  73.         final HorizontalLayout horizontalLayout = new HorizontalLayout();

  74.         final int browserWindowWidth = getChartWindowWidth();
  75.         final int browserWindowHeight = getChartWindowHeight(isFullPage);

  76.         horizontalLayout.setWidth(browserWindowWidth, Unit.PIXELS);
  77.         horizontalLayout.setHeight(browserWindowHeight, Unit.PIXELS);
  78.         horizontalLayout.setMargin(true);
  79.         horizontalLayout.setSpacing(false);
  80.         horizontalLayout.addStyleName("v-layout-content-overview-panel-level1");

  81.         final Panel formPanel = new Panel();
  82.         formPanel.setSizeFull();
  83.         formPanel.setContent(horizontalLayout);
  84.         formPanel.setCaption(caption);

  85.         layout.addComponent(formPanel);
  86.         layout.setExpandRatio(formPanel, ContentRatio.LARGE);

  87.         chart.setWidth(100, Unit.PERCENTAGE);
  88.         chart.setHeight(100, Unit.PERCENTAGE);
  89.         chart.setMarginRight(CHART_RIGHT_MARGIN);
  90.         chart.setMarginLeft(CHART_LEFT_MARGIN);
  91.         chart.setMarginBottom(CHART_BOTTOM_MARGIN_SIZE);
  92.         chart.setMarginTop(CHART_TOP_MARGIN_SIZE);

  93.         horizontalLayout.addComponent(chart);
  94.         chart.setCaption(caption);
  95.     }

  96.     /**
  97.      * Gets the party name.
  98.      *
  99.      * @param applicationManager the application manager
  100.      * @param partySummary the party summary
  101.      * @return the party name
  102.      */
  103.     public static String getPartyName(final ApplicationManager applicationManager, final String partySummary) {
  104.         final DataContainer<ViewRiksdagenParty, String> partyDataContainer = applicationManager.getDataContainer(ViewRiksdagenParty.class);
  105.         final ViewRiksdagenParty party = partyDataContainer.load(partySummary);
  106.         return Optional.ofNullable(party).map(ViewRiksdagenParty::getPartyName).orElse(null);
  107.     }

  108.     /**
  109.      * Gets the role color.
  110.      *
  111.      * @param roleCode the role code
  112.      * @param role1 the role 1
  113.      * @param role2 the role 2
  114.      * @return the role color
  115.      */
  116.     public static String getRoleColor(final String roleCode, final String role1, final String role2) {
  117.         if (roleCode.equalsIgnoreCase(role1)) {
  118.             return "red";
  119.         } else if (roleCode.equalsIgnoreCase(role2)) {
  120.             return "blue";
  121.         } else {
  122.             return "green";
  123.         }
  124.     }
  125. }