question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to format legend labels within a Calendar?

See original GitHub issue

Hi! I’ve searched the docs, storybook and this repo to find a way to do it but I was not successful.

I need to format legend labels within a Calendar. Currently, my raw values range from 0 to ~70,000.

image

I’d like to format these values as follows:

image

Is there anyway to do it?

I’ve tried to pass a function inside the legend object, but it didn’t work:

    // ...
    legends: [
      {
        // ...
        legendFormat: ({ value }) => myFormatter(value),
        // ...
      },
    ],

Thanks! 😃

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:8
  • Comments:5

github_iconTop GitHub Comments

1reaction
rovaniemicommented, Jan 11, 2020

I also needed this feature, but the library does not support it yet. However, this feature can be used with a little workaround, but we should do a PR if we want to use it the proper way.

Workaround

@nivo/calendar uses colorScale function when it does create legends.

const legendData = colorScale.ticks(legend.itemCount).map(value => ({
  id: value,
  label: value,
  color: colorScale(value),
}))

We can pass new colorScale function on @nivo/calendar props

import { scaleQuantize } from 'd3-scale'

const computeDomain = (data, minSpec, maxSpec) => {
  const allValues = data.map(d => d.value)
  const minValue = minSpec === 'auto' ? Math.min(...allValues) : minSpec
  const maxValue = maxSpec === 'auto' ? Math.max(...allValues) : maxSpec
  return [minValue, maxValue]
}

const createColorScale = (data, minValue, maxValue, colors) => {
  const domain = computeDomain(data, minValue, maxValue)
  const defaultColorScale = scaleQuantize()
      .domain(domain)
      .range(colors)
  const colorScale = value => {
      const number = prettyIntegerToNumber(value) // Formatted number back to number
      return defaultColorScale(number)
  }
  colorScale.ticks = count => {
     return defaultColorScale.ticks(count).map(e => prettyInteger(e)) // Format number
  }
  return colorScale
}
0reactions
stale[bot]commented, Nov 5, 2020

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add and format a chart legend - Microsoft Support
Right-click the legend, and then click Format Legend. · In the Format Legend task pane, pick the options that you want. Format Legend...
Read more >
What's a Calendar Legend? | Keep&Share Support
To set up a Calendar Legend label for your Calendar, first, click on the “Customize” button in the blue bar above your Calendar....
Read more >
How to Label a Legend in Google Sheets - YouTube
In this video, I show how to label a legend in Google Sheets. I cover how to add labels to legends and how...
Read more >
Use color labels to track calendar entries - Google Support
From the Time Insights panel: · Create a new color label: At the bottom left, click Add a new color label . ·...
Read more >
How to edit legend labels in google spreadsheet plots?
I had the same question this year (2021), and it looks like there's support for it now. In ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found