score:70
I would try:
const canvas = <HTMLCanvasElement> document.getElementById('myChart');
const ctx = canvas.getContext('2d');
the purpose of Typescript is to avoid wrong types. By default document.getElementById
returns a HTMLElement
type which is a generic type.
In order to make your app understand it is a canvas element you need to cast it using <CastedToType>
syntax.
score:0
simply cast canvas as HTMLCanvasElement inline as follows
const canvas = document.getElementById('myChart');
const ctx = (canvas as HTMLCanvasElement).getContext('2d');
cheers.
score:1
const canvas = document.getElementsByTagName('canvas')[0];
will return an HTMLCanvasElement, but
const canvas = document.getElementById('someId');
will return an HTMLElement object instead of HTMLCanvasElement, which has no 'getContext' method.
score:1
Best to use runtime type validation which TS understands.
const canvas = document.querySelector('#my-canvas')
if (!(canvas instanceof HTMLCanvasElement)) return
canvas.getContext(...)
This handles the case where the element you query for has the wrong tag name.
<div id="my-canvas" />
<canvas id="my-canvas1" />
score:19
Try this:
const canvas = document.getElementById('myChart') as HTMLCanvasElement;
const ctx = canvas.getContext('2d');
Source: stackoverflow.com
Related Query
- Property 'getContext' does not exist on type 'HTMLElement'
- I am getting TS2339: Property 'chart' does not exist on type 'Window'?
- Property 'elements' does not exist on type 'typeof Chart'
- I am having this error in charts v3 chartjs-chart-treemap: fontColor does not exist in type 'ChartDataset<"treemap", TreemapDataPoint[]>
- Chart.options returns "Property 'options' does not exist on type 'chart'"
- Chart.js Types of property 'type' are incompatible. Type 'string' is not assignable to type '"line" | "bar" | "scatter"
- Showing 'undefined%' in the graph if data does not exist
- Chartjs does not show annotations when x axis is of type time
- fill: 'origin' property does not work for my chart
- Chartjs v2.8 showAllTooltips does not exist
- ChartJS, updating chart type does not update xAxis properly
- react-chartjs-2 fill property not working?
- Html chart does not fit a small Android WebView
- chartjs height does not follow parent container
- Chart looks only grey, does not show the color - Chartjs,discordjs
- Wkhtmltopdf does not render Chart.JS 2.5.0 graph
- Angular-chart.js - Make line chart does not curve
- Chart.JS Can not read property '_meta' of Undefined
- ng2-charts\ng2-charts.js does not export ChartsModule
- Vue Chart.js component does not render
- Chart.js Chart in Angular does not load until resizing browser window
- Type can not be assigned in react-chartjs-2
- How do I keep chart.js charts in one JS file, and not get errors when the ID from the JS file don't exist on one specific html page?
- Chart.js in AngularJS tabset does not render
- Labels attribute of chartsjs not taking context variable of type string
- Chart.js click event to open a custom tooltip does not work sometimes
- error TS2322: Type 'string' is not assignable to type 'keyof ChartTypeRegistry'
- TypeError Not reading property correctly
- My pie chart (chartJs) does not appear
- Chart.js chart does not display when inside an ngIf.
More Query from same tag
- ng2-chart tooltip position change option?
- Is it possible to make chartjs charts scrollable?
- chartjs dataset from single line to multiple line
- How to execute a JavaScript function with ChartJS and FreeMarker?
- Unable to set Global Options in Chart.js
- Add all data in the tooltip of Chart JS
- ChartJS multiple dataset issue. second dataset gets over 1st instead of overlaying
- How to sort MMMYY on Chart.Js and display the values that match the month?
- How to create a histogram in charts.js with MYSQL data
- php/laravel: Creating chartdata, return 0 if no data is being submitted to database
- Django ValueError The view todo_lists.views.visualisation didn't return an HttpResponse object. It returned None instead
- Rotate left legend chartjs
- Setting up min and max in chartjs did not work
- Creating a Doughnut chart using JSON and chart.js
- how to setting chart js customization
- Issue in implementing ChartJS in my new assignment of ReactJS App, please let me know where I am wrong
- Attach onAnimationComplete for Chart.js after creation
- Chart.js - PieChart defined background color for specific dataset element
- How can I use an array of {x,y} objects to make a chart?
- Chart.js Scaling issue when initially loading the page
- Chartjs-plugin: How to add a different color to each label?
- Stacked bar chart with chartjs with included values
- Chart.js - Ajax response not accept
- Display with Month name in the Tooltip
- How do I hide values past the x-axis in chartjs 2.0?
- Angular 6 with chart.js TypeError: item is null
- Chart.js zeros handling
- chartkick chart.js change colour of axis and colour of axis title
- Change tooltips title at chart.js?
- accessing returned data from a states resolve function