开发者

How to refresh jqplot bar chart without redrawing the chart

I have a jqplot bar chart and I want the chart data to be changed when the user changes the value on a drop-down list. That works, but the problem is the bar chart redraws, one over another, each time the user changes the values.

How can I update or reload the bars without drawing the whole thing again? Is there any property value to be set?

Chart data changes according to an ajax call:

$.ajax({
    url: '/Home/ChartData',
    type: 'GET',
    data: { Id: Id },
    dataType: 'json',
    success: function (data) {
        $.jqplot('chartDiv', [a, b], CreateBarChartOptions(xAxis));
}});

function CreateBarChartOptions(xAxis) {
    var optionsObj = {
        title: 'Stat',
        axes: {
            xaxis: {
                renderer: $.jqplot.CategoryAxisRenderer,
                ticks: xAxis
            },
            yaxis: { min: 0 }
        },
        series: [{ label: 'A' }, { label: 'B'}],

        seriesDefaults: {
            sha开发者_如何学运维dow: true,
            renderer: $.jqplot.BarRenderer,
            rendererOptions: {
                barPadding: 8,
                barMargin: 10
            }
        },

    };
    return optionsObj;
}

A reply would be highly appreciated. Thanks.


What you want to do is call jqPlot's .replot() method when you draw the new chart. Change your ajax call to look like this:

$.ajax({
        url: '/Home/ChartData',
        type: 'GET',
        data: { Id: Id },
        dataType: 'json',
        success: function (data) {

            $.jqplot('chartDiv', [a, b], CreateBarChartOptions(xAxis)).replot();
        }});


Try having your chart object as a global variable in your script as:

var plot1 = $.jqplot('chartDiv', [a, b], CreateBarChartOptions(xAxis));

Then on success reset the data, axesScale and replot as:

var newData = [['a',1],['b',2],['c',3]]; 
plot1.series[0].data = newData; 
plot1.resetAxesScale(); 
plot1.replot(); 

Ref: https://groups.google.com/group/jqplot-users/browse_thread/thread/59df82899617242b?pli=1


Each time before redrawing the graph, just destroy the existing1.

$.ajax({
    url: '/Home/ChartData',
    type: 'GET',
    data: { Id: Id },
    dataType: 'json',
    success: function (data) {
        if(plot)
        {
           plot.destroy();
         }
        var plot=$.jqplot('chartDiv', [a, b], CreateBarChartOptions(xAxis));
}});


Took me a while to find an answer for the script generated data, so I'm going to post this right here. I used a combination of the above code.

I created a global var, named plot3 within my script file. Then created the below function. When this is called with a redraw boolean, it determines if I need to destroy and redraw or draw for the first time.

What first bit of code does is gets data from my jqgrid, (which is being updated in a different function), and updates the arrays. The second bit, determines my interval ticks, on the x-axis dependent upon my length of data.

function DrawGraph(bRedraw){
      var testTimes = [];
      testTimes = $('#polarizationTable').jqGrid('getCol', 'TestTime', testTimes, false);
      var RdgA = $('#polarizationTable').jqGrid('getCol', 'RdgA', RdgA, false);
      var RdgB = $('#polarizationTable').jqGrid('getCol', 'RdgB', RdgB, false); 

      var readingLineA = []; 
      for (var i=0; i<testTimes.length; i++){ 
         readingLineA.push([testTimes[i], RdgA[i]]); 
      }

      var readingLineB = []; 
      for (var i=0; i<testTimes.length; i++){ 
         readingLineB.push([testTimes[i], RdgB[i]]); 
      }

     var maxX = $("#testLength").val();
     var lengthX = testTimes.length;
     var tickIntervalX = Math.round(maxX/10);
     if(bRedraw == true)
     {
         plot3.destroy();
         bRedraw = false;
     }
     if(bRedraw == false)
     {
         plot3 = $.jqplot('chart3', [readingLineA, readingLineB], 
         { 
          title:'Graph',
          series:[{label:'Reading - A'}, {label:'Reading - B'}  ],
          legend:{show:true, location:'se'},
          // You can specify options for all axes on the plot at once with
          // the axesDefaults object.  Here, we're using a canvas renderer
          // to draw the axis label which allows rotated text.
          axes:{
            xaxis:{
              label:'Minutes',
              syncTicks: true,
              min: 0,
              numberTicks: 10,
              tickInterval: tickIntervalX,
              max: maxX*1.1,
              labelRenderer: $.jqplot.CanvasAxisLabelRenderer,
              labelOptions: {
                fontSize: '12pt'
              },
            },
            yaxis:{
              label:'Data',
              min: 0,
              numberTicks: 10,
              labelRenderer: $.jqplot.CanvasAxisLabelRenderer,
              labelOptions: {
                fontSize: '12pt'
              }
            },
          }
      });
     }
}


Here is a full example of how to dynamically update the plot with new data without reloading the page:

    <div id="chart1" style="height: 300px; width: 500px; position: relative;"></div>
    <button>New data point</button>

    <script type="text/javascript">
    var storedData = [3, 7];

    var plot1;
    renderGraph();

    $('button').click( function() {
        doUpdate();
    });

    function renderGraph() {
        if (plot1) {
            plot1.destroy();
        }
        plot1 = $.jqplot('chart1', [storedData]);
    }

    function doUpdate() {
        var newVal = Math.random();
        storedData.push(newVal);
        renderGraph();
    }
    </script>

It's a simplified version of this guy's post: JQPlot auto refresh chart with dynamic ajax data


$('#chart).html('');

chart is the DIV where chart is created.

this does the trick, nothing fancy by effective.


Maybe this will help. I on the other hand is having problem with getting replot to work at all, but i'am using a dataRenderer.

$.ajax({
    url: '/Home/ChartData',
    type: 'GET',
    data: { Id: Id },
    dataType: 'json',
    success: function (data) {

        $('chartDiv').empty();
        $.jqplot('chartDiv', [a, b], CreateBarChartOptions(xAxis));
    }});


hope this helps

jQuery(document).ready(function(){
   jQuery.ajax({
    url: '/review_graphs/show',
    type: 'GET',

    success: function (data) {

        var plot1 = jQuery.jqplot('chartDiv', [data,data],
  {
    title: 'Bianual Reviews percentage',
    series:[
      {
        renderer:jQuery.jqplot.BarRenderer,
        label:'Average',
        stackSeries: true,
        dragable: {color: '#ff3366',constrainTo: 'x'},
        trendline:{show: false}
      },
      {
        label:'Trend Line',trendline:{show: false}}
      ],
    legend: {
            show: true,
            placement: 'outsideGrid'
        },
    axesDefaults: {
        tickRenderer: jQuery.jqplot.CanvasAxisTickRenderer ,
        tickOptions: {
          angle: -30,
          fontSize: '10pt'
        }
    },
    axes: {
      xaxis: {
        renderer: jQuery.jqplot.CategoryAxisRenderer
      }
    }
  });
    }});

  });


The best method I got is, the div in which you're drawing, clear that before you draw your new graph.

$('#graph_area).children().remove();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜