简体   繁体   English

如何更改 React Recharts 库 X 轴/Y 轴刻度的字体系列?

[英]How to change font family for React Recharts library X-axis / Y-axis ticks?

My task is fairly simple: all I want to do is change the font-family to Roboto, sans-serif for the x-axis and y-axis "ticks" (ie. the values along both axes).我的任务相当简单:我想要做的就是将 x 轴和 y 轴“刻度”(即沿两个轴的值)的字体系列更改为Roboto, sans-serif

I can change the font size of the x-axis tick with: <XAxis tick={{ fontSize: 'sans-serif!important' }} />我可以更改 x 轴刻度的字体大小: <XAxis tick={{ fontSize: 'sans-serif!important' }} />

But this doesn't work: <XAxis tick={{ fontFamily: 'sans-serif' }} />但这不起作用: <XAxis tick={{ fontFamily: 'sans-serif' }} />

The only other option I can see is to use the tickFormatter property specified in the documentation which only takes a function, see here: http://recharts.org/#/en-US/api/XAxis我能看到的唯一其他选项是使用文档中指定的tickFormatter属性,该属性仅采用一个函数,请参见此处: http : tickFormatter

In this project we are using the functional-style of programming in React using the Recompose utility library so we are rendering JSX in the "pure" function style and not with normal React classes.在这个项目中,我们使用 Recompose 实用程序库在 React 中使用函数式编程,因此我们以“纯”函数风格而不是普通的 React 类来渲染 JSX。

This is the best guess I could come up with is:这是我能想到的最好的猜测是:

const xAxisTickFormatter = name => {
  return <div style={{ fontFamily: 'sans-serif' }}>{name}</div>;
};

export const LineChart = ({ data, isMobile }) => {
  console.log(data);
  return (
    <C
      width={isMobile ? 400 : 650}
      height={400}
      data={data}
      margin={{ top: 5, right: 20, left: 10, bottom: 5 }}
    >
      <XAxis
        tickFormatter={xAxisTickFormatter}
        dataKey="date"
        name="Date"
        style={{ fontFamily: 'sans-serif' }}
      />
      <YAxis />
      <CartesianGrid strokeDasharray="3 3" />
      <Tooltip wrapperStyle={{ fontFamily: 'Roboto, sans-serif' }} />
      <Legend
        wrapperStyle={{ fontFamily: 'Roboto, sans-serif' }}
        verticalAlign="bottom"
        height={36}
      />
      <Line
        type="linear"
        dataKey="price"
        name="Price"
        stroke="#8884d8"
        activeDot={{ r: 8 }}
      />
      <Line
        type="linear"
        dataKey="marketAverage"
        name="Market Average"
        stroke="#82ca9d"
      />
    </C>
  );
};

This outputs [object, Object] along the x-axis, see screenshot:这将沿 x 轴输出 [object, Object],请参见屏幕截图:

在此处输入图片说明

The only similar official example from the docs uses the old createClass syntax here: https://jsfiddle.net/alidingling/9y9zrpjp/文档中唯一类似的官方示例在此处使用旧的createClass语法: https : createClass

Any help would be greatly appreciated as I have googled as many similar problems as possible and spent about half a day on this problem so far.任何帮助将不胜感激,因为我已经搜索了尽可能多的类似问题,并且到目前为止在这个问题上花了大约半天时间。

Why don't just set the font family and whatever else you want to overwrite via CSS?为什么不只设置字体系列以及您想通过 CSS 覆盖的任何其他内容?

We have something similar:我们有类似的东西:

.recharts-cartesian-axis-tick {    
    font-size: 1.4rem;
    font-family: Roboto, sans-serif;
}

The style attribute works fine for me; style属性对我来说很好用; in recharts 2.0.0-beta.1 at least.至少在 recharts 2.0.0-beta.1 中。

<XAxis
    dataKey="date"
    style={{
        fontSize: '1rem',
        fontFamily: 'Times New Roman',
    }}
/>
<YAxis
    style={{
        fontSize: '0.8rem',
        fontFamily: 'Arial',
    }}
/>

使用样式属性的轴中的字体大小和系列

Your tickFormatter function should return just a string, not a React Element.你的tickFormatter函数应该只返回一个字符串,而不是一个 React 元素。

CSS is the best way to go. CSS 是最好的方法。 However, there is at least one scenario where you will want to insert the font-family directly.但是,至少在一种情况下您需要直接插入字体系列。 When trying to convert your Recharts Component from SVG to PNG/JPEG, your CSS will not render the font you set.当尝试将您的 Recharts 组件从 SVG 转换为 PNG/JPEG 时,您的 CSS 将不会呈现您设置的字体。

NOTE: Your Recharts is rendered as an SVG on the screen.注意:您的 Recharts 在屏幕上呈现为 SVG。

In my CSS, I had 'Roboto' as my font (first image) and when I converted to PNG, my font rendered as 'Times New Roman' (second image)在我的 CSS 中,我将“Roboto”作为我的字体(第一张图片),当我转换为 PNG 时,我的字体呈现为“Times New Roman”(第二张图片)

在此处输入图片说明 在此处输入图片说明

To solve this, do this:要解决此问题,请执行以下操作:

  <XAxis
    fontFamily={'Roboto, sans-serif'}
  />
  <YAxis 
    fontFamily={'Roboto, sans-serif'}
  />

Just add className property in parent class like in your case只需在父类中添加 className 属性,就像您的情况一样

<C className="fontName">

and in css并在 css 中

.fontName {
  font: normal normal 900 9px/12px Avenir; // font name 
  letter-spacing: -0.29px;
}

I had a hard time trying to change the color of ticks of axes.我很难尝试更改轴刻度的颜色。 Actually, I had a conflict between "tick:{color...}" and "stroke" which both impact it.实际上,我在“tick:{color...}”和“stroke”之间存在冲突,这两者都会影响它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM