拖动数据点并提交值

发布于 2024-12-24 01:51:39 字数 222 浏览 2 评论 0原文

在页面 jqPlot 上有一个在 jqPlot 图表上拖动数据点的示例。

我如何提交(例如使用 jQuery ajax)到服务器更改的值?更改的(当前)值是否存储在 jqplot 对象中的某处?

On page jqPlot there is an example of dragging data point on jqPlot chart.

How can I submit (e.g. with jQuery ajax) to server changed values? Are changed (current) values stored somewhere in jqplot object?

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

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

发布评论

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

评论(3

枫林﹌晚霞¤ 2024-12-31 01:51:40

这里最难的事情是知道用户何时拖动一个点,而不是事后获取数据。我建议您使用 postDrawSeries 挂钩,如下所示:

$(document).ready(function () {

  // set up plot
  $.jqplot.config.enablePlugins = true;

  s1 = [['23-May-08',1],['24-May-08',4],['25-May-08',2],['26-May-08', 6]];

  plot1 = $.jqplot('chart1',[s1],{
     title: 'Highlighting, Dragging, Cursor and Trend Line',
     axes: {
         xaxis: {
             renderer: $.jqplot.DateAxisRenderer,
             tickOptions: {
                 formatString: '%#m/%#d/%y'
             },
             numberTicks: 4
         },
         yaxis: {
             tickOptions: {
                 formatString: '$%.2f'
             }
         }
     },
     highlighter: {
         sizeAdjust: 10,
         tooltipLocation: 'n',
         tooltipAxes: 'y',
         tooltipFormatString: '<b><i><span style="color:red;">hello</span></i></b> %.2f',
         useAxesFormatters: false
     },
     cursor: {
         show: true
     }
  });

  // add our hook, to fire after a series update
  $.jqplot.postDrawSeriesHooks.push(updatedSeries);

  function updatedSeries(sctx, options) {
    console.log(plot1.series[0].data); //data for the series is here
  }

});

每次拖动的输出为(包含更新的数据点):

[
Array[2]
, 
Array[2]
, 
Array[2]
, 
Array[2]
]

Here's一个例子。(你必须在浏览器中缓存 jqPlot js 文件,因为它们不允许热链接。)

你必须实现某种计时器来等待 5 秒左右,然后再执行调用 ajax 因为 postdrawseries 钩子会在每个拖动事件上触发。但这应该不会太难。

The hardest thing here is knowing when the user drags a point, not getting the data afterward. I recommend you use a postDrawSeries hook like so:

$(document).ready(function () {

  // set up plot
  $.jqplot.config.enablePlugins = true;

  s1 = [['23-May-08',1],['24-May-08',4],['25-May-08',2],['26-May-08', 6]];

  plot1 = $.jqplot('chart1',[s1],{
     title: 'Highlighting, Dragging, Cursor and Trend Line',
     axes: {
         xaxis: {
             renderer: $.jqplot.DateAxisRenderer,
             tickOptions: {
                 formatString: '%#m/%#d/%y'
             },
             numberTicks: 4
         },
         yaxis: {
             tickOptions: {
                 formatString: '$%.2f'
             }
         }
     },
     highlighter: {
         sizeAdjust: 10,
         tooltipLocation: 'n',
         tooltipAxes: 'y',
         tooltipFormatString: '<b><i><span style="color:red;">hello</span></i></b> %.2f',
         useAxesFormatters: false
     },
     cursor: {
         show: true
     }
  });

  // add our hook, to fire after a series update
  $.jqplot.postDrawSeriesHooks.push(updatedSeries);

  function updatedSeries(sctx, options) {
    console.log(plot1.series[0].data); //data for the series is here
  }

});

Output on every drag is (containing the updated data point):

[
Array[2]
, 
Array[2]
, 
Array[2]
, 
Array[2]
]

Here's an example. (You'll have to cache the jqPlot js files in your browser since they do not allow hotlinking.)

You'll have to implement some sort of timer to wait for 5 seconds or so before calling your ajax since the postdrawseries hook fires on EVERY drag event. That shouldn't be too hard though.

独自唱情﹋歌 2024-12-31 01:51:40

postDrawSeriesHooks 的问题在于,它在您单击图表的整个过程中都会运行。如果您正在进行 ajax 调用,这可能会造成真正的混乱。我会将 jqplotClick 事件绑定到您的图表。您甚至可以到达特定点,或者每次都可以保存所有数据。但每次点击只会保存一次。

$("#chart1").on("jqplotClick", function(ev, gridpos, datapos, neighbor) {
    if ( neighbor ) {
        // specific point that has been clicked
        console.log('x:' + neighbor.data[0] + ' y:' + neighbor.data[1]);
    }
    // all the data from this plot's single series
    console.log(plot1.series[0].data);
});

这里是一个包含多个图表的示例,每次单击都会对 php 文件进行 ajax 调用,并存储两个图表中的所有数据点。但这只允许每个图有一个系列。

var plots = [
    $.jqplot('chart1', [s0], graph_opts),
    $.jqplot('chart2', [s1], graph_opts)
];
$("#chart1, #chart2").on("jqplotClick", function(ev, gridpos, datapos, neighbor) {
    var data = [];
    $.each(plots, function(key, plot) {
        data[key] = plot.series[0].data;
    });

    var ajax_opts = {
        url:      ajax_info.url,
        type:     'post',
        async:    true,
        cache:    false,
        dataType: 'json',
        data: {
            action:  'store_graphs',
            data:    data
        },
        success: function( response ) {
            console.log(response);
        },
        error: function( xhr, textStatus, e ) {
            console.log(xhr.responseText);
        }
    };
    $.ajax(ajax_opts);
});

The problem with postDrawSeriesHooks is that it runs the entire time you are clicking on the graph. This could be a real mess if you are making ajax calls. I would bind the jqplotClick event to your chart instead. You can even get to specific point or you can save all of the data each time. But it will only get saved once per click.

$("#chart1").on("jqplotClick", function(ev, gridpos, datapos, neighbor) {
    if ( neighbor ) {
        // specific point that has been clicked
        console.log('x:' + neighbor.data[0] + ' y:' + neighbor.data[1]);
    }
    // all the data from this plot's single series
    console.log(plot1.series[0].data);
});

Here an example with multiple graphs that makes an ajax call to a php file every click and stores all the data points from both graphs. This only allows for a single series for each plot though.

var plots = [
    $.jqplot('chart1', [s0], graph_opts),
    $.jqplot('chart2', [s1], graph_opts)
];
$("#chart1, #chart2").on("jqplotClick", function(ev, gridpos, datapos, neighbor) {
    var data = [];
    $.each(plots, function(key, plot) {
        data[key] = plot.series[0].data;
    });

    var ajax_opts = {
        url:      ajax_info.url,
        type:     'post',
        async:    true,
        cache:    false,
        dataType: 'json',
        data: {
            action:  'store_graphs',
            data:    data
        },
        success: function( response ) {
            console.log(response);
        },
        error: function( xhr, textStatus, e ) {
            console.log(xhr.responseText);
        }
    };
    $.ajax(ajax_opts);
});
执手闯天涯 2024-12-31 01:51:40

这个答案确实对我们有帮助。

我注意到还有另一个钩子,jqplotDragStop。

所以通过使用这个,我们可以在每次拖动停止时调用ajax。这正是我们所寻找的。

$('#chart1').bind('jqplotDragStop',
  function (seriesIndex, pointIndex, pixelposition, data) {
  // do your stuff here
}); 

希望这会有所帮助(抱歉,不知道如何添加评论,这是 stackoverflow 的新手)。

This answer really helped us.

I noticed there is another hook, jqplotDragStop.

So by using this, we can call ajax every time the dragging stops. Just what we were looking for.

$('#chart1').bind('jqplotDragStop',
  function (seriesIndex, pointIndex, pixelposition, data) {
  // do your stuff here
}); 

Hope this helps (Sorry, could not figure out how to add a comment, new to stackoverflow).

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