Create a Vue Dashboard Application
- 6 minutes to read
Important
If you are not familiar with the basic concepts and patterns of Vue, please review the fundamentals before you continue: vuejs.org.
The Web Dashboard consists of client and server parts:
- Client
- The client is a JavaScript application that supplies users with a UI to design and interact with a dashboard. The DashboardControl is the underlying control. The client communicates with the server using HTTP requests.
- Server
- The server is an ASP.NET Core or ASP.NET MVC application that handles client data requests and provides access to data sources, dashboard storage and other backend capabilities.
The tutorial creates and configures a client Vue application that contains the Web Dashboard and a server ASP.NET Core application that targets .NET 6.
Prerequisites
Requirements
- The script version on the client should match the library version on the server.
- Versions of the DevExpress npm packages should be identical.
Step 1. Create a Client Application
In the command prompt, create a Vue application with a default preset:
vue create dashboard-vue-app
Navigate to the created folder after the project is created:
cd dashboard-vue-app
Install the following npm packages:
npm install devexpress-dashboard@24.1.7 devexpress-dashboard-vue@24.1.7 @devexpress/analytics-core@24.1.7 devextreme@24.1.7 --save devextreme-vue@24.1.7 --save
You can find all the libraries in the node_modules folder after the installation is completed.
Open the src/App.vue file replace its content with the following markup to display a dashboard component on the page.
<template>
<div>
<DxDashboardControl
style="height:900px; display: 'block'; width: '100%';"
endpoint="https://demos.devexpress.com/services/dashboard/api"/>
</div>
</template>
<script>
import { DxDashboardControl } from 'devexpress-dashboard-vue';
export default {
components: {
DxDashboardControl
}
}
</script>
Open the main.js file and add the following global styles:
import { createApp } from 'vue'
import App from './App.vue'
import "ace-builds/css/ace.css";
import "ace-builds/css/theme/dreamweaver.css";
import "ace-builds/css/theme/ambiance.css";
import 'devextreme/dist/css/dx.light.css';
import "@devexpress/analytics-core/dist/css/dx-analytics.common.css";
import "@devexpress/analytics-core/dist/css/dx-analytics.light.css";
import "@devexpress/analytics-core/dist/css/dx-querybuilder.css";
import "devexpress-dashboard/dist/css/dx-dashboard.light.css";
createApp(App).mount('#app')
Use the command below to launch the application.
npm run serve
Open http://localhost:8080/
in your browser to see the result. The Web Dashboard displays the dashboard stored on the preconfigured server (https://demos.devexpress.com/services/dashboard/api
).
Tip
Related Article: Dashboard Component for Vue
Step 2. Create a Server Application
Create the Application
Create a server application to show your data. In Visual Studio, create an ASP.NET Core Empty application with .NET 6 as the target framework. Name it asp-net-core-server.
Install NuGet Packages
In the project, open the NuGet Package Manager and set the package source to All. Install the following NuGet package:
DevExpress.AspNetCore.Dashboard
Configure the Dashboard Controller
In the root directory, create an empty MVC Controller named DefaultDashboardController. Inherit the DashboardController:
using DevExpress.DashboardAspNetCore;
using DevExpress.DashboardWeb;
using Microsoft.AspNetCore.DataProtection;
namespace AspNetCoreDashboardBackend {
public class DefaultDashboardController : DashboardController {
public DefaultDashboardController(DashboardConfigurator configurator, IDataProtectionProvider? dataProtectionProvider)
: base(configurator, dataProtectionProvider) {
}
}
}
Configure the Server
In your application, create the Data folder and add your data files to the folder. In this example, data is obtained from a JSON file. You can find sample data in the following repository:
Create the Data/Dashboards folder that will store dashboards.
Replace the content of the Program.cs file with the code below to configure the following parameters:
- Configure CORS policy.
- Add and use services for DevExpress Controls.
- Map the dashboard route.
- Add a JSON Data Source to a data source storage.
using DevExpress.DashboardAspNetCore;
using DevExpress.DashboardWeb.Native;
using DevExpress.DashboardWeb;
using Microsoft.Extensions.FileProviders;
using DevExpress.AspNetCore;
using DevExpress.DashboardCommon;
using DevExpress.DataAccess.Json;
var builder = WebApplication.CreateBuilder(args);
IFileProvider? fileProvider = builder.Environment.ContentRootFileProvider;
IConfiguration? configuration = builder.Configuration;
// Configures CORS policies.
builder.Services.AddCors(options => {
options.AddPolicy("CorsPolicy", builder => {
builder.AllowAnyOrigin();
builder.AllowAnyMethod();
builder.WithHeaders("Content-Type");
});
});
// Adds the DevExpress middleware.
builder.Services.AddDevExpressControls();
// Adds controllers.
builder.Services.AddControllersWithViews();
// Configures the dashboard backend.
builder.Services.AddScoped<DashboardConfigurator>((IServiceProvider serviceProvider) => {
DashboardConfigurator configurator = new DashboardConfigurator();
configurator.SetDashboardStorage(new DashboardFileStorage(fileProvider.GetFileInfo("Data/Dashboards").PhysicalPath));
configurator.SetDataSourceStorage(CreateDataSourceStorage());
configurator.SetConnectionStringsProvider(new DashboardConnectionStringsProvider(configuration));
configurator.ConfigureDataConnection += Configurator_ConfigureDataConnection;
return configurator;
});
void Configurator_ConfigureDataConnection(object sender, ConfigureDataConnectionWebEventArgs e) {
if (e.ConnectionName == "jsonSupport") {
Uri fileUri = new Uri(fileProvider.GetFileInfo("Data/support.json").PhysicalPath, UriKind.RelativeOrAbsolute);
JsonSourceConnectionParameters jsonParams = new JsonSourceConnectionParameters();
jsonParams.JsonSource = new UriJsonSource(fileUri);
e.ConnectionParameters = jsonParams;
}
if (e.ConnectionName == "jsonCategories") {
Uri fileUri = new Uri(fileProvider.GetFileInfo("Data/categories.json").PhysicalPath, UriKind.RelativeOrAbsolute);
JsonSourceConnectionParameters jsonParams = new JsonSourceConnectionParameters();
jsonParams.JsonSource = new UriJsonSource(fileUri);
e.ConnectionParameters = jsonParams;
}
}
DataSourceInMemoryStorage CreateDataSourceStorage() {
DataSourceInMemoryStorage dataSourceStorage = new DataSourceInMemoryStorage();
DashboardJsonDataSource jsonDataSourceSupport = new DashboardJsonDataSource("Support");
jsonDataSourceSupport.ConnectionName = "jsonSupport";
jsonDataSourceSupport.RootElement = "Employee";
dataSourceStorage.RegisterDataSource("jsonDataSourceSupport", jsonDataSourceSupport.SaveToXml());
DashboardJsonDataSource jsonDataSourceCategories = new DashboardJsonDataSource("Categories");
jsonDataSourceCategories.ConnectionName = "jsonCategories";
jsonDataSourceCategories.RootElement = "Products";
dataSourceStorage.RegisterDataSource("jsonDataSourceCategories", jsonDataSourceCategories.SaveToXml());
return dataSourceStorage;
}
var app = builder.Build();
// Registers the DevExpress middleware.
app.UseDevExpressControls();
// Registers routing.
app.UseRouting();
// Registers CORS policies.
app.UseCors("CorsPolicy");
// Maps the dashboard route.
app.MapDashboardRoute("api/dashboard", "DefaultDashboard");
// Requires CORS policies.
app.MapControllers().RequireCors("CorsPolicy");
app.Run();
In the Properties folder, open launchSettings.json. Modify the applicationUrl
setting as shown below to use port 5001 for HTTPS and port 5000 for HTTP:
//...
"profiles": {
"asp_net_core_server": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
}
Start the Server
Open the asp-net-core-server folder and run the following command:
dotnet run
To use this server in the client application, go to the src/App.vue file. Set the following URL as an endpoint: http://localhost:5000/api/dashboard
<template>
<div>
<DxDashboardControl
style="height:900px; display: 'block'; width: '100%';"
endpoint="http://localhost:5000/api/dashboard"/>
</div>
</template>
<script>
import { DxDashboardControl } from 'devexpress-dashboard-vue';
export default {
components: {
DxDashboardControl
}
}
</script>
Use the command below to launch the application.
npm run serve
Open http://localhost:8080/
in your browser to see the result. The client Web Dashboard app uses data from the newly created server (http://localhost:5000/api/dashboard
).
Step 3. Switch to Viewer Mode
In Designer
mode, the control loads the extensions required to design dashboards. Users can access the Data Source Wizard, preview underlying data, and modify dashboards from storage. Requests from the dashboard backend server can be sent to third-party resources. A user can also switch the control to Viewer
mode.
After you created and saved a dashboard, switch your Dashboard Designer application to ViewerOnly mode to prevent users from modifying the dashboard and its data.
Open the App.vue file and set the workingMode property to ViewerOnly:
<template>
<div>
<DxDashboardControl
style="height:900px; display: 'block'; width: '100%';"
endpoint="http://localhost:5000/api/dashboard"
workingMode="ViewerOnly"/>
</div>
</template>
Refer to the following topic for more information on how to switch the component’s properties: Property Binding.
Warning
Working mode does not influence server settings. Initially, the server works at the ClientTrustLevel.Full trust level. Verify trust level and specify the actions a client can initiate on the server. See the following topic for details: Working Mode Access Rights.
Next Steps
- Create Dashboards on the Web
- Describes how to create and configure dashboards in the Web Dashboard control.
- Dashboard Component for Vue
- Contains instructions on how to use the Dashboard Component for Vue.