10

I am drawing some charts for a school project pulling data from a mysql database. Here is what I've done so far:

DONUT CHARTS

JS CODE:

Morris.Donut({
    element: 'donut-quanti',
    data: [
    {label: "USE FACEBOOK", value: <?php echo $fb_yes;?> },
    {label: "DON'T USE FACEBOOK", value: <?php echo $fb_no;?>}
    ]
    });

BAR CHARTS

JS CODE:

Morris.Bar({
        element: 'bars-status',
        data: [
        {x:'RARELY',a:<?php echo $fb_rar;?>},
        {x:'EV WEEK.',a:<?php echo $fb_ew;?>},
        {x:'EV DAY',a:<?php echo $fb_ed;?>},
        {x:'MULT. TIMES PER DAY',a:<?php echo $fb_mtd;?>}                   
        ],
        xkey:'x',
        ykeys:'a',
        labels:['TOTAL']
        });

Is there a way to display the numeric values (rapresented by the php variables $fb_*) IN PERCENTAGE FORMAT from javascript code (not echoing variable/total * 100 in php ) ?

2
  • are you trying to format the axis, or the number that shows up when the mouse hovers over a point?
    – chiliNUT
    Commented May 17, 2014 at 23:27
  • the number that shows up when the mouse hovers Commented May 18, 2014 at 6:37

3 Answers 3

23

For the donut you need to use the formatter parameter

formatter: function (value, data) { return (value/total *100) + '%'; }

See: http://morrisjs.github.io/morris.js/donuts.html


For the bar you need to use hover callback

hoverCallback: function (index, options, content) {
  var row = options.data[index];
  //assumes you have already calculated the total of your own dataset
  return (value/total *100)+'%';
}

See: http://morrisjs.github.io/morris.js/bars.html

7
  • I read the documentation but I can't understand that part...what is y referring to in the donut chart example? Commented May 18, 2014 at 6:12
  • its just a function argument referring to the "value" part of a donut {"label":"value"} pair, it could be anything but the function docs use y. I've edited that one to maybe make it clearer
    – chiliNUT
    Commented May 18, 2014 at 17:30
  • 1
    Morris.Donut({ element: 'donut-quanti', data: [ {label: "USANO FACEBOOK", value: <?php echo $fb_si;?> }, {label: "NON USANO FACEBOOK", value: <?php echo $fb_no;?>} ] formatter:function(value,data){return 'value*<?php echo $v_android;?>/100'+'%';} }); Commented May 18, 2014 at 19:46
  • 1
    Yes you also need the brackets. I had put them in on mine without even thinking! Good spot. Commented Apr 30, 2015 at 6:52
  • 1
    For both examples you need to calculate the 'total' somewhere first. Commented May 31, 2016 at 2:47
1

To add the percentage symbol, please use this property. I did not find this in the docs but it works perfectly. postUnits: ["%"]

0
donutChartOptions = {
    resize: true,
    colors: ["#8e54e9", "#4776e6"],
    formatter: function (value) { return (value) + '%'; }
}
1
  • 4
    While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
    – Dharman
    Commented Feb 6, 2020 at 13:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.