From a3ea22339845680b8cae710c4632ba8e339b9a8c Mon Sep 17 00:00:00 2001 From: dsc Date: Thu, 29 Mar 2012 02:12:20 -0700 Subject: [PATCH] Renames 'chart-library' to 'chart-type', as it makes sense that one library might service multiple chart types, each with their own options schema. --- lib/chart/chart-library.co | 155 ---------------------------------------- lib/chart/chart-type.co | 156 +++++++++++++++++++++++++++++++++++++++++ lib/chart/dygraphs-library.co | 14 ---- lib/chart/dygraphs.co | 14 ++++ lib/chart/index.co | 6 +- lib/main.co | 4 +- lib/vis/vis-model.co | 12 ++-- www/modules.yaml | 4 +- www/presets/root.yaml | 16 ++-- 9 files changed, 191 insertions(+), 190 deletions(-) delete mode 100644 lib/chart/chart-library.co create mode 100644 lib/chart/chart-type.co delete mode 100644 lib/chart/dygraphs-library.co create mode 100644 lib/chart/dygraphs.co diff --git a/lib/chart/chart-library.co b/lib/chart/chart-library.co deleted file mode 100644 index 75c7a13..0000000 --- a/lib/chart/chart-library.co +++ /dev/null @@ -1,155 +0,0 @@ -_ = require 'kraken/util/underscore' -op = require 'kraken/util/op' -{EventEmitter} = require 'events' -{Parsers, ParserMixin} = require 'kraken/util/parser' - - -/** - * @class Specification for an option. - */ -class exports.ChartOption - SPEC_KEYS : <[ name type default desc tags examples ]> - - name : null - type : 'String' - default : null - desc : '' - tags : null - examples : null - - - (@library, @spec) -> - throw new Error('Each ChartOption requires a name!') unless @spec.name - - for k of @SPEC_KEYS - v = @spec[k] - @[k] = v if v? - @tags or= [] - @parse = @library.getParser @type - - parse : Parsers.parseString - - isDefault: (v) -> - # @default is v - _.isEqual @default, v - - toString: -> "(#{@name}: #{@type})" - - - -/** - * Map of known libraries by name. - * @type Object - */ -KNOWN_LIBRARIES = exports.KNOWN_LIBRARIES = {} - - -/** - * @class Abstraction of a charting library, encapsulating its logic and options. - */ -class exports.ChartLibrary extends EventEmitter - /** - * Ordered ChartOption objects. - * @type ChartOption[] - */ - options_ordered : null - - /** - * Map of option name to ChartOption objects. - * @type { name:ChartOption, ... } - */ - options : null - - - - /** - * @constructor - * @param {String} name Library name. - * @param {Array} options List of options objects, each specifying the - * name, type, default, description (etc) of a chart library option. - */ - (@name, options) -> - @options_ordered = _.map options, (opt) ~> new ChartOption this, opt - @options = _.synthesize @options_ordered, -> [it.name, it] - ChartLibrary.register this - - - /** - * @returns {ChartOption} Get an option's spec by name. - */ - get: (name, def) -> - @options[name] or def - - /** - * @returns {Array} List of values found at the given attr on each - * option spec object. - */ - pluck: (attr) -> - _.pluck @spec, attr - - /** - * @returns {Object} An object, mapping from option.name to the - * result of the supplied function. - */ - map: (fn, context=this) -> - _.synthesize @spec, ~> [it.name, fn.call(context, it, it.name, this)] - - - /** - * @returns {Boolean} Whether the supplied value is the same as - * the default value for the given key. - */ - isDefault: (name, value) -> - @get name .isDefault value - - - serialize: (v, k) -> - # if v!? - # v = '' - if _.isBoolean v - v = Number v - else if _.isObject v - v = JSON.stringify v - String v - - - ### Parsers - - /** - * When implementing a ChartLibrary, you can add or override parsers - * merely by subclassing. - */ - this:: import ParserMixin:: - - getParserFor: (name) -> - @getParser @get(name).type - - parseOption: (name, value) -> - @getParserFor(name)(value) - - parseOptions: (options) -> - out = {} - for k, v in options - out[k] = @parseOption k, v - out - - - - ### Class Methods - - /** - * Register a new library. - */ - @register = (library) -> - KNOWN_LIBRARIES[library.name] = library - - /** - * Look up a library by name. - */ - @lookupLibrary = (name) -> - KNOWN_LIBRARIES[name] - - - - - diff --git a/lib/chart/chart-type.co b/lib/chart/chart-type.co new file mode 100644 index 0000000..6a145f5 --- /dev/null +++ b/lib/chart/chart-type.co @@ -0,0 +1,156 @@ +_ = require 'kraken/util/underscore' +op = require 'kraken/util/op' +{EventEmitter} = require 'events' +{Parsers, ParserMixin} = require 'kraken/util/parser' + + +/** + * @class Specification for an option. + */ +class exports.ChartOption + SPEC_KEYS : <[ name type default desc tags examples ]> + + name : null + type : 'String' + default : null + desc : '' + tags : null + examples : null + + + (@chartType, @spec) -> + throw new Error('Each ChartOption requires a name!') unless @spec.name + + for k of @SPEC_KEYS + v = @spec[k] + @[k] = v if v? + @tags or= [] + @parse = @lookup.getParser @type + + parse : Parsers.parseString + + isDefault: (v) -> + # @default is v + _.isEqual @default, v + + toString: -> "(#{@name}: #{@type})" + + + +/** + * Map of known libraries by name. + * @type Object + */ +KNOWN_CHART_TYPES = exports.KNOWN_CHART_TYPES = {} + + +/** + * @class Abstraction of a chart-type or charting library, encapsulating its + * logic and options. + */ +class exports.ChartType extends EventEmitter + /** + * Ordered ChartOption objects. + * @type ChartOption[] + */ + options_ordered : null + + /** + * Map of option name to ChartOption objects. + * @type { name:ChartOption, ... } + */ + options : null + + + + /** + * @constructor + * @param {String} name Library name. + * @param {Array} options List of options objects, each specifying the + * name, type, default, description (etc) of a chart option. + */ + (@name, options) -> + @options_ordered = _.map options, (opt) ~> new ChartOption this, opt + @options = _.synthesize @options_ordered, -> [it.name, it] + ChartType.register this + + + /** + * @returns {ChartOption} Get an option's spec by name. + */ + get: (name, def) -> + @options[name] or def + + /** + * @returns {Array} List of values found at the given attr on each + * option spec object. + */ + pluck: (attr) -> + _.pluck @spec, attr + + /** + * @returns {Object} An object, mapping from option.name to the + * result of the supplied function. + */ + map: (fn, context=this) -> + _.synthesize @spec, ~> [it.name, fn.call(context, it, it.name, this)] + + + /** + * @returns {Boolean} Whether the supplied value is the same as + * the default value for the given key. + */ + isDefault: (name, value) -> + @get name .isDefault value + + + serialize: (v, k) -> + # if v!? + # v = '' + if _.isBoolean v + v = Number v + else if _.isObject v + v = JSON.stringify v + String v + + + ### Parsers + + /** + * When implementing a ChartType, you can add or override parsers + * merely by subclassing. + */ + this:: import ParserMixin:: + + getParserFor: (name) -> + @getParser @get(name).type + + parseOption: (name, value) -> + @getParserFor(name)(value) + + parseOptions: (options) -> + out = {} + for k, v in options + out[k] = @parseOption k, v + out + + + + ### Class Methods + + /** + * Register a new chart type. + */ + @register = (chartType) -> + KNOWN_CHART_TYPES[chartType.name] = chartType + + /** + * Look up a chart type by name. + */ + @lookup = (name) -> + KNOWN_CHART_TYPES[name] + + + + + diff --git a/lib/chart/dygraphs-library.co b/lib/chart/dygraphs-library.co deleted file mode 100644 index f08468d..0000000 --- a/lib/chart/dygraphs-library.co +++ /dev/null @@ -1,14 +0,0 @@ -_ = require 'kraken/util/underscore' -{ ChartLibrary, ChartOption, -} = require 'kraken/chart/chart-library' - - -class exports.DygraphsLibrary extends ChartLibrary - name : 'dygraphs' - - (options) -> - super 'dygraphs', options - - render: -> - ... - diff --git a/lib/chart/dygraphs.co b/lib/chart/dygraphs.co new file mode 100644 index 0000000..056bc39 --- /dev/null +++ b/lib/chart/dygraphs.co @@ -0,0 +1,14 @@ +_ = require 'kraken/util/underscore' +{ ChartType, ChartOption, +} = require 'kraken/chart/chart-type' + + +class exports.DygraphsChartType extends ChartType + name : 'dygraphs' + + (options) -> + super 'dygraphs', options + + render: -> + ... + diff --git a/lib/chart/index.co b/lib/chart/index.co index fe6f0b8..6709cab 100644 --- a/lib/chart/index.co +++ b/lib/chart/index.co @@ -1,3 +1,3 @@ -library = require 'kraken/chart/chart-library' -dygraphs = require 'kraken/chart/dygraphs-library' -exports import library import dygraphs +chart_type = require 'kraken/chart/chart-type' +dygraphs = require 'kraken/chart/dygraphs' +exports import chart_type import dygraphs diff --git a/lib/main.co b/lib/main.co index fe468c1..2098f7e 100644 --- a/lib/main.co +++ b/lib/main.co @@ -3,7 +3,7 @@ Backbone = require 'backbone' { _, op, } = require 'kraken/util' -{ ChartLibrary, DygraphsLibrary, +{ ChartType, DygraphsChartType, } = require 'kraken/chart' { BaseView, BaseModel, BaseList, } = require 'kraken/base' @@ -29,7 +29,7 @@ main = -> # for opt of root.CHART_OPTIONS_SPEC # opts[opt.name] = opt.default - dyglib = new DygraphsLibrary CHART_OPTIONS_SPEC + dyglib = new DygraphsChartType CHART_OPTIONS_SPEC # TODO: create a preset manager # Remove chart options from data so we don't have to deepcopy diff --git a/lib/vis/vis-model.co b/lib/vis/vis-model.co index 1ec02e8..0ff9361 100644 --- a/lib/vis/vis-model.co +++ b/lib/vis/vis-model.co @@ -2,7 +2,7 @@ Seq = require 'seq' _ = require 'kraken/util/underscore' Cascade = require 'kraken/util/cascade' -{ ChartLibrary, +{ ChartType, } = require 'kraken/chart' { BaseModel, BaseView, BaseList, } = require 'kraken/base' @@ -25,9 +25,9 @@ VisModel = exports.VisModel = BaseModel.extend do # {{{ /** * The chart type backing this graph. - * @type ChartLibrary + * @type ChartType */ - library : null + chartType : null /** * List of graph parents. @@ -55,7 +55,7 @@ VisModel = exports.VisModel = BaseModel.extend do # {{{ dataset : '/data/non_mobile_pageviews_by.timestamp.language.csv' width : 'auto' height : 320 - library : 'dygraphs' + chartType : 'dygraphs' parents : <[ root ]> options : {} } @@ -78,7 +78,7 @@ VisModel = exports.VisModel = BaseModel.extend do # {{{ @parents = new VisList # TODO: Load on-demand - @library = ChartLibrary.lookupLibrary @get('library') + @chartType = ChartType.lookup @get('chartType') # unless @id or @get('id') or @get('slug') # @set 'slug', "unsaved_graph_#{@cid}" @@ -190,7 +190,7 @@ VisModel = exports.VisModel = BaseModel.extend do # {{{ * @returns {Boolean} Whether the value for option `k` is the graph default or not. */ isDefaultOption: (k) -> - @library.isDefault k, @getOption k + @chartType.isDefault k, @getOption k /** * Whether the value for option `k` differs from that of its parent graphs. diff --git a/www/modules.yaml b/www/modules.yaml index 0199df8..99cb4f0 100644 --- a/www/modules.yaml +++ b/www/modules.yaml @@ -58,8 +58,8 @@ dev: - index - base - chart: - - chart-library - - dygraphs-library + - chart-type + - dygraphs - index - template: - graph.jade diff --git a/www/presets/root.yaml b/www/presets/root.yaml index bfb9783..e324d29 100644 --- a/www/presets/root.yaml +++ b/www/presets/root.yaml @@ -1,13 +1,13 @@ # The root graph, holding defaults inherited by all graphs. -id : root -slug : root -dataset : '/data/non_mobile_pageviews_by.timestamp.language.csv' -width : 'auto' -height : 400 -library : 'dygraphs' -parents : [] -options : +id : root +slug : root +dataset : '/data/non_mobile_pageviews_by.timestamp.language.csv' +width : 'auto' +height : 400 +chartType : 'dygraphs' +parents : [] +options : animatedZooms : true avoidMinZero : false axis : null -- 1.7.0.4