All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.svg.SvgGenerationSettings Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.svg;

import java.awt.Font;

import com.hfg.graphics.FontUtil;
import com.hfg.graphics.units.GfxSize;
import com.hfg.graphics.units.GfxUnits;
import com.hfg.graphics.units.Pixels;
import com.hfg.math.Range;
import com.hfg.setting.Settings;
import com.hfg.setting.StringSetting;
import com.hfg.util.StringUtil;

//------------------------------------------------------------------------------
/**
 * General settings for use with generating SVGs.
 *
 * @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class SvgGenerationSettings extends Settings
{

   private static final String WIDTH                   = "width";
   private static final String HEIGHT                  = "height";
   private static final String FONT                    = "font";
   private static final String PADDING                 = "padding";

   private static GfxSize sDefaultWidth                = new Pixels(600);
   private static GfxSize sDefaultHeight               = new Pixels(500);
   private static Font    sDefaultFont                 = Font.decode("Arial-PLAIN-10");
   private static GfxSize sDefaultPadding              = new Pixels(40);

   //###########################################################################
   // CONSTRUCTORS
   //###########################################################################

   //---------------------------------------------------------------------------
   public SvgGenerationSettings()
   {

   }

   //---------------------------------------------------------------------------
   @Override
   protected void init()
   {
      super.init();

      add(new StringSetting(WIDTH));
      add(new StringSetting(HEIGHT));
      add(new StringSetting(FONT));
      add(new StringSetting(PADDING));

      setWidth(sDefaultWidth);
      setHeight(sDefaultHeight);
      setFont(sDefaultFont);
      setPadding(sDefaultPadding);
   }

   //###########################################################################
   // PUBLIC METHODS
   //###########################################################################

   //---------------------------------------------------------------------------
   public SvgGenerationSettings setWidth(GfxSize inValue)
   {
      get(WIDTH).setValue(inValue != null ? inValue.toString() : null);
      return this;
   }

   //---------------------------------------------------------------------------
   public GfxSize getWidth()
   {
      String stringValue = (String) get(WIDTH).getValue();
      return (StringUtil.isSet(stringValue) ? GfxSize.allocate(stringValue) : null);
   }


   //---------------------------------------------------------------------------
   public SvgGenerationSettings setHeight(GfxSize inValue)
   {
      get(HEIGHT).setValue(inValue != null ? inValue.toString() : null);
      return this;
   }

   //---------------------------------------------------------------------------
   public GfxSize getHeight()
   {
      String stringValue = (String) get(HEIGHT).getValue();
      return (StringUtil.isSet(stringValue) ? GfxSize.allocate(stringValue) : null);
   }


   //---------------------------------------------------------------------------
   public SvgGenerationSettings setPadding(GfxSize inValue)
   {
      get(PADDING).setValue(inValue != null ? inValue.toString() : null);
      return this;
   }

   //---------------------------------------------------------------------------
   public GfxSize getPadding()
   {
      String stringValue = (String) get(PADDING).getValue();
      return (StringUtil.isSet(stringValue) ? GfxSize.allocate(stringValue) : null);
   }

   //---------------------------------------------------------------------------
   public SvgGenerationSettings setFont(Font inValue)
   {
      CharSequence value = null;

      if (inValue != null)
      {
         value = FontUtil.encode(inValue);
      }

      get(FONT).setValue(value != null ? value.toString() : null);
      return this;
   }

   //---------------------------------------------------------------------------
   public Font getFont()
   {
      Font value = null;

      String stringValue = (String) get(FONT).getValue();
      if (StringUtil.isSet(stringValue))
      {
         value = Font.decode(stringValue);
      }

      return value;
   }

   //---------------------------------------------------------------------------
   public GfxSize calculateXStart()
   {
      return getPadding(); // Left padding
      // TODO: add for the scale tick width, labels, and Y-axis label
   }

   //---------------------------------------------------------------------------
   public GfxSize calculateXEnd()
   {
      return calculateXStart().add(calculateChartWidth());
   }

   //---------------------------------------------------------------------------
   public GfxSize calculateChartWidth()
   {
      GfxSize chartWidth = getWidth()
            .subtract(getPadding())   // Left padding
            .subtract(getPadding());  // Right padding

      // TODO: subtract for the scale tick width, labels, and Y-axis label

      return new Pixels(chartWidth.to(GfxUnits.pixels));
   }

   //---------------------------------------------------------------------------
   public float calculateXScalingFactor(Range inXRange)
   {
      GfxSize chartWidth = calculateChartWidth();

      return chartWidth.value() / inXRange.length().floatValue() ;
   }

   //---------------------------------------------------------------------------
   public GfxSize calculateYTop()
   {
      return getPadding(); // Top padding
      // TODO: add for the title
   }

   //---------------------------------------------------------------------------
   public GfxSize calculateYBottom()
   {
      return calculateYTop().add(calculateChartHeight());
   }

   //---------------------------------------------------------------------------
   public GfxSize calculateChartHeight()
   {
      GfxSize chartHeight = getHeight()
            .subtract(getPadding())   // Top padding
            .subtract(getPadding())   // Bottom padding
            .subtract(getPadding());  // X-axis labels
      // TODO: subtract for the scale tick width, labels, and X-axis label

      return new Pixels(chartHeight.to(GfxUnits.pixels));
   }

   //---------------------------------------------------------------------------
   public float calculateYScalingFactor(Range inYRange)
   {
      GfxSize chartHeight = calculateChartHeight();

      return chartHeight.value() / (inYRange.length().floatValue() - 1);
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy