Categories
JavaScript

Add Charts to Our JavaScript App with Anychart — Open-High-Low-Close, Pie, and Pareto Charts

Anychart is an easy to use library that lets us add chart into our JavaScript web app.

In this article, we’ll look at how to create basic charts with Anychart.

Open-High-Low-Close (OHLC) Chart

We can add an OHLC chart easily with Anychart.

For instance, we can write:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>chart</title>
  </head>
  <body>
    <script
      src="https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js"
      type="text/javascript"
    ></script>
    <div id="container" style="width: 500px; height: 400px;"></div>
    <script>
      const data = [
        [Date.UTC(2020, 07, 23), 23.55, 23.88, 23.38, 23.62],
        [Date.UTC(2020, 07, 24), 22.65, 23.7, 22.65, 23.36],
        [Date.UTC(2020, 07, 25), 22.75, 23.7, 22.69, 23.44],
        [Date.UTC(2020, 07, 26), 23.2, 23.39, 22.87, 22.92],
        [Date.UTC(2020, 07, 27), 23.98, 24.49, 23.47, 23.49]
      ];

      const chart = anychart.ohlc();
      const series = chart.ohlc(data);
      chart.container("container");
      chart.draw();
    </script>
  </body>
</html>

We add the script tag for adding the Anychart base package.

And then we add a div for rendering the chart inside.

Next, we create the data array with the date and the open, high, low, and close data in this order.

And then we call anychart.ohlc to create the chart.

Next, we call chart.ohlc to set the chart data.

chart.container lets us set the ID of the chart container element.

And chart.draw draws the chart in the container element.

Pareto Chart

To add a Pareto chart, we write:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>chart</title>
  </head>
  <body>
    <script
      src="https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js"
      type="text/javascript"
    ></script>
    <script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-pareto.min.js"></script>
    <div id="container" style="width: 500px; height: 400px;"></div>
    <script>
      const chart = anychart.pareto([
        { x: "apple", value: 19 },
        { x: "orange", value: 9 },
        { x: "grape", value: 28 }
      ]);
      chart.container("container");
      chart.draw();
    </script>
  </body>
</html>

We call anychart.pareto with the data we want to render.

x has the x-axis value.

value has the y-axis value.

The rest of the code is the same as the other charts.

We’ve to add the pareto package to render a Pareto chart.

Pie Chart

We can add a pie chart with Anychart.

For instance, we can write:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>chart</title>
  </head>
  <body>
    <script
      src="https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js"
      type="text/javascript"
    ></script>
    <div id="container" style="width: 500px; height: 400px;"></div>
    <script>
      const data = [
        { x: "A", value: 677 },
        { x: "B", value: 243 },
        { x: "C", value: 431 }
      ];
      const chart = anychart.pie(data);
      chart.container("container");
      chart.draw();
    </script>
  </body>
</html>

We create the data array with the pie chart values.

x has pie labels.

value has the pie chunk values.

We pass the data array into anychart.pie to create the chart.

And we draw them the same way as the previous examples.

We can set the fill and outline color of the pie chunks with fill and stroke respectively:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>chart</title>
  </head>
  <body>
    <script
      src="https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js"
      type="text/javascript"
    ></script>
    <div id="container" style="width: 500px; height: 400px;"></div>
    <script>
      const data = [
        { x: "A", value: 677 },
        { x: "B", value: 243 },
        { x: "C", value: 431 }
      ];
      const chart = anychart.pie(data);
      chart.normal().fill("#669999", 0.5);
      chart.hovered().fill("#666699", 0.5);
      chart.selected().fill("#666699", 0.7);
      chart.normal().stroke("#669999", 2);
      chart.hovered().stroke("#669999", 2);
      chart.container("container");
      chart.draw();
    </script>
  </body>
</html>

Conclusion

We can create OHLC, Pareto charts and pie charts easily with Anychart.

Categories
JavaScript

Add Charts to Our JavaScript App with Anychart — Marker Charts and Network Graphs

Anychart is an easy to use library that lets us add chart into our JavaScript web app.

In this article, we’ll look at how to create basic charts with Anychart.

Marker Chart

We can create a marker chart by writing the following code:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>chart</title>
  </head>
  <body>
    <script
      src="https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js"
      type="text/javascript"
    ></script>
    <div id="container" style="width: 500px; height: 400px;"></div>
    <script>
      const data = [
        ["2000", 1100],
        ["2001", 880],
        ["2002", 1100],
        ["2003", 1500],
        ["2004", 921]
      ];
      const chart = anychart.cartesian();
      chart.marker(data);
      chart.container("container");
      chart.draw();
    </script>
  </body>
</html>

We have the data in the data array.

Each entry has the x-axis value as the first value and y-axis values as the 2nd value.

anychart.cartesian creates the marker chart.

chart.marker sets the data.

chart.container sets the ID of the container element to render the chart in.

And chart.draw draws the chart.

We can set the size of the markers with the size method:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>chart</title>
  </head>
  <body>
    <script
      src="https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js"
      type="text/javascript"
    ></script>
    <div id="container" style="width: 500px; height: 400px;"></div>
    <script>
      const data = [
        ["2000", 1100],
        ["2001", 880],
        ["2002", 1100],
        ["2003", 1500],
        ["2004", 921]
      ];
      const chart = anychart.cartesian();
      const series = chart.marker(data);
      series.normal().size(10);
      series.hovered().size(15);
      series.selected().size(15);
      chart.container("container");
      chart.draw();
    </script>
  </body>
</html>

We can set the markets to a different shape with the type method:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>chart</title>
  </head>
  <body>
    <script
      src="https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js"
      type="text/javascript"
    ></script>
    <div id="container" style="width: 500px; height: 400px;"></div>
    <script>
      const data = [
        ["2000", 1100],
        ["2001", 880],
        ["2002", 1100],
        ["2003", 1500],
        ["2004", 921]
      ];
      const chart = anychart.cartesian();
      const series = chart.marker(data);
      series.normal().type("star4");
      series.hovered().type("star5");
      series.selected().type("star6");
      chart.container("container");
      chart.draw();
    </script>
  </body>
</html>

Network Graph

We can add a network graph easily with Anychart.

For instance, we can write

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>chart</title>
  </head>
  <body>
    <script
      src="https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js"
      type="text/javascript"
    ></script>
    <script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-graph.min.js"></script>
    <div id="container" style="width: 500px; height: 400px;"></div>
    <script>
      const data = {
        nodes: [
          { id: "Richard" },
          { id: "Larry" },
          { id: "Marta" },
          { id: "Jane" },
          { id: "Norma" }
        ],
        edges: [
          { from: "Richard", to: "Larry" },
          { from: "Richard", to: "Marta" },
          { from: "Larry", to: "Marta" },
          { from: "Marta", to: "Jane" },
          { from: "Jane", to: "Norma" },
          { from: "Brett", to: "Frank" }
        ]
      };
      const chart = anychart.graph(data);
      chart.container("container");
      chart.draw();
    </script>
  </body>
</html>

We add the Anychart graph module with the 2nd script tag.

Then we add the data array to add the data.

We define the nodes in the nodes array.

And we define how they’re connected with the edges array.

from is the node to connect from and to is the node to connect to.

Then we just pass the data to anychart.graph and draw it like any other kind of chart.

Conclusion

We can add marker charts and network graphs with Anychart.

Categories
JavaScript

Add Charts to Our JavaScript App with Anychart — Line, and Marimekko Charts

Anychart is an easy to use library that lets us add chart into our JavaScript web app.

In this article, we’ll look at how to create basic charts with Anychart.

Line Chart

We can add a line chart with Anychart.

To add one, we write the following:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>chart</title>
  </head>
  <body>
    <script
      src="https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js"
      type="text/javascript"
    ></script>
    <div id="container" style="width: 500px; height: 400px;"></div>
    <script>
      const data = [
        ["January", 100],
        ["February", 120],
        ["March", 180]
      ];

      const chart = anychart.line();
      const series = chart.line(data);
      chart.container("container");
      chart.draw();
    </script>
  </body>
</html>

We add the first script tag to add the Anychart library.

Below that, we add the div for the chart container.

And below that, we add the script tag for the line chart.

data has an array with the x and y-axis values in the array entry.

anychart.line creates the line chart.

chart.line adds the data for the line chart.

chart.container is called with the ID of the chart container element inside to render the chart in the container.

And chart.draw draws the chart.

Marimekko Charts

We can create Marimekko charts with Anychart.

To do so, we write:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>chart</title>
  </head>
  <body>
    <script
      src="https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js"
      type="text/javascript"
    ></script>
    <script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-mekko.min.js"></script>

    <div id="container" style="width: 500px; height: 400px;"></div>
    <script>
      const data = anychart.data.set([
        ["QTR1", 10000, 12500],
        ["QTR2", 12000, 15000],
        ["QTR3", 13000, 16500],
        ["QTR4", 10000, 13000]
      ]);
      const series1 = data.mapAs({ x: 0, value: 1 });
      const series2 = data.mapAs({ x: 0, value: 2 });
      chart = anychart.mekko();
      chart.mekko(series1);
      chart.mekko(series2);
      chart.container("container");
      chart.draw();
    </script>
  </body>
</html>

We add the mekko package with the 2nd script tag.

Then we call anychart.darta.set with the data for our chart.

Then we call data.mapAs with an object to map the x-axis to the index of the value in the array.

x is set to 0, so it takes the value from the data array above it.

value is set to 1 and 2 respectively so the value from 1 and 2 is taken as the data.

Then we call anychart.mekko to create the chart.

And we call chart.mekko with the series1 and series2 data to set the chart data.

The rest of the code is the same as the previous example.

Bar Mekko Chart

We can create a bar mekko chart with the barmekko method.

For instance, we can write:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>chart</title>
  </head>
  <body>
    <script
      src="https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js"
      type="text/javascript"
    ></script>
    <script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-mekko.min.js"></script>

    <div id="container" style="width: 500px; height: 400px;"></div>
    <script>
      const data = [
        { x: "Spring", value: 20 },
        { x: "Summer", value: 30 },
        { x: "Autumn", value: 80 },
        { x: "Winter", value: 40 }
      ];
      const chart = anychart.barmekko();
      const series = chart.mekko(data);
      chart.container("container");
      chart.draw();
    </script>
  </body>
</html>

We have the data array with objects with the x and y-axis data respectively.

Then we call anychart.barmekko to create the bar mekko chart.

Next, we call chart.mekko to set the data.

And the rest of the code is the same as the previous examples.

Conclusion

We can create line charts, Marimekko, and bar mekko charts easily with Anychart.

Categories
JavaScript

Add Charts to Our JavaScript App with Anychart — High-Low, Jump Line, Japanese Candlestick Charts

Anychart is an easy to use library that lets us add chart into our JavaScript web app.

In this article, we’ll look at how to create basic charts with Anychart.

High-Low (HiLo) Chart

We can create high-low charts easily with Anychart.

To add one, we write:

<script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js" type="text/javascript"></script>

<div id="container" style="width: 500px; height: 400px;"></div>

Then we write:

const data = [
  ["January", 1000, 5000],
  ["February", 500, 2000],
  ["March", 3000, 6000],
  ["April", 4000, 19680],
  ["May", 6000, 58581]
];

chart = anychart.hilo();
const series = chart.hilo(data);
chart.title("HiLo Chart");
chart.container("container");
chart.draw();

We add the script tag for the base package.

Then we add the container div for rendering the chart.

data has an array of data.

The first value of each entry is the x-axis value.

The 2nd value is the low value.

The 3rd value is the high value.

Then we create the high-low chart with anychart.hilo .

chart.hilo creates the series from the data.

chart.title sets the title.

chart.container sets the ID of the container to render the chart in.

chart.draw draws the chart.

Japanese Candlestick Chart

To create a Japanese candlestick chart, we write:

const data = [
  [Date.UTC(2007, 07, 23), 23.55, 23.88, 23.38, 23.62],
  [Date.UTC(2007, 07, 24), 22.65, 23.7, 22.65, 23.36],
  [Date.UTC(2007, 07, 25), 22.75, 23.7, 22.69, 23.44],
  [Date.UTC(2007, 07, 26), 23.2, 23.39, 22.87, 22.92],
  [Date.UTC(2007, 07, 27), 23.98, 24.49, 23.47, 23.49],
];

const chart = anychart.candlestick();
const series = chart.candlestick(data);
chart.container("container");
chart.draw();

The HTML is the same as the previous example.

We have the date value for the x-axis in the first data array entry.

Then other values are the open, high, low, and close values respectively.

We then create the Japanese candlestick chart with anychart.candlestuck .

chart.candlestick takes the data.

The rest are the same as the previous example.

Jump Line Chart

We can create a jump line chart by writing:

const data = [{
    x: "January",
    value: 10000
  },
  {
    x: "February",
    value: 12000
  },
  {
    x: "March",
    value: 18000
  }
];

const chart = anychart.jumpLine();
const series = chart.jumpLine(data);
chart.container("container");
chart.draw();

We have the data array with the x property with the x-axis value,

And value has the y-axis value.

anychart.jumpLine creates the jump line chart.

chart.jumpLine takes the data.

And the rest are the same as the previous examples.

Line Chart

We can add a line chart with the base Anychart package.

To add one, write:

const data = [
  ["January", 10000],
  ["February", 12000],
  ["March", 18000],
];

const chart = anychart.line();
const series = chart.line(data);
chart.container("container");
chart.draw();

We have the data array with the x and y-axis values respectively in each array entry.

Then we call anychart.line to create the line chart.

chart.line sets the data for the line chart.

And the rest are the same as the previous examples.

Conclusion

We can add high-low, jump line, Japanese candlestick, and line charts easily with Anychart.

Categories
JavaScript

Add Charts to Our JavaScript App with Anychart — Donut, Error, and Heat Map Charts

Anychart is an easy to use library that lets us add chart into our JavaScript web app.

In this article, we’ll look at how to create basic charts with Anychart.

Doughnut Chart

We can add a doughnut chart into our web app with Anychart.

For instance, we can write the following HTML:

<script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js" type="text/javascript"></script>

<div id="container" style="width: 500px; height: 400px;"></div>

Then we can add the following JavaScript:

const data = [{
    x: "A",
    value: 30
  },
  {
    x: "B",
    value: 53
  },
  {
    x: "C",
    value: 34
  },
];

const chart = anychart.pie(data);
chart.innerRadius("30%");
chart.container("container");
chart.draw();

We add the base Anychart package with the script tag.

Then we add the div for rendering the chart in.

In the JavaScript code, we add the data for the chart with the data array.

anychart.pie lets us create the donut chart.

chart.innerRadius lets us add a role with the radius being the given percentage.

chart.container sets the ID of the container element to render the chart in.

chart.draw draws the chart.

Error Chart

We can create an error chart with the series.error method.

For instance, we can write:

const data = [
  ["January", 10000],
  ["February", 12000],
  ["March", 13000],
];

const chart = anychart.column();
const series = chart.column(data);
series.error("10%");
chart.container("container");
chart.draw();

We have the data for our chart.

Then we create our column chart with anychart.column .

And we add the error bar to it with the series.error method.

This will make the error bar display on the column.

We can add error bars to area charts,. bar charts, line charts, scatter charts, stick charts, and more.

Funnel Chart

We can add a funnel chart with the following HTML:

<script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js" type="text/javascript"></script>
<script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-pyramid-funnel.min.js"></script>

<div id="container" style="width: 500px; height: 400px;"></div>

We need the funnel-pyramid package to add the chart.

The rest of the HTML is the same as the other examples.

Then we add the funnel chart by writing:

const data = [
  ["apple", 2320],
  ["orange", 940],
  ["grape", 490],
];

const chart = anychart.funnel(data);
chart.container("container");
chart.draw();

We call anychart.funnel to create the funnel chart.

And call chart.container to set the ID of the container to render the chart in.

Heat Map Chart

To create a heat map chart, we write the following HTML:

<script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js" type="text/javascript"></script>
<script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-heatmap.min.js"></script>

<div id="container" style="width: 500px; height: 400px;"></div>

We need to add the heatmap package to let us create a heat map chart.

Then we write the following JavaScript code:

const data = [{
    x: "2010",
    y: "A",
    heat: 15
  },
  {
    x: "2011",
    y: "A",
    heat: 17
  },
  {
    x: "2012",
    y: "A",
    heat: 21
  },
  {
    x: "2010",
    y: "B",
    heat: 34
  },
  {
    x: "2011",
    y: "B",
    heat: 33
  },
  {
    x: "2012",
    y: "B",
    heat: 32
  },
];

const chart = anychart.heatMap(data);
chart.container("container");
chart.draw();

data has objects with the x and y labels. heat is the value of the given x and y locations.

anychart.heatMap lets us create the chart.

Conclusion

We can add donut charts, error charts, and heat map charts easily with Anychart.