javascript - Morris donut chart always start at 12 oclock (top middle) -
i using morris donut charts , want try , ensure segments start @ 12 o'clock there consistency when viewing multiple charts on same screen.
previously used jquery circliful https://github.com/pguso/jquery-plugin-circliful worked great quality fuzzy on retina screens checked out morris , want bar this.
i want looks first segment starts @ top middle:
does know how achieve this?
var donut= morris.donut({ element: 'donut-example', data: [ {label: "new clients", value: 35}, {label: "in-store sales", value: 65} ], colors: ['#90c070', '#eeeeee'], select :0 }); donut.select(0);
no, far can tell source code, there's no api (public or private) in morris that. see donut's redraw()
how renders chart: starts lowest point , goes in counterclockwise direction.
now, recommend consider switching other chart libraries, before proceeding. example there's d3.js — not simplest library out there, provides freedom anything, may hard work with. there a ton of examples of every thing think of. like, here's blog post creating similar looking donut charts using d3.
but hey, it's javascript, can patch things, nothing impossible!
warning: patching library code not best idea! must careful , when it's way.
the easiest (certainly not simplest) way monkey-patch morris.donut.redraw()
start math.pi (middle top) , go in clockwise direction. that, initial value of last
local variable should changed 0
math.pi
, logic finds next starting point tweaked bit.
here's working example i've surrounded lines patched special patched
comments:
/** * careful: patched lib-function! * * in case of problems, see <a href="https://github.com/morrisjs/morris.js/blob/master/morris.js#l1894">original</a> */ morris.donut.prototype.redraw = function() { var c, cx, cy, i, idx, last, max_value, min, next, seg, total, value, w, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _results; this.raphael.clear(); cx = this.el.width() / 2; cy = this.el.height() / 2; w = (math.min(cx, cy) - 10) / 3; total = 0; _ref = this.values; (_i = 0, _len = _ref.length; _i < _len; _i++) { value = _ref[_i]; total += value; } min = 5 / (2 * w); c = 1.9999 * math.pi - min * this.data.length; // ------------ patched: start top last = math.pi; // original: // last = 0 // ------------ idx = 0; this.segments = []; _ref1 = this.values; (i = _j = 0, _len1 = _ref1.length; _j < _len1; = ++_j) { value = _ref1[i]; // ------------ patched: change direction of rendering next = last - min - c * (value / total); seg = new morris.donutsegment(cx, cy, w * 2, w, next, last, this.data[i].color || this.options.colors[idx % this.options.colors.length], this.options.backgroundcolor, idx, this.raphael); // original: // next = last + min + c * (value / total); // seg = new morris.donutsegment(cx, cy, w * 2, w, last, next, this.data[i].color || this.options.colors[idx % this.options.colors.length], this.options.backgroundcolor, idx, this.raphael); // ------------ seg.render(); this.segments.push(seg); seg.on('hover', this.select); seg.on('click', this.click); last = next; idx += 1; } this.text1 = this.drawemptydonutlabel(cx, cy - 10, this.options.labelcolor, 15, 800); this.text2 = this.drawemptydonutlabel(cx, cy + 10, this.options.labelcolor, 14); max_value = math.max.apply(math, this.values); idx = 0; _ref2 = this.values; _results = []; (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) { value = _ref2[_k]; if (value === max_value) { this.select(idx); break; } _results.push(idx += 1); } return _results; }; var donut = morris.donut({ element: 'donut-example', data: [{ label: "in-store sales", value: 55 }, { label: "new clients", value: 25 }, { label: "reseller stores", value: 20 }], colors: ['#90c070', '#ee9090', '#9090ee'], select: 0 }); donut.select(0);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="https://rawgit.com/morrisjs/morris.js/master/morris.js"></script> <script src="http://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script> <link href="http://cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css" rel="stylesheet" /> <div id="donut-example" style="height: 250px;"></div>
of course, there's may way achieve same thing: maybe overlooked local variable changing sign of result in same rendering.
Comments
Post a Comment