简体   繁体   English

如何在 react-chartjs-2 中获取动态步长

[英]how to get dynamic step size in react-chartjs-2

I am using Chart.js to draw graphs in typescript.我正在使用Chart.js在 typescript 中绘制图形。 I want to get a dynamic weight and bring a minimum and a maximum .我想获得动态权重并带来minimummaximum And with maxTicksLimit as 5 , I want to keep 5 Ticks no matter what data comes in. The decimal point of body weight is taken to the first decimal point.并且 maxTicksLimit 为5 ,无论输入什么数据,我都想保留5 Ticks。体重的小数点被带到第一个小数点。

ex) 50.3例如)50.3

I want to show the difference between minimum and maximum as much as possible.我想尽可能地显示minimummaximum之间的差异。

please help me!!!请帮我!!!

ex1) maximum weight: 74.5, minimum weight: 71 ex1) 最大重量:74.5,最小重量:71

result结果
Y Axis maximum weight: 76, Y Axis minimum weight: 71 Y轴最大重量:76,Y轴最小重量:71

ex1 result image ex1 结果图片

enter image description here在此处输入图像描述

ex2) maximum weight: 76.9, minimum weight: 62 ex2) 最大重量:76.9,最小重量:62

result结果
Y Axis maximum weight: 76, Y Axis minimum weight: 61 Y轴最大重量:76,Y轴最小重量:61

ex2 result image ex2 结果图片

enter image description here在此处输入图像描述

I solved this problem in the following way.我通过以下方式解决了这个问题。

There was a callback function of afterBuildTicks in chart.js在chart.js中有一个afterBuildTicks的回调afterBuildTicks

chart.js documents link . chart.js 文件链接

It becomes possible to customize the ticks.可以自定义刻度。

//... middle code skip
const getChartMaxAndMin = (
    maxValue,
    minValue
): { max: number; min: number } => {
    // Convert all decimal places to integers
    let max = Math.ceil(maxValue);
    let min = Math.floor(minValue);
    // a multiple of 5
    const MULTIPLES = 5;
    const DIVIDED_REMAINING_VALUE_LIMIT = 3;
    // Maximum to Minimum difference
    const diff = max - min;
    const diffDividedRemainingValue = diff % MULTIPLES;

    const remainingValue =
        MULTIPLES *
            (diffDividedRemainingValue > DIVIDED_REMAINING_VALUE_LIMIT
                ? Math.floor(diff / MULTIPLES) + 2
                : Math.floor(diff / MULTIPLES) + 1) -
        diff;

    if (remainingValue % 2 !== 0) {
        max = max + Math.floor(remainingValue / 2) * 2;
        min = min - Math.floor(remainingValue % 2);
        return { max, min };
    }

    max = max + remainingValue / 2;
    min = min - remainingValue / 2;
    return { max, min };
};

const customizedAxesTicks = (axis) => {
    const EQUAL_PARTS = 5; // set 5 parts
    const max = axis.max;
    const min = axis.min;
    const steps = (max - min) / EQUAL_PARTS;
    const ticks = [];

    for (let i = min; i <= max; i += steps) {
        ticks.push(i);
    }
    axis.ticks = ticks;
    return;
};

const {max, min} = getChartMaxAndMin(68,57);

const chartOptions = {
  //... code skip
  scales: {
    yAxes: [
      {
        //... code skip
        ticks: {
          max: max,
          min: min
  },
  afterBuildTicks: customizedAxesTicks,
  //... code skip
};

I would appreciate it if you could let me know if there is a better way如果您能告诉我是否有更好的方法,我将不胜感激

The beforeBuildTicks callback also works to create a dynamic tick step size. beforeBuildTicks 回调也可用于创建动态刻度步长。 Here is the code:这是代码:

{
    maintainAspectRatio: false,
    tooltips: {
        mode: 'label',
        position: 'nearest',
    },
    scales: {
        xAxes:[
            {...}
        ],
        yAxes: [
            {
                position: 'left',
                id: 'y-axis-0',
                scaleLabel: {
                    display: true,
                    labelString: 'label string goes here',
                    fontSize: 16,
                },
                ticks: {
                    beginAtZero: true,
                    stepSize: .25,
                },
                beforeBuildTicks: function(axis) {
                    if (axis.max >= 17) {
                        axis.options.ticks.stepSize = 1;
                    } else if (axis.max >= 10) {
                        axis.options.ticks.stepSize = .5;
                    }
                }
            }
        ]
    }
}

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

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