首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >chartjs-plugin-zoom:在页面首次加载时获取当前可见的值

chartjs-plugin-zoom:在页面首次加载时获取当前可见的值
EN

Stack Overflow用户
提问于 2021-08-16 19:33:55
回答 1查看 43关注 0票数 0

我有一个名为genStarts的图表组件,它的视图依赖于以下图表(singleChart)的可视数据。我使用onZoomComplete和onPanComplete选项来插入我的getVisibleValues函数,以便可以在缩放和平移时更新genStarts。但是,当页面第一次启动时,我无法获得当前可见的值。我的getVisibleValues函数不能在chartStyle对象之外工作,而且我还没有找到在启动时执行的图表选项。

换句话说,只有在缩放或平移SingleChart时,才会填充我的genStarts图,但我希望立即填充它。

代码语言:javascript
复制
const SingleChart = React.memo(({ setVisible }) => {

  function getVisibleValues({ chart }) {
    setVisible(chart.scales.x.ticks);
  }
  const chartStyle = {
    options: {
      animation: false,
      maintainAspectRatio: false,
      responsive: true,
      plugins: {
        zoom: {
          zoom: {
            wheel: {
              enabled: true,
              modifierKey: "shift",
            },
            pinch: {
              enabled: true,
            },
            enabled: true,
            drag: true,
            mode: "x",
            onZoomComplete: getVisibleValues,
          },
          pan: {
            enabled: true,
            mode: "x",
            speed: 2,
            onPanComplete: getVisibleValues,
          },
          mode: "xy",
        },
        legend: {
          display: false,
        },
      },

      scales: {
        y: {
          type: "linear",
          display: "true",
          position: "left",
          grid: {
            drawBorder: true,
            color: "#000000",
          },
          ticks: {
            beginAtZero: true,
            color: "#000000",
          },
          title: {
            display: yAxisLabel != "",
            text: yAxisLabel,
          },
        },

        x: {
          max: 9,
          grid: {
            drawBorder: true,
            color: "#00000",
          },
          ticks: {
            beginAtZero: false,
            color: "#000000",
          },
        },
      },
    },
  };

 // ... some code to get chartData

  return (
    <div>
      <Line
        data={chartData}
        options={chartStyle.options}
        width={20}
        height={195}
      />
    </div>
  );
});

export default SingleChart;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-17 01:13:23

你可以使用动画,它们有一个属性来检查它是否是第一次触发,尽管由于你不想要动画,你需要将main设置为一个非常低的数字,至少将工具提示设置为false,并将活动元素的过渡持续时间设置为0。

代码语言:javascript
复制
const getVisibleValues = ({
  chart
}) => {
  console.log(chart.scales.x.ticks.map(el => ({
    value: el.value,
    label: el.label
  }))) // Map because stack console prints whole circular context which takes long time
}

const options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 3, 5, 2, 3],
      borderColor: 'pink'
    }]
  },
  options: {
    animation: {
      onComplete: (ani) => {
        if (ani.initial) {
          getVisibleValues(ani)
        }
      },
      duration: 0.0001
    },
    transitions: {
      active: {
        animation: {
          duration: 0
        }
      }
    },
    plugins: {
      tooltip: {
        animation: false
      }
    }
  }
}

const ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
代码语言:javascript
复制
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.0/chart.js"></script>
</body>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68808328

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档