jQuery.Gantt icon indicating copy to clipboard operation
jQuery.Gantt copied to clipboard

scale: 'auto'

Open usmonster opened this issue 11 years ago • 4 comments

Allow the scale, minScale and maxScale options to take a value of 'auto' (this should become the default value for scale.)

When specified, a value of 'auto' should choose a reasonable zoom level for that option, based on the date range of the data supplied to the plugin (and probably also the initial width of the right panel). For example, maxScale: 'auto' for a Gantt whose data span just one week would probably limit the max zoom to the 'days' scale, since that zoom level would fit the entire chart, and it would just look silly on the 'weeks' and 'months' scales.

scale: 'auto' would probably be something like max(minScale, maxScale minus a level). minScale: 'auto' would likely keep track of the granularity of the smallest item in the chart; for example, if your shortest task spans a day or two, you probably won't need the 'hours' scale, so we'd want to set the minimum chart resolution to 'days'.

If useCookie is true, then the cookie-set initial scale would still override the scale: 'auto' setting, though new max and min scales might be set (if the data have changed).

I have an early-stage proof-of-concept buried in some of my own code, but I should be able to pony up a pull request "soon"..

usmonster avatar Aug 01 '13 13:08 usmonster

Hello. Have you found any solution to do it? I have same trouble now, i need to scale view to fit all data to grid.

sock4proxy avatar Mar 20 '14 10:03 sock4proxy

@sock4proxy I once hacked together something specific to a project I was working on, but it might be useful to you as a pre-processing step until I integrate it more naturally into the plugin. Here's a slightly modified, insufficiently tested, sparsely commented version of a function I use to get subjectively reasonable max/min/initial scales for a chart based on its given data range:

function get_scale_limits() {
        // normalizes scale(s) to reasonable fit
        var range = element.dateEnd - element.dateEnd;
        var one_day = 8.64e7;
        var limits = {
                months: one_day * 30 * 6, // if range > 6 months, allow month scale
                weeks: one_day * 7 * 3, // if range > 3 weeks, allow week scale
                days: one_day * 10, // if range > 10 days, allow day scale
                hours: one_day * 0 // otherwise, stick to hour scale
        };
        var minScale = (range < limits.days/2) ? 'hours' : 'days';
        var maxScale, nextSmallest;
        for (var scale in limits) {
                if ( maxScale ) {
                        nextSmallest = (maxScale === minScale ? minScale : scale);
                        break;
                } else if ( range > limits[scale] || scale === minScale ) {
                        maxScale = scale;
                }
        }
        // auto-fit initial scale to a reasonable view
        var initialScale = ( range * 0.6 > limits[maxScale] ) ? maxScale : (nextSmallest || minScale);
        return {min: minScale, max: maxScale, initial: initialScale};
}

Please note that it doesn't exactly scale the view to fit all data, but it just chooses the pre-set scale that's the most appropriate fit. It also doesn't take into account the width of the right panel when choosing the scale. Hope it's helpful anyway!

usmonster avatar Mar 20 '14 13:03 usmonster

yep, i already realize this logic with choosing best preset, but your limits working better. thank you for help. additional question: maybe you have any ideas how to do something like combined presets, for example half a day or two hours... i looked into zoom function of original plugin, and think about calling this function, but have no idea how to use it like a scale preset

sock4proxy avatar Mar 20 '14 21:03 sock4proxy

@sock4proxy Glad to help. As for the custom zoom scales, I suggest you subscribe to #82, which is about exactly that. So far no work has been done on the issue to my knowledge, but that will probably require a non-negligible amount of dev work..

usmonster avatar Mar 21 '14 11:03 usmonster