DEV Community

Explorer
Explorer

Posted on • Edited on

🎨 Dynamic Dashboard Colors: Mapping Pie Chart Slices to Workflow Status in Joget

1. Overview

Dashboards are only effective when they communicate information clearly. When using Joget's Report Builder (which uses ECharts) to create a Status Pie Chart, the default colors are meaningless. We need Green for "Authorized," Red for "Rejected," and Yellow for "Pending."

This client-side JavaScript solution, placed in a Custom HTML element on your Userview page, allows you to hijack the chart after it loads and programmatically apply meaningful colors based on the data label (the workflow status).


2. How It Works

This script performs two main actions: Color Customization and Interactive Filtering.

Action 1: Applying Status Colors 🌈

  1. Chart Initialization: The script waits for the page to load, then targets the specific ECharts instance using its unique HTML ID (e.g., echarts_XYZ).
  2. Data Grab: It pulls the raw data (series[0].data) from the chart.
  3. Color Loop: It loops through every slice. For each slice, it checks the slice's name (the status, like "Authorized") and manually injects a custom itemStyle property with a specific Hex color.
  4. Redraw: Finally, it uses aChart.setOption(option) to force ECharts to redraw the chart with the new color mappings.

Action 2: Interactive Filtering πŸ–±οΈ

The second part of the script uses the ECharts built-in on('click', ...) event listener to turn each pie slice into a functional link, routing users to specific, pre-filtered Userview dashboards.


3. Full Code

Paste this code into a Custom HTML element (placed after the Report Grid element) in your Joget Userview.

Security Note: The unique ECharts ID (echarts_CHART_ID_HERE) and the Userview paths have been made generic. You must replace these placeholders with your actual values.

<script>
    $(document).ready(function () {
        // IMPORTANT: Replace 'echarts_CHART_ID_HERE' with the actual ID of your chart element.
        var aChart = echarts.init(document.getElementById('echarts_CHART_ID_HERE'));
        var option = aChart.getOption();

        var pieData = aChart.getOption().series[0].data;

        // --- DYNAMIC COLOR MAPPING LOGIC ---
        for (var i = 0; i < pieData.length; i++) {
            var status = pieData[i].name;

            // Set color based on status (Use Hex codes for consistency)
            if (status === 'Approval Pending') {
                option.series[0].data[i].itemStyle = { color: '#FFDD7D' }; // Yellow/Amber
            } else if (status === 'Authorized') {
                option.series[0].data[i].itemStyle = { color: '#9AC4A1' }; // Green
            } else if (status === 'Draft') {
                option.series[0].data[i].itemStyle = { color: '#EEA94E' }; // Orange
            } else if (status === 'Escalated') {
                option.series[0].data[i].itemStyle = { color: 'red' }; // Red Flag
            } else if (status === 'Rejected' || status === 'Request Rejected') {
                option.series[0].data[i].itemStyle = { color: '#DC143C' }; // Crimson (Strong Red)
            } else {
                // Default color for any unexpected or unmapped values
                option.series[0].data[i].itemStyle = { color: 'gray' };
            }
        }

        // Apply the new colors to the chart
        aChart.setOption(option);

        // --- INTERACTIVE CLICK LOGIC ---
        aChart.on('click', function (params) {

            // IMPORTANT: Replace the generic Userview links with your actual paths.

            if (params.name == 'Approval Pending') {
                window.open(`/jw/web/userview/app_name/v/_/pending_dashboard`, "_blank");
            } else if (params.name == 'Authorized') {
                window.open(`/jw/web/userview/app_name/v/_/authorized_dashboard`, "_blank");
            } else if (params.name == 'Rejected') {
                window.open(`/jw/web/userview/app_name/v/_/rejected_dashboard`, "_blank");
            } else if (params.name == 'Escalated') {
                window.open(`/jw/web/userview/app_name/v/_/escalated_dashboard`, "_blank");
            }
            // Generic fall-through: For all other statuses, open the main list filtered by the clicked status name
            else {
                // The hash variable used here (d-6985709-fn_Status) is a Report Grid filter hash.
                window.open(`/jw/web/userview/app_name/v/_/all_document_list?d-GRID_HASH-fn_Status=${params.name}`);
            }
        });
    });
</script>
Enter fullscreen mode Exit fullscreen mode


`

4. Example Use Cases

  • βœ… Compliance Dashboards: Instantly highlight records that are "Expired" (Red) or "Compliant" (Green).
  • βœ… Service Desk: Show tickets based on priority: "High" (Red), "Medium" (Orange), "Low" (Blue).
  • βœ… Financial Approvals: Use strong, clear colors to represent the stage of the approval process: "Submitted" (Yellow), "Finance Approved" (Light Green), "CEO Approved" (Dark Green).

5. Customization Tips

  • πŸ’‘ Using Lookup Objects: For cleaner code, consider replacing the long if/else if chain with a simple JavaScript lookup object (or map). This is easier to update when new statuses are added.
  • πŸ’‘ Userview Pathing: The click logic uses window.open(...) to direct users. Ensure you replace app_name and the specific Userview categories (_) with your application's actual URLs.
  • πŸ’‘ ECharts ID Safety: If you modify and save your Report Grid element, Joget sometimes changes the unique ECharts ID. If your colors disappear, this is the first place to check!

6. Key Benefits

  • πŸš€ Enhanced Clarity: Makes dashboards instantly readable by associating colors with conventional meanings (red=bad, green=good).
  • πŸš€ Improved UX: Turns a static visualization into an interactive filter, drastically reducing the clicks needed for users to drill down into data.
  • πŸš€ Maintainability: All coloring rules are contained in one simple, client-side script, separate from the core reporting configuration.

7. Security Note

πŸ”’ Userview Security: The click logic exposes Userview URLs. Ensure that the target dashboard pages (/pending_dashboard, /rejected_dashboard, etc.) are protected with appropriate Userview Permission settings so unauthorized users cannot access the underlying data lists directly.


8. Final Thoughts

By using client-side scripting to customize the ECharts object, you transform a generic Joget dashboard into a highly intuitive and interactive data visualization tool. This simple technique is fundamental for professional-grade reporting in Joget.
`

Top comments (0)