This tutorial aims to a provide step-by-step guide on how to implement, customize and render a chart with the library. It assumes you have basic knowledge of JavaScript and are absolutely new to MindFusion.Charting for JavaScript. Feel free to skip content you are already familiar with.
Follow the steps outlined in the Getting Started topic and set up your project either by loading the necessary charting modules (ES6/CommonJs) or by loading the namespaced UMD scripts.
Add the following references to your web page:
HTML Copy Code |
---|
<script src="umd/drawing.js" type="text/javascript"></script> |
JavaScript Copy Code |
---|
<script type="text/javascript" src="piechart.js"></script> |
JavaScript Copy Code |
---|
<script type="importmap"> <script src="piechart.js" type="module"></script> |
HTML Copy Code |
---|
npm install @mindfusion/charting |
In your HTML file you should add a code like this in order to initialize the Canvas needed by the chart library to render itself onto:
JavaScript Copy Code |
---|
<canvas id="myChart" width="400" height="400"></canvas> |
The id would let you access the Canvas later in the *.js file, so be sure to include it.
We recommend that you add some namespace mappings to make writing code shorter:
JavaScript Copy Code |
---|
var Charting = MindFusion.Charting; |
Beside that you don't need to do anything more.
Add the necessary imports to your code-behind:
For ES6 Modules Copy Code |
---|
import * as Charting from '@mindfusion/charting'; |
For CommonJS Modules Copy Code |
---|
const Charting = require('@mindfusion/charting'); |
The chart is created with the constructor of the chosen chart type:
JavaScript Copy Code |
---|
var myChart = new Controls.PieChart(document.getElementById('myChart')); |
You can look at the Charting.Controls namespace for a reference of the available predefined chart controls. The title and title font are set as follows:
JavaScript Copy Code |
---|
myChart.title = "Corporate Sales"; |
The chart.theme property is very important for adjusting chart appearance. It contains most of the properties you'll need to customize the look of your chart.
Generally charts have series property, which holds a Series with the data, title and labels. Let's assume you are building a pie chart. We create the data with a simple array:
JavaScript Copy Code |
---|
var values = new Collections.List([20, 30, 10, 40, 35]); |
Then we create a series:
JavaScript Copy Code |
---|
pieChart.series = new Charting.SimpleSeries(values, labels); |
Once the chart series is assigned we can draw the chart. To actually render the chart, call:
JavaScript Copy Code |
---|
chart.draw(); |
The appearance of the chart can be changed with direct properties like commonSeriesFills, uniformSeriesFill etc. or with special SeriesStyle objects like PerElementSeriesStyle or PerSeriesStyle. Loading a theme is also an option as well are separate styling properties found on different chart elements. Here we’ll use the properties that are likely to be set in the most common-case scenario. When you use a SeriesStyle object you assign it to the plot of the chart.
For a pie chart we use PerElementSeriesStyle. Let's define the brushes for the pie slices:
JavaScript Copy Code |
---|
var brushes = new Collections.List([ |
We repeat the code for the strokes:
JavaScript Copy Code |
---|
var strokes = new Collections.List([ |
And for the stroke thickness:
JavaScript Copy Code |
---|
var strokeThicknesses = new Collections.List([ |
JavaScript Copy Code |
---|
myChart.plot.seriesStyle = new PerElementSeriesStyle(seriesBrushes, seriesStrokes, seriesStrokeThicknesses); |
All arguments are nested lists, each list is responsible for setting the appearance of the elements in a single series in the chart.
In short this is all you need to know to create a chart with the MindFusion.Charting for JavaScript library. For more advanced and creative scenarios, refer to the other tutorials: