plouc / nivo

nivo provides a rich set of dataviz components, built on top of the awesome d3 and React libraries
https://nivo.rocks
MIT License
13.19k stars 1.03k forks source link

How to change color and size of label of the axis #132

Closed dortamiguel closed 6 years ago

dortamiguel commented 6 years ago

I want to use a dark background and increase the font for mobile.

How I can do that?

image

plouc commented 6 years ago

@ellipticaldoor, you can use the theme property, I've created an example of it.

dortamiguel commented 6 years ago

Awesome! thank you very much.

cameronaziz commented 6 years ago

@plouc Thanks, I cant seem to find the documentation on the theme prop

plouc commented 6 years ago

@cameronaziz, you're right, it's missing, the TypeScript definition might help to figure out what's available.

caribouflex commented 5 years ago

Hi guy's, it seems this theme property doesn't work [anymore]. And it doesn't appear to be documented. Is it still available to do that ? I tried on the codesandbox that @plouc forwarded, by updated the dependencies. Thanks for the help.

[EDIT]

Sorry I commented too fast, got my answer in the #311

cbfrance commented 5 years ago

As of at least 0.58.0 it looks like there is a default theme in node_modules/@nivo/core/dist/nivo-core.cjs.js — I pulled it out and am starting to use it as the basis for my theming. Here's a snapshot of how it looks today (it's probably under active development and likely to change.)

Not sure yet how this interacts with each of the components, but figured I'd share it since some of the googleable docs are out of date.

export default {
  background: 'transparent',
  fontFamily: 'sans-serif',
  fontSize: 11,
  textColor: '#333333',
  axis: {
    domain: {
      line: {
        stroke: 'transparent',
        strokeWidth: 1
      }
    },
    ticks: {
      line: {
        stroke: '#777777',
        strokeWidth: 1
      },
      text: {}
    },
    legend: {
      text: {
        fontSize: 12
      }
    }
  },
  grid: {
    line: {
      stroke: '#dddddd',
      strokeWidth: 1
    }
  },
  legends: {
    text: {
      fill: '#333333'
    }
  },
  labels: {
    text: {}
  },
  markers: {
    lineColor: '#000000',
    lineStrokeWidth: 1,
    text: {}
  },
  dots: {
    text: {}
  },
  tooltip: {
    container: {
      background: 'white',
      color: 'inherit',
      fontSize: 'inherit',
      borderRadius: '2px',
      boxShadow: '0 1px 2px rgba(0, 0, 0, 0.25)',
      padding: '5px 9px'
    },
    basic: {
      whiteSpace: 'pre',
      display: 'flex',
      alignItems: 'center'
    },
    table: {},
    tableCell: {
      padding: '3px 5px'
    }
  },
  crosshair: {
    line: {
      stroke: '#000000',
      strokeWidth: 1,
      strokeOpacity: 0.75,
      strokeDasharray: '6 6'
    }
  },
  annotations: {
    text: {
      fontSize: 13,
      outlineWidth: 2,
      outlineColor: '#ffffff'
    },
    link: {
      stroke: '#000000',
      strokeWidth: 1,
      outlineWidth: 2,
      outlineColor: '#ffffff'
    },
    outline: {
      fill: 'none',
      stroke: '#000000',
      strokeWidth: 2,
      outlineWidth: 2,
      outlineColor: '#ffffff'
    },
    symbol: {
      fill: '#000000',
      outlineWidth: 2,
      outlineColor: '#ffffff'
    }
  }
}
Caree12 commented 5 years ago

Hi Guys,

On bar verison "@nivo/bar": "^0.59.2" and line version "@nivo/line": "^0.59.3" I was having no luck updating the font size of the axis. The below worked for me! Hope this helps someone else!

const lineGraphSettings = { theme: { fontSize: '14px', textColor: 'blue', }, };

<ResponsiveBar animate={lineGraphSettings.animate == false ? lineGraphSettings.animate : true} axisTop={null} theme={lineGraphSettings.theme} />

jessikadg commented 3 years ago

To change fontSize all over the chart, the following code worked for me:

<ResponsiveLine 
    theme={{
    fontSize: 16,
   }}
/>
Gungrave223 commented 3 years ago

So setting via theme={{}} set's the font size globally for the chart....is it possible to set the font size for just the y axis?

wyze commented 3 years ago

@Gungrave223 it is not currently possible to adjust the theme per axis, only for all axes.

Gungrave223 commented 3 years ago

@wyze Thanks for the response....

For anyone looking to do something similar... The solution is to use the axis format function to wrap the value in a <tspan> and add the styling directly to the <tspan>.

ShaviniM commented 3 years ago

how to add bold letters in nivo charts?

nastystanevich commented 2 years ago

properties axisBottom, axisLeft have ability to recieve formatting function for axis values. Here there's example of adding $ to the beginning.

axisLeft={{
    format: (value) => `$${value}`
}}

As well you can return value wrapped into <tspan> and pass to this tag all necessary styles.

alissaVrk commented 1 year ago

@Gungrave223 Unfortunately it doesn't seem to work anymore format: (value) => <tspan>{yAxisValue(value as number)}</tspan>, gives [object Object]

nastystanevich commented 1 year ago

@alissaVrk it still works for me, check your yAxisValue may be it returns [object Object].

In my code it looks this way:

import { ResponsiveLine } from "@nivo/line";
<ResponsiveLine 
...
axisLeft={{
  format: (value: number) => <tspan style={{ fill: green[600], fontWeight: 600 }}>{value}</tspan>
}}
/>
zsteiner commented 1 year ago

I am running into a similar issue using tspan in the format function. My format functions work when returning a string, but cannot return JSX/SVG. They render [object Object] string.

@nastystanevich Are you using the most current version (v0.83.0)? It's possible that this method was broken by a more recent update?

alissaVrk commented 1 year ago

format: (value) => <tspan>aaaaa</tspan> also doesn't work

nastystanevich commented 1 year ago

@zsteiner you're right, the issue's appeared in some latest patches, I had v0.80.0 and as soon as I installed v0.83.0 I observed the same issue with returning value.

SandyGifford commented 1 year ago

Confirmed that downgrading to 0.80.0 fixes the [object Object] problem. Looks like this change was made about 2 months ago and was released going from 81 to 82.

Kartstig commented 1 year ago

I am running into a similar issue using tspan in the format function. My format functions work when returning a string, but cannot return JSX/SVG. They render [object Object] string.

@nastystanevich Are you using the most current version (v0.83.0)? It's possible that this method was broken by a more recent update?

I am also seeing this on 0.83.0

evnp commented 1 year ago

Another report, seeing this behavior on 0.83.0, [object Object] when we try to return JSX from the axisLeft format function. Downgrading to 0.80.0 has fixed the issue for now.

JoeShep commented 1 year ago

Encountered the [object Object] problem as well. Really need to use a format method in a component library so each consuming app can define how to format its X-axis labels. Downgrading would be, well, a downgrade. Hoping for a more robust fix

plouc commented 1 year ago

format is strictly for formatting now, if you wish to change the rendering, please use renderTick.

nastystanevich commented 11 months ago

@plouc, thanks for remind us about renderTick, it solved the problem with formatting in latest version. Here is an example of solution:

<ResponsiveBar
  ...
  axisBottom={{
         ...
          renderTick: ({ x, y,  textAnchor, textX, textY, value }) => (
            <g transform={`translate(${x},${y})`}>
              <text
                textAnchor={textAnchor}
                transform={`translate(${textX},${textY + 5})`} // places label 5px lower than origin place
                style={{ fontSize: 8px }}
              >
                {value}
              </text>
            </g>
          )
        }}
  ...
/>
fabiojonathanarifin commented 10 months ago

this solution works for me.

theme={{ text: { fill: "#ffffff" }, }}

ThurberISEC commented 10 months ago

As of 17 January, 2024: In case anyone still wonders, here's how to change the legend text on the axes: theme={{axis: {legend: {text: {fontSize: 20, fontWeight: 700, fill: "#0000ff"}}}}} Substitute your size, weight, color, etc. It changes for both axes. I don't know how to change one axis at a time.