-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Support specifying a function for tickformat #1464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
No, this won't happen. The plotly.js Sorry for the inconvenience. |
@etpinard What are your ideas on something like the following? This would allow you to still support JSON, but allow people who need customization to specify the name of a function to reference. In my case, I'm looking to convert
|
@bputt I'm afraid that won't do hit. Guaranteeing JSON-serializability for our |
@etpinard I understand why you chose JSON and appreciate portability, just trying to think through a problem and finding solutions. Let's say I want to take bytes and make it human-readable, so if I provided ALL of the
|
@etpinard Well I am not sure if json compatibility is such a high value, that avoids a useful functionality (Infact it would not be a big deal to implement). But anyway I want to suggest a (coffeescript) solution for @bputt: # monkey patch d3 locale to support a new "milliseconds" number format
org_locale = Plotly.d3.locale
Plotly.d3.locale = (locale) ->
result = org_locale(locale)
org_number_format = result.numberFormat
twodigts = org_number_format("02d")
threedigts = org_number_format("03d")
result.numberFormat = (format) ->
return org_number_format(format) if format != "milliseconds"
return (miliseconds) ->
seconds = miliseconds // 1000
miliseconds %= 1000
minutes = seconds // 60
seconds %= 60
hours = minutes // 60
minutes %= 60
miliseconds = threedigts(miliseconds)
seconds = twodigts(seconds)
minutes = twodigts(minutes)
return "#{hours}:#{minutes}:#{seconds}#{locale.decimal}#{miliseconds}"\
if hours
return "#{minutes}:#{seconds}#{locale.decimal}#{miliseconds}"
# there is also result.timeFormat, that can be patched
return result If you set |
Sorry. JSON-compatibility is probably the most important thing about plotly.js. That's how as a company we extend plotly.js to python, R and many more languages, not to mention saving graphs in the cloud on https://plot.ly/. So yeah, there's a 0% chance of us breaking that assumption. |
Just a matter of thought: You will never reach full customization by a pure declarative model, but there will be always special cases, users want to implement. |
I've done this...a bit ugly, but solved the issue for me because I needed to add a suffix: // PATCH: extend d3.format to allow a string suffix
var oldFormat = Plotly.d3.format;
Plotly.d3.format = function(str) {
var data = str.split("###");
return function(val) {
return oldFormat(str)(val) + " " + data[1];
}
} Then, in the hoverformat, anything after "###" is treated as a suffix. This way, you can express the format like |
@etpinard While it's commendable that But nevertheless, working within that restriction, what about an API to customise the format function? Something not a part of For example, something like: const d3Format = require('d3-format');
Plotly.tickFormat = function tickFormat(specifier) {
if (specifier === 'specialSauce') { return specialSauceFormatter; }
return d3Format(specifier);
}; In theory this API could be used across all languages, if someone wanted to implement their |
@vdh Could I please ask if you might be able to provide more hints for how to get your suggestion to work? I tried to copy your code and then use this, but it did not work: var layout = {xaxis: {tickformat: 'specialSauce'}} The tick labels appear unchanged, as if |
Following in the footsteps of @kochelmonster, this is what worked for me: // Somewhere in the global scope, overwrite the original locale function:
var org_locale = Plotly.d3.locale;
Plotly.d3.locale = (locale) => {
var result = org_locale(locale);
var org_number_format = result.numberFormat;
result.numberFormat = (format) => {
if (format != 'pow2') {
return org_number_format(format)
}
return (x) => {
return Math.pow(2, Number.parseFloat(x)).toString();
}
}
return result;
}
// When specifying tickformat, now we can use the string we chose:
var layout = {xaxis: {tickformat: 'pow2'}} I wanted to convert the tick labels to be |
@slowkow It was only an API suggestion example, I didn't actually implement it. |
@slowkow Odd, that doesn't seem to have any effect for me. even adding a |
@slowkow d3.locale seems to be not a part of Plotly.js anymore. Neither is d3_locale_numberFormat. Can you suggest an alternate way to achieve the same result? |
Not being able to provide a custom formatter is a bit of a blocker: in our use case we need the ticks along the Y axis to automatically adjust to trillion/billion/million, and show the full word for the unit accordingly. Using @slowkow's example above works great with v1.58.5, but this means we'll most likely soon be stuck with an unsupported version. Could the |
Hi all, is there any chance this will be implemented? |
It would be useful if a function could be specified as the tickformat to give complete control.
For example, I would like to use the multiFormat function from https://github.com/d3/d3-time-format/blob/master/README.md to format dates.
The text was updated successfully, but these errors were encountered: