📜  角度的 amcharts - TypeScript (1)

📅  最后修改于: 2023-12-03 14:57:23.628000             🧑  作者: Mango

Introduction to 'amcharts - TypeScript from an Angular Perspective'

Overview

Amcharts is an open source charting library designed to support multiple formats such as JavaScript, TypeScript, React, Angular, Vue and more. It is a powerful and versatile charting library that provides comprehensive documentation, examples and tutorials to help developers create interactive charts, graphs and maps for their web applications.

In this introduction, we will focus on using Amcharts with TypeScript within an Angular application. We will explore how to set up Amcharts for an Angular project and create interactive charts using TypeScript.

Installing Amcharts in an Angular Project

To get started with Amcharts in an Angular project, you should first install the necessary Amcharts packages.

npm install @amcharts/amcharts4-core
npm install @amcharts/amcharts4-charts
npm install @amcharts/amcharts4-geodata

It is worth noting that for the geodata package, you may need to install additional geodata packages if you want to use specific maps. For example, if you want to use maps of the United States, you would need to install the @amcharts/amcharts4-geodata/usaHigh package.

Once you have installed the necessary packages, you can import them into your Angular component like so:

import * as am4core from "@amcharts/amcharts4/core";
import * as am4charts from "@amcharts/amcharts4/charts";
import * as am4maps from "@amcharts/amcharts4/maps";
import am4themes_animated from "@amcharts/amcharts4/themes/animated";

Next, you will need to initialize the Amcharts library like so:

ngOnInit() {
  am4core.useTheme(am4themes_animated);
}

The useTheme method applies the animated theme to your charts.

Creating a Simple Chart

Now that we have installed and initialized Amcharts in our Angular project, we can begin creating our chart. Let's start with a simple bar chart.

First, we create a container element in our template where the chart will be rendered:

<div id="chartdiv"></div>

Next, we create a TypeScript component for our chart and define the chart configuration inside the ngAfterViewInit() lifecycle hook:

ngAfterViewInit() {
  let chart = am4core.create("chartdiv", am4charts.XYChart);
  
  chart.data = [
    { "category": "Category 1", "value": 9 },
    { "category": "Category 2", "value": 13 },
    { "category": "Category 3", "value": 20 },
    { "category": "Category 4", "value": 17 },
    { "category": "Category 5", "value": 15 }
  ];

  let categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
  categoryAxis.dataFields.category = "category";

  let valueAxis = chart.yAxes.push(new am4charts.ValueAxis());

  let series = chart.series.push(new am4charts.ColumnSeries());
  series.dataFields.valueY = "value";
  series.dataFields.categoryX = "category";
  series.name = "Value";

  chart.legend = new am4charts.Legend();
}

The code above creates an XY chart, sets the chart data with categories and values, adds a category axis and value axis, adds a column series for the values, and adds a legend.

The result should be a simple bar chart rendered in the chartdiv container.

Conclusion

Amcharts is a powerful and versatile charting library that provides comprehensive documentation, examples and tutorials to help developers create interactive charts, graphs and maps for their web applications. By using Amcharts with TypeScript within an Angular project, developers can easily create beautiful and interactive charts with ease.