Home  >  Article  >  Backend Development  >  Visualizing using Tableau API in PHP

Visualizing using Tableau API in PHP

PHPz
PHPzOriginal
2023-06-20 12:17:38773browse

Tableau is a popular business intelligence tool that transforms data into easy-to-understand charts and visualizations. In order to visualize data, you usually need to use Tableau Desktop or Tableau Server. However, sometimes you need to embed Tableau visualizations into other applications. In this case, you can use the Tableau API to achieve this.

In this article, we will detail how to use the Tableau API in PHP to create and present Tableau visualizations.

  1. Install Tableau API

First, you need to download and install the Tableau API from the Tableau official website. Tableau API is available in various programming languages, including PHP.

The installation process requires registering the Tableau API dll file in the system and adding the required PHP extensions to the PHP.ini file. During the installation process, be sure to select the appropriate version number to match your PHP version.

  1. Configure Tableau API

After completing the installation, you need to configure the Tableau API in PHP code. First you need to use the following code block to import the Tableau API:

// Load Tableau PHP API
require 'path/to/TableauAPI/autoload.php';

Next, you need to use the following Code to set Tableau API connection parameters:

// Set Tableau API Connection Parameters
$server = 'http://localhost';
$username = 'admin';
$ password = 'password';

// Create a Tableau Server Client
$client = new TableauTableauServerClient($server, $username, $password);

// Sign-In to Tableau Server
$client->signIn();

In the above code, you need to provide the URL, username and password of Tableau Server. Subsequently, create a Tableau Server client using the TableauServerClient class. Finally, use the signIn() method to log in.

  1. Create a Tableau workbook

Now that you have set the Tableau API connection parameters and logged in to Tableau Server, you need to create a Tableau workbook. You can use the following code block to create a workbook named "SalesDashboard":

// Create a Tableau Workbook
$workbook = new TableauWorkbook('SalesDashboard');

// Publish Workbook to Tableau Server
$client->publishWorkbook($workbook);

In the above code, you need to use the Workbook class to create a Tableau workbook. Next, publish the workbook using the publishWorkbook() method.

  1. Loading Data

By completing steps 2 and 3, you have created an empty Tableau workbook. Now you need to load the data source. You can use the following code block to load an Excel workbook:

// Load Excel Workbook Data
$connection = new TableauConnectionsExcelConnection('path/to/SalesData.xlsx');
$dataSource = new TableauDataSource('Sales Data', [$connection]);
$workbook->addDataSource($dataSource);

In the above code block, you need to provide the path to the Excel workbook. Next, create a connection object using the ExcelConnection class and pass it to the constructor of the DataSource class. Finally, add the data source to the Tableau workbook.

  1. Create a worksheet

After completing the 4 steps, you can use the following code to create a worksheet in the Tableau workbook:

// Create a Worksheet
$worksheet = new TableauWorksheet('Sales by Region', $dataSource);

// Define the View for the Worksheet
$view = new TableauViewsTabularView('Region', 'Sales' );

// Add the View to the Worksheet
$worksheet->addView($view);

In the above code, you need to provide the worksheet name and data source. Create a view using the TabularView class and add it to the worksheet.

  1. Publish View

After completing the 5 steps, you have created a Tableau workbook and a worksheet. Next, you need to publish the view to Tableau Server. The custom code is as follows:

// Create a View of the Worksheet
$view = $worksheet->createView();

// Publish the View to Tableau Server
$client->publishView('http://localhost/views/SalesDashboard/SalesbyRegion', $view);

In the above code, createView( ) method converts the worksheet into a view and publishes it to Tableau Server using the publishView() method.

  1. Rendering Tableau Visualization in PHP

Now that the view has been successfully published to Tableau Server, you can render it in your PHP application using the following code:

// Embed Tableau View in PHP Webpage
echo '';

at In the above code, the