【问题标题】:Interval Selection Event in Primefaces ChartPrimefaces 图表中的区间选择事件
【发布时间】:2015-09-21 15:50:41
【问题描述】:

我有一个动态折线图。我将缩放设置为false,但我希望能够在选择图表中的区域时触发 Ajax 事件(例如选择要缩放的区域时的缩放功能),我想调用服务器方法和获取所选区域的最大和最小 x 轴值。 有人可以帮忙吗?

如果我将缩放设置为true,有没有办法从缩放区域获取值?

【问题讨论】:

  • 这不是重复的,你发布的问题他是在javascript中手动生成jqPlot图表,但在我的情况下它是在我的java bean中动态生成的。
  • 好的,这是我的图表源代码,你能告诉我如何在我的情况下使用该问题中发布的答案吗? <p:chart id="myPlot" type="line" model="#{actionsBean.prevSnapshotModel}" responsive="true" widgetVar="myLineChartWV" />
  • 他/她将一个函数绑定到缩放事件并在那里接收数据。因此,这并不意味着数据“生成”的方式很重要。或者换句话说:查看图表生成的 html 源代码。里面有很多 javascript 点
  • JSF(和 PrimeFaces)在这种情况下只不过是一个 html/css/javascript 生成器。

标签: jsf jsf-2 primefaces jqplot linechart


【解决方案1】:

PrimeFaces 和 JSF 在这种情况下只不过是一个 html/javascript/css 生成器。如果您在浏览器开发人员工具中查看生成页面的源代码,您会看到很多 javascript,包括所有数据点。

<script id="j_idt87_s" type="text/javascript">
    $(function(){PrimeFaces.cw('Chart','chart'{
        id:'j_idt87',
        type:'line',
        data:[[[1,2],[2,1],[3,3],[4,6],[5,8]],[[1,6],[2,3],[3,2],[4,7],[5,9]]],
        title:"Zoom",
        legendPosition:"e",
        axes:{
            xaxis:{
              label:"",
              renderer:$.jqplot.LinearAxisRenderer,
              tickOptions:{angle:0}},
            yaxis: {
              label:"",
              min:0,
              max:10,
              renderer:$.jqplot.LinearAxisRenderer,
              tickOptions:{angle:0}}
         },
         series:[{label:'Series 1',renderer: $.jqplot.LineRenderer,showLine:true,markerOptions:{show:true, style:'filledCircle'}},{label:'Series 2',renderer: $.jqplot.LineRenderer,showLine:true,markerOptions:{show:true, style:'filledCircle'}}],zoom:true,datatip:true},'charts');});
    </script>

所以this 问题的答案也适用于 PrimeFaces 图表。

zoom examples 上的浏览​​器开发工具中运行以下代码(来自上面的链接)将显示落在缩放区域内的数据点

chart = PF('chart').plot;
PF('chart').plot.target.bind('jqplotZoom', function(ev, gridpos, datapos, plot, cursor){
    var plotData =  plot.series[0].data;
    for (var i=0; i< plotData.length; i++) {
        if(plotData[i][0] >= chart.axes.xaxis.min && plotData[i][0] <= chart.axes.xaxis.max ) {
            //this dataset from the original is within the zoomed region
            //You can save these datapoints in a new array
            //This new array will contain your zoom dataset
            //for ex: zoomDataset.push(plotData[i]);
            console.log(plotData[i]);
        }
    }
});

PF(çhart') 替换为PF('myLineChartWV') 将使其适用于您的示例

收集数据后,您可以在例如一个 PrimeFaces remoteCommand 将其传递给服务器。

请注意,此仅考虑缩放的 x 轴值。如果您希望考虑 y 轴(因此真正的缩放区域),请添加

  plotData[i][1] >= chart.axes.yaxis.min && plotData[i][1] <= chart.axes.yaxis.max

到循环中的 if 语句。

还要注意它只是拍摄了第一个系列。如果你有多个,你有一些简单的功课要做。

【讨论】:

  • 我在控制台中收到Uncaught TypeError: Cannot read property 'plot' of undefined
  • 那你要么做错了什么(PF('myLineChartWV') 在你的控制台上显示什么(通过点击它们来挖掘))或者你有一个不支持这个的旧 PF 版本
  • 我正在使用 PF 5.2 并且 PF('myLineChartWV') 正在打印未定义
  • 那么 myLineChartWV 不是你的 widgetVar
  • 好的,我正在弄清楚为什么它现在打印未定义,因为包含图表的面板是在数据表的一行中生成的,并且在用户展开该行时呈现。展开行时我会尝试打印 widgetVar 值
猜你喜欢
  • 1970-01-01
  • 2013-05-24
  • 2014-11-13
  • 2016-06-19
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
相关资源
最近更新 更多