src/d3/Defaults.ts

   1/**

   2 * # Defaults Configuration

   3 * 

   4 */

   5

   6/** */

   7

   8import { GraphDefaults }    from './DefaultTypes';

   9import { PlotDefaults }     from './DefaultTypes';

  10import { SeriesDefaults }   from './DefaultTypes';

  11import { AxesDefaults }     from './DefaultTypes';

  12import { Color }            from './DefaultTypes';

  13import { RectStyle }        from './DefaultTypes';

  14import { Line }             from './DefaultTypes';

  15import { UnitVp }           from './ConfigTypes';

  16import { ScaleDefaults }    from './DefaultTypes';

  17import { TextStyle }        from './DefaultTypes';

  18import { GraphCfg }         from './ConfigTypes';

  19

  20

  21/**

  22 * Create a `Config` object and populate it with default values.

  23 */

  24export class Defaults {

  25    private axes = {};

  26    private config: GraphCfg;

  27    private scales = {};

  28

  29    constructor(config: GraphCfg) {}

  30

  31    get Graph():GraphDefaults {

  32        return { canvas: defaultRect('#fff', 2, '#ccc') };

  33    }

  34

  35    get Plot():PlotDefaults {

  36        return { area: defaultRect('#fff') };

  37    }

  38

  39    get Series():SeriesDefaults[]  {

  40        return [];

  41    }

  42

  43    get Axes():AxesDefaults { return {

  44        hor: {

  45            line:       defaultLine(2),

  46            tickMarks:  defaultLine(2),

  47            tickLabel:  defaultText()

  48        },

  49        ver: {

  50            line:       defaultLine(2),

  51            tickMarks:  defaultLine(2),

  52            tickLabel:  defaultText()

  53        }

  54    };}

  55

  56    Scales(dataCol: string):ScaleDefaults {

  57        return this.scales[dataCol] = this.scales[dataCol] || defaultScale(0, 100);

  58    }

  59}

  60

  61export function defaultLine(width:UnitVp, color:Color='#000'):Line {

  62    return {

  63        width: width,

  64        color: color,

  65        opacity: 1

  66    };

  67}

  68

  69/**

  70 * convenience function to create a default `RectStyle` object with configurable fill color and border. 

  71 * @param area  the fill color

  72 * @param borderWidth the border width in pixel

  73 * @param borderColor the border color

  74 */

  75function defaultRect(area:Color, borderWidth:UnitVp=0, borderColor:Color='#fff'):RectStyle {

  76    return {

  77        rx: 0,

  78        ry: 0,

  79        fill: {

  80            color: area,

  81            opacity: 1

  82        },

  83        stroke: {

  84            width: borderWidth,

  85            color: borderColor,

  86            opacity: 1

  87        }

  88    };

  89}

  90

  91function defaultScale(minRange: number, maxRange:number):ScaleDefaults { return {

  92    type:   'linear',

  93    domain: {min: 'auto', max: 'auto'},

  94    range:  { min: minRange, max: maxRange }

  95};}

  96

  97function defaultText():TextStyle {

  98    return {

  99        color: '#000',

 100        font: {

 101            family: 'initial',

 102            size:   '100%',

 103            style:  'initial',    // 'normal''italic'

 104            weight: 'initial'    // 'normal''bold'

 105        }

 106    };

 107}