反应本地-SVG-charts x日期轴所有标签在同一位置

发布于 2025-01-23 14:11:43 字数 2899 浏览 2 评论 0原文

我尝试了图表的不同设置,但无法正确显示X轴日期的标签。所有标签都显示在同一位置,即使日期有不同,图表也很好地显示:

“在此处输入图像说明”

这是我的代码:

import { View } from 'react-native';
import {
  Grid,
  AreaChart,
  XAxis,
  YAxis,
  AxisProps,
} from 'react-native-svg-charts';
import { subYears, format } from 'date-fns';
import * as scale from 'd3-scale';
import { useTheme } from 'styled-components/native';

const PerformanceChart = () => {
  const theme = useTheme();

  const data = [
    { at: subYears(new Date(), 8), value: 85, open: 10, close: 20 },
    { at: subYears(new Date(), 7), value: 91, open: 10, close: 20 },
    { at: subYears(new Date(), 6), value: 35, open: 10, close: 20 },
    { at: subYears(new Date(), 5), value: 53, open: 10, close: 20 },
    { at: subYears(new Date(), 4), value: 53, open: 10, close: 20 },
    { at: subYears(new Date(), 3), value: 24, open: 10, close: 20 },
    { at: subYears(new Date(), 2), value: 50, open: 10, close: 20 },
    { at: subYears(new Date(), 1), value: 20, open: 10, close: 20 },
    { at: new Date(), value: 80, open: 10, close: 20 },
  ];

  const axesSvg: AxisProps<{
    at: Date;
    value: number;
    open: number;
    close: number;
  }>['svg'] = {
    fontSize: 11,
    fontWeight: '100',
    stroke: theme.colors.neutral[300],
  };
  const verticalContentInset = { top: 10, bottom: 20 };
  const xAxisHeight = 30;

  return (
    <View style={{ flex: 3, padding: 20, flexDirection: 'row' }}>
      <View style={{ flex: 1, marginRight: 10 }}>
        <AreaChart
          style={{ flex: 1 }}
          data={data}
          xAccessor={({ item }) => item.at.getTime()}
          yAccessor={({ item }) => item.value}
          xScale={scale.scaleTime}
          contentInset={verticalContentInset}
          svg={{ stroke: 'rgb(51, 160, 255)', fill: 'rgba(51, 160, 255, 0.2)' }}
        >
          <Grid svg={{ stroke: theme.colors.neutral[500] }} />
        </AreaChart>
        <XAxis
          data={data}
          xAccessor={({ item }) => item.at}
          style={{ height: xAxisHeight, marginTop: 11 }}
          formatLabel={(value) => format(value as Date, 'yyyy')}
          contentInset={{ left: 10, right: 10 }}
          svg={{ ...axesSvg, translateX: 15 }}
        />
      </View>
      <YAxis
        data={data}
        yAccessor={({ item }) => item.value}
        style={{ marginBottom: xAxisHeight }}
        contentInset={verticalContentInset}
        svg={{ ...axesSvg, translateX: 8 }}
      />
    </View>
  );
};

export default PerformanceChart;

这是我的最后尝试(设置xaccessor> AREACHART使用GetTime,它也没有工作。那对我工作。

I have tried different settings for the chart, but can't make the labels for the X axis dates show correctly. All labels are displayed on the same place, even if there are different dates and the chart displays well:

enter image description here

Here is my code:

import { View } from 'react-native';
import {
  Grid,
  AreaChart,
  XAxis,
  YAxis,
  AxisProps,
} from 'react-native-svg-charts';
import { subYears, format } from 'date-fns';
import * as scale from 'd3-scale';
import { useTheme } from 'styled-components/native';

const PerformanceChart = () => {
  const theme = useTheme();

  const data = [
    { at: subYears(new Date(), 8), value: 85, open: 10, close: 20 },
    { at: subYears(new Date(), 7), value: 91, open: 10, close: 20 },
    { at: subYears(new Date(), 6), value: 35, open: 10, close: 20 },
    { at: subYears(new Date(), 5), value: 53, open: 10, close: 20 },
    { at: subYears(new Date(), 4), value: 53, open: 10, close: 20 },
    { at: subYears(new Date(), 3), value: 24, open: 10, close: 20 },
    { at: subYears(new Date(), 2), value: 50, open: 10, close: 20 },
    { at: subYears(new Date(), 1), value: 20, open: 10, close: 20 },
    { at: new Date(), value: 80, open: 10, close: 20 },
  ];

  const axesSvg: AxisProps<{
    at: Date;
    value: number;
    open: number;
    close: number;
  }>['svg'] = {
    fontSize: 11,
    fontWeight: '100',
    stroke: theme.colors.neutral[300],
  };
  const verticalContentInset = { top: 10, bottom: 20 };
  const xAxisHeight = 30;

  return (
    <View style={{ flex: 3, padding: 20, flexDirection: 'row' }}>
      <View style={{ flex: 1, marginRight: 10 }}>
        <AreaChart
          style={{ flex: 1 }}
          data={data}
          xAccessor={({ item }) => item.at.getTime()}
          yAccessor={({ item }) => item.value}
          xScale={scale.scaleTime}
          contentInset={verticalContentInset}
          svg={{ stroke: 'rgb(51, 160, 255)', fill: 'rgba(51, 160, 255, 0.2)' }}
        >
          <Grid svg={{ stroke: theme.colors.neutral[500] }} />
        </AreaChart>
        <XAxis
          data={data}
          xAccessor={({ item }) => item.at}
          style={{ height: xAxisHeight, marginTop: 11 }}
          formatLabel={(value) => format(value as Date, 'yyyy')}
          contentInset={{ left: 10, right: 10 }}
          svg={{ ...axesSvg, translateX: 15 }}
        />
      </View>
      <YAxis
        data={data}
        yAccessor={({ item }) => item.value}
        style={{ marginBottom: xAxisHeight }}
        contentInset={verticalContentInset}
        svg={{ ...axesSvg, translateX: 8 }}
      />
    </View>
  );
};

export default PerformanceChart;

this was my last try (setting the xAccessor of the AreaChart using the getTime function. Without this, it didn't work as well. Any ideas? Tried to look at other similar questions and couldn't find a solution that work for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

才能让你更想念 2025-01-30 14:11:43

对于任何类似“怪异”问题的人,我都发现了我的问题。我尚未安装react-native-svg库,这就是为什么我遇到奇怪的问题,尽管在React Native调试器上根本没有错误。

For anyone having similar "weird" problems like this, I found what was my problem. I haven't installed the react-native-svg library and that's why I was having weird issues, although there were no errors at all on React Native Debugger.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文