Formatting y-axis - d3.js

How can I format monetary values ​​of the y-axis of my graph bar to be so:
R$ 123.456,00
Instead of:
R$ 123,456.00
Currently I'm using this function to format, but can't make this simple change:
var format = d3.format(',.2f'); // Need to change this, but don't know how
chart.yAxis.tickFormat(function(d) {
return "R$ " + format(d);
});
I've already searched in D3 documentation, but can't find anything.

With d3 5.5 you can create a custom locale
https://github.com/d3/d3-format#formatLocale
Also note in the specifier(arg passed to .format) I now include a $ This will automatically include the currency prefix in the formatted string.
const customD3Locale = d3.formatLocale({
decimal: ",",
thousands: ".",
grouping: [3],
currency: ["R$",""]
})
const format = customD3Locale.format('$,.2f');

The format method don't seems to allow custom thousands and decimal separators. I think that you should replace the symbols yourself:
var format = d3.format(',.2f');
// Format the number, adding thousands and decimal separators
var label = format(1234.00);
// Replace the . and the , symbols. The ! symbol is necessary to do the swap
// it can be other symbol though
label = label.replace('.', '!');
label = label.replace(',', '.');
label = label.replace('!', ',');
// The result is 'R$ 1.234,00'
d3.select('#chart').append('p').text('R$ ' + label);
This jsfiddle have the replacement code.

Related

how to remove extra spaces in Power Query

I want to remove extra spaces from the text, i referenced the code from the internet as below:
(text as text)=>
let
x = Text.Split(text," "),
y = Text.Select(x,each _<>""),
z = Text.Combine(y," ")
in
z
when i apply this function for my data , it show the error is "Expression.Error: We cannot convert a value of type List to type Text." , my column is definitely is text format already , i don't know root of the issue, could you please help look ?
my data is very simple, like below:
You can use below code as a custom function:
(text as text, optional char_to_trim as text) =>
let
char = if char_to_trim = null then " " else char_to_trim,
split = Text.Split(text, char),
removeblanks = List.Select(split, each _ <> ""),
result=Text.Combine(removeblanks, char)
in
result

VBScript: How can I trim a number to 4 decimal places but not round it?

My code currently looks like this:
FormatNumber((CDbl(0.05935)),4)
The returned value is 0.0594 rather than 0.0593 which is what I need.
You can try parsing this number to string then trimming it and again parsing back to float.
example:
v = 100.0097
x = Str$(v) ' Gives " 100.0097"
//This adds a leading space for positive numbers
or
x = CStr(v) ' Gives "100.0097"
and then trim it as your need
finalstr = LEFT(variable, (LEN(variable)-4))
then parse it to float
finaltrimed = CDbl(finalstr)

d3 tickFormat, remove comma as thousands separator

In a d3 graph, we get comma as thousands separator as default and I would like to remove them and have the x-axis ticks displayed as for example "2 000 000" instead of "2,000,000"
This is what I've tried:
private scaleX: ScaleLinear<number, number>;
private xAxisComponent: any;
// Define an x Axis
const axisNumberFormat: FormatLocaleDefinition = {
'decimal': '.',
'thousands': ' ',
'grouping': [3],
'currency': ['', ''],
};
const axisFormatLocale: FormatLocaleObject = this.d3.formatDefaultLocale(axisNumberFormat);
this.xAxisComponent = this.d3.axisBottom(this.scaleX).tickFormat(axisFormatLocale);
However, the assignment on the last line does not work, it says "Argument of type 'FormatLocaleObject' is not assignable to parameter of type 'null'." Any tips? Or am I doing this the wrong way, is there another way to put together a format string to send to the tickFormat() method?
The last line will work if .format('') is added to axisFormatLocale:
this.xAxisComponent = this.d3.axisBottom(this.scaleX).tickFormat(axisFormatLocale.format(''));

Building a D3 table with json and comma delimited array

I'm trying to build an html table from a 2D array using a row for each array element and a cell for each string in the row. I get a cell for each character instead. I've tried some combinations for splitting the strings by comma, but haven't one that works. How do I get
onetwothree
http://jsfiddle.net/johnpoole/BfTWP/
var json_data = ["one,two,three","red,green,blue"];
var table = d3.select("body").append("table");
var rows = table.selectAll("tr").data(json_data).enter().append("tr");
rows.selectAll("td").data(function(d){return d;}).enter().append("td").text(function(d) {return d;});
The last line needs to return d.split(",") to break the string into an array. Otherwise, JS iterates through the characters in the string.
Alternatively, you could keep the code as is and change the data to:
var json_data = [["one","two","three"],["red","green","blue"]];
jsFiddle updated both ways.

Getting locale format for Windows

how can I get correct locale's format for Windows in Delphi ?
I trying to do next
LCID := 2048;
FormatSettings := TFormatSettings.Create(LCID);
but this doesn't work fine if set shortdate format as example '07-13\2012'.
and variable will be equal
FormatSettings = 'MM/dd\yyyy' ?????
You could use this?
var
formatSettings : TFormatSettings;
begin
// Furnish the locale format settings record
GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, formatSettings);
// And use it in the thread safe form of CurrToStrF
ShowMessage('1234.56 formats as = '+
CurrToStrF(1234.56, ffCurrency, 4, formatSettings));
end;
http://www.delphibasics.co.uk/RTL.asp?Name=GetLocaleFormatSettings
in fact you should consider date as:
TShortDateFormatParts = (sdfpPrefix, sdfpDatePart1, sdfpSplitter1, sdfpDatePart2, sdfpSplitter2, sdfpDatePart3, sdfpSuffix);
in your code you should:
Find and get everything before leading "d", or "M" or "Y" (prefix).
Find and get text before first splitter.
Find and get end of first splitter.
Find and get text before second splitter.
Find and get end of second splitter.
Find and get everything before final text (suffix).
Get what we have now is final part
after:
Get position of DAY, MONTH and YEAR in current format string
The first lines of TFormatSettings.Create(Locale) are:
if not IsValidLocale(Locale, LCID_INSTALLED) then
Locale := GetThreadLocale;
When I pass LOCALE_SYSTEM_DEFAULT (2048) as my locale, IsValidLocale returns false and GetThreadLocale returns 4105 (Canadian-English). You may want to investigate this further. Are you getting the locale that you expecting?
The correct format of the locale for countries
var
formatSettings : TFormatSettings;
begin
GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, formatSettings);
ShowMessage('LOCALE_SYSTEM_DEFAULT = ' + DateTimeToStr(now, formatSettings));
GetLocaleFormatSettings(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), formatSettings);
ShowMessage('LANG_ENGLISH = ' + DateTimeToStr(now, formatSettings));
GetLocaleFormatSettings(MAKELANGID(LANG_RUSSIAN, SUBLANG_DEFAULT), formatSettings);
ShowMessage('LANG_RUSSIAN = ' + DateTimeToStr(now, formatSettings));
end;

Resources