Number Formatting Issue with Custom Power BI Visual - powerbi

Above is the card from the tutorial at https://learn.microsoft.com/en-us/power-bi/developer/custom-visual-develop-tutorial. Here is the related code for the visual.
I would like the number to be shown as a percentage value, i.e. 15.4% which is how it is formatting in the measure.
I have searched github, microsoft, google, etc. for a solution to this issue. I also skimmed all the recommended questions on Stack Overflow before posting. Any help get the number formatting to match the measures formatting would be greatly appreciated.
Here is the related code for the visual. (sorry for the length)
visual.ts
"use strict";
import "core-js/stable";
import "./../style/visual.less";
import powerbi from "powerbi-visuals-api";
import VisualConstructorOptions = powerbi.extensibility.visual.VisualConstructorOptions;
import VisualUpdateOptions = powerbi.extensibility.visual.VisualUpdateOptions;
import IVisual = powerbi.extensibility.visual.IVisual;
import EnumerateVisualObjectInstancesOptions = powerbi.EnumerateVisualObjectInstancesOptions;
import DataView = powerbi.DataView;
import * as d3 from "d3";
import VisualObjectInstanceEnumeration = powerbi.VisualObjectInstanceEnumeration;
import { VisualSettings } from "./settings";
import { rgb, RGBColor, Color } from "d3";
export class Visual implements IVisual {
private visualSettings: VisualSettings;
private svg: d3.Selection<SVGElement, any, any, any>;
private container: d3.Selection<SVGElement, any, any, any>;
private circle: d3.Selection<SVGElement, any, any, any>;
private textLabel: d3.Selection<SVGElement, any, any, any>;
private textValue: d3.Selection<SVGElement, any, any, any>;
private settings: VisualSettings;
constructor(options: VisualConstructorOptions) {
console.log('Visual constructor', options);
this.svg = d3.select(options.element)
.append('svg')
.classed('circlecard', true);
this.container = this.svg.append("g")
.classed('container', true);
this.circle = this.container.append("circle")
.classed('circle', true);
this.textValue = this.container.append("text")
.classed("textValue", true);
this.textLabel = this.container.append("text")
.classed("textLabel", true);
}
public enumerateObjectInstances(options: EnumerateVisualObjectInstancesOptions): VisualObjectInstanceEnumeration {
const settings: VisualSettings = this.visualSettings ||
VisualSettings.getDefault() as VisualSettings;
return VisualSettings.enumerateObjectInstances(settings, options);
}
public update(options: VisualUpdateOptions) {
this.settings = Visual.parseSettings(options && options.dataViews && options.dataViews[0]);
let dataView: DataView = options.dataViews[0];
let width: number = options.viewport.width;
let height: number = options.viewport.height;
this.svg.attr("width", width)
.attr("height", height);
let radius: number = Math.min(width, height) / 2.2;
this.visualSettings = VisualSettings.parse<VisualSettings>(dataView);
this.visualSettings.circle.circleThickness = Math.max(0, this.visualSettings.circle.circleThickness);
this.visualSettings.circle.circleThickness = Math.min(10, this.visualSettings.circle.circleThickness);
this.circle
.style("fill", this.visualSettings.circle.circleColor)
.style("stroke", this.visualSettings.circle.circleColor)
.style("stroke-width",this.visualSettings.circle.circleThickness)
.attr("r", radius)
.attr("cx", width / 2)
.attr("cy", height / 2);
let fontSizeValue: number = Math.min(width, height) / 5;
this.textValue
.text(dataView.single.value as string)
.attr("x", "50%")
.attr("y", "50%")
.attr("dy", "0.35em")
.attr("text-anchor", "middle")
.style("font-size", fontSizeValue + "px");
let fontSizeLabel: number = fontSizeValue / 4;
this.textLabel
.text(dataView.metadata.columns[0].displayName)
.attr("x", "50%")
.attr("y", height / 2)
.attr("dy", fontSizeValue / 1.2)
.attr("text-anchor", "middle")
.style("font-size", fontSizeLabel + "px");
}
private static parseSettings(dataView: DataView): VisualSettings {
return VisualSettings.parse(dataView) as VisualSettings;
}
/**
* This function gets called for each of the objects defined in the capabilities files and allows you to select which of the
* objects and properties you want to expose to the users in the property pane.
*
*/
}
capabilities.json
{
"dataRoles": [
{
"displayName": "Measure",
"name": "measure",
"kind": "Measure"
}
],
"objects": {
"circle": {
"displayName": "Circle",
"properties": {
"circleColor": {
"displayName": "Color",
"description": "The fill color of the circle.",
"type": {
"fill": {
"solid": {
"color": true
}
}
}
},
"circleThickness": {
"displayName": "Thickness",
"description": "The circle thickness.",
"type": {
"numeric": true
}
}
}
}
},
"dataViewMappings": [
{
"conditions": [
{ "measure": { "max": 1 } }
],
"single": {
"role": "measure"
}
}
]
}
settings.ts
"use strict";
import { dataViewObjectsParser } from "powerbi-visuals-utils-dataviewutils";
import DataViewObjectsParser = dataViewObjectsParser.DataViewObjectsParser;
export class CircleSettings {
public circleColor: string = "white";
public circleThickness: number = 2;
}
export class VisualSettings extends DataViewObjectsParser {
public circle: CircleSettings = new CircleSettings();
}
Thank you for viewing this.

One way I always handle my formats (when not possible via the interface)
is creating custom Measures that contain the format I want.
I created 2 cards to show unformatted and formatted values:
Measure formula :
Fmt_value = FORMAT(SUM('Table'[Column1]),"#,##0.0%;(#,##0.0%)")
see pictures:
Formatting Numeric values as %
Adding a Measure for Formatting
Hope this helps.

Related

Numeric input in Power BI with min and max

I'm doing some custom R HTML visuals in Power BI. I can get a number input in Power BI by adding
"TestNumeric": {
"displayName": "Number",
"description": "test number",
"type": {
"numeric": true
}
}
in capabilities.json (and adapting src/settings.ts accordingly).
I would like to constrain this number input with a minimum and a maximum value. How can I do that?
I've found a solution. One has to modify the file src/visual.ts.
At the beginning, in the block of imports, add this import:
import VisualObjectInstanceEnumeration = powerbi.VisualObjectInstanceEnumeration;
Then, at the end, replace the function enumerateObjectInstances with:
public enumerateObjectInstances(options: EnumerateVisualObjectInstancesOptions): VisualObjectInstanceEnumeration {
var enumeratedObjects: VisualObjectInstanceEnumerationObject =
<VisualObjectInstanceEnumerationObject>VisualSettings.enumerateObjectInstances(
this.settings || VisualSettings.getDefault(), options
);
if (options.objectName === "YOUR_OBJECT_NAME") {
enumeratedObjects.instances[0].validValues = {
YOUR_PROPERTY_NAME: { numberRange: { min: 8, max: 20 } }
};
}
return enumeratedObjects;
}

Amcharts multi series line chart not displaying

I thought this would fairly straightforward.
I am trying to create a multi series stacked timeseries chart, at the moment I am unable to make it work with even one of the series series.
var rawdata = [{compoundInterest:100,
date:"2020-12-01",
investment:1000,
label:1,
lostToFees:0.25074911830097335,
returnOnInvestment:4.374847450990728
},
{
compoundInterest:104.83147148630928,
date:"2021-01-01",
investment:1100,
label:1,
lostToFees:0.5287042515940357,
returnOnInvestment:4.374847450990728
},
{
compoundInterest:105.30923247234422,
date:"2021-02-01",
investment:1200,
label:1,
lostToFees:0.8340920982343505,
returnOnInvestment:9.187179647080587
},
{
compoundInterest:105.8101368090525,
date:"2021-03-01",
investment:1300,
label:1,
lostToFees:1.167140792415239,
returnOnInvestment:14.436996588269672
},
{
compoundInterest:106.33619967408472,
date:"2021-04-01",
investment:1400,
label:1,
lostToFees:1.5280799122806457,
returnOnInvestment:20.124298274557844
},
{
compoundInterest:106.88944506118636,
date:"2021-05-01",
investment:1500,
label:1,
lostToFees:1.9171404880812588,
returnOnInvestment:26.24908470594496
},
{
compoundInterest:107.47190581876677,
date:"2021-06-01",
investment:1600,
label:1,
lostToFees:2.3345550103745154,
returnOnInvestment:32.81135588243117
},
{
compoundInterest:108.08562368863693,
date:"2021-07-01",
investment:1700,
label:1,
lostToFees:2.7805574382662144,
returnOnInvestment:39.81111180401638
},
{
compoundInterest:108.73264934491681,
date:"2021-08-01",
investment:1800,
label:1,
lostToFees:3.255383207697605,
returnOnInvestment:47.24835247070063
},
{
compoundInterest:109.41504243311283,
date:"2021-09-01",
investment:1900,
label:1,
lostToFees:3.759269239776131,
returnOnInvestment:55.123077882483805
},
{
compoundInterest:110.13487160936633,
date:"2021-10-01",
investment:2000,
label:1,
lostToFees:4.292453949148694,
returnOnInvestment:63.435288039366114
}]
am4core.ready(function() {
am4core.addLicense("xxxxxxxx");
var projectionChart = am4core.create("projectionchartdiv", am4charts.XYChart);
var chartData = rawdata;
for (i = 0; i < chartData.length; i++) {
chartData[i].date = new Date(chartData[i].date);
}
var projectionChart = am4core.create("profitabilitychartdiv", am4charts.XYChart);
projectionChart.data = chartData;
var dateAxis = projectionChart.xAxes.push(new am4charts.DateAxis());
// Create value axis
var valueAxis = projectionChart.yAxes.push(new am4charts.ValueAxis());
valueAxis.tooltip.disabled = true;
valueAxis.title.text = "$";
// Create series
var series = projectionChart.series.push(new am4charts.LineSeries());
series.dataFields.dateX = "date";
series.dataFields.valueY = "compoundInterest";
series.fillOpacity = 0.3;
});
Currently it just renders an SVG with empty g tags.
Can anyone point me in the direction of where I'm going wrong?
I am somewhat inexperienced with amcharts and I'm flumoxxed.
Thank You

Application Insights Data Limited Importing Into PowerBI

I'm attempting to import data from Azure Application Insights into PowerBI. The issue is that, regardless of the timespan I set, I seem to only be pulling about a week's worth of data. Here's what the M query looks like:
let AnalyticsQuery =
let Source = Json.Document(Web.Contents("https://api.applicationinsights.io/v1/apps/<uuid>/query",
[Query=[#"query"="customEvents
| project customDimensions
",#"x-ms-app"="AAPBI",#"timespan"="P30D"],Timeout=#duration(0,0,60,0)])),
TypeMap = #table(
{ "AnalyticsTypes", "Type" },
{
{ "string", Text.Type },
{ "int", Int32.Type },
{ "long", Int64.Type },
{ "real", Double.Type },
{ "timespan", Duration.Type },
{ "datetime", DateTimeZone.Type },
{ "bool", Logical.Type },
{ "guid", Text.Type },
{ "dynamic", Text.Type }
}),
DataTable = Source[tables]{0},
Columns = Table.FromRecords(DataTable[columns]),
ColumnsWithType = Table.Join(Columns, {"type"}, TypeMap , {"AnalyticsTypes"}),
Rows = Table.FromRows(DataTable[rows], Columns[name]),
Table = Table.TransformColumnTypes(Rows, Table.ToList(ColumnsWithType, (c) => { c{0}, c{3}}))
in
Table
in
AnalyticsQuery
I was thinking this was a size issue, but I've already narrowed it down to a single column (albeit a wide one) and it's still not returning any more data.
Narrowing the returned dataset to two columns has increased the dataset to include a few weeks instead of less than a week, but I'm still looking for a bigger dataset. Here's the latest query:
let AnalyticsQuery =
let Source = Json.Document(Web.Contents("https://api.applicationinsights.io/v1/apps/<uuid>/query",
[Query=[#"query"="customEvents
| extend d=parse_json(customDimensions)
| project timestamp, d[""Properties""]
| order by timestamp desc
| where timestamp <= now() and d_Properties <> """"
",#"x-ms-app"="AAPBI"],Timeout=#duration(0,0,4,0)])),
TypeMap = #table(
{ "AnalyticsTypes", "Type" },
{
{ "string", Text.Type },
{ "int", Int32.Type },
{ "long", Int64.Type },
{ "real", Double.Type },
{ "timespan", Duration.Type },
{ "datetime", DateTimeZone.Type },
{ "bool", Logical.Type },
{ "guid", Text.Type },
{ "dynamic", Text.Type }
}),
DataTable = Source[tables]{0},
Columns = Table.FromRecords(DataTable[columns]),
ColumnsWithType = Table.Join(Columns, {"type"}, TypeMap , {"AnalyticsTypes"}),
Rows = Table.FromRows(DataTable[rows], Columns[name]),
Table = Table.TransformColumnTypes(Rows, Table.ToList(ColumnsWithType, (c) => { c{0}, c{3}}))
in
Table,
#"Sorted Rows" = Table.Sort(AnalyticsQuery,{{"timestamp", Order.Ascending}})
in
#"Sorted Rows"
You should look into either table buffering or directquery: see this discussion

Swift Storing Appending multiple Dictionary into array

I want to store multiple dictionary into an array so that the final results looks like so
(
{
id: 12,
task : completed
},
{
id: 15,
task : error
},
{
id: 17,
task : pending
},
)
I tried with code below but it does not give me what I want Please can someone help me out. Thanks
var FinalTaskData = [[String:AnyObject]]()
for i in 0..<taskObj.count{
let dict = ["id":taskObj[i].id!,"task":taskObj[i].task!] as [String : AnyObject]
FinalTaskData.append(dict)
}
And this gives me the output of
(
{
id = 190;
},
{
task = "Task To Be Edited";
},
{
id = 191;
},
{
task = "Also To Be Edited";
}
)
Which is not what I want. Thanks

How do I hide values past the x-axis in chartjs 2.0?

How do I hide values past the x-axis in chartjs 2.0? You will notice the chart juts past the -60 mark. The x-axis uses a time scale and I have the max and min values set.
Here's my chart configuration:
{
"type":"line",
"data":{
"datasets":[
{
"label":"Scatter Dataset",
"data":[
{
"x":"2016-09-16T16:36:53Z",
"y":88.46153846153845
},
...
{
"x":"2016-09-16T16:37:54Z",
"y":88.3076923076923
}
],
"pointRadius":0,
"backgroundColor":"rgba(0,0,255,0.5)",
"borderColor":"rgba(0,0,255,0.7)"
}
]
},
"options":{
"title":{
"display":true,
"text":"Water Level Over Last 60 Seconds"
},
"animation":false,
"scales":{
"xAxes":[
{
"type":"time",
"position":"bottom",
"display":true,
"time":{
"max":"2016-09-16T16:37:54Z",
"min":"2016-09-16T16:36:54.000Z",
"unit":"second",
"unitStepSize":5
},
"ticks":{
callback: function(value, index, values) {
return "-" + (60 - 5 * index);
}
}
}
],
"yAxes":[
{
"display":true,
"ticks":{
}
}
]
},
"legend":{
"display":false
}
}
}
You can achieve this using Chart.js plugins. They let you handle events occuring while creating, updating or drawing the chart.
Here, you'll need to affect before the chart is initialised :
// We first create the plugin
var cleanOutPlugin = {
// We affect the `beforeInit` event
beforeInit: function(chart) {
// Replace `ticks.min` by `time.min` if it is a time-type chart
var min = chart.config.options.scales.xAxes[0].ticks.min;
// Same here with `ticks.max`
var max = chart.config.options.scales.xAxes[0].ticks.max;
var ticks = chart.config.data.labels;
var idxMin = ticks.indexOf(min);
var idxMax = ticks.indexOf(max);
// If one of the indexes doesn't exist, it is going to bug
// So we better stop the program until it goes further
if (idxMin == -1 || idxMax == -1)
return;
var data = chart.config.data.datasets[0].data;
// We remove the data and the labels that shouldn't be on the graph
data.splice(idxMax + 1, ticks.length - idxMax);
data.splice(0, idxMin);
ticks.splice(idxMax + 1, ticks.length - idxMax);
ticks.splice(0, idxMin);
}
};
// We now register the plugin to the chart's plugin service to activate it
Chart.pluginService.register(cleanOutPlugin);
The plugin is basically a loop through the data to remove the values that shouldn't be displayed.
You can see this plugin working in a live example on jsFiddle.
For instance, the following chat with a min set to 2 and a max to 6 ...
... would give the following result :