jsf - 在 p:chart (bar) 的 datatip 中动态显示 x 轴标签
问题描述
使用 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) 吗?此外,我想让它们使用条形模型动态更新。即,如果添加另一个点,如 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];
}
推荐阅读
- android - AlertDialog 单选项填充
- node.js - 带有 pg-promise 的友好堆栈跟踪查询错误
- javascript - 在内联 JavaScript 加载后加载一个 JavaScript 外部文件
- javascript - Leaflet TimeDimension 显示当地时间
- c - 编程珍珠:文件中所有单词的频率
- mysql - mysql替换和更改
- r - 阻止文本标签在 ggplot2 中重叠
- flutter - 有没有办法在颤动中对列表视图进行排序?
- javascript - 将 CSS.supports() 与媒体查询一起使用?
- python - 为什么我的模拟类的实例共享属性?