Search
Serialization

MindFusion.Dashboards provides complete JSON serialization out-of-the-box. The entire dashboard state including layout structure, grid positions, widget dimensions, data source bindings, visual settings, and themes can be serialized into a lightweight JSON string and restored seamlessly.

Saving Dashboard State

To serialize a dashboard, call the toJson method on either DashboardView or the underlying Dashboard instance:

JavaScript  Copy Code

// Serialize the entire dashboard layout and widget hierarchy
var dashboardJson = dashView.toJson();

// Save to browser localStorage or send via HTTP POST to a backend server
localStorage.setItem("user_dashboard_layout", JSON.stringify(dashboardJson));

What gets serialized in toJson:

  • Dashboard metadata: Layout type (Grid or Diagram), theme name, grid columns, rows, and gap settings.
  • Data Sources: Registered data source definitions and configurations.
  • Widgets: Widget type identifiers, column and row positions, columnSpan and rowSpan.
  • Widget Configurations:
    - KPI: Header, footer, value format, trend settings.
    - Chart: ChartType, series array, showLegend, doughnut, lineType.
    - Gauge: GaugeType, minValue, maxValue, targetValue, margins.
    - Grid: Columns, dataPath, sorting.

Restoring Dashboard State

To restore a saved dashboard state, pass the JSON object or string to the fromJson method:

JavaScript  Copy Code

// Retrieve the saved layout JSON string
var savedData = localStorage.getItem("user_dashboard_layout");

if (savedData)
{
    var jsonObj = JSON.parse(savedData);

    // Reconstruct the dashboard and all its widgets
    dashView.fromJson(jsonObj);
}

Custom Widget Serialization

When creating custom widget classes deriving from DashboardWidget, override the toJson and fromJson methods to persist custom properties:

JavaScript  Copy Code

export class CustomMetricWidget extends MindFusion.Dashboards.DashboardWidget
{
    constructor()
    {
        super();
        this.alertThreshold = 100;
    }

    toJson()
    {
        var json = super.toJson();
        json.alertThreshold = this.alertThreshold;
        return json;
    }

    fromJson(json)
    {
        super.fromJson(json);
        if (json.alertThreshold !== undefined)
        {
            this.alertThreshold = json.alertThreshold;
        }
    }
}

Complete Save & Load Workflow Example

JavaScript  Copy Code

// Export dashboard layout
function saveDashboardToBackend()
{
    var state = dashView.toJson();
    fetch("/api/dashboards/save", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(state)
    });
}

// Load dashboard layout
function loadDashboardFromBackend()
{
    fetch("/api/dashboards/load")
        .then(res => res.json())
        .then(data => {
            dashView.fromJson(data);
        });
}