Jump to content
We've recently updated our Privacy Statement, available here ×
  • How to use a custom hyperlink handler with a Dashboard in TIBCO JasperReports® Server


    kkumlien
    • Features: Ad Hoc, Charts, Dashboards, JasperReports Server, Parameters, Reports, Repository, URLs Version: v7.5, v7.3, v7.2, v7.1, v6, v6.1 Product: JasperReports® Server

    IMPORTANT: to make this work for Dashboards in TIBCO JasperReports@ Server versions 6.4.2 and later, replace define(function(require) with define("dashboardHyperlinkHandlerUpdateDashboardParams", function(require) in the JavaScript file described in this article, as per ktalarico's comment below.

    For a single Report outside a Dashboard, see example at the bottom of this article.

    Problem

    Drill-down or drill-across hyperlinks in a Dashboard are available out of the box in Ad Hoc Dashlets (Hyperlinks tab in Dashlet Properties), but not in Dashlets containing production reports designed in TIBCO Jaspersoft® Studio. A possible solution is using a custom hyperlink handler.

    For cross-linking in a Dashboard, the same approach is required in both cases - See a complete implementation here: https://github.com/ernestoongaro/jrs-helper-scripts/tree/master/crosslink-dashboard

    Here's the related section from the JasperReports Ultimate Guide (p.166 of the PDF):

    CUSTOM HYPERLINKS

    In addition to the standard hyperlink types, users can define hyperlinks having custom types. A custom-typed hyperlink can have arbitrary parameters and is meant to be processed by a hyperlink handler registered while exporting the report.

    When a hyperlink is declared as having a type other than the built-in types, the hyperlink is considered of custom type and the user is expected to provide handlers to process the hyperlink when the report is exported. Arbitrary hyperlink parameters can be added to a custom hyperlink using the <hyperlinkParameter> tag. These parameters are made available to the custom hyperlink handler so that it can generate a final hyperlink depending on the parameter values. Hyperlink parameter expressions are evaluated along with the hyperlink, and the results are kept in the generated hyperlink object as parameter values.

    When exporting the report to other formats such as HTML or PDF, the user can set a factory of hyperlink handlers using the HYPERLINK_PRODUCER_FACTORY export parameter. A factory is an implementation of net.sf.jasperreports.engine.export.JRHyperlinkProducerFactory, which is responsible for creating a hyperlink handler for a custom hyperlink type. This hyperlink handler created by the factory is a net.sf.jasperreports.engine.export.JRHyperlinkProducer instance, and it is used for generating a hyperlink reference in the export document by assembling hyperlink parameters and other information supplied at export time.

    To handle custom hyperlinks in the built-in Swing viewer, you need to register a hyperlink listener by calling addHyperlinkListener(listener) on the net.sf.jasperreports.view.JRViewer component. The listener is an implementation of the net.sf.jasperreports.view.JRHyperlinkListener interface. When a report hyperlink gets clicked, the listener queries the hyperlink type and performs the desired actions.


    Limitations

    The sample code below was tested in JasperReports Server 6.1 and might not work in previous or following releases.

    Additionally, it will only work when viewing a saved Dashboard and will not work when previewing it from the Dashboard Designer.


    Solution

    1. Create a custom hyperlink type in your main report. Here's an example for an HTML5 chart. In this case we named it "UpdateDashboardParams":

      <hc:series name="Measure1">
          <hc:contributor name="SeriesItemHyperlink">
              <hc:contributorProperty name="hyperlinkType" valueType="Constant" value="UpdateDashboardParams"/>
              <hc:contributorProperty name="area" valueType="Bucket" value="State.area"/>
          </hc:contributor>
      </hc:series>

      Here's an example for other elements:

      Customlinktype.png.ab3d80ab6054a636709b8ae646cb62f7.png

    2. Create a new Dashboard and put all your reports on canvas

    3. Go to Filter Manager in the Dashboard and manually create a filter (will create a section named "Manually Created Filters") with the same name as it is produced by your custom hyperlink. In this example it is "area". Then wire it to corresponding parameters in your "child" reports:

      ScreenShot2016-03-07at3_09_00pm.png.80955ace4e6377069a34b3b2fa4dde74.png

    4. Save the Dashboard.

    5. Create a custom handler for your new hyperlink type, save it as dashboardHyperlinkHandler<YOUR_HYPERLINK_TYPE>.js (in this case it is dashboardHyperlinkHandlerUpdateDashboardParams.js) and put it to both /scripts and /optimized-scripts folders of your server deployment:

      define(function(require) {
          "use strict";
      
          var $ = require("jquery"),
              _ = require("underscore");
      
          return {
              events: {
                  click: function(event, linkData) {
                      if (window.location.href.indexOf("dashboard/viewer") > -1) {
                          var hashParts = window.location.hash.split("&"),
                              reportUri = hashParts[0],
                              params = {};
      
                          for (var i = 1; i < hashParts.length; i++) {
                              if (hashParts[i].indexOf("=") > 0) {
                                  var key = hashParts[i].split("=")[0],
                                      value = hashParts[i].split("=")[1];
      
                                  if (key in params) {
                                      params[key].push(value);
                                  } else {
                                      params[key] = [value];
                                  }
                              }
                          }
                          location.replace(reportUri + "&" + $.param(_.extend(params, linkData.parameters), true));
                      }
                  }
              }
          }
      });
    6. Open your dashboard in Dashboard Viewer and try hyperlinks.

      To make hyperlinks work in Visualize.js, you'll have to override linkOptions:

      linkOptions: {
                  events: {
                      click: function(ev, link, defaultHandler) {
                          if (link.type === "UpdateDashboardParams") {
                              dashboard.params(convertParams(link.parameters)).run();
                          } else {
                              defaultHandler();
                          }
                      }
                  }
              }

    A working Viz.js sample can be found at http://jsfiddle.net/v6a23vz7/

    Just update the host name to your server and dashboard URI if needed.


    For a single Report

    For a single Report not in a Dashboard, see the attached example jasperreports-custom-hyperlink-handler.jar.

    The Javascript is inside the JAR that you need to deploy to WEB-INF/lib of JasperReports Server. After deploying that file and restarting the instance, import export-expandcollapse.zip (also attached) into organization_1 as superuser: 'Manage' > 'Organizations' > right-click on 'Organizations' > 'Import...'.

    Then you can test this sample "expand/collapse" functionality in this report: '/Public/Accounts Report'.

    Customlinktype.png.6abd5813ddff0fc62683e8296a6cc5e5.png

    ScreenShot2016-03-07at3_09_00pm.png.9428c5e676ef677593c15d400535d95c.png

    jasperreports-custom-hyperlink-handler.jar

    export-expandcollapse.zip


    User Feedback

    Recommended Comments

    Does anyone know how to resolve the Mismatched anonymous define() module error? 

     

    jquery.js:3 [Violation] 'load' handler took 263msrequire.js:1 Uncaught Error: Mismatched anonymous define() module: function(require) {    "use strict";     var $ = require("jquery"),        _ = require("underscore");     return {        events: {            click: function(event, linkData) {                if (window.location.href.indexOf("dashboard/viewer") > -1) {                    var hashParts = window.location.hash.split("&"),                        reportUri = hashParts[0],                        params = {};                     for (var i = 1; i < hashParts.length; i++) {                        if (hashParts[i].indexOf("=") > 0) {                            var key = hashParts[i].split("=")[0],                                value = hashParts[i].split("=")[1];                             if (key in params) {                                params[key].push(value);                            } else {                                params[key] = [value];                            }                        }                    }                    location.replace(reportUri + "&" + $.param(_.extend(params, linkData.parameters), true));                }            }        }    }}https://requirejs.org/docs/errors.html#mismatch    at makeError (require.js:1)    at v (require.js:1)    at require.js:1makeError @ require.js:1v @ require.js:1(anonymous) @ require.js:1setTimeout (async)

    Link to comment
    Share on other sites

    I have this working in 7.2 with the following change provided by Tibco support. 

    "Our engineering team investigated this and says that the issue seems to be caused by some strange behavior in require.js"[/code]

     

    replace define(function(require) {with define("dashboardHyperlinkHandlerUpdateDashboardParams", function(require) {

    Link to comment
    Share on other sites



    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

×
×
  • Create New...