如何刷新jqplot条形图而不重绘图表

发布于 2024-10-20 11:56:59 字数 954 浏览 6 评论 0原文

我有一个 jqplot 条形图,我希望当用户更改下拉列表上的值时更改图表数据。这可行,但问题是每次用户更改值时,条形图都会重新绘制。

如何更新或重新加载栏而不重新绘制整个内容?是否需要设置任何属性值?

图表数据根据 ajax 调用发生变化:

$.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: {
            shadow: true,
            renderer: $.jqplot.BarRenderer,
            rendererOptions: {
                barPadding: 8,
                barMargin: 10
            }
        },

    };
    return optionsObj;
}

非常感谢您的回复。谢谢。

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: {
            shadow: true,
            renderer: $.jqplot.BarRenderer,
            rendererOptions: {
                barPadding: 8,
                barMargin: 10
            }
        },

    };
    return optionsObj;
}

A reply would be highly appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

奢望 2024-10-27 11:56:59

您想要做的是在绘制新图表时调用 jqPlot 的 .replot() 方法。将您的 ajax 调用更改为如下所示:

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

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

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();
        }});
遮了一弯 2024-10-27 11:56:59

尝试将图表对象作为脚本中的全局变量:

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

然后成功重置数据、axesScale 和重新绘制为:

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

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

删除→记忆 2024-10-27 11:56:59

每次重新绘制图形之前,只需销毁现有的1。

$.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));
}});

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));
}});
双马尾 2024-10-27 11:56:59

我花了一段时间才找到脚本生成数据的答案,所以我将把它发布在这里。我使用了上述代码的组合。

我在脚本文件中创建了一个名为plot3 的全局变量。然后创建了以下函数。当使用重绘布尔值调用它时,它确定我是否需要销毁并重绘或第一次绘制。

第一段代码的作用是从我的 jqgrid 获取数据(正在不同的函数中更新),并更新数组。第二位确定我的间隔刻度,在 x 轴上取决于我的数据长度。

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'
              }
            },
          }
      });
     }
}

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'
              }
            },
          }
      });
     }
}
两相知 2024-10-27 11:56:59

以下是如何在不重新加载页面的情况下使用新数据动态更新绘图的完整示例:

    <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>

这是此人帖子的简化版本:JQPlot 使用动态ajax数据自动刷新图表

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

热鲨 2024-10-27 11:56:59

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

图表是创建图表的 DIV。

这很有效,没什么花哨的。

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

chart is the DIV where chart is created.

this does the trick, nothing fancy by effective.

还如梦归 2024-10-27 11:56:59

也许这会有所帮助。另一方面,我在重新绘图工作方面遇到了问题,但我正在使用数据渲染器。

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

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

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));
    }});
北渚 2024-10-27 11:56:59

希望这有帮助

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
      }
    }
  });
    }});

  });

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
      }
    }
  });
    }});

  });
悲欢浪云 2024-10-27 11:56:59

我得到的最好的方法是,在绘制新图表之前,先清除您正在绘制的 div。

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文