In this chapter, we take a break from designing charts and instead take a detailed look at the expressions used by Charticulator when you bind data to an attribute. You also may have been at a loss as to how Charticulator’s rather unusual numeric formatting method works. This is where you edit characters inside the curly braces. It’s a syntax known as “d3-format.” Have you ever heard of it? No? Well, neither had I nor indeed have any of my more “techie” friends. It doesn’t help that Charticulator’s documentation doesn’t throw much light on how you’re meant to use this syntax (it took me some time to find out how to insert a comma separator). Just to fill you in, “d3-format” is used by JavaScript programmers to format numbers, apparently for “human consumption,” according to GitHub (https://github.com/d3/d3-format), and is based on Python 3’s formatting specifications. However, I don’t suppose knowing this really helps a lot, so in this chapter I will throw some light on this rarefied syntax language. What we will also discover is that there is an alternative approach to formatting numerical fields using DAX, and we will explore this possibility at the end of this chapter.
All Charticulator’s expressions are introduced by the dollar ($) sign if they are in the Text attribute of a text mark or by an “f of x” button if they are in an attribute that defines a scale, such as the Height or Fill attribute. In the latter case, you can click the button to change the function. The major difference between DAX or Excel formulas and those in Charticulator is that in Charticulator part of the expression can include the format specifier. Also, a field is referenced without a qualifying table name because all Power BI visuals, including Charticulator visuals, can only ever see their own underlying data set, not the data model.
- 1.
Referencing field names
- 2.
The aggregation of the data
- 3.
The format of the numerical expressions using d3-format
- 4.
The format of the tick label on the numerical axis using d3-format
- 5.
Using DAX to format numerical fields
Referencing Field Names
It’s interesting to note that although the space character requires double quotes surrounding it, the dash character does not.
Using Charticulator’s Aggregate Functions
Just like DAX measures, Charticulator supports functions that return scalar values such as “sum,” “avg,” “max,” “min,” “count,” “stdev,” “variance,” “median,” “first,” and “last.”
Expressions Using Categorical Data
Charticulator, as all Power BI visuals, must use a function to retrieve a single value, but because you can’t aggregate text values, it uses the “first” function by default.
Expressions Using Numerical Data
The reason the “avg” function is used is because, unless you use the “Group by…” attribute, each shape represents a single row from the underlying data. Therefore, expressing the average of one value is that value (as indeed would using “sum,” “max,” “min,” “first,” or “last”). In this case, there is no advantage to changing the function. The exception to this is when you group your data using “Group by….”
In Figure 8-5, you can see that the calculation divides the “Sales” value by 1,000,000, and this calculation sits inside the braces.
Expressions Using Grouped Data
In Chapter 11, I’ll show you how you can use the “Group by…” attribute to great effect in your chart designs.
Formatting Numerical Expressions
Charticulator uses a numeric formatting syntax based on the “d3-format” specification used by JavaScript. You may never have heard of “d3-format,” but have you ever created custom Excel formats? If so, you’ll be familiar with the idea of placing format specifiers in specific positions to determine the format of a number (e.g., Excel’s custom format uses “positive ; negative ; zero ; text”). Well, d3-format is a bit like that, only a little more complicated. Here are the d3-format specifiers:
[[fill]align][sign][symbol][0][width][,][.precision][~][type]
You can find more information regarding this syntax here:
https://d3-wiki.readthedocs.io/zh_CN/master/Formatting/
“d3-format” identifiers used in Charticulator expressions
Specifier | Value | Description | Example | Result |
---|---|---|---|---|
[type] | f | Fixed decimal | 334915.00 | |
[type] | % | Percent | 33.49% | |
[type] | r | Round to significant digits | 33500 | |
[type] | s | For M or K units | 334.92 | |
[.precision] | .n | For n decimal places | 334915.00 | |
[ , ] | , | Comma separator | 334,915 |
You can find information on the other “type” identifiers in the documentation on GitHub.
There is an alternative way to generate currency formats that we explore in the last section (see the section on “Using the DAX FORMAT Function” below).
Formatting Tick Labels
Note the use of the currency symbol typed outside the braces.
Using the DAX FORMAT Function
The measure that defines the format is added to the data that comprises the chart and so it then shows in the Fields pane. It can then be used in the Text attribute of a text mark.
For more information on the DAX FORMAT function and format strings that you can use, follow this link:
https://docs.microsoft.com/en-us/dax/format-function-dax
I appreciate that all of this has been a bit dry. Nevertheless, it’s very frustrating if you don’t know how to format your numerical data, and Charticulator does not make it very user-friendly! However, in this chapter, you have learned how to reference field names and how to work with expressions involving both categorical and numerical data. You also now understand a little more about d3-format and can use it to format numerical expressions for the Text and the Tick Format attributes.
I know that you’re itching to get back to building visuals, but before we can do so, there’s a mandatory topic that we have yet to address. In order to move forward with more challenging charts, we need to get to grips with the rather odd world of Charticulator’s scales and legends, which are the subject of the next chapter. I know we’ve already been using legends, and we’ve touched on the workings of the Scales pane, but like always with Charticulator, there remains a lot more to say. I don’t think you’ll find that very surprising.