Integration with Google Charts
Read this tutorial to learn how to integrate the WebDataRocks reporting tool with Google Charts.
Supported chart types
WebDataRocks supports the following chart types:
If you need a chart that is not on the list, you can implement a custom logic of data processing in the options.prepareDataFunction
parameter of googlecharts.getData().
To start a new Vue data visualization project with WebDataRocks Pivot Table and Google Charts, follow the steps below.
Step 1. Add WebDataRocks to your project
Step 1.1. Complete the integration guide. Your code of the component with WebDatarocks should look similar to the following:
<script setup>
import { Pivot } from "@webdatarocks/vue-webdatarocks";
import "@webdatarocks/webdatarocks/webdatarocks.css";
</script>
<template>
<div>
<Pivot
toolbar
/>
</div>
</template>
Step 1.2. Create a report for WebDataRocks — connect to the data source and define which fields should be displayed in rows, columns, and measures:
<script setup>
import { Pivot } from "@webdatarocks/vue-webdatarocks";
import "@webdatarocks/webdatarocks/webdatarocks.css";
const report = {
dataSource: {
filename: "https://cdn.webdatarocks.com/data/data.csv",
},
slice: {
rows: [
{
uniqueName: "Country",
},
],
columns: [
{
uniqueName: "Measures",
},
],
measures: [
{
uniqueName: "Price",
aggregation: "sum",
},
],
},
};
</script>
<template>
<div>
<Pivot
toolbar
v-bind:report="report"
/>
</div>
</template>
The fields you’ve specified in the report will be shown on the chart.
Step 2. Get a reference to the WebDataRocks instance
Some of WebDataRocks methods and events are needed to create a chart. Using a reference to the WebDataRocks instance, we can access WebDataRocks API.
Get the reference as follows:
<script setup>
// ...
import { ref } from "vue";
const pivotRef = ref(null);
// ...
</script>
<template>
<div>
<Pivot
ref="pivotRef"
toolbar
v-bind:report="report"
/>
</template>
Now it’s possible to interact with the component through pivotRef.value.webdatarocks
.
Step 3. Add Google Charts
Step 3.1. Download the Google Charts wrapper for Vue:
npm install vue-google-charts
Step 3.2. Import Google Charts for Vue 2:
import { GChart } from "vue-google-charts/legacy";
Step 3.3. Now you can add a chart to your component. Let’s create a column chart:
<template>
<div>
<Pivot
ref="pivotRef"
toolbar
v-bind:report="report"
/>
<GChart
type="ColumnChart"
/>
</div>
</template>
If you run the project now, the chart won’t show any data because WebDataRocks is not integrated with Google Charts yet. We will connect them in the next step.
Step 4. Show the data from the pivot table on the chart
Step 4.1. Create a reactive variable to store data for the chart (e.g., chartData
):
const chartData = ref([]);
The chartData
variable is now empty, but soon we’ll fill it with data from our component.
Step 4.2. Import the WebDataRocks Connector for Google Charts:
import "@webdatarocks/webdatarocks/webdatarocks.googlecharts.js";
The Connector provides the googlecharts.getData() method, which gets data from WebDataRocks and converts it to the format required for a specific chart type.
Step 4.3. If we call the googlecharts.getData() method before WebDataRocks is fully loaded, it will return an empty result. To know when WebDataRocks is ready to provide data for the chart, handle the reportcomplete event:
<script setup>
// ...
function onReportComplete() {
// Unsubscribing from reportcomplete
// We need it only to track the initialization of WebDataRocks
pivotRef.value.webdatarocks.off("reportComplete");
createChart();
}
</script>
<template>
<div>
<Pivot
ref="pivotRef"
toolbar
v-bind:report="report"
v-bind:reportcomplete="onReportComplete"
/>
<GChart
type="ColumnChart"
/>
</div>
</template>
Now the chart will be created only when the data is loaded and the report is ready.
Step 4.4. Implement the createChart()
function. It will use the googlecharts.getData() method from the Connector:
function createChart() {
pivotRef.value.webdatarocks.googlecharts.getData(
{
type: "column",
},
// Function called when data for the chart is ready
(chartConfig) => chartData.value = chartConfig.data,
// Function called on report changes (filtering, sorting, etc.)
(chartConfig) => chartData.value = chartConfig.data
);
}
Notice the type
configuration — it must correspond to the chart type specified in step 3.3 (in our case, it is "column"
).
Step 4.5. Finally, pass the data to the chart:
<GChart
type="ColumnChart"
v-bind:data="chartData"
/>
Since the chartData
is reactive and is assigned using the v-bind directive, the chart will be re-rendered each time the chartData
’s value is updated. As a result, all your changes to the WebDataRocks report will be reflected on the chart.
Step 5. Run the project
Run your project with the following command:
npm run dev
Open http://localhost:5173/
in the browser to see how the pivot table looks in combination with Google Charts.
Now try experimenting: filter the data, change the measures and the aggregation functions, and see how the results are reflected on the chart.
To learn more about configurations available for Google Charts, please refer to the Google Charts documentation.
Check out the full code
After completing this tutorial, the full code of the component should look as follows:
<script setup>
import { Pivot } from "@webdatarocks/vue-webdatarocks";
import "@webdatarocks/webdatarocks/webdatarocks.css";
import { ref } from "vue";
import { GChart } from "vue-google-charts/legacy";
import "@webdatarocks/webdatarocks/webdatarocks.googlecharts.js";
let pivotRef = ref(null);
const chartData = ref([]);
const report = {
dataSource: {
filename: "https://cdn.webdatarocks.com/data/data.csv",
},
slice: {
rows: [
{
uniqueName: "Country",
},
],
columns: [
{
uniqueName: "Measures",
},
],
measures: [
{
uniqueName: "Price",
aggregation: "sum",
},
],
},
};
function onReportComplete() {
// Unsubscribing from reportcomplete
// We need it only to track the initialization of WebDataRocks
pivotRef.value.webdatarocks.off("reportComplete");
createChart();
}
function createChart() {
pivotRef.value.webdatarocks.googlecharts.getData(
{
type: "column",
},
// Function called when data for the chart is ready
(chartConfig) => chartData.value = chartConfig.data,
// Function called on report changes (filtering, sorting, etc.)
(chartConfig) => chartData.value = chartConfig.data
);
}
</script>
<template>
<div>
<Pivot
ref="pivotRef"
toolbar
v-bind:report="report"
v-bind:reportcomplete="onReportComplete"
/>
<GChart
type="ColumnChart"
v-bind:data="chartData"
/>
</div>
</template>
See also
Read this tutorial to learn how to integrate the WebDataRocks reporting tool with Google Charts.
Supported chart types
WebDataRocks supports the following chart types:
If you need a chart that is not on the list, you can implement a custom logic of data processing in the options.prepareDataFunction
parameter of googlecharts.getData().
To start a new React data visualization project with WebDataRocks Pivot Table and Google Charts, follow the steps below.
Step 1. Add WebDataRocks to your project
Step 1.1. Complete the integration guide. Your code of the component with WebDatarocks should look similar to the following:
import * as WebDataRocksReact from "@webdatarocks/react-webdatarocks";
function App() {
return (
<div>
<WebDataRocksReact.Pivot
toolbar={true}
/>
</div>
);
}
export default App;
Step 1.2. Create a report for WebDataRocks — connect to the data source and define which fields should be displayed in rows, columns, and measures:
function App() {
const report = {
dataSource: {
filename: "https://cdn.webdatarocks.com/data/data.csv",
},
slice: {
rows: [
{
uniqueName: "Country",
},
],
columns: [
{
uniqueName: "Measures",
},
],
measures: [
{
uniqueName: "Price",
aggregation: "sum",
},
],
},
};
return (
<div>
<WebDataRocksReact.Pivot
toolbar={true}
report={report}
/>
</div>
);
}
The fields you’ve specified in the report will be shown on the chart.
Step 2. Get a reference to the WebDataRocks instance
Some of WebDataRocks methods and events are needed to create a chart. Using a reference to the WebDataRocks instance, we can access WebDataRocks API.
Get the reference with the useRef hook:
React + ES6
// ...
import { useRef } from "react";
function App() {
const pivotRef = useRef(null);
// ...
return (
<div>
<WebDataRocksReact.Pivot
ref={pivotRef}
toolbar={true}
report={report}
/>
</div>
);
}
export default App;
React + TypeScript
// ...
import { RefObject, useRef} from "react";
function App() {
const pivotRef: RefObject<WebDataRocksReact.Pivot | null> = useRef<WebDataRocksReact.Pivot>(null);
// ...
return (
<div>
<WebDataRocksReact.Pivot
ref={pivotRef}
toolbar={true}
report={report}
/>
</div>
);
}
export default App;
Now it’s possible to interact with the component through pivotRef.current.webdatarocks
.
Step 3. Add Google Charts
Step 3.1. Download the Google Charts wrapper for React:
npm install react-google-charts
Step 3.2. Import Google Charts into your component:
import { Chart } from "react-google-charts";
Step 3.3. Now you can add a chart to your component. Let’s create a column chart:
return (
<div>
<WebDataRocksReact.Pivot
ref={pivotRef}
toolbar={true}
report={report}
/>
<Chart
chartType="ColumnChart"
/>
</div>
);
If you run the project now, the chart won’t show any data because WebDataRocks is not integrated with Google Charts yet. We will connect them in the next step.
Step 4. Show the data from the pivot table on the chart
Step 4.1. Create a state variable to store data for the chart (e.g., chartData
):
import { useState, /* Other imports */ } from "react";
function App() {
// ...
const [chartData, setChartData] = useState([]);
// ...
}
The chartData
variable is now empty, but soon we’ll fill it with data from our component.
Step 4.2. Import the WebDataRocks Connector for Google Charts:
import "@webdatarocks/webdatarocks/webdatarocks.googlecharts.js";
The Connector provides the googlecharts.getData() method, which gets data from WebDataRocks and converts it to the format required for a specific chart type.
Step 4.3. If we call the googlecharts.getData() method before WebDataRocks is fully loaded, it will return an empty result. To know when WebDataRocks is ready to provide data for the chart, handle the reportcomplete event:
React + ES6
const onReportComplete = () => {
// Unsubscribing from reportcomplete
// We need it only to track the initialization of WebDataRocks
pivotRef.current.webdatarocks.off("reportComplete");
createChart();
};
return (
<div>
<WebDataRocksReact.Pivot
ref={pivotRef}
toolbar={true}
report={report}
reportcomplete={onReportComplete}
/>
<Chart
chartType="ColumnChart"
/>
</div>
);
React + TypeScript
const onReportComplete = () => {
// Unsubscribing from reportcomplete
// We need it only to track the initialization of WebDataRocks
pivotRef.current?.webdatarocks.off("reportComplete");
createChart();
};
return (
<div>
<WebDataRocksReact.Pivot
ref={pivotRef}
toolbar={true}
report={report}
reportcomplete={onReportComplete}
/>
<Chart
chartType="ColumnChart"
/>
</div>
);
Now the chart will be created only when the data is loaded and the report is ready.
Step 4.4. Implement the createChart()
function. It will use the googlecharts.getData() method from the Connector:
React + ES6
const createChart = () => {
pivotRef.current.webdatarocks.googlecharts.getData(
{
type: "column",
},
// Function called when data for the chart is ready
(chartConfig) => setChartData(chartConfig.data),
// Function called on report changes (filtering, sorting, etc.)
(chartConfig) => setChartData(chartConfig.data),
);
};
React + TypeScript
const createChart = () => {
pivotRef.current?.webdatarocks.googlecharts.getData(
{
type: "column",
},
// Function called when data for the chart is ready
(chartConfig: any) => setChartData(chartConfig.data),
// Function called on report changes (filtering, sorting, etc.)
(chartConfig: any) => setChartData(chartConfig.data),
);
};
Notice the type
configuration — it must correspond to the chartType
specified in step 3.3 (in our case, the type
value is "column"
).
As you can see, here we are using the chartData
’s setter setChartData()
. Each time the chartData
’s value is updated, the chart will be re-rendered. As a result, all your changes to the WebDataRocks report will be reflected on the chart.
Step 4.5. Finally, pass the data to the chart:
<Chart
chartType="ColumnChart"
data={chartData}
/>
Step 5. Run the project
Run your project with the following command:
npm run dev
Open http://localhost:5173/
in the browser to see how the pivot table looks in combination with Google Charts.
Now try experimenting: filter the data, change the measures and the aggregation functions, and see how the results are reflected on the chart.
To learn more about configurations available for Google Charts, please refer to the Google Charts documentation.
Check out the full code
After completing this tutorial, the full code of the component should look as follows:
React + ES6
import * as WebDataRocksReact from "@webdatarocks/react-webdatarocks";
import { useRef, useState } from "react";
import { Chart } from "react-google-charts";
import "@webdatarocks/webdatarocks/webdatarocks.googlecharts.js";
function App() {
const pivotRef = useRef(null);
const [chartData, setChartData] = useState([]);
const report = {
dataSource: {
filename: "https://cdn.webdatarocks.com/data/data.csv"
},
slice: {
rows: [
{
uniqueName: "Country",
},
],
columns: [
{
uniqueName: "Measures",
},
],
measures: [
{
uniqueName: "Price",
aggregation: "sum",
},
],
},
};
const onReportComplete = () => {
// Unsubscribing from reportcomplete
// We need it only to track the initialization of WebDataRocks
pivotRef.current.webdatarocks.off("reportComplete");
createChart();
};
const createChart = () => {
pivotRef.current.webdatarocks.googlecharts.getData(
{
type: "column",
},
// Function called when data for the chart is ready
(chartConfig) => setChartData(chartConfig.data),
// Function called on report changes (filtering, sorting, etc.)
(chartConfig) => setChartData(chartConfig.data),
);
};
return (
<div>
<WebDataRocksReact.Pivot
ref={pivotRef}
toolbar={true}
report={report}
reportcomplete={onReportComplete}
/>
<Chart
chartType="ColumnChart"
data={chartData}
/>
</div>
);
}
export default App;
React + TypeScript
import * as WebDataRocksReact from "@webdatarocks/react-webdatarocks";
import { RefObject, useRef, useState } from "react";
import { Chart } from "react-google-charts";
import "@webdatarocks/webdatarocks/webdatarocks.googlecharts.js";
function App() {
const pivotRef: RefObject<WebDataRocksReact.Pivot> = useRef<WebDataRocksReact.Pivot>(null);
const [chartData, setChartData] = useState([]);
const report = {
dataSource: {
filename: "https://cdn.webdatarocks.com/data/data.csv"
},
slice: {
rows: [
{
uniqueName: "Country",
},
],
columns: [
{
uniqueName: "Measures",
},
],
measures: [
{
uniqueName: "Price",
aggregation: "sum",
},
],
},
};
const onReportComplete = () => {
// Unsubscribing from reportcomplete
// We need it only to track the initialization of WebDataRocks
pivotRef.current?.webdatarocks.off("reportComplete");
createChart();
};
const createChart = () => {
pivotRef.current?.webdatarocks.googlecharts.getData(
{
type: "column",
},
// Function called when data for the chart is ready
(chartConfig: any) => setChartData(chartConfig.data),
// Function called on report changes (filtering, sorting, etc.)
(chartConfig: any) => setChartData(chartConfig.data),
);
};
return (
<div>
<WebDataRocksReact.Pivot
ref={pivotRef}
toolbar={true}
report={report}
reportcomplete={onReportComplete}
/>
<Chart
chartType="ColumnChart"
data={chartData}
/>
</div>
);
}
export default App;
See also
We hope your time using WebDataRocks Pivot Table has been productive and satisfying. WebDataRocks provides all the basic features for data visualization and analysis, such as connecting to JSON or CSV data sources and essential data aggregation functions.
In case you want to have more features, be it advanced customization or more data source options, we recommend looking at Flexmonster Pivot Table & Charts — a commercial advanced component developed by the same team.
Flexmonster has all of the WebDataRocks features and more, so you have nothing to lose by migrating to it. In addition, the number of changes required to move from WebDataRocks is minimal.
Continue with this page to discover all the Flexmonster features your project can benefit from👇
No data size limit
WebDataRocks works with JSON and CSV datasets no larger than 1 MB, which should satisfy the majority of use cases.
Flexmonster doesn’t set limits on the dataset’s size, so the maximum size depends only on the browser’s capabilities. Moreover, Flexmonster can connect to server-side data storage and load only the necessary data subsets into the browser, allowing you to work with much larger datasets.
More data source options
In addition to having no strict limit on the data size, Flexmonster offers a wider selection of data sources.
The following data sources are supported out of the box:
- JSON
- CSV
- Microsoft Analyses Services
- SQL databases
- Elasticsearch
- MongoDB
You can also connect Flexmonster Pivot to any data source using the custom data source API — Flexmonster’s communication protocol that helps you build a custom data source using your server.
See the full list of the data sources supported by Flexmonster.
Built-in pivot charts
Flexmonster offers one more exclusive feature — built-in pivot charts. This allows you to visualize data using the charts out of the box without integrating with third-party charting libraries.
Check out the video that shows how to work with Flexmonster Pivot Charts:
Advanced customer support
Another important aspect of Flexmonster is Rockstar Support — a team of technical specialists who are always ready to answer your questions or help you resolve issues. You can also check out the collection of resolved support tickets to find a solution for your case.
Other benefits
In addition to everything listed, Flexmonster provides other benefits:
Where do I start with Flexmonster?
Flexmonster offers a 30-day free trial requiring no payment details, so you can test Flexmonster’s functionality and decide whether it fits your project. Moreover, you can get a special discount as a WebDataRocks user if you decide to buy the product license.
To smoothly move from WebDataRocks to Flexmonster, check out our migration guide.
If you want to see Flexmonster in action, watch this video for a detailed overview of Flexmonster Pivot and then try out the live demo:
Data type prefixes are added to field names in the first data record. Use the prefixes to set field data types.
The following prefixes are available:
Name | Description |
---|---|
- | Numbers. |
+ | Strings. |
d+ | Dates divided into 3 subfields: Year, Month, Day. |
D+ | Dates represented as the multilevel hierarchy: Year > Month > Day. |
D4+ | Dates represented as the multilevel hierarchy: Year > Quarter > Month > Day. |
ds+ | Dates displayed in the "dd/MM/yyyy" format. |
dt+ | Dates displayed in the "dd/MM/yyyy HH:mm:ss" format. |
t+ | Time intervals displayed in the "HH:mm:ss" format. |
m+ | Months. Natural sorting is applied to field members. |
w+ | Days of the week. Natural sorting is applied to field members. |
Examples
1) Here is a sample CSV data where the ds+
and w+
prefixes are added to the field names:
ds+Invoice Date, Quantity, Country, w+Week Day 2018-05-15, 3, France, Tuesday 2018-05-16, 4, Italy, Wednesday 2018-05-17, 2, Spain, Thursday 2018-05-12, 2, Japan, Saturday
After loading the CSV data and selecting fields in the Field List, you can see that the Invoice Date
is displayed as a string in the "dd/MM/yyyy"
format, and the Week Day
is interpreted as a day of the week:
2) You can represent a date as a multilevel hierarchy using the D+
or the D4+
prefix.
Here is an example with the D+
prefix:
D+Invoice Date, -Quantity, -Price, +Country 2018-05-15, 3, 329, France 2018-05-16, 4, 139, Italy 2018-05-17, 2, 429, Spain 2018-05-12, 2, 559, Japan
See how the Invoice Date
is displayed in the pivot table when the CSV file is loaded to WebDataRocks:
This demo is also available on our CodePen.
To create multilevel hierarchies from fields of other types, use the JSON data source.
See also
In this guide, you can learn how to format your CSV data so WebDataRocks can process it.
CSV format
WebDataRocks supports the following CSV format:
- The first data record contains field names and optional data type prefixes.
- Each data record is on a separate line.
- Field names and values are separated by the same character: comma
,
, semicolon;
, or a custom field separator.
Here is an example of a valid CSV file:
Invoice Date, Quantity, Country, Week Day
2018-05-15, 3, France, Tuesday
2018-05-16, 4, Italy, Wednesday
2018-05-17, 2, Spain, Thursday
2018-05-12, 2, Japan, Saturday
Input value formats
Number field format
Number values can contain digits, -
, and +
characters. Point .
must be used as a decimal separator. Numbers in exponential notation are also supported. Examples of valid values: -20
, 2.50
, 1.0E+2
.
String field format
String values can be enclosed in double quotation marks or specified without them. If a field value contains line breaks or a field separator, it must be enclosed in double quotation marks. If a field is quoted, it must be escaped with double quotation marks. Examples of valid values: Apple
, "A-Z" section
, "1, 2, 3"
, "The ""A, B, C"" magazine"
.
Date field format
Date values must be specified in the ISO 8601 format. Examples: "2018-01-10"
(date), "2018-01-10T08:14:00"
(date and time), "2018-01-10T06:14:00Z"
(date and time in UTC).
Time field format
Time values must be specified as a number of seconds. The component displays values in the "HH:mm:ss"
format. Examples of valid values: 5400
(displayed as "01:30:00"
in the component).
To ensure the detection of time values, set the field data type prefix to +t
. Otherwise, they will be processed as numbers.
Month field format
Month values must start with a capital letter. Full names and 3-letter abbreviations of months are supported. Examples of valid values: "October"
, "Dec"
, "May"
.
To ensure the detection of month values, set the field data type prefix to +m
. Otherwise, they will be processed as strings.
Weekday field format
Weekday values must start with a capital letter. Full names and 3-letter abbreviations of the days of the week are supported. Examples of valid values: "Monday"
, "Sun"
, "Friday"
.
To ensure the detection of weekday values, set the field data type prefix to +w
. Otherwise, they will be processed as strings.
See also
The metadata object is used for setting data types, creating multilevel hierarchies, and changing captions. This object is specified as the first element of the JSON array.
Properties
Name | Type | Description |
---|---|---|
type | String | optional The field’s data type. Possible values:
|
caption | String | optional The field’s caption. |
hierarchy | String | optional The name of the hierarchy. Used for multilevel hierarchies. Set this property to make the field a level of the specified hierarchy. In this case, the type must be set to "level" . |
parent | String | optional The caption of the parent hierarchy level. Used for multilevel hierarchies. In this case, the type must be set to "level" . |
level | String | optional The caption of the hierarchy level. Used for multilevel hierarchies. In this case, the type must be set to "level" . |
Examples
1) Setting the metadata object in a JSON array of objects:
[
{
"Product": {
"type": "string"
},
"Price": {
"type": "number"
}
},
{
"Product": "Apple",
"Price": 2.50
},
{
"Product": "Cherry",
"Price": 5.25
}
]
See the full example on CodePen.
2) Setting the metadata object in a JSON array of arrays:
[
{
"Product": {
"type": "string"
},
"Price": {
"type": "number"
},
},
["Apple", 2.50],
["Cherry", 5.25]
]
Try a live demo on CodePen.
See also
Using the metadata object for JSON, you can create multilevel hierarchies from fields of any type.
In this guide, we’ll create a Food
hierarchy with Category
, Item
, and Serving Size
levels based on the data below:
[ { "Category": "Breakfast", "Item": "Frittata", "Serving Size": "4.8 oz (136 g)", "Calories": 300 }, { "Category": "Breakfast", "Item": "Boiled eggs", "Serving Size": "4.8 oz (135 g)", "Calories": 250 } ]
Step 1. In the metadata object, set the type of Category
, Item
, and Serving Size
fields as "level"
:
[
{
"Category": {
type: "level"
},
"Item": {
type: "level"
},
"Serving Size": {
type: "level"
},
"Calories": {
type: "number"
}
},
{
"Category": "Breakfast",
"Item": "Frittata",
"Serving Size": "4.8 oz (136 g)",
"Calories": 300
},
{
"Category": "Breakfast",
"Item": "Boiled eggs",
"Serving Size": "4.8 oz (135 g)",
"Calories": 250
}
]
Step 2. Use the hierarchy, parent, and level properties of the metadata object to create the Food
hierarchy:
[
{
"Category": {
type: "level",
hierarchy: "Food"
},
"Item": {
type: "level",
hierarchy: "Food",
level: "Dish",
parent: "Category"
},
"Serving Size": {
type: "level",
hierarchy: "Food",
level: "Size",
parent: "Dish"
},
"Calories": {
type: "number"
}
},
{
"Category": "Breakfast",
"Item": "Frittata",
"Serving Size": "4.8 oz (136 g)",
"Calories": 300
},
{
"Category": "Breakfast",
"Item": "Boiled eggs",
"Serving Size": "4.8 oz (135 g)",
"Calories": 250
}
]
See how this dataset will be visualized in WebDataRocks:
Check out a live demo on CodePen.
See also
In this guide, you can learn how to format your JSON data so WebDataRocks can process it.
JSON formats
WebDataRocks supports two JSON formats:
1) Array of objects. Each object is an unordered collection of key-value pairs, where the key is a field name and the value is a field member. Example:
[ { "Product": "Apple", "Price": 2.50 }, { "Product": "Cherry", "Price": 5.25 } ]
2) Array of arrays. The first subarray contains field names, while the other subarrays contain the respective field members. The field’s name and members must have the same position in the subarrays. Example:
[ ["Product", "Price"], ["Apple", 2.50], ["Cherry", 5.25] ]
Regardless of what JSON format you choose, the first element of each array can be the metadata object for JSON. This object is used to set data types and create multilevel hierarchies.
Input value formats
Number field format
Number values can contain digits, -
, and +
characters. Point .
must be used as a decimal separator. Numbers in exponential notation are also supported. Examples of valid values: -20
, 2.50
, 1.0E+2
.
String field format
String values must be enclosed in double quotation marks. Special characters, such as line breaks or double quotation marks, must be escaped using the backslash. Examples of valid values: "Apple"
, "\"A-Z\" section"
.
Date field format
Date fields must be defined in one of the following formats:
- ISO 8601. Examples:
"2018-01-10"
(date),"2018-01-10T08:14:00"
(date and time),"2018-01-10T06:14:00Z"
(date and time in UTC). - Unix timestamps in milliseconds. Examples:
1515535200000
(timestamp of the"2018-01-10"
date),1515564860000
(timestamp of the"2018-01-10 08:14:20"
date and time).
Note. By default, Unix timestamps are interpreted in WebDataRocks as numbers. To use them as dates, explicitly set the type property in the metadata object to the needed date type. - JavaScript Date objects. Can be used only with inline JSON data. Examples:
new Date(2018, 0, 10)
(creates the"2018-01-10"
date),new Date("January 10, 2018 08:14:20")
(creates"2018-01-10 08:14:20"
date and time).
Note. In the array of arrays, JavaScript Date objects are interpreted in WebDataRocks as strings. To use them as dates, explicitly set the type property in the metadata object to the needed date type.
Time field format
Time values must be specified as a number of seconds. The component displays values in the "HH:mm:ss"
format. Example: 5400
(displayed as "01:30:00"
).
To ensure the detection of time values, set the field type to "time"
in the metadata object. Otherwise, they will be processed as numbers.
Month field format
Month values must start with a capital letter. Full names and 3-letter abbreviations of months are supported. Examples: "October"
, "Dec"
, "May"
.
To ensure the detection of month values, set the field type to "month"
in the metadata object. Otherwise, they will be processed as strings.
Weekday field format
Weekday values must start with a capital letter. Full names and 3-letter abbreviations of the days of the week are supported. Examples: "Monday"
, "Sun"
, "Friday"
.
To ensure the detection of weekday values, set the field type to "weekday"
in the metadata object. Otherwise, they will be processed as strings.
See also
This guide describes how to integrate WebDataRocks with Flutter.
Prerequisites
- Flutter 2.17.6 or greater
- Internet connection
Run our sample project from GitHub
Step 1. Download or clone the sample project from GitHub with the following commands:
git clone https://github.com/WebDataRocks/pivot-flutter
cd pivot-flutter
Step 2. Install dependencies described in the pubspec.yaml
file:
dart pub get
If you get an error on this step, add the Flutter SDK location to the PATH
environment variable. Learn more in the Flutter docs.
Step 3. Connect a mobile device. Learn how to configure Android and iOS devices.
Step 4. Run the project:
flutter run
You can see the result on your mobile device.
To stop the project, press Ctrl + C
(Control + C
on macOS) in the console.
Available examples
The sample project contains the following examples:
- Pivot table demo – Shows how to specify WebDataRocks initialization parameters in Flutter.
- Using API calls – Shows how to invoke WebDataRocks API calls in Flutter.
- Handling events – Shows how to handle WebDataRocks events in Flutter.
- Customizing cells – Shows how to access all WebDataRocks features using JavaScript.
Integrate with Flutter
To integrate WebDataRocks into a Flutter app, take the following steps:
Step 1 (optional). Create a project
If you don’t have an existing Flutter project, create one with the following command:
flutter create wdr_project && cd wdr_project
If you get an error on this step, add the Flutter SDK location to the PATH
environment variable. Learn more in the Flutter docs.
Step 2. Download WebDataRocks
Get WebDataRocks package for Flutter by running the following command from your project’s root folder:
flutter pub add flutter_webdatarocks
Note. Our flutter_webdatarocks wrapper loads WebDataRocks files from CDN.
Step 3. Configure build settings
Ensure the required Android and iOS versions are used in your Flutter project.
Android
compileSdkVersion
≥ 32.targetSdkVersion
≥ 32.minSdkVersion
≥ 19.
iOS
iOS Deployment Target
≥ 9.MinimumOSVersion
≥ 9.
Learn how to configure Android and iOS build settings in Flutter.
Step 4. Add WebDataRocks
Step 4.1. Create a .dart
file (e.g., WDR.dart
) in the lib/
folder of your project:
import 'package:flutter/material.dart'; class WDR extends StatelessWidget { const WDR({super.key}); @override Widget build(BuildContext context) { } }
Step 4.2. Import flutter_webdatarocks into the WDR.dart
file:
import 'package:flutter/material.dart';
import 'package:flutter_webdatarocks/flutter_webdatarocks.dart';
class WDR extends StatelessWidget {
const WDR({super.key});
@override
Widget build(BuildContext context) {
}
}
Step 4.3. Create a WebDataRocks instance and return it from the build() method:
import 'package:flutter/material.dart';
import 'package:flutter_webdatarocks/flutter_webdatarocks.dart';
class WDR extends StatelessWidget {
const WDR({super.key});
@override
Widget build(BuildContext context) {
WebDataRocks pivot = WebDataRocks(
toolbar: true,
report: const {
"dataSource": {
"dataSourceType": "csv",
"filename": "https://cdn.webdatarocks.com/data/data.csv"
}
}
);
return pivot;
}
}
The toolbar
and the report
are WebDataRocks initialization parameters. See the list of supported parameters.
Step 4.4. Add the WebDataRocks instance to a Flutter widget where the pivot table should appear. For example, copy and paste the code below into your main.dart
file:
import 'package:flutter/material.dart';
import 'WDR.dart';
void main() {
runApp(MaterialApp(
title: 'WebDataRocks Demo',
home: Scaffold(
appBar: AppBar(
backgroundColor: const Color.fromARGB(255, 5, 152, 223),
foregroundColor: const Color.fromARGB(255, 233, 233, 233),
title: const Text('WebDataRocks Demo')),
// The WebDataRocks instance
body: const WDR()))
);
}
Step 5. Launch the app
Step 5.1. Run the following command to make sure a mobile device is connected:
flutter devices
If the device is not shown, check out Flutter setup guides for Android and iOS.
Step 5.2. Launch your app with the following command:
flutter run
See the results on the mobile device.
To stop the project, press Ctrl + C
(Control + C
on macOS) in the console.
WebDataRocks initialization parameters
In Flutter, most WebDataRocks initialization parameters are supported, except for the following:
container
customizeCell
- Event handlers. See how to subscribe to WebDataRocks events.
Use methods and events
Using methods
You can invoke WebDataRocks methods via the variable with the WebDataRocks instance (e.g., pivot
):
pivot.openFieldsList();
Check out an example on GitHub.
Note. In Flutter, all WebDataRocks API calls are asynchronous, except for on() and off().
Supported WebDataRocks methods
Most WebDataRocks methods are supported in Flutter, apart from the following:
Note. Unsupported methods can be accessed via JavaScript.
Using events
Subscribing
Attach a handler to a WebDataRocks event using the on() API call:
void updateHandler() { // Event handler }
pivot.on("update", updateHandler);
An event handler can also be defined as an anonymous function:
pivot.on("update", () { // Event handler });
To get data from an event, add the data
input parameter to the handler:
pivot.on("cellclick", (data) { // Event handler });
Check out an example on GitHub.
See the full list of WebDataRocks events.
Unsubscribing
Event handlers can be removed via the off() API call:
- Remove all handlers from a specific event:
pivot.off("cellclick");
- Remove a specific event handler:
pivot.off("cellclick", cellclickHandler);
Access unsupported WebDataRocks features through JavaScript
Using JavaScript, you can access WebDataRocks methods that are not directly supported in Flutter.
For instance, let’s highlight cells you click using customizeCell:
Step 1. Import webview_flutter in the .dart
file with WebDataRocks (e.g., WDR.dart
):
import 'package:flutter/material.dart';
import 'package:flutter_webdatarocks/flutter_webdatarocks.dart';
import 'package:webview_flutter/webview_flutter.dart';
class WDR extends StatelessWidget {
const WDR ({super.key});
@override
Widget build(BuildContext context) {
WebDataRocks pivot = WebDataRocks(
toolbar: true,
report: const {
"dataSource": {
"dataSourceType": "csv",
"filename": "https://cdn.webdatarocks.com/data/data.csv"
}
}
);
return pivot;
}
}
Step 2. Create an anonymous async
handler for the cellclick event. JavaScript code will be run in the handler:
@override
Widget build(BuildContext context) {
WebDataRocks pivot = WebDataRocks(
toolbar: true,
report: const {
"dataSource": {
"dataSourceType": "csv",
"filename": "https://cdn.webdatarocks.com/data/data.csv"
}
}
);
pivot.on("cellclick", (data) async {
// Your JavaScript code will be run here
});
return pivot;
}
Step 3. Create a WebViewController instance (e.g., controller
) in the handler:
pivot.on("cellclick", (data) async {
// Your JavaScript code will be run here
WebViewController controller = await pivot.controller.future;
});
Step 4. Call the runJavascript() method on the controller
and specify JavaScript code as a parameter:
pivot.on("cellclick", (data) async {
// Your JavaScript code will be run here
WebViewController controller = await pivot.controller.future;
controller.runJavascript('''
webdatarocks.customizeCell((cellStyle, cellData) => {
if(cellData.label === "${data['label']}") {
cellStyle.style.background = "rgba(5, 152, 223, 0.5)";
}
});
''');
});
Note. runJavascript() does not return the result of your JavaScript code. To return the result, use the runJavascriptReturningResult() method instead.
Now every time you click a grid cell, it is highlighted with a blue color.
Check out another example of using JavaScript in our sample GitHub project.
We use only open-source components in WebDataRocks. Here is a list of these libraries and links to their respective licenses:
- html2canvas v1.0.0-rc.1 – MIT License
- FileSaver.js v1.3.1 (modified by Flexmonster) – MIT License
- jsPDF v1.5.3 – MIT License
- JSZip v3.1.5 – MIT License