Flot With "String" x-axis

Flot

Flot Problem Overview


When using flot I would like to have a string based x-axis. For example I have a list of customers "Bob", "Chris", "Joe" and would like to plot their revenue on the Y-Axis. (this is a bar graph)

It seems at first glance flot only supports numeric types on the x-axis. Is this true?

Flot Solutions


Solution 1 - Flot

@Matt is close, but it would make more sense to just use the ticks option to directly specify what ticks should have what labels:

var options = {

...
  xaxis: {
    ticks: [[0,'Bob'],[1,'Chris'],[2,'Joe']]
  }
...

};

EDIT: it looks like this (I added more data than labels, but you get the idea).

Solution 2 - Flot

You should be able to do this using the tickFormatter option as per this question. I haven't tried it myself, but give this a shot:

var xAxisLabels = ['Bob', 'Chris', 'Joe'];    
function xAxisLabelGenerator(x){
    return xAxisLabels[x];
}

var plot = $.plot($("#placeholder"), { 
    // snip other options...
    xaxis: {
       transform: xAxisLabelGenerator,
       tickFormatter: xAxisLabelGenerator 
    }
});

This means that the actual x-values should be 0, 1, 2, ...

Solution 3 - Flot

The Categories plugin (jquery.flot.categories.js) will do this quite nicely, so that data can be formatted like this:

var data = [ ["January", 10], ["February", 8], ["March", 4], ["April", 13], ["May", 17], ["June", 9] ];

and plot like this: enter image description here

See: http://www.flotcharts.org/flot/examples/categories/index.html

Categories
Recommended Flot Solutions

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChris MuenchView Question on Stackoverflow
Solution 1 - FlotRyleyView Answer on Stackoverflow
Solution 2 - FlotMatt BallView Answer on Stackoverflow
Solution 3 - Flotak112358View Answer on Stackoverflow