Visualizing data from a ClickHouse database
- Before you start
- Step 1. Define the data source for the dataset
- Step 2. Configure the dataset fields
- Step 3. Create a line chart
- Step 4. Create a column chart
- Step 5. Create a pivot table chart
- Step 6. Create a heat map
- Step 7. Create a dashboard
- Step 8. Add the charts to the dashboard
- Step 9. Add selectors to the dashboard
As a data source, we'll use a demo ClickHouse database on sales in a Moscow chain of stores.
The connection to this database is automatically created when a DataLens instance is created.
In this example, DataLens is used to visualize:
- Sales dynamics by day and month.
- Sales by product categories.
- Order heat map.
A demo Sample ClickHouse connection is used.
To visualize and analyze the data, make sure you have a ready-to-use cloud and follow these steps:
- Define the data source for the dataset.
- Configure the dataset fields.
- Create a line chart.
- Create a column chart.
- Create a pivot table chart.
- Create a heat map chart.
- Create a dashboard.
- Add the charts to the dashboard.
- Add selectors to the dashboard.
Create a connection to the ClickHouse database with the table.
Before you start
To use DataLens, sign up for Yandex.Cloud and create a billing account (without a billing account, DataLens is only available in demo mode):
- Go to the management console. Then log in to Yandex.Cloud or sign up if don't already have an account.
- On the billing page, make sure you linked a billing account, and it has the
ACTIVE
orTRIAL_ACTIVE
status. If you don't have a billing account, create one.
When you activate your billing account, you automatically switch to the Free DataLens plan. By default, you can create a DataLens instance for no more than 1 folder. If you haven't done this yet:
- Select a folder on the cloud page.
- Select the DataLens service.
- Click Activate DataLens.
An instance of the service is created in the selected folder and you can go directly to the DataLens interface.
Learn more about clouds and folders.
Step 1. Define the data source for the dataset
Create a dataset using the connection Sample ClickHouse created from a ClickHouse database.
-
Go to DataLens.
-
Click Create dataset.
-
Click Add under Connections on the selection panel. Select the Sample ClickHouse connection.
-
Drag the MS_SalesFacts table to the workspace.
-
Drag the MS_Clients table to the workspace. The tables are automatically linked.
-
To check the link, click the link icon between the tables.
-
Tables are linked by the ClientID field. If needed, you can modify or expand the link by specifying another pair of fields. To close the link settings window, click the cross button or anywhere outside the window.
-
Drag the MS_Products table to the workspace. The table is automatically linked to the left (root) table MS_SalesFacts.
-
Drag the MS_Shops table to the workspace. The table is automatically linked to the left (root) table MS_SalesFacts.
Step 2. Configure the dataset fields
-
Go to the Fields tab.
-
Delete the duplicate fields left over from joining the tables: ClientID (1), ProductID (1), and ShopID (1).
-
Create an order date field named OrderDate.
-
Duplicate the OrderDatetime field.
-
Rename the OrderDatetime (1) duplicate field to OrderDate: click the row name, delete the current name, and enter the new one.
-
Change the data type from Date and time to Date.
-
-
For the ShopDistrictCoordinates field, change the data type to Geopolygon.
-
For the DeliveryDistrictCoordinates field, change the data type to Geopoint.
-
In the Aggregation column, select Sum for the Sales field.
The aggregation field changes color to blue: Sales is now a measure.
-
Create a measure for the number of orders.
-
Duplicate the OrderID field.
-
Rename the OrderID (1) duplicate field to OrderCount.
-
Change the aggregation type to Number of unique.
-
-
Create a calculated field for the average sales amount per order.
-
In the upper-right corner, click Add field.
-
In the Name field, specify Sales per Order.
-
In the column to the left, click on the Sales field.
-
Enter a
/
. -
In the column to the left, click the OrderCount field.
-
Click Create.
-
-
Click Save in the upper-right corner to save the dataset.
-
Enter a name for the dataset: Moscow Sales dataset, then click Create.
-
When the dataset is saved, click Create chart.
Step 3. Create a line chart
To visualize sales dynamics by month, create a chart: line chart.
-
For the visualization type, choose Line chart.
-
Add the sales date to the chart. Drag the OrderDate field from the Dimensions section to the X section.
-
Add a sales measure to the chart. To do this, drag the Sales field from the Measures section to the Y section.
-
Add the delivery type to the chart. To do this, drag the PaymentType field from the Dimensions section to the Colors section.
-
Display the chart by month.
- Click the calendar icon next to the OrderDate field in the X section.
- In the drop-down list of grouping options, select Month under Rounding.
- Click Apply.
-
Save the chart.
-
Click Save in the upper-right corner to save the chart.
-
In the window that opens, enter a name for the chart: Sales dynamics by month and payment type, and click Save.
-
Step 4. Create a column chart
To visualize sales by brands and product categories, create a chart: column chart.
-
Copy the chart from the previous step.
-
Click the down arrow next to the Save button in the upper-right corner.
-
Choose Save as.
-
In the window that opens, enter the name Sales by brand and category for the new chart and click Save.
-
-
For the visualization type, choose Column chart.
-
The OrderDate, Sales, and PaymentType fields are copied automatically to the X, Y, and Colors sections, respectively.
-
Replace the months with brands on the X-axis. From the Dimensions section, drag the ProductBrand field to the X section and hold it over the OrderDate field until it turns red.
-
Replace payment types for product categories in Colors. Drag ProductCategory from the Dimensions section to the X section and hold it over the PaymentType field until it turns red.
-
Sort the chart in descending order of sales measure. Drag the Sales field from the Measures section to the Sorting section.
-
Save the chart.
Step 5. Create a pivot table chart
To visualize sales in terms of products and time, create a chart: pivot table.
-
Copy the chart from the previous step.
- Click the down arrow next to the Save button in the upper-right corner.
- Click Save as.
- In the window that opens, enter the name Sales by year and product for the new chart.
- Click Ready.
-
Choose the Pivot table visualization type.
-
The ProductBrand and Sales fields are automatically copied to the Columns and Measures sections, respectively.
-
Remove ProductBrand from the table.
-
Add the order date to the table. To do this, drag the OrderDate field from the Dimensions section to the Columns section.
-
Change the display format in the OrderDate field to years.
-
Click the calendar icon next to the OrderDate field in the Columns section.
-
In the drop-down list of grouping options, select Year under Date part.
-
Click Apply.
-
-
Add the product category and subcategory to the table. To do this, drag the ProductCategory and ProductSubcategory fields from the Dimensions section to the Rows section.
-
Change the color of the sales measure in the table. To do this, drag the Sales field from the Measures section to the Colors section.
-
Save the chart.
Step 6. Create a heat map
To visualize the density of orders on the Moscow map, create a chart: heat map.
-
Copy the chart from the previous step.
- Click the icon next to the Save button in the upper-right corner and select Save as.
- In the window that opens, enter the name Sales heat map for the new chart.
- Click Ready.
-
Select the Map visualization type.
-
Delete the Sales field from the Colors section.
-
Select the Points (heat map) layer type.
-
Add the delivery point coordinates to the map. To do this, drag the DeliveryAddressCoord field from the Dimensions section to the Points (heat map) section.
-
Save the chart.
Step 7. Create a dashboard
Create a Dashboard to add your charts to.
-
Go to the DataLens homepage.
-
Click Create dashboard.
-
Enter the name Moscow Shops dashboard for the dashboard and click Create.
Step 8. Add the charts to the dashboard
-
The first time you open the dashboard after saving, it opens in edit mode. If you open it later, click Edit in the upper-right corner.
-
Click Add and select Chart.
-
In the window that opens, click Select.
-
Select the chart Sales dynamics by month and payment type. This automatically fills in the Title field with the name of the selected chart.
-
Click Add.
-
Similarly, add the following charts:
- Sales by brand and category
- Sales by year and product
- Sales heat map
-
Position the charts on the dashboard however you like.
Step 9. Add selectors to the dashboard
Add selectors to filter the charts by date, Moscow districts, products, and customer statuses.
-
Click Add.
-
Choose Selector.
-
Add the calendar selector for the order date.
- Select the Moscow Sales dataset.
- Select the OrderDate field.
- This automatically fills in Title with the name of the selected field. Click the Show checkbox next to the selector title.
- Select the Calendar type.
- Enable Range.
- Click Add.
-
Add a product category selector.
- Select the Moscow Sales dataset.
- Select the ProductCategory field.
- This automatically fills in Title with the name of the selected field. Click the Show checkbox next to the selector title.
- Enable the Multiple choice option.
- Click Add.
-
Similarly, add selectors for the following field:
- ProductBrand
- DeliveryDistrictName
- DeliveryType
- PaymentType
-
Position the selectors on the dashboard however you like.
-
Save the dashboard.
-
Your dashboard is ready. Now you can filter charts using selectors.