Prompt Detail:
To install Google Charts via NPM (Node Package Manager), follow these steps:
Open your terminal and navigate to the root directory of your project.
Type the following command to install the
google-charts
package:npm install google-charts
Wait for the installation process to complete. NPM will download and install the
google-charts
package along with its dependencies.Once the installation is complete, you can use the
google-charts
package in your JavaScript code.
Here's an example of how to use google-charts
to create a simple pie chart:
javascriptconst { google } = require('google-charts');
google.charts.load('current', { 'packages': ['corechart'] });
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
const data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Work', 8],
['Sleep', 8],
['Play', 8],
]);
const options = {
title: 'My Daily Activities',
};
const chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
Note that you'll need to include the google-charts
package in your HTML file and add a div
element with the id
of piechart
for the chart to be rendered:
html<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Google Charts Example</title>
<script src="https://www.gstatic.com/charts/loader.js"></script>
</head>
<body>
<div id="piechart"></div>
<script src="index.js"></script>
</body>
</html>