最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to reach Chart.helpers.color in a React context with chart.js 3.0.0? - Stack Overflow

programmeradmin3浏览0评论

I've managed to figure out how to import and register models for the new chart.js 3.0.0 in a React app, but I'm at a loss as to how to get Chart.helpers.color to be used. Here are my imports and register:

import React from 'react';
import { Chart, Tooltip, CategoryScale, LinearScale, Title } from 'chart.js';
import { Matrix, MatrixController } from 'chartjs-chart-matrix';

Chart.register(Tooltip, CategoryScale, LinearScale, Title, Matrix, MatrixController);

And yet, when I call Chart.helpers.color, as below:

        datasets: [{
            label: 'My Matrix',
            data: chartData,
            backgroundColor(context) {
                const value = context.dataset.data[context.dataIndex].v;
                const alpha = Math.log10(value) / 4;
                    return Chart.helpers.color('green').alpha(alpha).rgbString();
            },
            ...

the app throws TypeError: chart_js__WEBPACK_IMPORTED_MODULE_1__.Chart.helpers is undefined

Anyone know how I can get this React app to reach Chart.helpers? Thanks! (Or, of course, an alternative would be wele! This is for plotting a heat map using alpha to represent value.)

I've managed to figure out how to import and register models for the new chart.js 3.0.0 in a React app, but I'm at a loss as to how to get Chart.helpers.color to be used. Here are my imports and register:

import React from 'react';
import { Chart, Tooltip, CategoryScale, LinearScale, Title } from 'chart.js';
import { Matrix, MatrixController } from 'chartjs-chart-matrix';

Chart.register(Tooltip, CategoryScale, LinearScale, Title, Matrix, MatrixController);

And yet, when I call Chart.helpers.color, as below:

        datasets: [{
            label: 'My Matrix',
            data: chartData,
            backgroundColor(context) {
                const value = context.dataset.data[context.dataIndex].v;
                const alpha = Math.log10(value) / 4;
                    return Chart.helpers.color('green').alpha(alpha).rgbString();
            },
            ...

the app throws TypeError: chart_js__WEBPACK_IMPORTED_MODULE_1__.Chart.helpers is undefined

Anyone know how I can get this React app to reach Chart.helpers? Thanks! (Or, of course, an alternative would be wele! This is for plotting a heat map using alpha to represent value.)

Share Improve this question asked Dec 15, 2020 at 22:03 Sam HokinSam Hokin 7201 gold badge7 silver badges16 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 16

I figured this out, and I'm going to post up the answer because this has taken me days to figure out. I found a blurb on the chart.js repo about it here:

https://github./chartjs/Chart.js/blob/master/docs/docs/developers/publishing.md

So in my example I have at the top:

import React from 'react';
import { Chart, Tooltip, CategoryScale, LinearScale, Title } from 'chart.js';
import { color } from 'chart.js/helpers';
import { Matrix, MatrixController } from 'chartjs-chart-matrix';
Chart.register(Tooltip, CategoryScale, LinearScale, Title, Matrix, MatrixController);

and then I access Chart.helpers.color this way:

return color('green').alpha(alpha).rgbString();
发布评论

评论列表(0)

  1. 暂无评论