Mercurial > hg > numerics
changeset 156:69940819bc8a
allow for labels in an assenine way
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Tue, 12 May 2015 16:25:37 -0700 |
parents | 2a124febcd49 |
children | c5e919aff552 |
files | numerics/bar.py numerics/templates/bar.d3.html |
diffstat | 2 files changed, 16 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/numerics/bar.py Tue May 12 16:04:05 2015 -0700 +++ b/numerics/bar.py Tue May 12 16:25:37 2015 -0700 @@ -30,7 +30,7 @@ d3 = f.read() -def bar_chart(data, title=None): +def bar_chart(data, labels=None, title=None): """ create a bar chart @@ -38,10 +38,13 @@ - http://bokeh.pydata.org/en/latest/tutorial/solutions/gallery/olympics.html """ # TODO: abstract this to a plot class + if labels is None: + labels = data template = tempita.Template.from_filename(bar_template) bar_chart = template.substitute(title=title or '', d3=d3, + labels=json.dumps(labels), data=json.dumps(data)) return bar_chart @@ -67,13 +70,17 @@ data = parser.typed_data() # ensure a mapping is given - if len(data) != 1: - raise NotImplementedError("TODO") + if len(data) == 1: + data = data[0] + labels = None + elif len(data) ==2: + labels = data[0] + data = data[1] else: - data = data[0] + raise NotImplementedError('TODO') # generate bar chart - options.output.write(bar_chart(data, title=options.title)) + options.output.write(bar_chart(data, labels=labels, title=options.title)) if options.output != sys.stdout: # print URL of file
--- a/numerics/templates/bar.d3.html Tue May 12 16:04:05 2015 -0700 +++ b/numerics/templates/bar.d3.html Tue May 12 16:25:37 2015 -0700 @@ -21,8 +21,9 @@ <svg class="svgchart"></svg> <script> var data = {{data}}; - var labels = {}; - var width = 420, + var labels = {{labels}}; + var ctr = 0; + var width = 960, barHeight = 20; var x = d3.scale.linear() .domain([0, d3.max(data)]) @@ -42,12 +43,7 @@ .attr("y", barHeight / 2) .attr("dy", ".35em") .text(function(d) { - var label = labels[String(d)]; - if (label == undefined) { - return d; - } else { - return label + " : " + d; - } + return labels[ctr++]; }); </script> </body>