Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redrawing jqPlot

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

like image 931
craigtb Avatar asked Dec 27 '22 04:12

craigtb


1 Answers

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.

like image 76
Trevor Owen Avatar answered Jan 05 '23 18:01

Trevor Owen