...
Days
Hours
Minutes
Seconds
x
Skip to content

Create Charts in JavaScript

This document shows you how to install the FroalaCharts library and all the other dependencies on your system and render a chart using Plain JavaScript.

Prerequisites

If you are using Froalacharts dependencies from CDN or Local Files, you can skip to the next section.

If you choose to install the froalacharts package via npm, make sure you have Node.js installed in your system. Make sure you have a bundler like webpack and parcel or have browserify installed in your system.

Installation and including dependencies

You can install the froalacharts components following any of the methods below:

To install the FroalaCharts follow these steps:

  1. Include the FroalaCharts JavaScript files from CDN in your static HTML file.
  2. Include the theme file.

Use the following code:

<head>
  <script type="text/javascript" src="https://unpkg.com/froalacharts@1.0.0/froalacharts.js"></script>
  <script type="text/javascript" src="https://unpkg.com/froalacharts@1.0.0/themes/froalacharts.theme.froala.js"></script>
</head>

Preparing the data

Let's create a chart showing a "Comparison of Quarterly Revenue".

Since we are plotting a single dataset, create a column chart with 'Quarter' as data labels along the x-axis and 'Revenues (In USD)' as data values along y-axis.

Quarter Revenues
Q1 10000
Q2 11500
Q3 12500
Q4 15000

FroalaCharts accepts the data in JSON format. The data for the chart is represented in the following way:

const chartData = [
{
  data: [
  {
    value: "10000",
  },
  {
    value: "11500",
  },
  {
    value: "12500",
  },
  {
    value: "15000",
  }]
}];

Configure your chart

Next, work on the styling, positioning and giving your chart a context.

// Create a JSON object to store the chart configurations
const chartConfig = {
  //Specify the chart type
  type: "column",
  //Set the container object
  renderAt: "ft",
  //Specify chart dimensions
  width: "800",
  height: "500",
  //Set the type of data
  dataSource: {
    chart: {
      //Set the theme for your chart
      theme: "froala",
      //Set the chart caption
      caption: "Comparison of Quarterly Revenue",
      //Set the x-axis name
      xAxisname: "Quarter",
      //Set the y-axis name
      yAxisName: "Revenues (In USD)",
      numberPrefix: "$",
    },
    categories: [
      {
        category: [
          {
            label: "Q1",
          },
          {
            label: "Q2",
          },
          {
            label: "Q3",
          },
          {
            label: "Q4",
          }
        ]
      }
    ],
    dataset: chartData
  }
};

Find the complete list of chart types with their respective alias here.

Render the chart

The consolidated code to render the chart is shown below:

<html>

<head>
  <title>My first chart using Froala Charts</title>
  <script src="https://unpkg.com/froalacharts@1.0.0/froalacharts.js"></script>
  <script src="https://unpkg.com/froalacharts@1.0.0/themes/froalacharts.theme.froala.js"></script>
  <script type="text/javascript">
  // Chart Data 
  const chartData = [
  {
    data: [
    {
      value: "10000",
    },
    {
      value: "11500",
    },
    {
      value: "12500",
    },
    {
      value: "15000",
    }]
  }];
  const chartConfig = {
    type: "column",
    renderAt: "ft",
    width: "800",
    height: "500",
    dataSource:
    {
      chart:
      {
        theme: "froala",
        caption: "Comparison of Quarterly Revenue",
        xAxisname: "Quarter",
        yAxisName: "Revenues (In USD)",
        numberPrefix: "$",
      },
      categories: [
      {
        category: [
        {
          label: "Q1",
        },
        {
          label: "Q2",
        },
        {
          label: "Q3",
        },
        {
          label: "Q4",
        }]
      }],
      dataset: chartData
    }
  };
  FroalaCharts.ready(function()
  {
    froalacharts = new FroalaCharts(chartConfig);
    froalacharts.render();
  });
  </script>
</head>

<body>
  <div id="ft">Froala Charts will load here!</div>
</body>

</html>

See your chart

You should be able to see the chart as shown below.

Froala Charts will load here!

Next Steps

Explore the List of Charts.

Dive into the API documentation.

Learn about other Concepts.

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.