how to make a bar graph in typescript

index.ts
import { Chart } from 'chart.js';

// Sample data points
const data = {
    labels: ['January', 'February', 'March', 'April', 'May', 'June'],
    datasets: [{
        label: 'Sales',
        data: [10, 20, 15, 25, 30, 28],
        backgroundColor: 'blue'
    }]
};

// Get the canvas element
const canvas = document.getElementById('myChart') as HTMLCanvasElement;

// Create a new bar chart instance
const barChart = new Chart(canvas, {
    type: 'bar',
    data: data
});
473 chars
21 lines

In this example, we first import the Chart class from chart.js library. We define sample data points for the bar graph, including labels and data values. We then select the canvas element where the bar graph will be rendered. Finally, we create a new instance of a bar chart and pass in the canvas element and the data points as parameters.

gistlibby LogSnag