简体   繁体   English

在JS中更新散景图

[英]Updating Bokeh Plot in JS

I am developing in Javascript to render a table using a DataTables (Table plug-in for jQuery). 我正在用Javascript开发,以使用DataTables(适用于jQuery的Table插件)呈现表。 I am also using Bootstrap Multiselect ( https://github.com/davidstutz/bootstrap-multiselect ) to filter the table. 我还使用Bootstrap Multiselect( https://github.com/davidstutz/bootstrap-multiselect )来过滤表。 I would like to re-render my BokehJS plot everytime the table is re-drawn. 我想在每次重新绘制表格时重新渲染我的BokehJS图。 I have hooked up the correct calls but I am also calling Bokeh.Plotting.show to re-render the graph. 我已经挂接了正确的调用,但是我也正在调用Bokeh.Plotting.show重新呈现图形。 This forces me to remove the last created div to avoid multiple graphs plotted. 这迫使我删除最后创建的div,以避免绘制多个图形。 I am new to the JS side of Bokeh and wanted to understand if there is a cleaner method to update the plot in JS? 我是Bokeh的JS方面的新手,并想了解是否存在一种更清洁的方法来更新JS中的绘图?

    var eventFired = function ( type ) {
    //var n = $('#demo_info')[0];
    //n.innerHTML += '<div>'+type+' event - '+new Date().getTime()+'</div>';
    //n.scrollTop = n.scrollHeight;   

    var plt = Bokeh.Plotting;

    // set up some data
    var M = 25;
    var xx = [];
    var yy = [];
    var colors = [];
    var radii = [];
    for (var y = 0; y <= M; y += 4) {
        for (var x = 0; x <= M; x += 4) {
            xx.push(x * (Math.random() +0.5));
            yy.push(y * (Math.random() +0.5));
            colors.push(plt.color(50+8*x, 30+8*y, 150));
            radii.push(Math.random() * 0.4 + 1.7)
        }
    }

    // create a data source
    var source = new Bokeh.ColumnDataSource({
        data: { x: xx, y: yy, radius: radii, colors: colors }
    });

    // make the plot and add some tools
    var tools = "pan,crosshair,wheel_zoom,box_zoom,reset,save";
    var p = plt.figure({ title: type+' event - '+new Date().getTime(),
                        height: 300,
                        width: 300,
                        tools: tools });

    // call the circle glyph method to add some circle glyphs
    var circles = p.circle({ field: "x" }, { field: "y" }, {
        source: source,
        radius: radii,
        fill_color: colors,
        fill_alpha: 0.6,
        line_color: null
    });

    //remove old plot on update conditions
    $('.bk-root').remove();

    // Show the plot, appending it to the end of the current
    // section of the document we are in.

    Bokeh.Plotting.show(p, document.getElementById('myplot'));
    //To Do: add in transition to improve the UI appearance
}

And here is the the datatable setting up the call back to the BokehJS script. 这是数据表,用于设置对BokehJS脚本的调用。

</script>
      $('#table').DataTable({
    responsive: {
        details: {
            display: $.fn.dataTable.Responsive.display.modal( {
                header: function ( row ) {
                    var data = row.data();
                    return 'Details for '+data[0]+' '+data[1];
                }
            } ),
            renderer: $.fn.dataTable.Responsive.renderer.tableAll( {
                tableClass: 'table'
            } )
        }
    },
    paginate: false,
    info: false,
    paging: true,
    autoWidth: true,
    dom: '<"dt-buttons">Bf<"clear">lirtp',
    "buttons": [ 'copy', 'csv', 'excel' ],
  }).on( 'draw.dt',   function () { eventFired( 'Draw' ); } );

Lastly, is there a good method to update the plot via a transition effect to improve the appearance of the re-plotting? 最后,是否有一种很好的方法可以通过过渡效果来更新图以改善重新绘制的外观?

I eventually figured out all of the components in the process in a modified use case compared to the one shown above. 与上述示例相比,我最终在修改后的用例中弄清了流程中的所有组件。 In my present approach I needed to utilize my source data and emit a 'change' trigger. 在我目前的方法中,我需要利用我的源数据并发出“更改”触发器。

source.data.factors = [my new factor list]
source.trigger('change');

The new factor list based on the jQuery datatable can be obtained as followed. 可以如下获得基于jQuery数据表的新因素列表。

$('mytable').DataTable().columns(0, {search: 'applied'}).data()[0]

Also in my use case I am working with a categorical axis. 同样在用例中,我正在使用分类轴。 In my specific use case, my factors will dynamically change upon redraw of the graph. 在我的特定用例中,我的因素将在重绘图形时动态变化。 This can be achieved using the plot's attributes. 这可以使用绘图的属性来实现。

p.attributes.x_range.factors = [ my new factor list from my updated source data]

There is also no need to remove the old plot, and the added plus is the redraw is very fast for my simple plots. 也无需删除旧图,而对我的简单图而言,添加的优点是重绘非常快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM