JohnCoene / echarts4r

🐳 ECharts 5 for R
http://echarts4r.john-coene.com/
Other
587 stars 82 forks source link

Can't seem to modify Axis Labels (visible tick values) #518

Closed DeepanshKhurana closed 6 months ago

DeepanshKhurana commented 1 year ago

Hello 👋🏼

I have a plot that is based on currencies. I have a custom function to shorten prices based on Indian denominations. All I'm trying to do is achieve something similar to what is shown in the eCharts Documentation here.

// Use string template; template variable is the default label of axis {value}
formatter: '{value} kg'
// Use callback.
formatter: function (value, index) {
    return value + 'kg';
}

But I cannot seem to plug it in correctly (or maybe it is not supported).

For reference, here is a gist with the function I'm hoping to use.

Assuming there are two columns, Account and Balance, where Balance is numeric, I can simply create a bar chart using the following snippet.

data |>
  e_charts(Account) |>
  e_bar(Balance)

Note: The values are random here

Screenshot 2023-04-22 at 1 25 50 PM

The behaviour I am talking about is to be able to apply any function on the y-axis labels to display.

Something akin to the following,

data |>
  e_charts(Account) |>
  e_bar(Balance) |>
  e_y_axis_labels(shorten_price(Balance))

which would give me a plot where the y-axis is processed (the screenshot below is edited with Photoshop for illustration)

Screenshot 2023-04-22 at 1 30 09 PM

Is something like this possible or in the pipeline?

DeepanshKhurana commented 1 year ago

I think I missed checking the locale parameter. Part of my problem gets solved here, since the commas are visible now!

data |>
  e_charts(Account) |>
  e_bar(Balance) |>
  e_y_axis(
          formatter = e_axis_formatter(
            style = "currency",
            currency = "INR",
            local = "en-IN")
  )

However, it would still be really neat just to be able to modify the tick labels however we want, especially using a wrapper function.

sactyr commented 9 months ago

Hope this is what you're after @DeepanshKhurana. I have added JS wrapper function (there is probably a better way to write this wrapper function, my JS knowledge is very limited). Feel free to update the wrapper function to include other aesthetics you might wish (commas, dollar signs etc as per your locale requirement).

set.seed(123)

df <- data.frame(
  Account = LETTERS[1:6]
  ,Balance = rnorm(6, mean = 1000000, sd = 500000)
)

shorten_currency <- 'function (value) {

val = Math.round(value)/10000

val = val + "L"

return val

}'

df %>% 
  e_charts(Account) %>% 
  e_bar(Balance) %>% 
  e_y_axis(
    axisLabel = list(
      show = TRUE
      ,formatter = htmlwidgets::JS(shorten_currency)
    )
  )

image

DeepanshKhurana commented 8 months ago

@sactyr Thank you for this. This sets me up in a good direction. 💯