在 p:chart 的数据提示中动态显示 x 轴标签(条)

Dynamically display the x-axis labels in the datatip of p:chart (bar)

使用 PrimeFaces p:chart,是否可以在条形图的数据提示中动态显示 x 轴标签而不是系列中的 'index'?

例如,如果我有以下代码:

<h:body>                                 
   <p:chart type="bar" model="#{barChartDatatipView.barChartModel}" />                                                                        
</h:body>

@ManagedBean
@ViewScoped
public class BarChartDatatipView {

  public BarChartModel getBarChartModel() {

    BarChartModel model = new BarChartModel();
    ChartSeries chartSeries = new BarChartSeries();
    chartSeries.set("car", 1222);
    chartSeries.set("bus", 3323); 
    model.addSeries(chartSeries);
    return model;
  }
}

数据提示将显示 (1,1222) 和 (2,3323)。我可以将它们显示为 (car,1222) 和 (bus,3323) 吗?此外,我想让它们用条形模型动态更新。即,如果添加另一个点,如 chartSeries.set("train",4455),数据提示也应相应更新。

我正在使用 Java 8、JSF2.2 和 Primefaces 6.2。

研究了很久jqplot结构,终于找到答案:

Java代码:

@ManagedBean
@ViewScoped
public class BarChartDatatipView {

  public BarChartModel getBarChartModel() {

    BarChartModel model = new BarChartModel();
    ChartSeries chartSeries = new BarChartSeries();
    chartSeries.set("car", 1222);
    chartSeries.set("bus", 3323); 
    model.addSeries(chartSeries);
    model.setDatatipEditor("tooltipContentEditor");  // Set the editor.
    return model;
  }
}

HTML 页数:

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:p="http://primefaces.org/ui">

  <h:head>
    <title>Bar Chart - datatip</title>
    <h:outputScript name="js/barchart-datatip.js" />   
  </h:head>
  <h:body>                                                 
    <p:chart type="bar" model="#{barChartDatatipView.barChartModel}" />                                                                        
  </h:body>
</html>

和Java脚本函数:

function tooltipContentEditor(str, seriesIndex, pointIndex, plot) {
    return plot.axes.xaxis.ticks[pointIndex] + ", " + plot.data[seriesIndex][pointIndex];
}