使用PrimeFaces p:chart
,是否可以在条形图的数据提示中动态显示x轴标签,而不是系列中的“索引”?
例如,如果我有以下代码:
<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)吗?此外,我希望使用bar模型对其进行动态更新。例如,如果添加了另一个点,如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>
和JavaScript函数:
function tooltipContentEditor(str, seriesIndex, pointIndex, plot) {
return plot.axes.xaxis.ticks[pointIndex] + ", " + plot.data[seriesIndex][pointIndex];
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句