views:

27

answers:

1

How do I get non-numeric values on a Flot graph axis?

For example, on the Y-axis I would like to have (excuse the crappy graph):

/*
    A|
    B|          _/*----------*
    C|        _/              \
    D|      _/                 \
    E|    _/                    \
    F|   /                       \
    G|  *                         *
      --------------------------------
       100        200       300   400

The data I would provide will look like this: var data = [[100, 'G'], [200, 'B'], [300, 'B'], [400, 'G']];

+1  A: 

You can feed a function to tickFormatter to markup an axis. Assuming G = 0 and A = 6:

var data = [[100, 0], [200, 5], [300, 5], [400, 0]];

$.plot($("#placeholder"), [data], {
    yaxis: { tickFormatter: function (v, axis) {
        return "GFEDCBA".charAt(v);
      }
    }
});

Flot example

The charAt function simply takes the the y-axis value (0, 5, 5, 0) and returns the letter from the string corresponding to that position.

Alec
Brilliant! Thanks a lot!
No problem. Be sure to accept the answer :)
Alec