Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

make multi axis charts work for single points

  • Loading branch information...
commit 86ed09959fcfaa41ceba84cd77038dd09dff6ce7 1 parent dc3df87
Joshua Carver authored
26 compiled/charts.js
@@ -1630,8 +1630,20 @@ LineChart = (function(_super) {
1630 1630 return [x, y];
1631 1631 };
1632 1632
  1633 + LineChart.prototype.create_scalers_for_single_point = function() {
  1634 + var x, y,
  1635 + _this = this;
  1636 + y = function(i) {
  1637 + return 0.5 * (_this.height - _this.options.y_padding);
  1638 + };
  1639 + x = function(i) {
  1640 + return 0.5 * (_this.width - _this.options.x_padding);
  1641 + };
  1642 + return [x, y];
  1643 + };
  1644 +
1633 1645 LineChart.prototype._draw_y_labels = function(labels, x_offset) {
1634   - var fmt, font_family, i, label, label_coordinates, offset, padding, size, x, y, _i, _len, _ref;
  1646 + var fmt, font_family, i, label, label_coordinates, offset, padding, size, x, y, _i, _len, _ref, _ref1;
1635 1647 if (x_offset == null) {
1636 1648 x_offset = 0;
1637 1649 }
@@ -1640,7 +1652,11 @@ LineChart = (function(_super) {
1640 1652 font_family = this.options.font_family;
1641 1653 padding = size + 5;
1642 1654 offset = this.options.multi_axis && x_offset > 0 ? x_offset : x_offset + padding;
1643   - _ref = this.create_scalers(labels), x = _ref[0], y = _ref[1];
  1655 + if (labels.length === 1) {
  1656 + _ref = this.create_scalers_for_single_point(), x = _ref[0], y = _ref[1];
  1657 + } else {
  1658 + _ref1 = this.create_scalers(labels), x = _ref1[0], y = _ref1[1];
  1659 + }
1644 1660 label_coordinates = [];
1645 1661 for (i = _i = 0, _len = labels.length; _i < _len; i = ++_i) {
1646 1662 label = labels[i];
@@ -1675,7 +1691,7 @@ LineChart = (function(_super) {
1675 1691 _ref1 = this.options.y_axis_scale, min_y = _ref1[0], max_y = _ref1[1];
1676 1692 }
1677 1693 if (max_y === min_y) {
1678   - return this._draw_y_labels([new Point(0, max_y)]);
  1694 + return this._draw_y_labels([new Point(0, max_y)], x_offset);
1679 1695 }
1680 1696 labels = [];
1681 1697 if (this.options.scale === 'log') {
@@ -1766,14 +1782,14 @@ LineChart = (function(_super) {
1766 1782 return;
1767 1783 }
1768 1784 this.r.clear();
1769   - _ref = this.create_scalers(this.all_points), x = _ref[0], y = _ref[1];
  1785 + _ref = this.all_points.length > 1 ? this.create_scalers(this.all_points) : this.create_scalers_for_single_point(), x = _ref[0], y = _ref[1];
1770 1786 _ref1 = this.line_indices;
1771 1787 for (i = _i = 0, _len = _ref1.length; _i < _len; i = ++_i) {
1772 1788 line_indices = _ref1[i];
1773 1789 begin = line_indices[0], end = line_indices[1];
1774 1790 raw_points = this.all_points.slice(begin, end + 1 || 9e9);
1775 1791 if (this.options.multi_axis) {
1776   - _ref2 = this.create_scalers(raw_points), line_x = _ref2[0], line_y = _ref2[1];
  1792 + _ref2 = this.all_points.length > 2 ? this.create_scalers(raw_points) : this.create_scalers_for_single_point(), line_x = _ref2[0], line_y = _ref2[1];
1777 1793 } else {
1778 1794 line_x = x;
1779 1795 line_y = y;
47 compiled/charts.min.js
@@ -43,26 +43,27 @@ this.options.radius]},1500,"<")};return b}(w);r.CircleProgress=v;t={}.hasOwnProp
43 43 i,j;j=[];h=0;for(i=d.length;h<i;h++){c=d[h];j.push(new l(c[0],c[1]))}return j}();f=this.all_points.length;this.line_indices.push([f,f+e.length-1]);this.all_points.push.apply(this.all_points,e);this.line_options.push(D.merge(this.options,a.options))}};b.prototype.draw_grid=function(a,c){var d,e,f,h,i,j;if(a==null)a=[];if(c==null)c=[];e=this.options.multi_axis?this.options.x_padding*2:this.options.x_padding;d=this.height-this.options.y_padding;h=this.width-e;e=this.r.set();i=0;for(j=a.length;i<j;i++){f=
44 44 a[i];e.push(this.r.path("M "+f+", "+this.options.y_padding+" L "+f+", "+d+" Z"))}d=0;for(i=c.length;d<i;d++){f=c[d];e.push(this.r.path("M "+this.options.x_padding+", "+f+" L "+h+", "+f+" Z"))}return e.attr({stroke:"#ccc","stroke-width":1}).toBack()};b.prototype.create_scalers=function(a){var c,d,e,f,h,i,j,k,m,o=this;f=void 0;e=void 0;h=void 0;f=void 0;i=void 0;if(this.options.scale==="log"){d=new H;e=function(){var n,p,q;q=[];n=0;for(p=a.length;n<p;n++){j=a[n];q.push(new l(j.x,d(j.y)))}return q}();
45 45 i=x.get_ranges_for_points(e);e=i[0];h=i[1];f=i[2];i=i[3]}else{i=x.get_ranges_for_points(a);e=i[0];h=i[1];f=i[2];i=i[3]}if(this.options.y_axis_scale.length===2){f=this.options.y_axis_scale;i=f[0];f=f[1]}k=this.options.multi_axis?this.options.x_padding*2:this.options.x_padding;e=(new A).domain([h,e]).range([this.options.x_padding,this.width-k]);m=(new A).domain([i,f]).range([this.options.y_padding,this.height-this.options.y_padding]);c=function(n){return o.height-m(n)};f=this.options.scale==="log"?
46   -function(n){return c(d(n))}:c;return[e,f]};b.prototype._draw_y_labels=function(a,c){var d,e,f,h,i,j,k,m,o;if(c==null)c=0;d=this.options.label_format;j=this.options.y_label_size;e=this.options.font_family;h=j+5;i=this.options.multi_axis&&c>0?c:c+h;k=this.create_scalers(a)[1];h=[];f=m=0;for(o=a.length;m<o;f=++m){f=a[f];(new z(this.r,i,k(f.y),f.y,d,j,e)).draw();h.push(k(f.y))}return h};b.prototype.calc_y_label_step_size=function(a,c,d){if(d==null)d=this.options.max_y_labels;a=(c-a)/(d-1);if(c>1){a=Math.round(a);
47   -if(a===0)a=1}return a};b.prototype.draw_y_labels=function(a,c){var d,e,f,h,i,j;if(c==null)c=0;f=x.get_ranges_for_points(a);h=f[2];e=f[3];if(this.options.y_axis_scale.length===2){h=this.options.y_axis_scale;e=h[0];h=h[1]}if(h===e)return this._draw_y_labels([new l(0,h)]);f=[];if(this.options.scale==="log"){d=new H;j=d(e);d=d(h);d=(d-j)/(this.options.max_y_labels-1);e=e;for(i=0;e<=h&&i<this.options.max_y_labels;){e=Math.pow(10,j+d*i);f.push(new l(0,e));i+=1}}else{j=e;for(d=this.calc_y_label_step_size(e,
48   -h);j<=h;){f.push(new l(0,j));j+=d}}if(h>1)f[f.length-1].y=Math.round(h);return this._draw_y_labels(f,c)};b.prototype.draw_x_label=function(a,c){var d,e,f;d=this.options.label_format;f=this.options.x_label_size;e=this.options.font_family;return(new z(this.r,c.x,this.height-f,a.is_date_type===true?new Date(a.x):Math.round(a.x),d,f,e)).draw()};b.prototype.draw_x_labels=function(a,c){var d,e,f,h,i,j;e=[];h=this.options.max_x_labels;this.draw_x_label(a[0],c[0]);e.push(c[0].x);if(!(h<2)){f=c.length-1;this.draw_x_label(a[f],
49   -c[f]);e.push(c[f].x);if(!(h<3)){f=c.length-2;h=f/(h-1);d=Math.round(h);if(h!==d)h=d+1;for(d=h;d<f;){j=a[d];i=c[d];this.draw_x_label(j,i);e.push(i.x);d+=h}return e}}};b.prototype.draw_line=function(a,c,d){return this.options.render==="bar"?(new Q(this.r,a,c,this.height,this.width,d)).draw():(new R(this.r,a,c,this.height,this.width,d)).draw()};b.prototype.clear=function(){b.__super__.clear.call(this);this.all_points=[];this.line_indices=[];return this.line_options=[]};b.prototype.draw=function(){var a,
50   -c,d,e,f,h,i,j,k,m,o,n;if(!(this.all_points.length<1)){this.r.clear();d=this.create_scalers(this.all_points);j=d[0];k=d[1];n=this.line_indices;d=m=0;for(o=n.length;m<o;d=++m){c=n[d];a=c[0];c=c[1];i=this.all_points.slice(a,c+1||9E9);if(this.options.multi_axis){a=this.create_scalers(i);e=a[0];f=a[1]}else{e=j;f=k}c=function(){var p,q,u;u=[];p=0;for(q=i.length;p<q;p++){h=i[p];u.push(new l(e(h.x),f(h.y)))}return u}();a=this.line_options[d];this.draw_line(i,c,a);if(d===0){if(this.options.show_x_labels===
51   -true)this.x_label_coordinates=this.draw_x_labels(i,c);if(this.options.multi_axis&&this.options.show_y_labels===true)this.y_label_coordinates=this.draw_y_labels(i);else if(this.options.show_y_labels===true)this.y_label_coordinates=this.draw_y_labels(this.all_points);this.options.show_grid===true&&this.draw_grid(this.x_label_coordinates,this.y_label_coordinates)}else d===1&&this.options.multi_axis&&this.options.show_y_labels===true&&this.draw_y_labels(i,this.width-this.options.x_padding)}}};return b}(w);
52   -r.LineChart=v;var Y,Z,$;t={}.hasOwnProperty;s=function(g,b){function a(){this.constructor=g}for(var c in b)if(t.call(b,c))g[c]=b[c];a.prototype=b.prototype;g.prototype=new a;g.__super__=b.prototype;return g};Z=function(g,b,a){return{label:g,raw_value:b,index_value:a}};$=function(g,b,a){return{label:g,index_value:b,opacity:a}};Y=function(g){function b(a,c){if(c==null)c={};b.__super__.constructor.call(this,a,new O(c));this.effects=new I(this.r);this.bars=[];this.guides=[];this.index=100}s(b,g);b.prototype.add=
53   -function(a,c,d){return this.bars.push(Z(a,c,d))};b.prototype.add_guide_line=function(a,c,d){if(d==null)d=1;return this.guides.push($(a,c,d))};b.prototype.add_raw_label=function(a){var c,d=this;c=(new E(this.r)).x(function(){return d.width-10}).y(function(e){return e*15+15}).size(this.options.label_size).attr({fill:"#333","text-anchor":"end","font-weight":"bold"});c.draw(a);return c.draw("(raw value)").attr({"font-weight":"normal","font-size":10})};b.prototype.set_bar_height=function(){return this.bar_height=
54   -(this.height-this.options.y_padding)/this.bars.length-this.options.bar_margin};b.prototype.set_threshold=function(){var a,c;c=function(){var d,e,f,h;f=this.guides;h=[];d=0;for(e=f.length;d<e;d++){a=f[d];h.push(a.index_value)}return h}.call(this);return this.threshold=Math.max.apply(Math.max,c)};b.prototype.format_tooltip=function(a){return a/100+"x"};b.prototype.draw_raw_bar=function(a,c){var d,e;d=Math.floor(7);e=this.options.x_padding_right-10;this.r.rect(this.width-e,c-d,this.width-this.options.x_padding_right,
55   -this.bar_height+14,this.options.rounding).attr({fill:this.options.raw_value_bar_color,stroke:"none"});return this.effects.straight_line(new l(this.width-e,this.options.y_padding-10),new l(this.width-e,this.height)).attr({stroke:"rgba(0,0,0,0.25)","stroke-width":0.1})};b.prototype.draw_bg_bar=function(a,c,d){c(a);c=this.options.bg_bar_padding;a=Math.floor(c/2);return this.r.rect(this.options.x_padding-a,d-a,this.width,this.bar_height+c,this.options.rounding).attr({fill:this.options.bar_bg_color,stroke:"none"}).toBack()};
56   -b.prototype.shade_bar=function(a,c){if(c==null)c=this.options.bar1_color;a.attr({fill:c,stroke:"none"});this.effects.one_px_shadow(a);return this.effects.one_px_highlight(a)};b.prototype.render_bar=function(a,c,d,e){if(e==null)e=this.options.bar1_color;a=this.r.rect(a,c,d,this.bar_height,this.options.rounding);this.shade_bar(a,e);return a};b.prototype.draw_bar=function(a,c,d){var e,f,h;h=c(x.threshold(a,this.threshold));if(a>this.index){e=c(this.index);c=this.render_bar(this.options.x_padding,d,e-
57   -this.options.x_padding);d=this.render_bar(e,d,h-e,this.options.bar2_color);f=new C(this.r,d,this.format_tooltip(a));f.translate(d.getBBox().width/2,0);c.mouseover(function(){return f.show()});return c.mouseout(function(){return f.hide()})}else{d=this.render_bar(this.options.x_padding,d,h-this.options.x_padding);f=new C(this.r,d,this.format_tooltip(a));return f.translate(d.getBBox().width/2,0)}};b.prototype.draw_guide_line=function(a,c,d,e){var f,h;if(e==null)e=1;h=new l(d,this.options.y_padding);
58   -f=new l(d,this.height);this.effects.vertical_dashed_line(h,f,this.options.dash_width).attr({fill:"rgba(0,0,0,"+e+")",stroke:"none"});e=(new E(this.r)).x(function(){return d}).y(function(i){return i*15+15}).size(this.options.label_size).attr({fill:"rgba(0,0,0,"+e+")"});e.draw(a).attr({"font-weight":"bold"});return e.draw(c).attr({"font-size":10})};b.prototype.sort_bars_by_index=function(){var a,c;c=function(){var d,e,f,h;f=this.bars;h=[];d=0;for(e=f.length;d<e;d++){a=f[d];h.push(a)}return h}.call(this);
59   -c.sort(function(d,e){return e.index_value-d.index_value});return c};b.prototype.clear=function(){b.__super__.clear.call(this);this.bars=[];return this.guides=[]};b.prototype.draw=function(){var a,c,d,e,f,h,i,j,k,m,o,n,p,q=this;this.set_bar_height();this.set_threshold();h=this.bar_height+this.options.bar_margin;d=this.bar_height/2;m=this.options.y_padding;j=this.options.x_padding;c=(new E(this.r)).y(function(u){return u*h+m+d}).x(function(){return j-30}).size(12).attr({fill:"#fff","text-anchor":"end"});
60   -f=(new E(this.r)).y(function(u){return u*h+m+d}).x(function(){return q.width-10}).size(this.options.label_size).attr({fill:"#333","text-anchor":"end"});i=(new A).domain([0,this.threshold]).range([this.options.x_padding,this.width-this.options.x_padding_right]);k=function(u){return u*(q.bar_height+q.options.bar_margin)+q.options.y_padding};p=this.sort_bars_by_index();e=o=0;for(n=p.length;o<n;e=++o){a=p[e];this.draw_bg_bar(a.index_value,i,k(e));this.draw_raw_bar(a.raw_value,k(e));this.draw_bar(a.index_value,
61   -i,k(e));f.draw(a.raw_value);a=c.draw(a.label);this.effects.black_nub(a)}o=this.guides;n=[];f=0;for(k=o.length;f<k;f++){c=o[f];n.push(this.draw_guide_line(c.label,c.index_value,i(c.index_value),c.opacity))}return n};return b}(w);r.IndexChart=function(g,b){return new Y(g,b)};var y;t={}.hasOwnProperty;s=function(g,b){function a(){this.constructor=g}for(var c in b)if(t.call(b,c))g[c]=b[c];a.prototype=b.prototype;g.prototype=new a;g.__super__=b.prototype;return g};y=function(g,b,a,c){return{label:g,value:b,
62   -average:a,comparison:c}};v=function(g){function b(a,c){if(c==null)c={};b.__super__.constructor.call(this,a,new N(c));this.bars=[]}s(b,g);b.prototype.add=function(){return this.bars.push(y.apply(y,arguments))};b.prototype.draw_background=function(a,c){return this.r.rect(this.options.x_padding,c,a.x,this.options.area_width).attr({fill:this.options.area_color,stroke:"none"})};b.prototype.draw_line=function(a,c){return this.r.rect(this.options.x_padding,c.y-this.options.line_width/2,a.x,this.options.line_width).attr({fill:this.options.line_color,
63   -stroke:"none"})};b.prototype.draw_average=function(a,c){return this.r.rect(a.x-this.options.average_width/2,c-this.options.average_height/2,this.options.average_width,this.options.average_height).attr({fill:this.options.average_color,stroke:"none"})};b.prototype.draw_label=function(){};b.prototype.clear=function(){b.__super__.clear.call(this);return this.bars=[]};b.prototype.draw=function(){var a,c,d,e,f,h,i,j,k,m;k=this.bars;m=[];a=i=0;for(j=k.length;i<j;a=++i){y=k[a];e=[new l(y.comparison,0),new l(y.value,
64   -0),new l(y.average,0),new l(0,0)];d=x.get_ranges_for_points(e);c=d[0];d=d[1];h=(new A).domain([d,c]).range([this.options.x_padding,this.width-this.options.x_padding]);c=function(){var o,n,p;p=[];o=0;for(n=e.length;o<n;o++){f=e[o];p.push(new l(h(f.x),0))}return p}();d=a*(this.options.area_width+this.options.bar_margin);this.draw_background(c[0],d);a=d+this.options.area_width/2;this.draw_line(c[1],new l(c[0].x,a));this.draw_average(c[2],a);m.push((new z(this.r,0,d+this.options.area_width/2,y.label,
65   -"",this.size=14,this.options.font_family)).draw())}return m};return b}(w);r.BulletChart=v;t={}.hasOwnProperty;s=function(g,b){function a(){this.constructor=g}for(var c in b)if(t.call(b,c))g[c]=b[c];a.prototype=b.prototype;g.prototype=new a;g.__super__=b.prototype;return g};w=function(g){function b(a,c){if(c==null)c={};b.__super__.constructor.call(this,a,new G(c));this.effective_height=this.height-this.options.y_padding;this.bar_options=[];this.bars=[];this.values=[]}s(b,g);b.prototype.add=function(a){var c,
66   -d;c=a.label;d=a.value;this.bar_options.push(G.merge(this.options,a.options));this.values.push(d);return this.bars.push({label:c,value:d})};b.prototype.render_bar=function(a,c,d,e){this.r.rect(d.x,d.y,this.options.bar_width,this.effective_height-d.y,this.options.rounding).attr({fill:e.bar_color,stroke:"none"});(new z(this.r,d.x+this.options.bar_width/2,this.height-(this.options.x_label_size+5),a,"",this.options.x_label_size,this.options.font_family,this.options.x_label_color)).draw();return(new z(this.r,
67   -d.x+this.options.bar_width/2,d.y-this.options.y_label_size-5,c,"",this.options.y_label_size,this.options.font_family,this.options.y_label_color)).draw()};b.prototype.clear=function(){b.__super__.clear.call(this);this.bars=[];return this.values=[]};b.prototype.draw=function(){var a,c,d,e,f,h,i,j,k,m,o,n,p=this;e=function(){var q,u,L,M;L=this.values;M=[];c=q=0;for(u=L.length;q<u;c=++q){i=L[c];M.push(new l(c,i))}return M}.call(this);e.push(new l(0,0));d=x.get_ranges_for_points(e);a=d[2];d=d[3];j=(new A).domain([d,
68   -a]).range([this.options.y_padding,this.height-this.options.y_padding]);d=function(q){return p.height-j(q)};o=this.bars;n=[];c=k=0;for(m=o.length;k<m;c=++k){a=o[c];f=c*(this.options.bar_width+this.options.bar_spacing)+this.options.x_padding;h=d(e[c].y);f=new l(f,h);n.push(this.render_bar(a.label,a.value,f,this.bar_options[c]))}return n};return b}(w);r.BarChart=w});})();
  46 +function(n){return c(d(n))}:c;return[e,f]};b.prototype.create_scalers_for_single_point=function(){var a=this;return[function(){return 0.5*(a.width-a.options.x_padding)},function(){return 0.5*(a.height-a.options.y_padding)}]};b.prototype._draw_y_labels=function(a,c){var d,e,f,h,i,j,k,m,o;if(c==null)c=0;d=this.options.label_format;j=this.options.y_label_size;e=this.options.font_family;i=j+5;i=this.options.multi_axis&&c>0?c:c+i;if(a.length===1){h=this.create_scalers_for_single_point();k=h[1]}else{h=
  47 +this.create_scalers(a);k=h[1]}h=[];f=m=0;for(o=a.length;m<o;f=++m){f=a[f];(new z(this.r,i,k(f.y),f.y,d,j,e)).draw();h.push(k(f.y))}return h};b.prototype.calc_y_label_step_size=function(a,c,d){if(d==null)d=this.options.max_y_labels;a=(c-a)/(d-1);if(c>1){a=Math.round(a);if(a===0)a=1}return a};b.prototype.draw_y_labels=function(a,c){var d,e,f,h,i,j;if(c==null)c=0;f=x.get_ranges_for_points(a);h=f[2];e=f[3];if(this.options.y_axis_scale.length===2){h=this.options.y_axis_scale;e=h[0];h=h[1]}if(h===e)return this._draw_y_labels([new l(0,
  48 +h)],c);f=[];if(this.options.scale==="log"){d=new H;j=d(e);d=d(h);d=(d-j)/(this.options.max_y_labels-1);e=e;for(i=0;e<=h&&i<this.options.max_y_labels;){e=Math.pow(10,j+d*i);f.push(new l(0,e));i+=1}}else{j=e;for(d=this.calc_y_label_step_size(e,h);j<=h;){f.push(new l(0,j));j+=d}}if(h>1)f[f.length-1].y=Math.round(h);return this._draw_y_labels(f,c)};b.prototype.draw_x_label=function(a,c){var d,e,f;d=this.options.label_format;f=this.options.x_label_size;e=this.options.font_family;return(new z(this.r,c.x,
  49 +this.height-f,a.is_date_type===true?new Date(a.x):Math.round(a.x),d,f,e)).draw()};b.prototype.draw_x_labels=function(a,c){var d,e,f,h,i,j;e=[];h=this.options.max_x_labels;this.draw_x_label(a[0],c[0]);e.push(c[0].x);if(!(h<2)){f=c.length-1;this.draw_x_label(a[f],c[f]);e.push(c[f].x);if(!(h<3)){f=c.length-2;h=f/(h-1);d=Math.round(h);if(h!==d)h=d+1;for(d=h;d<f;){j=a[d];i=c[d];this.draw_x_label(j,i);e.push(i.x);d+=h}return e}}};b.prototype.draw_line=function(a,c,d){return this.options.render==="bar"?
  50 +(new Q(this.r,a,c,this.height,this.width,d)).draw():(new R(this.r,a,c,this.height,this.width,d)).draw()};b.prototype.clear=function(){b.__super__.clear.call(this);this.all_points=[];this.line_indices=[];return this.line_options=[]};b.prototype.draw=function(){var a,c,d,e,f,h,i,j,k,m,o,n;if(!(this.all_points.length<1)){this.r.clear();d=this.all_points.length>1?this.create_scalers(this.all_points):this.create_scalers_for_single_point();j=d[0];k=d[1];n=this.line_indices;d=m=0;for(o=n.length;m<o;d=++m){c=
  51 +n[d];a=c[0];c=c[1];i=this.all_points.slice(a,c+1||9E9);if(this.options.multi_axis){a=this.all_points.length>2?this.create_scalers(i):this.create_scalers_for_single_point();e=a[0];f=a[1]}else{e=j;f=k}c=function(){var p,q,u;u=[];p=0;for(q=i.length;p<q;p++){h=i[p];u.push(new l(e(h.x),f(h.y)))}return u}();a=this.line_options[d];this.draw_line(i,c,a);if(d===0){if(this.options.show_x_labels===true)this.x_label_coordinates=this.draw_x_labels(i,c);if(this.options.multi_axis&&this.options.show_y_labels===
  52 +true)this.y_label_coordinates=this.draw_y_labels(i);else if(this.options.show_y_labels===true)this.y_label_coordinates=this.draw_y_labels(this.all_points);this.options.show_grid===true&&this.draw_grid(this.x_label_coordinates,this.y_label_coordinates)}else d===1&&this.options.multi_axis&&this.options.show_y_labels===true&&this.draw_y_labels(i,this.width-this.options.x_padding)}}};return b}(w);r.LineChart=v;var Y,Z,$;t={}.hasOwnProperty;s=function(g,b){function a(){this.constructor=g}for(var c in b)if(t.call(b,
  53 +c))g[c]=b[c];a.prototype=b.prototype;g.prototype=new a;g.__super__=b.prototype;return g};Z=function(g,b,a){return{label:g,raw_value:b,index_value:a}};$=function(g,b,a){return{label:g,index_value:b,opacity:a}};Y=function(g){function b(a,c){if(c==null)c={};b.__super__.constructor.call(this,a,new O(c));this.effects=new I(this.r);this.bars=[];this.guides=[];this.index=100}s(b,g);b.prototype.add=function(a,c,d){return this.bars.push(Z(a,c,d))};b.prototype.add_guide_line=function(a,c,d){if(d==null)d=1;
  54 +return this.guides.push($(a,c,d))};b.prototype.add_raw_label=function(a){var c,d=this;c=(new E(this.r)).x(function(){return d.width-10}).y(function(e){return e*15+15}).size(this.options.label_size).attr({fill:"#333","text-anchor":"end","font-weight":"bold"});c.draw(a);return c.draw("(raw value)").attr({"font-weight":"normal","font-size":10})};b.prototype.set_bar_height=function(){return this.bar_height=(this.height-this.options.y_padding)/this.bars.length-this.options.bar_margin};b.prototype.set_threshold=
  55 +function(){var a,c;c=function(){var d,e,f,h;f=this.guides;h=[];d=0;for(e=f.length;d<e;d++){a=f[d];h.push(a.index_value)}return h}.call(this);return this.threshold=Math.max.apply(Math.max,c)};b.prototype.format_tooltip=function(a){return a/100+"x"};b.prototype.draw_raw_bar=function(a,c){var d,e;d=Math.floor(7);e=this.options.x_padding_right-10;this.r.rect(this.width-e,c-d,this.width-this.options.x_padding_right,this.bar_height+14,this.options.rounding).attr({fill:this.options.raw_value_bar_color,stroke:"none"});
  56 +return this.effects.straight_line(new l(this.width-e,this.options.y_padding-10),new l(this.width-e,this.height)).attr({stroke:"rgba(0,0,0,0.25)","stroke-width":0.1})};b.prototype.draw_bg_bar=function(a,c,d){c(a);c=this.options.bg_bar_padding;a=Math.floor(c/2);return this.r.rect(this.options.x_padding-a,d-a,this.width,this.bar_height+c,this.options.rounding).attr({fill:this.options.bar_bg_color,stroke:"none"}).toBack()};b.prototype.shade_bar=function(a,c){if(c==null)c=this.options.bar1_color;a.attr({fill:c,
  57 +stroke:"none"});this.effects.one_px_shadow(a);return this.effects.one_px_highlight(a)};b.prototype.render_bar=function(a,c,d,e){if(e==null)e=this.options.bar1_color;a=this.r.rect(a,c,d,this.bar_height,this.options.rounding);this.shade_bar(a,e);return a};b.prototype.draw_bar=function(a,c,d){var e,f,h;h=c(x.threshold(a,this.threshold));if(a>this.index){e=c(this.index);c=this.render_bar(this.options.x_padding,d,e-this.options.x_padding);d=this.render_bar(e,d,h-e,this.options.bar2_color);f=new C(this.r,
  58 +d,this.format_tooltip(a));f.translate(d.getBBox().width/2,0);c.mouseover(function(){return f.show()});return c.mouseout(function(){return f.hide()})}else{d=this.render_bar(this.options.x_padding,d,h-this.options.x_padding);f=new C(this.r,d,this.format_tooltip(a));return f.translate(d.getBBox().width/2,0)}};b.prototype.draw_guide_line=function(a,c,d,e){var f,h;if(e==null)e=1;h=new l(d,this.options.y_padding);f=new l(d,this.height);this.effects.vertical_dashed_line(h,f,this.options.dash_width).attr({fill:"rgba(0,0,0,"+
  59 +e+")",stroke:"none"});e=(new E(this.r)).x(function(){return d}).y(function(i){return i*15+15}).size(this.options.label_size).attr({fill:"rgba(0,0,0,"+e+")"});e.draw(a).attr({"font-weight":"bold"});return e.draw(c).attr({"font-size":10})};b.prototype.sort_bars_by_index=function(){var a,c;c=function(){var d,e,f,h;f=this.bars;h=[];d=0;for(e=f.length;d<e;d++){a=f[d];h.push(a)}return h}.call(this);c.sort(function(d,e){return e.index_value-d.index_value});return c};b.prototype.clear=function(){b.__super__.clear.call(this);
  60 +this.bars=[];return this.guides=[]};b.prototype.draw=function(){var a,c,d,e,f,h,i,j,k,m,o,n,p,q=this;this.set_bar_height();this.set_threshold();h=this.bar_height+this.options.bar_margin;d=this.bar_height/2;m=this.options.y_padding;j=this.options.x_padding;c=(new E(this.r)).y(function(u){return u*h+m+d}).x(function(){return j-30}).size(12).attr({fill:"#fff","text-anchor":"end"});f=(new E(this.r)).y(function(u){return u*h+m+d}).x(function(){return q.width-10}).size(this.options.label_size).attr({fill:"#333",
  61 +"text-anchor":"end"});i=(new A).domain([0,this.threshold]).range([this.options.x_padding,this.width-this.options.x_padding_right]);k=function(u){return u*(q.bar_height+q.options.bar_margin)+q.options.y_padding};p=this.sort_bars_by_index();e=o=0;for(n=p.length;o<n;e=++o){a=p[e];this.draw_bg_bar(a.index_value,i,k(e));this.draw_raw_bar(a.raw_value,k(e));this.draw_bar(a.index_value,i,k(e));f.draw(a.raw_value);a=c.draw(a.label);this.effects.black_nub(a)}o=this.guides;n=[];f=0;for(k=o.length;f<k;f++){c=
  62 +o[f];n.push(this.draw_guide_line(c.label,c.index_value,i(c.index_value),c.opacity))}return n};return b}(w);r.IndexChart=function(g,b){return new Y(g,b)};var y;t={}.hasOwnProperty;s=function(g,b){function a(){this.constructor=g}for(var c in b)if(t.call(b,c))g[c]=b[c];a.prototype=b.prototype;g.prototype=new a;g.__super__=b.prototype;return g};y=function(g,b,a,c){return{label:g,value:b,average:a,comparison:c}};v=function(g){function b(a,c){if(c==null)c={};b.__super__.constructor.call(this,a,new N(c));
  63 +this.bars=[]}s(b,g);b.prototype.add=function(){return this.bars.push(y.apply(y,arguments))};b.prototype.draw_background=function(a,c){return this.r.rect(this.options.x_padding,c,a.x,this.options.area_width).attr({fill:this.options.area_color,stroke:"none"})};b.prototype.draw_line=function(a,c){return this.r.rect(this.options.x_padding,c.y-this.options.line_width/2,a.x,this.options.line_width).attr({fill:this.options.line_color,stroke:"none"})};b.prototype.draw_average=function(a,c){return this.r.rect(a.x-
  64 +this.options.average_width/2,c-this.options.average_height/2,this.options.average_width,this.options.average_height).attr({fill:this.options.average_color,stroke:"none"})};b.prototype.draw_label=function(){};b.prototype.clear=function(){b.__super__.clear.call(this);return this.bars=[]};b.prototype.draw=function(){var a,c,d,e,f,h,i,j,k,m;k=this.bars;m=[];a=i=0;for(j=k.length;i<j;a=++i){y=k[a];e=[new l(y.comparison,0),new l(y.value,0),new l(y.average,0),new l(0,0)];d=x.get_ranges_for_points(e);c=d[0];
  65 +d=d[1];h=(new A).domain([d,c]).range([this.options.x_padding,this.width-this.options.x_padding]);c=function(){var o,n,p;p=[];o=0;for(n=e.length;o<n;o++){f=e[o];p.push(new l(h(f.x),0))}return p}();d=a*(this.options.area_width+this.options.bar_margin);this.draw_background(c[0],d);a=d+this.options.area_width/2;this.draw_line(c[1],new l(c[0].x,a));this.draw_average(c[2],a);m.push((new z(this.r,0,d+this.options.area_width/2,y.label,"",this.size=14,this.options.font_family)).draw())}return m};return b}(w);
  66 +r.BulletChart=v;t={}.hasOwnProperty;s=function(g,b){function a(){this.constructor=g}for(var c in b)if(t.call(b,c))g[c]=b[c];a.prototype=b.prototype;g.prototype=new a;g.__super__=b.prototype;return g};w=function(g){function b(a,c){if(c==null)c={};b.__super__.constructor.call(this,a,new G(c));this.effective_height=this.height-this.options.y_padding;this.bar_options=[];this.bars=[];this.values=[]}s(b,g);b.prototype.add=function(a){var c,d;c=a.label;d=a.value;this.bar_options.push(G.merge(this.options,
  67 +a.options));this.values.push(d);return this.bars.push({label:c,value:d})};b.prototype.render_bar=function(a,c,d,e){this.r.rect(d.x,d.y,this.options.bar_width,this.effective_height-d.y,this.options.rounding).attr({fill:e.bar_color,stroke:"none"});(new z(this.r,d.x+this.options.bar_width/2,this.height-(this.options.x_label_size+5),a,"",this.options.x_label_size,this.options.font_family,this.options.x_label_color)).draw();return(new z(this.r,d.x+this.options.bar_width/2,d.y-this.options.y_label_size-
  68 +5,c,"",this.options.y_label_size,this.options.font_family,this.options.y_label_color)).draw()};b.prototype.clear=function(){b.__super__.clear.call(this);this.bars=[];return this.values=[]};b.prototype.draw=function(){var a,c,d,e,f,h,i,j,k,m,o,n,p=this;e=function(){var q,u,L,M;L=this.values;M=[];c=q=0;for(u=L.length;q<u;c=++q){i=L[c];M.push(new l(c,i))}return M}.call(this);e.push(new l(0,0));d=x.get_ranges_for_points(e);a=d[2];d=d[3];j=(new A).domain([d,a]).range([this.options.y_padding,this.height-
  69 +this.options.y_padding]);d=function(q){return p.height-j(q)};o=this.bars;n=[];c=k=0;for(m=o.length;k<m;c=++k){a=o[c];f=c*(this.options.bar_width+this.options.bar_spacing)+this.options.x_padding;h=d(e[c].y);f=new l(f,h);n.push(this.render_bar(a.label,a.value,f,this.bar_options[c]))}return n};return b}(w);r.BarChart=w});})();
4 examples/examples.js
@@ -246,7 +246,7 @@ $(document).ready(function() {
246 246
247 247
248 248 multi_axis.add_line({
249   - data: [[1, 828906],[2, 566933],[3, 584150],[4, 1072143],[5, 1622455],[6, 2466746],[7, 2427789]],
  249 + data: [[1, 828906]],
250 250 options: {
251 251 line_color: colors[0],
252 252 dot_color: colors[0],
@@ -258,7 +258,7 @@ $(document).ready(function() {
258 258 });
259 259
260 260 multi_axis.add_line({
261   - data: [[1, 305],[2, 336],[3, 378],[4, 449],[5, 635],[6, 935],[7, 782]],
  261 + data: [[1, 305]],
262 262 options: {
263 263 line_color: colors[2],
264 264 dot_color: colors[2],
16 src/coffeescript/charts/line_chart.coffee
@@ -101,6 +101,11 @@ class LineChart extends BaseChart
101 101
102 102 [x, y]
103 103
  104 + create_scalers_for_single_point: () ->
  105 + y = (i) => 0.5 * (@height - @options.y_padding)
  106 + x = (i) => 0.5 * (@width - @options.x_padding)
  107 + [x, y]
  108 +
104 109 _draw_y_labels: (labels, x_offset = 0) ->
105 110 fmt = @options.label_format
106 111 size = @options.y_label_size
@@ -109,7 +114,10 @@ class LineChart extends BaseChart
109 114 padding = size + 5
110 115 offset = if @options.multi_axis && x_offset > 0 then x_offset else x_offset + padding
111 116
112   - [x, y] = @create_scalers(labels)
  117 + if labels.length == 1
  118 + [x,y] = @create_scalers_for_single_point()
  119 + else
  120 + [x, y] = @create_scalers(labels)
113 121
114 122 label_coordinates = []
115 123 for label, i in labels
@@ -144,7 +152,7 @@ class LineChart extends BaseChart
144 152 [min_y, max_y] = @options.y_axis_scale
145 153
146 154 # draw 1 label if all values are the same
147   - return @_draw_y_labels([new Point(0, max_y)]) if max_y == min_y
  155 + return @_draw_y_labels([new Point(0, max_y)], x_offset) if max_y == min_y
148 156
149 157 labels = []
150 158
@@ -254,7 +262,7 @@ class LineChart extends BaseChart
254 262
255 263 @r.clear()
256 264
257   - [x, y] = @create_scalers(@all_points)
  265 + [x, y] = if @all_points.length > 1 then @create_scalers(@all_points) else @create_scalers_for_single_point()
258 266
259 267 for line_indices, i in @line_indices
260 268 [begin, end] = line_indices
@@ -262,7 +270,7 @@ class LineChart extends BaseChart
262 270
263 271 # scale points on their own axis if multi axis is set
264 272 if @options.multi_axis
265   - [line_x, line_y] = @create_scalers(raw_points)
  273 + [line_x, line_y] = if @all_points.length > 2 then @create_scalers(raw_points) else @create_scalers_for_single_point()
266 274 else
267 275 line_x = x
268 276 line_y = y

0 comments on commit 86ed099

Please sign in to comment.
Something went wrong with that request. Please try again.