Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JS Plotly

Plotly.js is a charting library offering more than 40 chart types, including:

  • Horizontal and Vertical Bar Charts
  • Pie and Donut Charts
  • Line Charts
  • Scatter and Bubble Plots
  • Equation Plots
  • 3D Charts
  • Statistical Graphs
  • SVG Maps
  • and more

Plotly.js is free and open-source under the MIT license, meaning it is free to install and use. You can access the source code, report issues, and contribute via GitHub.

Bar Charts

JS Plotly

Source Code

const xArray = [“Italy”,“France”,“Spain”,“USA”,“Argentina”];
const yArray = [5549442415];

const data = [{
  x: xArray,
  y: yArray,
  type: “bar”,
  orientation:“v”,
  marker: {color:“rgba(0,0,255)”}
}];

const layout = {title:“World Wide Wine Production”};

Plotly.newPlot(“myPlot”, data, layout);

Horizontal Bar Charts

JS Plotly 1

Source Code

const xArray = [5549442415];
const yArray = [“Italy”,“France”,“Spain”,“USA”,“Argentina”];

const data = [{
  x: xArray,
  y: yArray,
  type: “bar”,
  orientation: “h”,
  marker: {color:“rgba(255,0,0,0.6)”}
}];

const layout = {title:“World Wide Wine Production”};

Plotly.newPlot(“myPlot”, data, layout);

Pie Charts

pie chart

To display a pie chart instead of bars, replace x and y with labels and values, and set the type to “pie”.

const data = [{
  labels: xArray,
  values: yArray,
  type: “pie”
}];

Donut Charts

Donut chart

To transform a pie chart into a donut chart, simply add a hole in the center.

const data = [{
  labels: xArray,
  values: yArray,
  hole: .4,
  type: “pie”
}];

Plotting Equations

plotting

Source Code

let exp = “Math.sin(x)”;

// Generate values
const xValues = [];
const yValues = [];
for (let x = 0; x <= 10; x += 0.1) {
  xValues.push(x);
  yValues.push(eval(exp));
}

// Display using Plotly
const data = [{x:xValues, y:yValues, mode:“lines”}];
const layout = {title: “y = “ + exp};
Plotly.newPlot(“myPlot”, data, layout);

source code

To show scatter plots instead, set the mode to “markers.”

// Display using Plotly
const data = [{x:xValues, y:yValues, mode:“markers”}];
const layout = {title: “y = “ + exp};
Plotly.newPlot(“myPlot”, data, layout);

Scatter Plots

scatter

Source Code

const xArray = [50,60,70,80,90,100,110,120,130,140,150];
const yArray = [7,8,8,9,9,9,10,11,14,14,15];

// Define Data
const data = [{
  x: xArray,
  y: yArray,
  mode:“markers”,
  type:“scatter”
}];

// Define Layout
const layout = {
  xaxis: {range: [40160], title: “Square Meters”},
  yaxis: {range: [516], title: “Price in Millions”},
  title: “House Prices vs. Size”
};

Plotly.newPlot(“myPlot”, data, layout);

Line Graphs

line graphs

Source Code

const xArray = [50,60,70,80,90,100,110,120,130,140,150];
const yArray = [7,8,8,9,9,9,10,11,14,14,15];

// Define Data
const data = [{
  x: xArray,
  y: yArray,
  mode: “lines”,
  type: “scatter”
}];

// Define Layout
const layout = {
  xaxis: {range: [40160], title: “Square Meters”},
  yaxis: {range: [516], title: “Price in Millions”},
  title: “House Prices vs Size”
};

// Display using Plotly
Plotly.newPlot(“myPlot”, data, layout);