Search
Tutorial 4: Dashboard Layout

This tutorial assumes you have added the JChart.jar reference as described in Getting Started guide. The complete example project is available under Samples subfolder of library's distribution.

The Dashboard class represents a container of visual components such as plots, line graphics and text labels. Initially the dashboard contains two Panel classes to which its components should be added:

  • RootPanel is the root of dashboard components' hierarchy. Its elements are drawn overlapping on top of each other. Here you can add elements whose position should not depend on the size of the dashboard and their siblings. For example, Chart controls dispay LegendRenderer inside the RootPanel.
  • LayoutPanel is a vertical stack panel by default. Its components are arranged next to each other and dimensions are calculated automatically from available space. The components that we are going to create in this tutorial will be added here.

Let's start by adding a Dashboard component to the frame.

Java  Copy Code

Dashboard createDashboard()
{
    Dashboard dashboard = new Dashboard();
    ...
    return dashboard;
}

Tutorial4()
{
    JFrame frame = new JFrame();
    frame.setSize(1000, 600);

    frame.getContentPane().add(
        createDashboard(), BorderLayout.CENTER);

    frame.setVisible(true);
}

static public void main(String[] args)
{
    SwingUtilities.invokeLater(Tutorial4::new);
}

In createDashboard function, add a TextComponent that will display the dashboard's title. As LayoutPanel arranges its children in a vertical stack by default, the title will show at the top of the dashboard if it's the first component added. The following code creates and adds the title component, as well setting a few appearance properties:

Java  Copy Code

TextComponent title = new TextComponent();
title.setText("TITLE TEXT");
title.setFontName("Times New Roman");
title.setFontStyle(EnumSet.of(FontStyle.BOLD));
title.setFontSize(20.0);
title.setHorizontalAlignment(LayoutAlignment.Center);
dashboard.getLayoutPanel().getChildren().add(title);

We want to divide the remaining space in two rows and two columns, creating four cells in total. For that purpose, add a GridPanel, which can host multiple components in each of its cells. Its default constructor creates one GridRow and one GridColumn, i.e. it defines a single cell. We'll add one more row and column to respective collection properties of the panel:

Java  Copy Code

GridPanel grid = new GridPanel();
grid.getColumns().add(new GridColumn());
grid.getRows().add(new GridRow());
dashboard.getLayoutPanel().getChildren().add(grid);

Now that we have defined our layout, lets create sample data series. MindFusion.Charting offers multiple classes you could use to represent data. All of them implement the Series interface, which you could implement in your own model classes if you prefer. The data class we'll use in this example is BarSeries; it takes a list of values to display as bar height, and automatically assigns bar positions at equal distances. It is convenient for creation of barcharts, but can actually be used with any other type of SeriesRenderer.

Java  Copy Code

BarSeries data1 = new BarSeries(
    listOf(10, 15, 17, 16, 20, 15, 14, 13, 10, 17, 22, 25, 25, 25, 21, 21),
    null, null,
    List.of("nov\n2014","dec\n2014", "jan\n2015", "feb\n2015", "mar\n2015",
        "apr\n2015", "may\n2015", "jun\n2015", "jul\n2015", "aug\n2015", "sep\n2015",
        "oct\n2015", "nov\n2015", "dec\n2015", "jan\n2016", "feb\n2016"));
data1.setTitle("LP Vinyl");

BarSeries data2 = new BarSeries(
    listOf(13, 13, 14, 16, 23, 25, 20, 19, 16, 17, 15, 13, 13, 17, 18, 21),
    null, null);
data2.setTitle("CD");

BarSeries data3 = new BarSeries(
    listOf(29, 28, 29, 31, 33, 35, 33, 27, 28, 28, 27, 29, 29, 31, 33, 29),
    null, null);
data3.setTitle("MP3");

List<Double> listOf(double... elements)
{
    return DoubleStream.of(elements).boxed().collect(Collectors.toList());
}

You can see that BarSeries has two overloaded constructors. They let you specify different kinds of labels for the bars. MindFusion.Charting supports six kinds of labels displayed at various locations of rendered graphics. BarSeries lets you define inner labels drawn inside bars, top labels drawn above their bars, and X axis labels drawn along the axis. In the example above we'll only show axis labels, passing null for the other arguments. The Title values will be drawn by a LegendRenderer we are going to add later.

For visualizing the data, you must create an instance of a class derived from SeriesRenderer, each drawing a different kind of graphics. In this tutorial we'll see how several of them work, starting with a LineRenderer:

Java  Copy Code

LineRenderer lineRenderer = new LineRenderer(
    new ObservableList<Series>(data1, data2, data3));

Apart from the kind of graphics drawn, a SeriesRenderer lets you specify styling attributes for the graphic primitives, such as stroke and fill colors:

Java  Copy Code

PerSeriesStyle lineStyle = new PerSeriesStyle();
lineStyle.setStrokes(
    new Color(102, 154, 204),
    new Color(0, 52, 102),
    new Color(206, 0, 0));
lineStyle.setFills(
    new Color(102, 154, 204),
    new Color(0, 52, 102),
    new Color(206, 0, 0));
lineStyle.setStrokeThicknesses(List.of(3.0));
lineRenderer.setSeriesStyle(lineStyle);

SeriesRenderer objects must be placed inside Plot components. In this tutorial we'll use three different kinds of plots: Plot2D and Plot3D for drawing in 2D and 3D Cartesian coordinate systems respectively, and RadarPlot for drawing in polar coordinate system. For the LineRenderer we defined above, create a Plot2D and set its background:

Java  Copy Code

Plot2D linePlot = new Plot2D();
linePlot.setBackground(
    new GradientBrush(
        Colors.White,
        new Color(224, 224, 224),
        90 /*gradient angle*/));

Add the line renderer to plot's SeriesRenderers collection:

Java  Copy Code

linePlot.getSeriesRenderers().add(lineRenderer);

The data points renderered inside a Plot2D are scaled relatively to the Axis ranges defined via its XAxis and YAxis properties. For each Axis you can define its first and last visible values by setting MinValue and MaxValue properties. If their values are not specified, the control will calculate them automatically to fit all data provided through plot's SeriesRenderer objects. In adition, the Interval property lets you specify the distance between coordinate labels. The Axis class also has a Title property that lets you show description for the measure represented by an axis:

Java  Copy Code

linePlot.setXAxis(
    Axis.with()
        .minValue(0)
        .maxValue(15)
        .interval(1)
        .title("When?")
        .get());

linePlot.setYAxis(
    Axis.with()
        .minValue(0)
        .maxValue(35)
        .interval(5)
        .title("How many thousands?")
        .get());

If you want to show coordinates for the values drawn inside a plot, you must also add XAxisRenderer and YAxisRenderer components to the dashboard. Each of them can optionally display Axis coordinates and Title, ticks and data labels. In order to display data labels from a series, you must also set the LabelsSource property to either a SeriesRenderer or a Plot:

Java  Copy Code

XAxisRenderer xRenderer = XAxisRenderer.with()
    .axis(linePlot.getXAxis())
    .labelsSource(linePlot)
    .showCoordinates(false)
    .labelFontSize(6).get();

YAxisRenderer yRenderer = new YAxisRenderer(linePlot.getYAxis());

In order to align axes to their plot, we'll add an intermediate GridPanel inside the top-left cell of the parent grid. We can do this on a couple of lines of code with the help of LayoutBuilder. That class lets us build layout fragments from several components and add them to a parent panel. In this case we'll call the createPlotWithBottomAndLeftAxes method to automatically create a GridPanel and add specified components to it. Other variants of this method can also automatically add the created panel to dashboard's LayoutPanel, but in this tutorial we'll add the result to our custom child panel.

Java  Copy Code

LayoutBuilder builder = new LayoutBuilder(dashboard);
var panel1 = builder.CreatePlotWithBottomAndLeftAxes(
    linePlot, xRenderer, yRenderer);

We want to display this fragment inside top-left cell of the GridPanel we created earlier. This can be done by setting the GridRow and GridColumn properties defined in base Component class, which specify respectively the row or column index of a cell inside parent grid panel. The index is zero-based, so we set both properties to zero:

Java  Copy Code

panel1.setGridColumn(0);
panel1.setGridRow(0);
grid.getChildren().add(panel1);

Following the same approach, we can display same data using a different kind of graphics and with different appearance attributes. Lets draw bars this time:

Java  Copy Code

BarRenderer barDataRenderer = new BarRenderer(
    new ObservableList<Series>(data1, data2, data3));

We'll specify a different fill color for each series of bars by providing a list of colors to PerSeriesStyle class.

Java  Copy Code

PerSeriesStyle barStyle = new PerSeriesStyle();
barStyle.setFills(
    new Color(102, 154, 204, 150),
    new Color(0, 52, 102, 150),
    new Color(206, 0, 0, 150));
barDataRenderer.setSeriesStyle(barStyle);

You could add the BarRenderer to the same plot that now draws line graphics to create a mixed chart, but lets create a new plot for drawing bars only:

Java  Copy Code

Plot2D barPlot = new Plot2D();

Lets show axes for this plot too. Note that we are assigning same range properties as for the linechart, but through a new Axis instance. If you reused the Axis instance, by calling barPlot.setXAxis(linePlot.getXAxis()), the plots would scroll together when the user pans any of them.

Java  Copy Code

barPlot.setXAxis(
    Axis.with()
        .minValue(-1.0)
        .maxValue(15.0)
        .interval(1.0)
        .title("When?").get());

barPlot.setYAxis(
    Axis.with()
        .minValue(0.0)
        .maxValue(35.0)
        .interval(5.0)
        .title("How many thousands?").get());

Add the bar renderer to its plot:

Java  Copy Code

barPlot.getSeriesRenderers().add(barDataRenderer);

Use LayoutBuilder again to align axes to plot and add them to a grid panel:

Java  Copy Code

GridPanel panel2 = builder.createPlotWithBottomAndRightAxes(
    barPlot,
    XAxisRenderer.with()
        .axis(barPlot.getXAxis())
        .labelsSource(linePlot)
        .showCoordinates(false)
        .labelFontSize(6.0).get(),
    new YAxisRenderer(barPlot.getYAxis()));

panel2.setGridColumn(1);
panel2.setGridRow(0);
grid.getChildren().add(panel2);

Now lets draw a radar chart using RadarRenderer:

Java  Copy Code

RadarRenderer radarDataRenderer = new RadarRenderer(
    new ObservableList<Series>(data1, data2, data3));

Set the AreaOpacity property to fill the radar polygons using a semi-transparent fill:

Java  Copy Code

radarDataRenderer.setAreaOpacity(0.2);

Set the stroke and fill colors:

Java  Copy Code

PerSeriesStyle radarStyle = new PerSeriesStyle();
radarStyle.setStrokes(lineStyle.getStrokes());
radarStyle.setFills(lineStyle.getFills());
radarStyle.setStrokeThicknesses(List.of(5.0));
radarDataRenderer.setSeriesStyle(radarStyle);

Create a RadarPlot and set its data range:

Java  Copy Code

RadarPlot radarPlot = new RadarPlot();
radarPlot.getDefaultAxis().init()
    .minValue(0)
    .maxValue(35)
    .interval(5);

Set axis appearance attributes:

Java  Copy Code

radarPlot.setAxisOptions(
    new RadarAxisOptions(radarPlot.getDefaultAxis()));
radarPlot.getAxisOptions().setShowSeriesLabels(true);
radarPlot.getAxisOptions().setAxisStroke(
    new SolidBrush(Colors.LightGray));
radarPlot.getDefaultAxis().setTitle("");

Add the radar renderer to its plot:

Java  Copy Code

radarPlot.getSeriesRenderers().add(radarDataRenderer);

Add the plot to lower-left cell of parent grid:

Java  Copy Code

radarPlot.setGridColumn(0);
radarPlot.setGridRow(1);
grid.getChildren().add(radarPlot);

We now have one last unused cell in our parent grid - lets add four more plots to it using third-level grid panel. In its first cell we'll show horizontal bars. In other ones we'll show 3D bars and also show how to set them up to scroll together. We'll again show the same data as in previous plots:

Java  Copy Code

BarRenderer horizontalBarDataRenderer = new BarRenderer(
    new ObservableList<Series>(data1, data2, data3));

Set their fills:

Java  Copy Code

PerSeriesStyle bar3dStyle = new PerSeriesStyle();
bar3dStyle.setFills(
    new Color(224, 233, 233, 150),
    new Color(102, 154, 204, 150),
    new Color(0, 52, 102, 150));
bar3dStyle.setStrokeThicknesses(List.of(1.0));
horizontalBarDataRenderer.setSeriesStyle(bar3dStyle);

Set the bars orientation:

Java  Copy Code

horizontalBarDataRenderer.setHorizontalBars(true);

Add the horizontal bar renderer to a plot:

Java  Copy Code

Plot2D plot4 = new Plot2D();
plot4.getSeriesRenderers().add(horizontalBarDataRenderer);

Set up plot's axes:

Java  Copy Code

plot4.setYAxis(
    Axis.with()
        .minValue(0)
        .maxValue(4)
        .interval(1)
        .title("Months").get());
plot4.setXAxis(
    Axis.with()
        .minValue(0)
        .maxValue(40)
        .interval(5)
        .title("Thousands").get());

Specify that this plot should scroll vertically:

Java  Copy Code

plot4.setVerticalScroll(true);

Create a container panel for the horizontal bars plot, while also initializing axis renderers and their appearance:

Java  Copy Code

GridPanel panel6 = builder.createPlotWithTopAndLeftAxes(
    plot4,
    XAxisRenderer.with()
        .axis(plot4.getXAxis())
        .axisStroke(new Color(206, 0, 0))
        .axisStrokeThickness(3)
        .titleBrush(new Color(90, 121, 165))
        .titleFontName("Courier New")
        .titleFontSize(15).get(),
    YAxisRenderer.with()
        .axis(plot4.getYAxis())
        .axisStroke(new Color(206, 0, 0))
        .axisStrokeThickness(3)
        .titleBrush(new Color(90, 121, 165))
        .titleFontName("Courier New")
        .titleFontSize(15.0).get());

panel6.setGridColumn(0);
panel6.setGridRow(0);

Now let's create some 3D bars:

Java  Copy Code

BarRenderer3D bar3DDataRenderer = new BarRenderer3D(
    new ObservableList<Series>(data1, data2, data3));
bar3DDataRenderer.setSeriesStyle(bar3dStyle);

Create a plot for 3D bars:

Java  Copy Code

Plot3D plot3d1 = new Plot3D();
plot3d1.getSeriesRenderers().add(bar3DDataRenderer);

Set up axes. We'll reuse these Axis instances later in order to implement synchronized scrolling in several plots:

Java  Copy Code

Axis xAxis3d = new Axis();
xAxis3d.init()
    .minValue(0)
    .maxValue(5)
    .interval(1)
    .title("Months");
plot3d1.setXAxis(xAxis3d);

Axis rightY = new Axis();
rightY.init()
    .minValue(0)
    .maxValue(40)
    .interval(5)
    .title("Thousands");
plot3d1.setYAxis(rightY);

Add last plot and axes to the layout:

Java  Copy Code

GridPanel panel3 = builder.createPlotWithTopAndRightAxes(
    plot3d1,
    XAxisRenderer.with()
        .axis(plot3d1.getXAxis())
        .showCoordinates(false)
        .titleBrush(new Color(90, 121, 165))
        .titleFontName("Verdana")
        .titleFontSize(10.0).get(),
    YAxisRenderer.with()
        .axis(plot3d1.getYAxis())
        .titleBrush(new Color(90, 121, 165))
        .titleFontName("Verdana")
        .titleFontSize(10).get());

Create a BarOverlayRenderer3D instance, which draws each series as a different row of bars:

Java  Copy Code

BarOverlayRenderer3D barOverlay3DRenderer = new BarOverlayRenderer3D(
    new ObservableList<Series>(data1, data2, data3));
barOverlay3DRenderer.setSeriesStyle(bar3dStyle);

Create a plot and reuse axes of previous one:

Java  Copy Code

Plot3D plot3d3 = new Plot3D();
plot3d3.getSeriesRenderers().add(barOverlay3DRenderer);
plot3d3.setXAxis(xAxis3d);
plot3d3.setYAxis(rightY);

Add this plot and new axis renderers to the layout:

Java  Copy Code

GridPanel panel5 = builder.createPlotWithTopAndRightAxes(
    plot3d3,
    XAxisRenderer.with()
        .axis(plot3d2.getXAxis())
        .titleBrush(new SolidBrush(Colors.Purple))
        .titleFontName("Courier New")
        .titleFontSize(12).get(),
    YAxisRenderer.with()
        .axis(plot3d2.getYAxis())
        .titleBrush(new SolidBrush(Colors.Purple))
        .titleFontName("Courier New")
        .titleFontSize(12).get());

And the last one will show data series as 3D bar stacks:

Java  Copy Code

BarStackRenderer3D barStack3DRenderer = new BarStackRenderer3D(
    new ObservableList<Series>(data1, data2, data3));
barStack3DRenderer.setSeriesStyle(bar3dStyle);

Plot3D plot3d2 = new Plot3D();
plot3d2.getSeriesRenderers().add(barStack3DRenderer);
plot3d2.setXAxis(xAxis3d);

Here we want a different Y axis range because the stacks will get higher than standalone bars from older plots:

Java  Copy Code

plot3d2.setYAxis(
    Axis.with()
        .minValue(0)
        .maxValue(100)
        .interval(5)
        .title("Thousands")
        .get());

Add a panel for the last plot and axes:

Java  Copy Code

GridPanel panel4 = builder.createPlotWithTopAndLeftAxes(
    plot3d2,
    XAxisRenderer.with()
        .axis(plot3d2.getXAxis())
        .titleBrush(Colors.Blue)
        .titleFontName("Times New Roman")
        .titleFontSize(15).get(),
    YAxisRenderer.with()
        .axis(plot3d2.getYAxis())
        .titleBrush(Colors.Blue)
        .titleFontName("Times New Roman")
        .titleFontSize(15)
        .titleFontStyle(EnumSet.of(FontStyle.BOLD)).get());

Set the grid positions for all child panels:

Java  Copy Code

panel3.setGridColumn(1);
panel3.setGridRow(0);
panel4.setGridColumn(0);
panel4.setGridRow(1);
panel5.setGridColumn(1);
panel5.setGridRow(1);

Finally, arrange them in a grid in the lower-right corner of parent grid:

Java  Copy Code

GridPanel miniGrid = new GridPanel();
miniGrid.getColumns().add(new GridColumn());
miniGrid.getRows().add(new GridRow());

miniGrid.getChildren().add(panel3);
miniGrid.getChildren().add(panel4);
miniGrid.getChildren().add(panel5);
miniGrid.getChildren().add(panel6);

grid.getChildren().add(miniGrid);

Now create a legend and the dashboard is complete. The legend will display the series titles we assigned earlier:

Java  Copy Code

LegendRenderer legend = new LegendRenderer();
legend.getContent().add(lineRenderer);
dashboard.getRootPanel().getChildren().add(legend);

If you run the application, you should now see this image: