DEV Community

Ananya Deka
Ananya Deka

Posted on

Integrating WebDataRocks and CanvasJS for Interactive Data Visualization

Introduction

In modern web applications, data analysis and visualization play a crucial role in delivering insights. While pivot tables allow users to explore and manipulate data interactively, charts make it easier to understand trends and patterns at a glance. WebDataRocks, a JavaScript pivot table library, and CanvasJS, a versatile charting library, can be seamlessly integrated to combine the best of data exploration and visualization.

Image description

About the Libraries

WebDataRocks

WebDataRocks is a lightweight JavaScript library designed to simplify data analysis. It enables developers to create powerful pivot tables in web applications with ease.

Key features include:

  • A drag-and-drop interface for slicing and dicing data.
  • Support for JSON and CSV data formats.
  • Real-time filtering, sorting, and aggregation.
  • Cross-browser compatibility and a responsive design.

WebDataRocks empowers users to analyze data interactively without requiring advanced coding skills.

CanvasJS

CanvasJS is a robust charting library that offers:

  • A wide variety of chart types (bar, column, pie, etc.).
  • Interactive and responsiveness.
  • Lightweight performance optimized for web applications.
  • Customization options for styling charts to match application themes.

CanvasJS helps visualize complex datasets in a way that is both visually appealing and easy to understand.

Integration with Code Examples

Step 1: HTML Structure

Create placeholders for the pivot table and chart.

<div id="pivot-table" style="width: 100%; height: 400px;"></div>
<div id="chart-container" style="height: 400px; width: 100%;"></div>
Enter fullscreen mode Exit fullscreen mode

Step 2: Include Required Libraries

Include WebDataRocks and CanvasJS using their CDN links.

<link rel="stylesheet" href="https://cdn.webdatarocks.com/latest/webdatarocks.css">
<script src="https://cdn.webdatarocks.com/latest/webdatarocks.js"></script>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
Enter fullscreen mode Exit fullscreen mode

Step 3: Initialize WebDataRocks

Set up a pivot table to summarize your data.

var pivot = new WebDataRocks({
  container: "#pivot-table",
  toolbar: true,
  report: {
    dataSource: {
      data: [
        { Category: "Fruits", Sales: 100, Month: "January" },
        { Category: "Vegetables", Sales: 200, Month: "January" },
        { Category: "Fruits", Sales: 150, Month: "February" },
        { Category: "Vegetables", Sales: 250, Month: "February" },
      ],
    },
    slice: {
      rows: [{ uniqueName: "Category" }],
      columns: [{ uniqueName: "Measures" }],
      measures: [{ uniqueName: "Sales", aggregation: "sum" }],
    },
  },
  reportcomplete: function () {
    pivot.off("reportcomplete")
    updateChart()
  },
});
Enter fullscreen mode Exit fullscreen mode

Step 4: Fetch Summarized Data and Render Chart

Fetch pivot table data and render it as a chart using CanvasJS.

var chartData = [];
var chart = new CanvasJS.Chart("chart-container", {
  animationEnabled: true,
  theme: "light2",
  title: {
    text: "Sales by Category"
  },
  data: [
    {
      type: "column",
      dataPoints: chartData
    },
  ],
});

function updateChart() {
  pivot.getData({}, renderChart);
}

function renderChart(data) {
  if (data.data) {
    data.data.forEach((row) => {
      if (row.r0 && row.v0) {
        chartData.push({
          label: row.r0,
          y: row.v0
        })
      }
    })
  }
  chart.render();
}

// Update the chart dynamically on data changes
pivot.on("datachanged", updateChart);
Enter fullscreen mode Exit fullscreen mode

Take a look at this JSFiddle for the complete working example.

Conclusion

The integration of WebDataRocks and CanvasJS provides a comprehensive solution for building interactive, data-driven dashboards. WebDataRocks enables users to analyze and manipulate data in real-time, while CanvasJS transforms the data into beautiful, responsive charts. This combination empowers developers to deliver seamless analytical experiences that are both functional and visually engaging.

By following the steps above, you can create dashboards that meet modern data analysis and visualization demands, improving decision-making and enhancing user experience.

Top comments (0)