JavaScript Diagram Library, V2.8 Released

The new JavaScript library has been released with the following new features:

Fluent API

Builder objects with property setters and shortcut methods for font and brush creation add support for fluent programming style. Static With and instance init methods in DiagramItem, Style and Layout -derived classes return a builder instance that can be used to set up respective new or existing objects.

DiagramLink improvements

  • HeadStroke, HeadStrokeThickness and HeadStrokeDashStyle properties let you customize arrowhead strokes independently of line segments strokes.
  • The AllowSelfLoops property of Diagram class controls whether users are allowed to draw self-loop links.
  • The new Spline element of LinkShape enumeration draws links as interpolating splines that pass through all of their control points.
The new JS Diagram boasts improved DiagramLink-s.

The new JS Diagram boasts improved DiagramLink-s.

Miscellaneous

A trial version is available for download here:

Download MindFusion Diagram Library for JavaScript, V2.8

About Diagramming for JavaScript Library: Written 100% in JavaScript, this tool is a dynamic, browser based visualization library that uses HTML5 Canvas to draw impressive diagrams, schemes, flowcharts, trees and many more. It is browser independent, easy to use and allows you to integrate interactive diagrams for JavaScript and HTML into any web application. This MindFusion graphing library supports a variety of predefined node shapes, customizable links, rich event set and many appearance options.

The user interaction model includes resizing / moving / selecting and modifying any diagram element. The library boasts an elegant API, which is documented in details, numerous step-by-step guides and tutorials. The Diagramming API also provides TypeScript definitions. Various samples are provided to let you learn quickly how to use the most important features of the library – check them here. The JavaScript diagram builder is not only the perfect choice for creating any type of diagram in the browser – it can also arrange it the way you wish with a mouse click using one of its automatic graph layout algorithms. For more details about the features of the component, please visit the Diagram for JavaScript page.

Database Schema with the Java Diagram Library – Part II

The first part of this tutorial is uploaded here. Click here to watch the video that goes with these tutorials.

I. Table Relationships

Now that we have the DB tables from the sakila metadata, it’s time to connect them in a way that mirrors their relationship in the actual MySQL database.

We read the relationships metadata with the helper method in the DBMetaData class:

 ArrayList relations = DBMetaData.getRelationsMetadata(tables);

The method returns a list of our custom DBRelation objects, where each one contains data about two related tables and their fields.

TableNode source = (TableNode)diagram.findNodeById(relation.pk_table);
TableNode destination = (TableNode)diagram.findNodeById(relation.fk_table);

The DBRelation object provides the fields that enter into relation as strings. Since the Diagram.Factor.createDiagramLink method requires the indices of the DB rows we must identify them:

 for(int i = 0; i < rowCount; i++)
    {
         Cell cell = source.getCell(1, i);

         if(cell.getText().equals("" + relation.pk_key + ""))
           {
               pk_index = i;
               break;
            }
     }

Now that we have identified the indexes of the fields that actually relate to each other in both the source and destination tables, we can connect them:

DiagramLink link = diagram.getFactory().createDiagramLink(source, pk_index, destination, fk_index);

We use the capability of the Java diagram library to connect table nodes not generally but a specific row to another specific row.

We also make some visual customizations to make the connectors look more DB-like:

link.setBaseShape(ArrowHeads.RevWithLine);
link.setBaseShapeSize(3f);
link.setHeadShapeSize(3f);
link.setShape(LinkShape.Cascading);

For those rows, that are foreign keys but are not primary keys we add an icon:

try {
    File pathToFile = new File("res/fkey.png");
    Image image = ImageIO.read(pathToFile);
    if(fk_index > -1 && destination.getCell(0,fk_index).getImage() == null)
           destination.getCell(0,fk_index).setImage(image);
    } catch (IOException ex) {
         ex.printStackTrace();
} 

Here is what we have achieved so far:

The diagram is ready but needs a proper layout

The diagram is ready but needs a proper layout

II. Layout

Arranging such DB schema might be a challenging task when done manually but we’ll combine the impressive layout algorithms and arrangement features of the library to arrange the diagram 100% automatically.

A horizontal LayeredLayout provides a nice start. We specify the distance between the layers in the diagram and the nodes – the first two parameters. The last two are the left and top offset of the diagram from the edge of the document.

The diagram links can be arranged by a separate algorithm and we use this feature to polish the way they go around the tables.

 //re-route all links
 diagram.setLinkRouter(new GridRouter());
 diagram.routeAllLinks();

Another useful option is to render link crossings with arcs, which makes them much easier to follow.

//customize the links
diagram.setLinkCrossings(LinkCrossings.Arcs);
diagram.setRoundedLinks(true);
diagram.setRoundedLinksRadius(3);

Finally, the diagram must be updated:

//redraw the control
diagram.repaint();

With that our diagram is finished and it looks great:

The final version of the diagram.

The final version of the diagram.

You can download the complete sample from this link. The sample contains the sakila MySQL database with a PDF file with instructions how to install it if you haven’t already. You will need to have an installed and running MySQL server. Remember to change the login data in the DBConnection.java file to match your login credentials.

About Diagramming for Java Swing: MindFusion.Diagramming for Java Swing provides your Java application with all necessary functionality to create and customize a diagram. The library is very easy to integrate and program. There are numerous utility methods, rich event set, more than 100 predefined shapes. The tool supports a variety of ways to render or export the diagram, advanced node types like TreeView nodes, hierarchical nodes, tables, container nodes and many more. There are 15 automatic layouts, various input / output options and fully customizable appearance. A detailed list with JDiagram’s features is uploaded here. You can check the online demo to see some of the functionality implemented.

Diagramming for Java Swing is royalty free, there are no distribution fees. Licenses depend on the count of developers using the tool – check here the prices.

Database Schema with the Java Diagram Library – Part I

In this blog post we’ll build a database diagram reading the metadata of the sample MySQL database sakila.

Click here to watch the video that goes with these tutorials.

I. Configuration

For our sample to work we need to add the JDiagram.jar package to the project and a JDBC driver for MySQL. We download the JDBC driver from https://dev.mysql.com/downloads/connector/j/ and we add mysql-connector-java-5.1.40.jar to the libs folder of our project where we have placed JDiagram.jar.

The structure of the Class Diagram Java project.

The structure of the Class Diagram Java project.

II. Helper Classes

We create several helper classes that will handle the connection to the database and re-creation of the database metadata in a format which we can use to build the DB diagram.

1. We have created two classes for connecting to the sakila MySql database and reading the metadata. For the tables. In general we are interested in the names of the tables, the columns in them with column name, column data type and column data size. We also want the relationships in the database, with the primary and foreign keys. The classes that provide us with this information are DBConnection, which binds to the database and DBMetaData, which reads the database with the help of the DatabaseMetaData and ResultSet classes of the Java platform.
2. For the purpose of our project we have created two more classes. The first one represents a column in the database – DBColumn. It has just a few fields:

public class DBColumn {

    public String name;
    public String type;
    public String size;
    public boolean primaryKey;

The other class describes a relation in the database. It is equally simple:

public class DBRelation {

    public String pk_key;
    public String pk_table;

    public String fk_key;
    public String fk_table;

3. We have defined an ArrayList tables variable for the table names. We call the respective method of the DBMetaData class that reads the table names and populates the tables list.

III. UI Controls

We will use an instance of the Diagram class, a DiagramView control, a JScrollPane and a zoom control. What we want to show is a diagram pane that is scrollable both horizontally and vertically and a zoom control next to it.

We create the diagram and make sure it will resize itself when needed:

//diagram initialization
diagram = new Diagram();
diagram.setAutoResize(AutoResize.RightAndDown);

The diagram needs a diagramView to render itself onto. We create one and add it to a JScrollPane. The scroll pane provides the necessary scrollbars if the flowchart gets too big.

//initialize a diagramView that will render the diagram.
diagramView = new DiagramView(diagram);
diagramView.setVisible(true);

//use a scroll pane to host large diagrams
_scrollPane = new JScrollPane(diagramView);
_scrollPane.setVisible(true);
_scrollPane.setAutoscrolls(true);

The Java Diagram library provides a handful of auxiliary controls and in our sample we’ll use the Zoom control:

//provide a zoomer for the diagram
zoomer = new ZoomControl();
zoomer.setView(diagramView);
zoomer.setPreferredSize(new Dimension(70, 50));
zoomer.setVisible(true);

We use the setView() method to associate the zoom control with the diagramView. The BorderLayout algorithm helps us arrange the controls. First, we apply it on the JFrame ContentPane and then we align the zoom control to the right. Finally, we add the JScrollPane to the center, which lets it use all the available space left by the zoomer:

getContentPane().setLayout(new BorderLayout());
this.add(zoomer, BorderLayout.EAST);
this.add(_scrollPane, BorderLayout.CENTER);

IV. Creating the Tables

We first read the data for the DB columns and then for each table in the database we create a diagram TableNode.

ArrayList tableData = DBMetaData.getColumnsMetadata(tableName);

We use the Factory class of the diagram library:

Dimension tableSize = new Dimension(50, 30);
TableNode _table = diagram.getFactory().createTableNode(10, 10, 50, tableData.size() * 8, 4, tableData.size());
_table.setCaption("" + tableName + "");
_table.setId(tableName);

The createTableNode() method that we use takes as arguments the location of the table node, its width and height and the count of rows and columns. We use HTML formatting to make the caption of the table bold. For caption and for id of the tableNode we use the name of the table.

Since we need to use HTML styling on text in the diagram nodes we must specify:

diagram.setEnableStyledText(true);

Let’s add some more customization on the table nodes:

_table.setCaptionFormat(new TextFormat(Align.Center, Align.Center));
_table.setCaptionHeight(7f);
_table.setAllowResizeColumns(true);
_table.setShape(SimpleShape.RoundedRectangle);
_table.setBrush(new SolidBrush(new Color((int)204, (int)224, (int)255)));

The caption is aligned in the center and we increase slightly the default caption height. Then we allow users to resize table columns with the mouse and we change the table shape from rectangle to a rectangle with rounded edges. Finally, we color the table nodes with a light blue brush.

Note that once Factory creates a node it gets added to the DiagramNodes collection of the control automatically.
Now let’s populate the table with data:

int rowIndex = 0;

for(DBColumn column: tableData)
{
    _table.getCell(1, rowIndex).setText("" + column.name + "");
    _table.getCell(2, rowIndex).setText(column.type);
    _table.getCell(3, rowIndex).setText(column.size);

    //if the column is a primary key - set an image. If not, leave it empty.
    if(column.isPrimaryKey())
    {
        try {

            File pathToFile = new File("res/key.png");
            Image image = ImageIO.read(pathToFile);
            _table.getCell(0,rowIndex).setImage(image);


        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    rowIndex++;

}

Here we cycle through all DBColumn objects that we have created using the database metadata. In each row in the table node we write the name of the database field, the data type and the data size. Note that we start with the second column (index 1) because the first one is reserved for an image of the primary or foreign key, if applicable. So far we have gathered information only for the primary key.

Since we want the image to occupy the first column we must make some adjustments to the TableNode and its size:

//adjust the size of the tables and columns.
_table.resizeToFitText(true);
Rectangle2D.Float t_size = _table.getBounds();
_table.getColumns().get(0).setWidth(7);
_table.resize(t_size.width + 7, t_size.height);
_table.resizeToFitImage();

First we use resizeToFitText to make the table auto calculate the size it needs based on the text it holds. This, unfortunately would not include space for the image, so we must correct it manually. We resize the first column with 7 pixels to allow the image to fit and we increase the size of the table as well. Then we call resizeToFitImage() to let the TableNode update its new size.

With that we finish the first part of our tutorial. Part II will walk you through the steps to create the relationships among the nodes and arrange the database diagram using the automatic layout algorithms provided with the Java Diagram library. Here is our diagram so far:

Java Database Schema: Tables

Java Database Schema: Tables

You can download the complete sample from this link. The sample contains the sakila MySQL database with a PDF file with instructions how to install it if you haven’t already. You will need to have an installed and running MySQL server. Remember to change the login data in the DBConnection.java file to match your login credentials.

The second part of this tutorial is here.

About Diagramming for Java Swing: MindFusion.Diagramming for Java Swing provides your Java application with all necessary functionality to create and customize a diagram. The library is very easy to integrate and program. There are numerous utility methods, rich event set, more than 100 predefined shapes. The tool supports a variety of ways to render or export the diagram, advanced node types like TreeView nodes, hierarchical nodes, tables, container nodes and many more. There are 15 automatic layouts, various input / output options and fully customizable appearance. A detailed list with JDiagram’s features is uploaded here. You can check the online demo to see some of the functionality implemented.

Diagramming for Java Swing is royalty free, there are no distribution fees. Licenses depend on the count of developers using the tool – check here the prices.

Diagram Library for Java Swing, V4.3

We have released the new version of the Java Diagram library. Here is a brief summary of the new features:

Fluent API

Builder classes in com.mindfusion.diagramming.builders package add support for fluent programming style. Static with and instance init methods in DiagramItem, DiagramItemStyle and Layout -derived classes return a builder instance that can be used to set up respective new or existing objects.

DiagramLink improvements

  • The component no longer keeps a separate segmentCount field, removing a common source of errors. The SegmentCount property now calculates its value from ControlPoints elements. The updateFromPoints(updateGroups, updateSegments) overload has been removed too.
  • SegmentCount setter no longer refuses changing number of segments if auto-routing is enabled or the link is a self-loop.
  • The new Spline element of LinkShape enumeration draws links as interpolating splines that pass through all of their control points:
Java Diagram Library: Spline Links

Java Diagram Library: Spline Links

Enum types

Old-style enumeration classes with static finals have been replaced by enum types, improving type checking and auto-completion support. This change will not affect your code if only passing enum members to methods from the API. If storing them in fields on the other hand, you must change the field type from int to respective enum type.

Several bug fixes
We have fixed a setLicenseKey problem and a bug with the SvgExporter.

You can find details about the new release at the official announcement page here.

The trial version is available for download from the following link:

Download MindFusion.Diagramming for Java Swing, V4.3 Trial Version

Technical support
MindFusion puts special effort in providing high quality technical support to all its current and future clients. You can post your questions about Diagramming for Java at the forum, help desk or at support@mindfusion.eu. All support inquiries are usually answered within hours of being received.

About Diagramming for Java Swing: MindFusion.Diagramming for Java Swing provides your Java application with all necessary functionality to create and customize a diagram. The library is very easy to integrate and program. There are numerous utility methods, rich event set, more than 100 predefined shapes. The tool supports a variety of ways to render or export the diagram, advanced node types like TreeView nodes, hierarchical nodes, tables, container nodes and many more. There are 15 automatic layouts, various input / output options and fully customizable appearance. A detailed list with JDiagram’s features is uploaded here. You can check the online demo to see some of the functionality implemented.

Diagramming for Java Swing is royalty free, there are no distribution fees. Licenses depend on the count of developers using the tool – check here the prices.

Diagramming for Windows Forms, V6.4.2

We have released version 6.4.2 of FlowChart.NET. It contains several customer-requested features and improvements:

Fluent API
Extension methods in MindFusion.Diagramming.Fluent and MindFusion.Diagramming.Layout.Fluent namespaces add support for fluent programming style:

using MindFusion.Diagramming.Fluent;
using MindFusion.Diagramming.Layout.Fluent;
//...

diagram.Factory
	.CreateShapeNode(10, 10, 20, 20)
		.Brush(Color.LightGray)
		.Font("Arial", 12)
		.EnableStyledText(true)
		.Text("Task 1")
		.ToolTip("This is the task");

new TreeLayout()
	.LevelDistance(20)
	.NodeDistance(20)
	.LinkStyle(TreeLayoutLinkType.Cascading3)
	.Arrange(diagram); 

DiagramLink Improvements

  • The component no longer keeps a separate segmentCount field, removing a common source of errors. The SegmentCount property now calculates its value from ControlPoints elements. The UpdateFromPoints(updateGroups, updateSegments) overload has been removed too.
  • SegmentCount setter no longer refuses changing number of segments if auto-routing is enabled or the link is a self-loop.
  • The new Spline element of LinkShape enumeration draws links as interpolating splines that pass through all of their control points:
WinForms Diagram Control: Spline Links

WinForms Diagram Control: Spline Links

Miscellaneous

  • The ModifierKeyAction.ExtendSelection mode selects items within the lasso without deselecting old ones.
  • The Visio Stencils import API provides access to BeginArrow and EndArrow values.
  • The Visio Stencils import API provides access to Connection elements defined for a shape.
  • It is now enough to set EnableStyledText to enable styled-text mode without having to also enable PolygonalTextLayout.
  • Fixed bug where ItemAdded event wasn’t raised for interactively drawn items.
  • Miscellaneous UI control assemblies (NodeListView, LayerListView, Ruler, etc) have been merged into a single MindFusion.Diagramming.WinForms.Controls.dll.

A direct link to download the trial version follows:

Download MindFusion WinForms Diagram Component, V 6.4.2

Updated assemblies are also available as MindFusion.Diagramming NuGet package.

About MindFusion.Diagramming for WinForms: A programming component that provides any WinForms application with a full set of features for creating and customizing all types of diagrams, flowcharts, schemes, hierarchies, trees, graphs etc. The control provides numerous ways to save and load a diagram, six auxiliary controls and more than 12 automatic graph layout algorithms. Diagram elements include scrollable tables, container nodes, multi-segment arrows, custom diagram item types and many more. Further details here.

Diagramming for WinForms is a royalty-free component, clients get 12 month upgrade subscription when buying a license. The source code is also available for purchase. Visit the buy page for a list with the current license prices.