How to use FastCube .NET in React application

2021-10-01

UPD: Applies to the versions of FastCube .NET before 2022.1. License packages are now available on our NuGet server.

The ReactJs library has become widespread in the web development of single-page applications. Previously we have covered how to display reports and the online report designer in React SPA application. Now it is possible to display cubes and slices of data from FastCube.Core on a web page. Let's consider how to do this.

To create an ASP .NET Core app with a React frontend part, you can use a template in the .NET SDK. Run in the command line:

dotnet new react -o MyReactApp

This command will create a demo application that we can use to display the cube. Of course, for this, you must have the .NET Core SDK installed. In addition, the application will require Node.js.

Go to the directory of the created application:

cd MyReactApp

and install javascript packages using the command:

npm install

Let's start working with the created web application. First, let’s install the FastCube packages. Open the Nuget package manager. In the upper right corner of the window, you will see a gear icon, which opens the settings of package sources. Click on it and add a new package source - a folder with our FastCube packages, which are located in the C:\Program Files (x86)\FastReports\FastCube.Net Professional\Nuget folder.

Configuring a local package source

Select the added package source in the drop-down list and install the packages:

Installing FastCube Core packages

We connect FastCube in the Startup.cs file, add the code in the Configure () method:

app.UseFastCube();

Our application already contains WeatherForecastController. Let's add our web method to it:

[HttpGet("[action]")]
 public IActionResult ShowCube()
 {
 Cube cube = new Cube();
 Slice slice = new Slice()
 {
 Cube = cube
 };
 FilterManager filterManager = new FilterManager()
 {
 Cube = cube
 };
 WebGrid grid;
 grid = new WebSliceGrid()
 {
 Slice = slice
 };
 
 ViewBag.WebGrid = grid;
 
 cube.SourceType = SourceType.File;
 cube.Load(Path.Combine("C:\\Users\\FR\\Downloads\\fastcube-net-master\\Demos\\Data\\", "Cubes", "calculated_measures.mdc")); return View(); }

The Cube and Slice objects are related because, in fact, the slice is part of the cube. A WebGrid object is used to display an interactive crosstab. It can display both a WebCubeGrid slice and a WebSliceGrid cube. In our example, we loaded the cube that we previously created in the FastCube .NET desktop version.

Pay attention to the class from which the controller inherits. It should be Controller, not BaseController.

Now let's create a view for this method. This can be done by right-clicking on the ShowCube method signature. The view will contain a single line of the code:

@await ViewBag.WebGrid.Render()

Now let's move on to SPA application, which is located in the ClientApp folder. We need to add our component to the src->components folder. It will display the iframe with the view we created above. Add the Cube.js file with the following code:

import React, { Component } from 'react';
export class Cube extends Component {
 static getCube() { 
 return { __html: '<iframe width="1000" height="1000" src="weatherforecast/ShowCube" />' };
 }
 render() {
 return (
 < div dangerouslySetInnerHTML={Cube.getCube()} />
 );
 } 
}

Everything is easy here. We display iframe that refers to a method in the controller.

Now you need to add the component to the App.js application structure:

import React, { Component } from 'react';
import { Route } from 'react-router';
import { Layout } from './components/Layout';
import { Home } from './components/Home';
import { FetchData } from './components/FetchData';
import { Counter } from './components/Counter';
import { Cube } from './components/Cube';
 
import './custom.css'
 
export default class App extends Component {
 static displayName = App.name;
 
 render () {
 return (
 <Layout>
 <Route exact path='/' component={Home} />
 <Route path='/counter' component={Counter} />
 <Route path='/fetch-data' component={FetchData} />
 <Route path='/cube' component={Cube} />
 </Layout>
 );
 }
}

In addition, you need to add a new menu item in the NavMenu.js file:

import React, { Component } from 'react';
import { Collapse, Container, Navbar, NavbarBrand, NavbarToggler, NavItem, NavLink } from 'reactstrap';
import { Link } from 'react-router-dom';
import './NavMenu.css';
 
export class NavMenu extends Component {
 static displayName = NavMenu.name;
 
 constructor (props) {
 super(props);
 
 this.toggleNavbar = this.toggleNavbar.bind(this);
 this.state = {
 collapsed: true
 };
 }
 
 toggleNavbar () {
 this.setState({
 collapsed: !this.state.collapsed
 });
 }
 
 render () {
 return (
 <header>
 <Navbar className="navbar-expand-sm navbar-toggleable-sm ng-white border-bottom box-shadow mb-3" light>
 <Container>
 <NavbarBrand tag={Link} to="/">FastCubeReact</NavbarBrand>
 <NavbarToggler onClick={this.toggleNavbar} className="mr-2" />
 <Collapse className="d-sm-inline-flex flex-sm-row-reverse" isOpen={!this.state.collapsed} navbar>
 <ul className="navbar-nav flex-grow">
 <NavItem>
 <NavLink tag={Link} className="text-dark" to="/">Home</NavLink>
 </NavItem>
 <NavItem>
 <NavLink tag={Link} className="text-dark" to="/counter">Counter</NavLink>
 </NavItem>
 <NavItem>
 <NavLink tag={Link} className="text-dark" to="/fetch-data">Fetch data</NavLink>
 </NavItem>
 <NavItem>
 <NavLink tag={Link} className="text-dark" to="/cube">Cube</NavLink>
 </NavItem>
 </ul>
 </Collapse>
 </Container>
 </Navbar>
 </header>
 );
 }
}
 

The application is ready. Let's run it:

The web application displays the cube

Thus, you can display your web cube using FastCube.NET in a React SPA application.

October 04, 2023

How to generate a report from an ASP.NET Core application using FastReport.Core.Skia

We tell you how to generate a report on Windows and Linux using FastReport.Core.Skia and a private NuGet server.
March 22, 2023

Creating PDF report in JetBrains Rider (C#) on Ubuntu 22.04.1 LTS

In this article, we'll take a look at the world of the .NET platform on Ubuntu 22.04.1 LTS and create a PDF exportable report.
February 14, 2023

How to set up an Apache2 web server for FastReport .NET

We are launching the Apache2 web server on the Linux operating system for FastReport.NET and .NET 5 with a few simple commands.
Fast Reports
  • 800-985-8986 (English, US)
  • +4930568373928 (German)
  • +55 19 98147-8148 (Portuguese)
  • info@fast-report.com
  • 901 N Pitt Str #325 Alexandria VA 22314

© 1998-2024 Fast Reports Inc.