Table of Contents
安装FusionCharts
使用 CDN
从 NPM 安装
示例 2
index.html
说明
结论
Home Web Front-end JS Tutorial How to create your first chart using FusionCharts.js?

How to create your first chart using FusionCharts.js?

Sep 10, 2023 pm 10:41 PM

如何使用 FusionCharts.js 创建您的第一个图表?

FusionCharts 是一个 JavaScript 库,当您想要创建图表和地图并将其放入您的 Web 中时可以使用它应用。在本教程中,我们将展示如何使用 FusionChart.js 创建两个不同的图表。

在学习如何创建图表之前,第一件重要的事情是了解如何将 FusionCharts 安装到本地计算机上。

安装FusionCharts

我们可以通过多种方式安装 FusionCharts。

使用 CDN

您可以使用下面给出的CDN链接直接访问FusionCharts的文件。

<script src="https://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.js"></script>
Copy after login

从 NPM 安装

您可以使用 NPM 将库作为依赖项安装到代码中。使用以下命令安装FusionCharts。

npm install --save fusioncharts
Copy after login

上述命令会将 FusionCharts 保存为项目中的 dev 依赖项。

既然我们知道了如何安装 FusionCharts,那么让我们来探讨一下我们将使用 FusionCharts 创建的几个图表示例。

示例 1

第一个示例是一个简单的条形图,我们将在 FusionCharts 的帮助下创建它,该图按国家/地区描述了亿万富翁的数量。考虑下面所示的代码。

index.html

<html>
<head>
   <title>FusionChart Example - 1</title>
   <script type="text/javascript"src="https://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.js"></script>
   <script type="text/javascript"src="https://cdn.fusioncharts.com/fusioncharts/latest/themes/fusioncharts.theme.fusion.js"></script>
   <script type="text/javascript">
      const chartData = [{
         "label": "United States","value": "735"
      }, {
            "label": "Mainland China","value": "539"
      }, {
            "label": "India", "value": "166"
      }, {
            "label": "Germany", "value": "134"
      }, {
            "label": "Russia", "value": "83"
      }, {
            "label": "Hong Kong", "value": "67"
      }, {
            "label": "Canada", "value": "64"
      }, {
            "label": "Brazil", "value": "62"
      }, {
            "label": "Italy", "value": "52"
      }, {
            "label": "Taiwan", "value": "51"
      }];
      const chartConfig = {
         type: 'column2d',
         renderAt: 'chart-container',
         width: '100%',
         height: '400',
         dataFormat: 'json',
         dataSource: {
            // Configuration of Chart
            "chart": {
               "caption": "Billionares By Country Wise[2022]",
               "subCaption": "According to Forbes",
               "xAxisName": "Country",
               "yAxisName": "Billionares",
               "theme": "fusion",
            },
            // Actual Chart Data
            "data": chartData
         }
      };
      FusionCharts.ready(function() {
         var fusioncharts = new FusionCharts(chartConfig);
         fusioncharts.render();
      });
   </script>
</head>
<body>
   <div id="chart-container">FusionCharts!!</div>
</body>
</html>
Copy after login

如果我们在浏览器中运行上述代码,我们将得到一个绘制为输出的条形图。

说明

在此代码中,我们首先创建了一个 JSON 数据,我们将在创建 FusionChart 对象的实例时将其传递到 chartConfig 的“data”属性中。

除此之外,“chartConfig”对象中还存在一些属性,尤其是“chart”属性,其中我们有不同的子属性,例如 caption、subCaption,这些属性在以下情况下都是必需的:我们正在绘制一个条形图。

示例 2

在上面的示例中,我们使用简单的 JSON 数据创建了一个条形图,并将其呈现给浏览器。在下面的示例中,我们将创建一个更复杂的图表,其中我们将描绘具有多个块的融合图表。考虑下面所示的代码。

index.html

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <script type="text/javascript"src="https://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.js"></script>
   <script type="text/javascript"src="https://cdn.fusioncharts.com/fusioncharts/latest/themes/fusioncharts.theme.fusion.js"></script>
<title>FusionChart - Example 2</title>
</head>
<body>
   <div id="chart-container">For Rendering!!</div>
   <script>
      FusionCharts.ready(function() {
         var conversionChart = new FusionCharts({
            type: 'bubble',
            renderAt: 'chart-container',
            width: '600',
            height: '450',
            dataFormat: 'json',
            dataSource: {
               "chart": {
                  "theme": "fusion",
                  "caption": "Sales Analysis of Mobile Devices Brands",
                  "subcaption": "Last Quarter",
                  "xAxisMinValue": "0",
                  "xAxisMaxValue": "100",
                  "yAxisMinValue": "0",
                  "yAxisMaxValue": "300000",
                  "xAxisName": "Average Price",
                  "yAxisName": "Units Sold",
                  "numDivlines": "2",
                  "showValues": "1",
                  "showTrendlineLabels": "0",
                  "plotTooltext": "$name : Profit Contribution - $zvalue%",
                  "drawQuadrant": "1",
                  "quadrantLineAlpha": "80",
                  "quadrantLineThickness": "3",
                  "quadrantXVal": "50",
                  "quadrantYVal": "15000",
                  "quadrantLabelTL": "Low Price / High Sale",
                  "quadrantLabelTR": "High Price / High Sale",
                  "quadrantLabelBL": "Low Price / Low Sale",
                  "quadrantLabelBR": "High Price / Low Sale",
                  "yAxisLineThickness": "1",
                  "yAxisLineColor": "#999999",
                  "xAxisLineThickness": "1",
                  "xAxisLineColor": "#999999",
                  "theme": "fusion"
                  },
                  "categories": [{
                     "category": [{
                        "label": "$50",
                        "x": "0"
                     },{
                        "label": "$100",
                        "x": "20",
                        "showverticalline": "1"
                     },{
                        "label": "$150",
                        "x": "40",
                        "showverticalline": "1"
                     },{
                        "label": "$200",
                        "x": "60",
                        "showverticalline": "1"
                     },{
                        "label": "$250",
                        "x": "80",
                        "showverticalline": "1"
                     }, {
                        "label": "$300",
                        "x": "100",
                        "showverticalline": "1"
                        }
                     ]
                  }],
                  "dataset": [{
                     "color": "#00aee4",
                     "data": [{
                        "x": "35",
                        "y": "1500000",
                        "z": "24",
                        "name": "Xiaomi"
                     },{
                        "x": "80",
                        "y": "1850000",
                        "z": "26",
                        "name": "Samsung"
                     },{
                        "x": "45",
                        "y": "1945000",
                        "z": "19",
                        "name": "Nokia"
                     },{
                        "x": "65",
                        "y": "10500",
                        "z": "8",
                        "name": "OnePlus"
                     },{
                        "x": "100",
                        "y": "905000",
                        "z": "5",
                        "name": "Apple"
                     },{
                        "x": "32",
                        "y": "2200000",
                        "z": "10",
                        "name": "Asus"
                     },{
                        "x": "44",
                        "y": "1300000",
                        "z": "9",
                        "name": "Vivo"
                     }
                  ]
               }],
               "trendlines": [{
                  "line": [{
                     "startValue": "20000",
                     "endValue": "30000",
                     "isTrendZone": "1",
                     "color": "#aaaaaa",
                     "alpha": "14"
                  },{
                     "startValue": "10000",
                     "endValue": "20000",
                     "isTrendZone": "1",
                     "color": "#aaaaaa",
                     "alpha": "7"
                  }
               ]
            }],
            "vTrendlines": [{
               "line": [{
                  "startValue": "44",
                  "isTrendZone": "0",
                  "color": "#0066cc",
                  "thickness": "1",
                  "dashed": "1",
                  "displayValue": "Gross Avg."
               }]
            }]
         }
      });
      conversionChart.render();
   });
   </script>
</body>
</html>
Copy after login

使用“编辑并运行”按钮运行此代码并查看输出。它将显示具有多个象限的图。

说明

在此示例中,我们尝试借助 FusionCharts 描绘多家移动设备公司的销售额。

这里,“数据集”属性包含我们将在图表中显示的实际 JSON 数据,而“类别”包含 X 轴和 Y 轴坐标的数据。

结论

在本教程中,我们使用了几个示例来演示如何利用 FusionCharts 库中的可用功能在 JavaScript 中创建不同的图表。

The above is the detailed content of How to create your first chart using FusionCharts.js?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

How to implement panel drag and drop adjustment function similar to VSCode in front-end development? How to implement panel drag and drop adjustment function similar to VSCode in front-end development? Apr 04, 2025 pm 02:06 PM

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...

See all articles