Creating charts using PowerApps component framework

 

open-events-work-areaPowerApps component framework is the modern way how developers can create their custom controls and embed them into model-driven apps. This is a significant step towards empowering 3rd party developers to build compelling visual components in PowerApps and Dynamics 365 using the same framework which the Microsoft team uses. Our customers are often asking about some data visualization directly on the forms, so in this article, I would like to show you how using third-party typescript libraries you can build you simple visualization and embed it into your Dynamics CE implementation. We will create a simple custom control which will be using data in json format from the text field to create a pie. As a requirement, you need to know how to develop apps using TypeScript.

In case you just wanna download the source code, please use the link below: https://github.com/LrdSpr/PieChartControl

I would assume that you already have node.js installed and you are aware of how to use it. To set up everything you need and learn some basics, go and check the following article from Microsoft: Get tooling for PowerApps component framework. All my components are stored in C:\PowerAppsControls, so I would use this folder to create new controls or to prepare for publishing an existing one. Now when everything is installed, we can start by creating our project. You need to launch visual studio developer command prompt and execute the following command in the directory where you want your new component to be created, for me it is C:\PowerAppsControls\PieChartControl:

pac pcf init –namespace Pkoval.Xrm.Controls –name PieChart –template field

This command will create your project with appropriate namespace and using the provided template. In our case template is the field. The idea is just to replace the standard text field we have on our form with the pie chart. The text data which we can store in our field will be used to render our pie so that every time you change data in the field pie will be redrawn.

The next step here is to set up all the dependencies which our component requires. In the same directory, you need to launch:

npm install

After that, your control is ready for development.

Now, as we want to create a pie and I don’t want to draw it by myself, we need to set up an additional component which we can use to draw charts. Across several projects, we have been using d3, and I’m quite happy with it. Here are some examples of charts and diagrams you can build using d3

visuals

More examples you can find in their GitHub: https://github.com/d3/d3/wiki/Gallery

To set up d3 use following commands:

npm install –save d3
npm install –save-dev @types/d3

Now we are ready to start development, but before that let’s just briefly review the main parts of the PCF control:

In case you’ve used the same file structure as I just go to the following directory: C:\PowerAppsControls\PieChartControl\PieChart.

In this directory, you can find two files:

ControlManifest.Input.xml – Metadata about your control.

index.ts – TypeScript file which will contain your logic.

Let’s at first configure ours manifest.

manifest

Here you can define name, description, and properties which your component can use to retrieve and operate with some data. Also, you may define generic types which your component will support like SingleLine.Text or Currency. In this case, the component will only be shown for Text and Currency fields. For our simple component, we have only one property. This is SingleLine.Text property and also our control only support standard fields with the same type. Type-Group tag defines which standard types are supported for your component. Our custom property also has a name (JsonChart) and can be later used to get data from the mapped control in Dynamics CE or any other application which can use PCF controls. Properties can have different types: bound, input, and output. Bound property can be mapped to the field in Dynamics CE so that you can get data directly from the application. I would not go into the details here as there are plenty of articles which are written about it.

Let’s move to the coding part and implement our control.

First of all, we need to add some imports:

Now we need to define data structure which will be used to store information about our pie. Let’s use a very basic interface which contains two fields ‘category’ and ‘quantity.’

The category will be used for labels and quantity to render parts of the pie.

Now let’s define some variables in our PieChart class:

_value – used to store the current JSON representation of the Pie in Dynamics CE.
_container – identifier which can be used to find our control

Now we are ready to draw our pie, so let’s implement the drawChart function.

As you see d3 has it’s own methods to manipulate with HTML so the first parameter here is to provide identifier which can be used by d3 to locate where to add ‘Scalable Vector Graphics’ (SVG) tag and also before adding this tag I’m trying to find if it already exists and remove it. We need this as we want to redraw the pie on the fly. Of course, d3 supports animations and different transitions so that it is not necessary to remove this tag, you can simply implement the transformation.  As a second parameter, we are expecting the data which has to be rendered.  We can define several colors which can be used by the rendering engine as well as different angles and positions to describe how the pie should be drawn, so if you are ok with trigonometry, it should not cause any problems.

As we now can simply draw our pie according to the data, let’s finish our control by implementing the rest of the mandatory functions.

init is the main function of every control which is executed once you’ve opened the page. Here we just define the identifier of the place where we located in the DOM so that we can use it later to add our SVG tag. We try to get the current value from the CDS. For that, we call context.parameters.JsonChart.formatted. JsonChart property should be mapped to some standard field in Dynamics CE or any other application. If there is no data, we just show some defaults like ‘No Data’ and pie with the only section. In case there are some issues with the JSON we are showing ‘Wrong format’ message.

The last two functions we need to implement you can find below:

updateView always called when someone changes values in your mapped field as an example if someone will update assigned field using javascript. In this function, we just need to draw our chart again as source data was changed.

And here how our control can look like on the contact form:

examplechart

So as you see, it is quite simple to build custom controls for power platform. We can use lots of different libraries and implement a really great user experience for our end-customers. You can find the source code of this control here: https://github.com/LrdSpr/PieChartControl

Leave a Reply