相关文章
Develop a Power BI circle card visual as an example
2024-12-31 05:31

In this tutorial, you develop a Power BI visual named circle card that displays a formatted measure value inside a circle. The circle card visual supports customization of fill color and outline thickness.

In this tutorial, you learn how to:

Before you start developing your Power BI visual, verify that you have everything listed in this section.

  • A Power BI Pro or Premium Per User (PPU) account. If you don't have one, sign up for a free trial.

  • Visual Studio Code (VS Code). VS Code is an ideal Integrated Development Environment (IDE) for developing Javascript and Typescript applications.

  • Windows PowerShell version 4 or later (for Windows). Or Terminal (for Mac).

  • An environment ready for developing a Power BI visual. Set up your environment for developing a Power BI visual.

  • This tutorial uses the US Sales Analysis report. You can download this report and upload it to Power BI service, or use your own report. If you need more information about Power BI service, and uploading files, refer to the Get started creating in the Power BI service tutorial.

In this section, you create a project for the circle card visual.

  1. Open a new terminal in VS Code and navigate to the folder you want to create your project in.

  2. Enter the following command in the PowerShell terminal:

    
    
  3. Open the CircleCard folder in the VS Code explorer. (File > Open Folder).

    For a detailed explanation of the function of each of these files, see Power BI visual project structure.

  4. Check the terminal window and confirm that you're in the circleCard directory. Install the Power BI visual tools dependencies.

    
    
    
  5. Start the circle card visual.

    
    

    Your visual is now running while being hosted on your computer.

To test the visual in Power BI service, we'll use the US Sales Analysis report. You can download this report and upload it to Power BI service.

You can also use your own report to test the visual.

  1. Sign in to PowerBI.com and open the US Sales Analysis report.

  2. Select Edit.

  3. Create a new page for testing, by clicking on the New page button at the bottom of the Power BI service interface.

  4. From the Visualizations pane, select the Developer Visual.

    This visual represents the custom visual that you're running on your computer. It's only available when the custom visual debugging setting is enabled.

  5. Verify that a visual was added to the report canvas.

    This is a simple visual that displays the number of times its update method has been called. At this stage, the visual does not retrieve any data.

  6. While the new visual is selected, go to the Data pane, expand Sales, and select Quantity.

  7. To test how the visual is responding, resize it and notice that the Update count value increments every time you resize the visual.

In this section you learn how to turn your visual into a circle, and make it display text.

Set up the visual.ts file.

  1. In VS Code, in the Explorer pane, expand the src folder, and select the file visual.ts.

  2. Remove all the code under the MIT License comment.

  3. import the libraries and modules needed, and define the type selection for the d3 library:

    
    
    

    Notice that among the items you imported are:

    • IVisualHost - A collection of properties and services used to interact with the visual host (Power BI).
    • D3 library - Javascript library for creating data driven documents.
  4. Below the imports, create an empty visual class. The visual class implements the IVisual interface where all visuals begin:

    
    

    For information about what goes into the visual class, see Visual API. In the next three steps, we define this class.

  5. Add class-level private methods at the beginning of the visual class:

    
    

    Notice that some of these private methods use the Selection type.

  6. Define the circle and text elements in the constructor method. This method is called when the visual is instantiated. The D3 Scalable Vector Graphics (SVG) enable creating three shapes: a circle, and two text elements:

    
    
  7. Define the width and height in the update method. This method is called every time there's a change in the data or host environment, such as a new value or resizing.

    
    
  8. Save the visual.ts file.

Verify that the final code in the visual.ts file looks like this:



The circle card visual is a simple visual that doesn't create any objects in the Format pane. Therefore, you can safely remove the objects section of the file.

  1. Open your project in VS Code (File > Open Folder).

  2. Select the capabilities.json file.

  3. Remove the entire objects array.
    Don't leave any blank lines between dataRoles and dataViewMappings.

  4. Save the capabilities.json file.

Stop the visual from running and restart it.

  1. In the PowerShell window where you started the visual, enter Ctrl+C. If prompted to terminate the batch job, enter Y and then Enter.

  2. In PowerShell, start the visual again.

    
    

Verify that the visual displays the newly added elements.

  1. In Power BI service, open the Power BI US Sales Analysis report. If you're using a different report to develop the circle card visual, navigate to that report.

  2. Drag a value into the Measure box and make sure that the visual is shaped as a circle.

    If the visual isn't displaying anything, from the Fields pane, drag the Quantity field into the developer visual.

  3. Resize the visual.

    Notice that the circle and text scale to fit the dimensions of the visual. The update method is called when you resize the visual, and as a result the visual elements get rescaled.

Use this setting to ensure that the visual is automatically reloaded each time you save project changes.

  1. Navigate to the Power BI US Sales Analysis report (or to the project that has your circle card visual).

  2. Select the circle card visual.

  3. In the floating toolbar, select Toggle Auto Reload.

In this section, you define data roles and data view mappings. You also modify the visual to display the name of the value it's displaying.

Modify the capabilities.json file to define the data role, objects, and data view mappings.

  • Define the data role

    Define the dataRoles array with a single data role of the type measure. This data role is called measure, and is displayed as Measure. It allows passing either a measure field, or a summed up field.

    1. Open the capabilities.json file in VS Code.

    2. Remove all the content inside the dataRoles array.

    3. Insert the following code to the dataRoles array.

      
      
    4. Save the capabilities.json file.

  • Define the data view mapping

    Define a field called measure in the dataViewMappings array. This field can be passed to the data role.

    1. Open the capabilities.json file in VS Code.

    2. Remove all the content inside the dataViewMappings array.

    3. Insert the following code to the dataViewMappings array.

      
      
    4. Save the capabilities.json file.

Confirm that your capabilities.json file looks like this:



Verify that the circle card visual displays the measure field, and review the changes you made using the Show Dataview option.

  1. In Power BI service, open the Power BI US Sales Analysis report. If you're using a different report to develop the circle card visual, navigate to that report.

  2. Notice that the circle card visual can now be configured with a field titled Measure. You can drag and drop elements from the Data pane into the Measure field.

  3. In the floating toolbar, select Show Dataview.

  4. Select the three dots to expand the display, and select single to view the value.

  5. Expand metadata, then the columns array, and review the format and displayName values.

  6. To toggle back to the visual, in the toolbar floating above the visual, select Show Dataview.

So far, the visual renders, but doesn't display any data. In this section, you make changes to the visual.ts file, so that the circle card visual can consume data.

  1. Open the visual.ts file in VS Code.

  2. In the update method:

    • Add the following statement as the first statement. The statement assigns dataView to a variable for easy access, and declares the variable to reference the dataView object.

      
      
    • Replace .text("Value") with this line of code:

      
      
    • Replace .text("Label") with this line of code:

      
      
  3. Save the visual.ts file.

  4. Review the visual in the Power BI service.

The visual now displays the name and value of the selected data field.

    以上就是本篇文章【Develop a Power BI circle card visual as an example】的全部内容了,欢迎阅览 ! 文章地址:http://ww.kub2b.com/quote/11147.html 
     栏目首页      相关文章      动态      同类文章      热门文章      网站地图      返回首页 企库往资讯移动站http://ww.kub2b.com/mobile/,查看更多   
发表评论
0评