tw2.protovis.core

This package contains the basic core protovis resources to be used by the widgets packaged in the following:

  • tw2.protovis.conventional
  • tw2.protovis.custom
  • tw2.protovis.interaction
  • tw2.protovis.hierarchies
  • tw2.protovis.networks
  • tw2.protovis.maps
  • tw2.protovis.stats
  • tw2.protovis.art

Get this source from http://github.com/ralphbean/tw2.protovis.core

Protovis itself can be found here: http://vis.stanford.edu/protovis/

PVMark
    class PVWidget(PVMark):
        template = "mako:tw2.protovis.core.templates.widget"
        p_data = twc.Param('(list) data for the widget')
        p_width = twc.Param('The width of the panel, in pixel.', default=400)
        p_height = twc.Param('The height of the panel, in pixels.', default=200)
        p_bottom = twc.Param('The bottom margin, in pixels.', default=20)
        p_top = twc.Param('The top margin, in pixels.', default=5)
        p_left = twc.Param('The left margin, in pixels.', default=20)
        p_right = twc.Param('The right margin, in pixels.', default=10)
        init_js = twc.Param('JSSymbol', default=twc.JSSymbol(src=''))
        pvcls = pv.Panel
        parent_js_id = None
        js_id = 'vis'
        add_method = 'add'
    
    <%namespace name="tw" module="tw2.core.mako_util"/>
    <div ${tw.attrs(attrs=w.attrs)}>
    <script type="text/javascript+protovis">
    ${w.init_js.src}
    var vis = new pv.Panel()
    % for f in w._pv_prop_funcs:
    	${f.src}
    % endfor
    % for a in w._adds:
    ${a.display()}
    % endfor
    vis.render();
    </script>
    </div>
    

    PVWidget
    • pvcls
      • None
    • p_width
      • The width of the panel, in pixel.
    • p_height
      • The height of the panel, in pixels.
    • p_bottom
      • The bottom margin, in pixels.
    • p_top
      • The top margin, in pixels.
    • p_left
      • The left margin, in pixels.
    • p_right
      • The right margin, in pixels.
    • init_js
      • JSSymbol
    class DemoPVWidget(PVWidget):
        def __init__(self, *args, **kwargs):
            self.p_data = [
            [random.random() + 0.1 for j in range(4)] for i in range(3)]
            super(DemoPVWidget, self).__init__(*args, **kwargs)
            # Sizing and scales.
            self.init_js = js(
                """
                var data = %s;
                var n = data.length;
                var m = data[0].length;
                var w = %i,
                    h = %i,
                    x = pv.Scale.linear(0, 1.1).range(0, w),
                    y = pv.Scale.ordinal(pv.range(n)).splitBanded(0, h, 4/5);
                """ % (self.p_data, self.p_width, self.p_height))
            self.setupRootPanel()
            # The bars.
            bar = self.add(pv.Panel) \
                .data(js('data')) \
                .top(js('function() y(this.index)')) \
                .height(js('y.range().band')) \
              .add(pv.Bar) \
                .data(js('function(d) d')) \
                .top(js('function() this.index * y.range().band / m')) \
                .height(js('y.range().band / m')) \
                .left(0) \
                .width(js('x')) \
                .fillStyle(js('pv.Colors.category20().by(pv.index)'))
            # The value label.
            bar.anchor("right").add(pv.Label) \
                .textStyle("white") \
                .text(js('function(d) d.toFixed(1)'))
            # The variable label.
            bar._parent.anchor("left").add(pv.Label) \
                .textAlign("right") \
                .textMargin(5) \
                .text(js('function() "ABCDEFGHIJK".charAt(this.parent.index)'))
            # X-axis ticks.
            self.add(pv.Rule) \
                .data(js('x.ticks(5)')) \
                .left(js('x')) \
                .strokeStyle(js('function(d) d ? "rgba(255,255,255,.3)" : "#000"'))\
                .add(pv.Rule) \
                .bottom(0) \
                .height(5) \
                .strokeStyle("#000") \
              .anchor("bottom").add(pv.Label) \
                .text(js('x.tickFormat'))
    
    <%namespace name="tw" module="tw2.core.mako_util"/>
    <div ${tw.attrs(attrs=w.attrs)}>
    <script type="text/javascript+protovis">
    ${w.init_js.src}
    var vis = new pv.Panel()
    % for f in w._pv_prop_funcs:
    	${f.src}
    % endfor
    % for a in w._adds:
    ${a.display()}
    % endfor
    vis.render();
    </script>
    </div>