Plotly.js
Plotly.js is a chart library with more than 40 chart types, 3D charts, statistical charts, and SVG maps.
scatter plot
Quellcode
var xArray = [50,60,70,80,90,100,110,120,130,140,150]; var yArray = [7,8,8,9,9,9,10,11,14,14,15]; // Definition der Daten var data = [{ x: xArray, y: yArray, mode: "markers", type: "scatter" }; // Definition der Layout var layout = { xaxis: {range: [40, 160], title: "Square meters"}, yaxis: {range: [5, 16], title: "Price (in millions of yuan)"}, title: "House Price vs. Area" }; Plotly.newPlot("myPlot", data, layout);
line chart
Quellcode
var xArray = [50,60,70,80,90,100,110,120,130,140,150]; var yArray = [7,8,8,9,9,9,10,11,14,14,15]; // Definition der Daten var data = [{ x: xArray, y: yArray, mode: "lines", type: "scatter" }; // Definition der Layout var layout = { xaxis: {range: [40, 160], title: "Square meters"}, yaxis: {range: [5, 16], title: "Price (in millions of yuan)"}, title: "House Price vs. Area" }; // Plotly wird verwendet, um anzuzeigen Plotly.newPlot("myPlot", data, layout);
linear graph
Quellcode
var exp = "x + 17"; // Werte generieren var xValues = []; var yValues = []; for (var x = 0; x <= 10; x += 1) { yValues.push(eval(exp)); xValues.push(x); } // Definition der Daten var data = [{ x: xValues, y: yValues, mode: "lines" }; // Definition der Layout var layout = {title: "y = " + exp}; // Plotly wird verwendet, um anzuzeigen Plotly.newPlot("myPlot", data, layout);
Mehrzeilen
Quellcode
var exp1 = "x"; var exp2 = "1.5*x"; var exp3 = "1.5*x + 7"; // Werte generieren var x1Values = []; var x2Values = []; var x3Values = []; var y1Values = []; var y2Values = []; var y3Values = []; for (var x = 0; x <= 10; x += 1) { x1Values.push(x); x2Values.push(x); x3Values.push(x); y1Values.push(eval(exp1)); y2Values.push(eval(exp2)); y3Values.push(eval(exp3)); } // Definition der Daten var data = [ {x: x1Values, y: y1Values, mode:"lines"}, {x: x2Values, y: y2Values, mode:"lines"}, {x: x3Values, y: y3Values, mode:"lines"} ]; // Definition der Layout var layout = {title: "[y=" + exp1 + "] [y=" + exp2 + "] [y=" + exp3 + "]"}; // Plotly wird verwendet, um anzuzeigen Plotly.newPlot("myPlot", data, layout);
Balkendiagramme
Quellcode
var xArray = ["Italien","Frankreich","Spanien","Vereinigte Staaten","Argentinien"]; var yArray = [55, 49, 44, 24, 15]; var data = [{ x: xArray, y: yArray, type: "bar" }]; var layout = {title: "Globale Weinproduktion"}; Plotly.newPlot("myPlot", data, layout);
Horizontale Balkendiagramme
Quellcode
var xArray = [55, 49, 44, 24, 15]; var yArray = ["Italien","Frankreich","Spanien","Vereinigte Staaten","Argentinien"]; var data = [{ x: xArray, y: yArray, type: "bar", orientation: "h" }; var layout = {title: "Globale Weinproduktion"}; Plotly.newPlot("myPlot", data, layout);
Kuchen diagramm
Wenn Sie ein Diagramm in Form eines Kuchens anstatt einer Balken diagramm anzeigen möchten, ändern Sie x und y in labels und values um und ändern Sie den Typ in "pie":
var data = [{ labels: xArray, values: yArray, type: "pie" };
Donut-Diagramm
Um einen Donut anstatt eines Kuchens anzuzeigen, fügen Sie einen hole hinzu:
var data = [{ labels: xArray, values: yArray, hole: .4, type: "pie" };
Zeichnen Sie die Gleichung
Quellcode
var exp = "Math.sin(x)"; // Werte generieren var xValues = []; var yValues = []; for (var x = 0; x <= 10; x += 0.1) { yValues.push(eval(exp)); xValues.push(x); } // Plotly wird verwendet, um anzuzeigen var data = [{x:xValues, y:yValues, mode:"lines"}]; var layout = {title: "y = " + exp}; Plotly.newPlot("myPlot", data, layout);