4

I am working with a jqPlot and was wondering if theres a way to resize/redraw it when someone changes the window size. I know there is a redraw function but im not sure how to really invoke it... Can someone give me some pointers on how to do this?

Here is my code:

$.jqplot('chart1', [line1], {
              title:'Users Per Day',
              axes:{
                xaxis:{
                  renderer:$.jqplot.DateAxisRenderer,
                  tickRenderer: $.jqplot.CanvasAxisTickRenderer ,
               //   tickInterval:'1 week',
                  tickOptions:{
                    formatString:'%b %#d, %y',
                    angle:-30
                  } 
                },
                yaxis:{
                  tickOptions:{
                    formatString:'%.1f'
                    }
                }
              },
              highlighter: {
                show: true,
                sizeAdjust: 7.5
              },
              cursor: {
                  show: false
                  /*show: true,
                  zoom: true,
                  showTooltip: false */
              }
          });

'line1' is an array thats populated right before this code and chart1 is the div where the chart is plotted.

Any ideas?

Thanks,

Craig

John Slegers
  • 45,213
  • 22
  • 199
  • 169
craigtb
  • 647
  • 5
  • 12
  • 30

3 Answers3

8

This page on resizable plots is helpful: http://www.jqplot.com/deploy/dist/examples/resizablePlot.html

This is how I solved your particular problem for a project I'm working on (I'll just use your code as the example):

First, assign your plot to a variable:

plot = $.jqplot('chart1', [line1], {
          title:'Users Per Day',
          axes:{
            xaxis:{
              renderer:$.jqplot.DateAxisRenderer,
              tickRenderer: $.jqplot.CanvasAxisTickRenderer ,
           //   tickInterval:'1 week',
              tickOptions:{
                formatString:'%b %#d, %y',
                angle:-30
              } 
            },
            yaxis:{
              tickOptions:{
                formatString:'%.1f'
                }
            }
          },
          highlighter: {
            show: true,
            sizeAdjust: 7.5
          },
          cursor: {
              show: false
              /*show: true,
              zoom: true,
              showTooltip: false */
          }
      });

Then on your page add this function:

$(window).resize(function() {
        plot.replot( {resetAxes: true } );
    });

With resetAxes there it will rescale the axes as well (you do lose any min/max you may have set though). See this page for more info on replot: http://www.jqplot.com/docs/files/jqplot-core-js.html#jqPlot.replot.

2

I used

    var timer;
    $(window).resize(function () {
        clearTimeout(timer);
        timer = setTimeout(function () {
            plot.replot({});
        }, 100);
    });

so it doesn't keep replotting for every pixel, but rather at the end of the resize.

0

Normally jqplot chart occupies the full area which the chart div ID holds. So try to find a way which re-size the div and redraw the chart calling redraw on plot object.

Juliyanage Silva
  • 2,529
  • 1
  • 21
  • 33