在 PrimeFaces 图表中使用 jqPlot 插件在图表上画线

Using jqPlot plugins in PrimeFaces charts to draw lines on chart

我想在我的 PrimeFaces (v5.3) 图表上绘制一些额外的线条,尤其是在折线图上。查看 jqPlot 示例(PrimeFaces 使用 jqPlot 绘制图表),this example 显示了我想要做的事情。

我使用了this answer中描述的方法。

通过设置扩展器,我可以 运行 我自己的 javascript 功能,它允许我更改不同类型的配置。

Java 创建模式时:

private LineChartModel initLinearModel()
{
    LineChartModel model = new LineChartModel();
    model.setExtender("chartExtender");

    LineChartSeries series1 = new LineChartSeries();
    series1.setLabel("Series 1");
    series1.set(1, 2);
    series1.set(2, 1);
    series1.set(3, 3);
    series1.set(4, 6);
    series1.set(5, 8);

    model.addSeries(series1);

    return model;
}

Xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui">

<h:outputScript library="jqplot-plugin"
    name="jqplot.canvasOverlay.min.js" />
<h:outputScript library="js" name="extra_config.js" />

<h:head>
    <title>Chart</title>
</h:head>

<h:body>

    <p:chart type="line" model="#{primeChart.lineModel1}"
        style="height:300px;" />

</h:body>

</html>

Java脚本函数:

function chartExtender() {
    this.cfg.grid = {
         background : '#888888',
    }
    this.cfg.canvasOverlay = {
            show: true,
            objects: [{horizontalLine: {
                        name: 'pebbles',
                        y: 3,
                        lineWidth: 2,
                        color: 'rgb(255, 55, 124)',
                        shadow: true,
                        lineCap: 'butt',
                        xOffset: 0
                    }}]
        };

}

正在调用 javascript 函数,因为背景实际上已更改 但我没有看到任何变化 我尝试使用 canvas 覆盖。这是示例的输出:

我了解 PrimeFaces 附带的 jqPlot 版本不包含叠加插件。所以我下载了最新的 jqPlot 版本并在我的脚本中包含了覆盖插件(它被包含在 JSF 中)。但是我很可能错过了一些东西,或者在使用这个插件时采取了正确的方法。

firefox webconsole 报告它丢失了 jquery。我还尝试包含 jquery.min.jsjquery.jqplot.min.js(来自 jqplot 版本),这消除了错误,但不显示水平线。

如何包含 jqplot 插件?我怎样才能进一步调试这种情况,看看出了什么问题?

您的具体问题是由 JavaScript 资源的错误排序引起的,这些 JS 错误抱怨无法找到 jQuery 并且生成的 <script> 排序不正确HTML 输出如您在网络浏览器中右键单击 查看源代码 所见。基本上,您通过在 <h:head>.

之前错放 <h:outputScript> 来加载 jqPlot 脚本 before jQuery 和 PrimeFaces 脚本

如果将 <h:outputScript> 移动到 <h:body> 中,如下所示 target="head" ...

<h:head>
    <title>Chart</title>
</h:head>
<h:body>
    <h:outputScript library="jqplot-plugin" name="jqplot.canvasOverlay.min.js" target="head" />
    <h:outputScript library="js" name="extra_config.js" target="head" />

    <p:chart type="line" model="#{primeChart.lineModel1}" style="height:300px;" />
</h:body>

...然后魔法就会开始工作了。

另请参阅:

  • How to reference CSS / JS / image resource in Facelets template?
  • How to use jQuery and jQuery plugins with PrimeFaces

与具体问题无关library="js"是一种不好的做法。仔细阅读 What is the JSF resource library for and how should it be used? 它到底是什么意思以及应该如何使用它(快速回答:摆脱它并使用 name="js/extra_config.js")。