## Formula fields

Formula fields are calculated from DBFields, other formulas, summaries, and parameters, so they can present information that DBFields cannot do.

### Inserting a formula field in a report

A formula field can be inserted in the report areas listed in Component placement. Before you can insert a formula field into a report, you need to define it. For details about how to define formulas, see Creating a formula.

To insert a formula field into a report, you can use either of the following two ways:

- Using dialog
  1. Position the mouse pointer at the destination where you want to insert the formula and do one of the following:
     - Click Insert > Formula on the menu bar.
     - Click the Insert Formula button on the Insert toolbar.

     The Insert Fields dialog appears. See the dialog.

  2. Select the required formula from the Formulas node, then specify its layout in the Insert Layout box.
  3. Click the Insert button to insert the formula into the destination.
- By dragging and dropping

  Select the required formula from the Formulas node of the Resource View panel, then drag it to the destination.

When a formula field has been inserted into a report, you can customize its appearance and behavior by setting properties in the Report Inspector, which can be saved as a CSS style for future use (for details, see Creating a CSS style).

Notes:

- No matter which way you take, if the given formulas are not what your want, you can click the <Add Formula...> item to create one as required.
- If you insert a formula into the detail panel of a component, the name of the formula will automatically be inserted as a label into the corresponding header panel. Otherwise the formula and its name label will be placed in the same panel. If you do not want to insert the name label automatically, you can uncheck Insert field name label with field in the Options dialog (File > Options > Component > Insert field name label with field).

### Changing the display type of a formula field

The display type of a formula field can be changed, that is to say, you can map the formula field to another value or image to be displayed instead.

To change the display type a formula field:

1. Right-click the formula field and select Display Type from the shortcut menu.
2. In the Display Type dialog, specify the required display type in the Type box, and set the options for the selected type in the Web Options panel.
3. Upon finishing, click the OK button to close the dialog.

For details about display types, see Changing the display type of a label.

### Applying web actions to a formula field

A formula field can be bound with some web actions. This allows you to customize a formula field to make it respond to some events, and execute corresponding actions, such as sorting and filtering.

To apply web actions to a formula field:

1. Right-click the formula field and select Display Type from the shortcut menu.
2. In the Web Behaviors box of the Display Type dialog, choose an event from the Events column, then click in the Actions column and click that appears in the text box.
3. In the Web Action List dialog, select a web action and define the action as required.
4. Click in the Display Type dialog to add more web behavior lines and specify the events and actions accordingly. If a web behavior is not required, click to remove it.
5. Adjust the order of the added web actions by clicking or . Then, when an event that has been bound with more than one action happens, the upper action will be triggered first.
6. When done , click OK to accept the settings.

For more information about web actions, see Applying web actions to a label.

### Binding a link to a formula field

You can make a formula field linked with another report, a location specified by a URL, an e-mail address, or a Blob data type field. The link can either be a simple link or a conditional link. With conditional link, different targets can be loaded based on different conditions.

To bind a link to a formula field:

1. Right-click the formula field and click Link on the shortcut menu.
2. In the Insert Link dialog, specify whether it is a conditional link or a simple link, then select the link target and set the options for the link target as required.
3. When done, click OK.

For details about different link targets, see Binding a link to a label.

### Adding conditional formats to a formula field

You can add some conditional formats to a formula field, then when a specified condition is fulfilled, the format bound with the condition will be applied to the field values automatically for distinguishing.

To add conditional formats to a formula field:

1. Right-click the formula field and select Conditional Formatting from the shortcut menu.
2. In the Conditional Formatting dialog, click the button to add a condition.
3. Set the format that will be applied to the field values when the specified condition is fulfilled.
4. Repeat the above two steps to add more conditions and define the format for each condition.
5. When done, click OK to save the settings.

For more details about conditional formatting, see Adding conditional formats to a DBField.

See an example: The SampleComponents catalog, included with JReport Designer, contains reports that have examples of how each component type could be used in a report. For the formula field example, open `<install_root>\Demo\Reports\SampleComponents\UsingFormula.cls`.