001package com.hfg.svg;
002
003import java.awt.Font;
004
005import com.hfg.graphics.FontUtil;
006import com.hfg.graphics.units.GfxSize;
007import com.hfg.graphics.units.GfxUnits;
008import com.hfg.graphics.units.Pixels;
009import com.hfg.math.Range;
010import com.hfg.setting.Settings;
011import com.hfg.setting.StringSetting;
012import com.hfg.util.StringUtil;
013
014//------------------------------------------------------------------------------
015/**
016 * General settings for use with generating SVGs.
017 *
018 * @author J. Alex Taylor, hairyfatguy.com
019 */
020//------------------------------------------------------------------------------
021// com.hfg XML/HTML Coding Library
022//
023// This library is free software; you can redistribute it and/or
024// modify it under the terms of the GNU Lesser General Public
025// License as published by the Free Software Foundation; either
026// version 2.1 of the License, or (at your option) any later version.
027//
028// This library is distributed in the hope that it will be useful,
029// but WITHOUT ANY WARRANTY; without even the implied warranty of
030// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
031// Lesser General Public License for more details.
032//
033// You should have received a copy of the GNU Lesser General Public
034// License along with this library; if not, write to the Free Software
035// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
036//
037// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
038// jataylor@hairyfatguy.com
039//------------------------------------------------------------------------------
040
041public class SvgGenerationSettings extends Settings
042{
043
044   private static final String WIDTH                   = "width";
045   private static final String HEIGHT                  = "height";
046   private static final String FONT                    = "font";
047   private static final String PADDING                 = "padding";
048
049   private static GfxSize sDefaultWidth                = new Pixels(600);
050   private static GfxSize sDefaultHeight               = new Pixels(500);
051   private static Font    sDefaultFont                 = Font.decode("Arial-PLAIN-10");
052   private static GfxSize sDefaultPadding              = new Pixels(40);
053
054   //###########################################################################
055   // CONSTRUCTORS
056   //###########################################################################
057
058   //---------------------------------------------------------------------------
059   public SvgGenerationSettings()
060   {
061
062   }
063
064   //---------------------------------------------------------------------------
065   @Override
066   protected void init()
067   {
068      super.init();
069
070      add(new StringSetting(WIDTH));
071      add(new StringSetting(HEIGHT));
072      add(new StringSetting(FONT));
073      add(new StringSetting(PADDING));
074
075      setWidth(sDefaultWidth);
076      setHeight(sDefaultHeight);
077      setFont(sDefaultFont);
078      setPadding(sDefaultPadding);
079   }
080
081   //###########################################################################
082   // PUBLIC METHODS
083   //###########################################################################
084
085   //---------------------------------------------------------------------------
086   public SvgGenerationSettings setWidth(GfxSize inValue)
087   {
088      get(WIDTH).setValue(inValue != null ? inValue.toString() : null);
089      return this;
090   }
091
092   //---------------------------------------------------------------------------
093   public GfxSize getWidth()
094   {
095      String stringValue = (String) get(WIDTH).getValue();
096      return (StringUtil.isSet(stringValue) ? GfxSize.allocate(stringValue) : null);
097   }
098
099
100   //---------------------------------------------------------------------------
101   public SvgGenerationSettings setHeight(GfxSize inValue)
102   {
103      get(HEIGHT).setValue(inValue != null ? inValue.toString() : null);
104      return this;
105   }
106
107   //---------------------------------------------------------------------------
108   public GfxSize getHeight()
109   {
110      String stringValue = (String) get(HEIGHT).getValue();
111      return (StringUtil.isSet(stringValue) ? GfxSize.allocate(stringValue) : null);
112   }
113
114
115   //---------------------------------------------------------------------------
116   public SvgGenerationSettings setPadding(GfxSize inValue)
117   {
118      get(PADDING).setValue(inValue != null ? inValue.toString() : null);
119      return this;
120   }
121
122   //---------------------------------------------------------------------------
123   public GfxSize getPadding()
124   {
125      String stringValue = (String) get(PADDING).getValue();
126      return (StringUtil.isSet(stringValue) ? GfxSize.allocate(stringValue) : null);
127   }
128
129   //---------------------------------------------------------------------------
130   public SvgGenerationSettings setFont(Font inValue)
131   {
132      CharSequence value = null;
133
134      if (inValue != null)
135      {
136         value = FontUtil.encode(inValue);
137      }
138
139      get(FONT).setValue(value != null ? value.toString() : null);
140      return this;
141   }
142
143   //---------------------------------------------------------------------------
144   public Font getFont()
145   {
146      Font value = null;
147
148      String stringValue = (String) get(FONT).getValue();
149      if (StringUtil.isSet(stringValue))
150      {
151         value = Font.decode(stringValue);
152      }
153
154      return value;
155   }
156
157   //---------------------------------------------------------------------------
158   public GfxSize calculateXStart()
159   {
160      return getPadding(); // Left padding
161      // TODO: add for the scale tick width, labels, and Y-axis label
162   }
163
164   //---------------------------------------------------------------------------
165   public GfxSize calculateXEnd()
166   {
167      return calculateXStart().add(calculateChartWidth());
168   }
169
170   //---------------------------------------------------------------------------
171   public GfxSize calculateChartWidth()
172   {
173      GfxSize chartWidth = getWidth()
174            .subtract(getPadding())   // Left padding
175            .subtract(getPadding());  // Right padding
176
177      // TODO: subtract for the scale tick width, labels, and Y-axis label
178
179      return new Pixels(chartWidth.to(GfxUnits.pixels));
180   }
181
182   //---------------------------------------------------------------------------
183   public float calculateXScalingFactor(Range inXRange)
184   {
185      GfxSize chartWidth = calculateChartWidth();
186
187      return chartWidth.value() / inXRange.length().floatValue() ;
188   }
189
190   //---------------------------------------------------------------------------
191   public GfxSize calculateYTop()
192   {
193      return getPadding(); // Top padding
194      // TODO: add for the title
195   }
196
197   //---------------------------------------------------------------------------
198   public GfxSize calculateYBottom()
199   {
200      return calculateYTop().add(calculateChartHeight());
201   }
202
203   //---------------------------------------------------------------------------
204   public GfxSize calculateChartHeight()
205   {
206      GfxSize chartHeight = getHeight()
207            .subtract(getPadding())   // Top padding
208            .subtract(getPadding())   // Bottom padding
209            .subtract(getPadding());  // X-axis labels
210      // TODO: subtract for the scale tick width, labels, and X-axis label
211
212      return new Pixels(chartHeight.to(GfxUnits.pixels));
213   }
214
215   //---------------------------------------------------------------------------
216   public float calculateYScalingFactor(Range inYRange)
217   {
218      GfxSize chartHeight = calculateChartHeight();
219
220      return chartHeight.value() / (inYRange.length().floatValue() - 1);
221   }
222}