question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use chartjs-chart-financial

See original GitHub issue

Describe the bug How to use an external chart, like chartjs-chart-financial? How to register the ‘plugin’ with Chartjs?

I tried this:

import { OhlcElement, OhlcController, CandlestickElement, CandlestickController } from 'chartjs-chart-financial'
import Chart from 'chart.js/auto' // Easy way of importing everything

Chart.register(OhlcElement, OhlcController, CandlestickElement, CandlestickController)

However, I get a TypeError: Cannot read property 'register' of undefined.

How should I do this?

Versions

  • NodeJS version: 14.18.1
  • Chart.JS version: 3.6.0
  • Typescript version: 4.4.4

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
mercteilcommented, Feb 20, 2022

I managed it to run properly with everything.

I will follow up shortly with a working example posting here. Thank you for looking into it.

1reaction
SeanSobeycommented, Feb 20, 2022

Hi @dimisus,

Yeah, I had a propper look now and unfortunately getting the new(ish) adaptors working is not simple and definitely needs to be addressed in forthcoming versions of this lib. I managed to get it working with the below (at least I think so, based on the image 😃 ):

example

import { ChartJSNodeCanvas, ChartCallback, freshRequire } from './'; // Change to 'chartjs-node-canvas'
import { ChartConfiguration } from 'chart.js';
import { promises as fs } from 'fs';
import 'chartjs-chart-financial'; // For types only!

const width = 400;
const height = 400;
const configuration: ChartConfiguration = {
	type: 'candlestick',
	data: {
		datasets: [
			{
				label: 'some data',
				data: [
					{
						"x": 1491004800000,
						"o": 30.33,
						"h": 33.33,
						"l": 30.27,
						"c": 31.61
					},
					{
						"x": 1491177600000,
						"o": 32.17,
						"h": 37.09,
						"l": 31.7,
						"c": 33.75
					},
					{
						"x": 1491264000000,
						"o": 34.81,
						"h": 36.73,
						"l": 34.3,
						"c": 36.18
					},
					{
						"x": 1491350400000,
						"o": 36.95,
						"h": 40.58,
						"l": 33.43,
						"c": 37.19
					},
					{
						"x": 1491436800000,
						"o": 37.78,
						"h": 40.17,
						"l": 34.3,
						"c": 38.78
					},
					{
						"x": 1491523200000,
						"o": 38.18,
						"h": 41.52,
						"l": 36.58,
						"c": 37.21
					},
				]
			},
		],
	},
	options: {},
};
const chartJSNodeCanvas = new ChartJSNodeCanvas({
	width, height, plugins: {
		modern: [ 'chartjs-chart-financial' ],
		globalVariableLegacy: [ 'chartjs-adapter-luxon' ]
	}
});

// Needs to run after the constructor but before any render function
(global as any).window = (global as any).window || {};
(global as any).window.luxon = freshRequire('luxon'); // Can just use normal require();

async function main(): Promise<void> {

	const buffer = await chartJSNodeCanvas.renderToBuffer(configuration);
	await fs.writeFile('./example.png', buffer, 'base64');
}
main();

node version: 16.13.0 this lib version: 4.1.6

deps:

{
...
    "chart.js": "^3.5.1",
    canvas": "^2.8.0",
    "chartjs-adapter-luxon": "^1.1.0",
    "chartjs-chart-financial": "^0.1.1",
    "luxon": "^2.3.0",
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Chart.js - Financial chart
Chart.js - Financial chart. See the source for this example, README, and Chart.js docs for more details. Sample Chart. Bar Type: Candlestick, OHLC....
Read more >
chartjs-chart-financial - npm
Chart.js module for charting financial securities. ... Start using chartjs-chart-financial in your project by running `npm i ...
Read more >
Chart.js module for charting financial securities - GitHub
The plugins below may be particularly interesting to use with financial charts. See the Chart.js plugin API and longer list of plugins for...
Read more >
chartjs-chart-financial examples - CodeSandbox
Learn how to use chartjs-chart-financial by viewing and forking example apps that make use of chartjs-chart-financial on CodeSandbox. ; Latest version0.1.1.
Read more >
Financial | chartjs-plugin-streaming - nagix
For plain JavaScript, use script tags in the following order. <script src="path/to/chartjs/dist/chart.min.js"></script> <script ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found