X-Axis ticks on min/max only

699 views
Skip to first unread message

Konrad

unread,
Sep 21, 2011, 7:58:53 AM9/21/11
to Flot graphs
Hi,

I've an X-axis in DateTime format and I want to place ticks with
labels only at the min and the max values left an right. Setting the
number of ticks to 2 I got some misaligned ticks at uneuqal distances.
Setting count to 1 I didn't get a tick at the most centered position
between min and max.

So, what parameters do I have to tweak to get min/max ticks? Hope for
a solution without writing the complete tick-algorithm on my own.

Thanks
Konrad

Michael Wood

unread,
Sep 21, 2011, 8:12:10 AM9/21/11
to flot-...@googlegroups.com

Something like this:

var mintick = 10000, maxtick = 50000;

...
xaxis: {
...
ticks: [mintick, maxtick],
...
}

or:

ticks: [[mintick, "This is min"], [maxtick, "This is max"]]

See the API.txt for details.

--
Michael Wood <esio...@gmail.com>

Juan Mendes

unread,
Sep 29, 2011, 2:31:29 PM9/29/11
to flot-...@googlegroups.com
I think Michael's solution requires that you know the min, max up front

What I did was to create a tickFormatter for the axis that returns blank for all the values except for the first and last one. This means you need to find a way for the tickFormatter function to have access to the axis object so you can determine the max and min for the current data.
--
Juan Mendes

Juan Mendes

unread,
Sep 29, 2011, 2:41:22 PM9/29/11
to flot-...@googlegroups.com
Oops, the axis is already the second argument to the tickFormatter, so it should be really simple

tickFormatter: function(val, axis) {
   if (val == axis.ticks[0]) {
      return  $.plot.formatDate(val, axis.timeFormat);
   }

   // The following statement may not work correctly
   if (val == axis.ticks[axis.ticks.length - 1]) {
      return  $.plot.formatDate(val, axis.timeFormat);
   }
}

May need some fudging but should be more than halfway there
--
Juan Mendes

Juan Mendes

unread,
Sep 29, 2011, 5:27:21 PM9/29/11
to flot-...@googlegroups.com
What I suggested is not going to work,

The ticks array is not fully populated when tickFormatter is called, there's no way to know when it's the last tick.

I looked back at how I got this working and it ended up being a hook on draw, that simply removed the unwanted ticks from the DOM, since they are implemented in HTML.


--
Juan Mendes

Vivek Warpe

unread,
Sep 30, 2011, 4:54:20 AM9/30/11
to flot-...@googlegroups.com
Hi,
 
Just try following.
 
In options set xaxis options to
xaxis: { max: 10, tickSize: 1, tickFormatter: xTicksFormatter, labelWidth: 10,}
 
Then add following piece of code.
 

 var counter = 0;
 var xoptions = options['xaxis'];
 var lasttick = (xoptions.max * xoptions.tickSize).toFixed(0);
       
function xTicksFormatter(v, axis) {
            if (counter == 0 || counter == lasttick) {
                counter++;
                return v;
            }
            else {
                counter++;
                return " ";
            }

        }
 
I guess it should work.
 
Reply all
Reply to author
Forward
0 new messages