Axis label in Flot
Asked Answered
M

7

49

Does anyone know how one can set the label or title of an axis in Flot?

I've read the API but it doesn't seem to have that feature...

Thanks :)

Malloy answered 6/4, 2011 at 8:10 Comment(0)
B
37

There are none built-in to flot.

Your best bet is to do it yourself via positioned divs, but if you are adventurous, you can look at the issue (Or the original issue) and see how other people have dealt with it.

Specifically, there are two people who have recently made label-related revisions to flot:

https://github.com/RuiPereira/flot/raw/axislabels/jquery.flot.axislabels.js

http://github.com/xuanluo/flot-axislabels

Bubal answered 6/4, 2011 at 15:28 Comment(4)
I'm going to try doing myself with positioned divs. Thank you.Malloy
The y axis label on the homepage is done just as this answer says: by carefully positioned divs. It appears however that version 0.9 of Flot will include axis labels.Permittivity
Thanks @MatthewWalker - once 0.9 is released, I will update my answerBubal
welp. ... it worked, but it also removed my custom label formatting, so no goMaidenhood
E
37

Shameless self-plug: I fixed and greatly extended xuanluo's flot-axislabels plugin: http://github.com/markrcote/flot-axislabels/ As far as I know, it is the best solution for axis labels at the moment.

Enginery answered 12/3, 2012 at 20:0 Comment(3)
this worked for me woo hoo. methinks this can be the accepted answerMaidenhood
Should be the accepted answer. Integration was painlessLiborio
This is absolutely the right answer! Thank you.Glycosuria
P
36

i'm using this workaround:

yaxis: {
tickFormatter: function(val, axis) { return val < axis.max ? val.toFixed(2) : "CZK/l";}
}

Very simple, the max value on the Y axis is replaced by a custom string. I've not tested on the X axis, but I see no reason why it shouldn't work.

Panic answered 8/2, 2012 at 16:53 Comment(3)
sadly it doesn't show on mine as Flot decided to hide the last (max) tick mark from graphMaidenhood
what's the purpose of toFixed(2)?Vagrant
@Vagrant Without the toFixed(2), you might end up seeing this sort of thing on your graph: 11.5000000004. Not very attractive.Unequaled
A
6

A suggestion I saw that works pretty well is to put the graph in the middle of a 3x3 table. Then the labels can be put in the others cells.

<table style="text-align:center">
  <tr>
    <td></td>

    <td>Plot Title Goes Here</td>

    <td> </td>
  </tr>

  <tr>
    <td>Y Axis Label</td>

    <td>
      <div id="graph here" style="width:600px;height:300px"></div>
    </td>

    <td></td>
  </tr>

  <tr>
    <td></td>

    <td>X Axis Label Goes Here</td>

    <td></td>
  </tr>

</table>
Articulate answered 22/6, 2012 at 2:4 Comment(1)
A decent workaround. I think for the y axis, maybe use an image so you can get it vertical.Antimonic
A
3

Example for 2dims graph (x and y axis) solved with pure css.

Y axis:

#placeholder:after {
    content: 'Speed';
    position: absolute;
    top: 50%;
    left: -50px;
    font-weight: 600;
    /* Safari */
    -webkit-transform: rotate(-90deg);
    /* Firefox */
    -moz-transform: rotate(-90deg);
    /* IE */
    -ms-transform: rotate(-90deg);
    /* Opera */
    -o-transform: rotate(-90deg);
    /* Internet Explorer */
    filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3);
}

X axis:

#placeholder:before {
    content: 'Time';
    position: absolute;
    bottom: -30px;
    left: 50%;
    font-weight: 600;
}
Anaesthetize answered 31/8, 2015 at 16:11 Comment(0)
V
0
$("<div class='axisLabel yaxisLabel'></div>")
        .text("Pressure")
        .appendTo($("#yl_1"));

This will works too.

Violaviolable answered 15/7, 2013 at 5:9 Comment(0)
R
0

I use szpapas idea.

I added more jquery code below it to overwrite x-axis like this.

            $('div.flot-x-axis div.flot-tick-label.tickLabel:nth-child(1)').html("Berhad")
            $('div.flot-x-axis div.flot-tick-label.tickLabel:nth-child(2)').html("")
            $('div.flot-x-axis div.flot-tick-label.tickLabel:nth-child(3)').html("Sdn Bhd")
            $('div.flot-x-axis div.flot-tick-label.tickLabel:nth-child(4)').html("")
            $('div.flot-x-axis div.flot-tick-label.tickLabel:nth-child(5)').html("Enterprise")
            $('div.flot-x-axis div.flot-tick-label.tickLabel:nth-child(6)').html("")
            $('div.flot-x-axis div.flot-tick-label.tickLabel:nth-child(7)').html("Koperasi")
Retrogressive answered 16/1, 2018 at 15:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.