2
0
tentone 4 жил өмнө
parent
commit
bf8c7af444

+ 121 - 0
build/escher.js

@@ -4988,6 +4988,126 @@
 		this.barStyle = data.barStyle !== null ? Style.parse(data.barStyle) : null;
 	};
 
+	/**
+	 * Pie chart represents a set of data in a pie like chart graph.
+	 * 
+	 * The values are drawn in porportion relative to their sum.
+	 *
+	 * @class
+	 * @extends {Object2D}
+	 */
+	function PieChart()
+	{
+		Object2D.call(this);
+
+		/**
+		 * Data to be displayed on the pie chart. Each element should store a value and a stroke/fill styles.
+		 * 
+		 * Each element should use the following structure {value: 0.0, fillStyle: ..., strokestyle: ...}.
+		 */
+		this.data = [
+			{value: 10, fillStyle: new ColorStyle("#FD5748"), strokeStyle: new ColorStyle("#AAAAAA")},
+			{value: 15, fillStyle: new ColorStyle("#23AB48"), strokeStyle: new ColorStyle("#AAAAAA")},
+			{value: 5, fillStyle: new ColorStyle("#6285F8"), strokeStyle: new ColorStyle("#AAAAAA")}
+		];
+
+		/**
+		 * Radius of the pie chart object.
+		 *
+		 * @type {number}
+		 */
+		this.radius = 50;
+
+		/**
+		 * The line width of each pie chart section.
+		 *
+		 * @type {number}
+		 */
+		this.lineWidth = 1.0;
+
+		/**
+		 * Start angle of the pie chart.
+		 *
+		 * @type {number}
+		 */
+		this.startAngle = 0;
+
+		/**
+		 * End angle of the pie chart.
+		 *
+		 * @type {number}
+		 */
+		this.endAngle = 2 * Math.PI;
+	}
+
+	PieChart.prototype = Object.create(Object2D.prototype);
+	PieChart.prototype.constructor = PieChart;
+	PieChart.prototype.type = "PieChart";
+	Object2D.register(PieChart, "PieChart");
+
+	PieChart.prototype.isInside = function(point)
+	{
+		return point.length() <= this.radius;
+	};
+
+	PieChart.prototype.draw = function(context)
+	{
+		var sum = 0;
+		for(var i = 0; i < this.data.length; i++)
+		{
+			sum += this.data[i].value;
+		}
+
+		context.lineWidth = this.lineWidth;
+		
+		var angleRange = this.endAngle - this.startAngle;
+		var angle = this.startAngle;
+
+		for(var i = 0; i < this.data.length; i++)
+		{
+			var section = angleRange * (this.data[i].value / sum);
+			
+			context.beginPath();
+			context.arc(0, 0, this.radius, angle, section);
+			
+			angle += section;
+
+			if(this.data[i].strokeStyle)
+			{
+				context.strokeStyle = this.data[i].strokeStyle.get(context);
+				context.stroke();
+			}
+
+			if(this.data[i].fillStyle)
+			{
+				context.fillStyle = this.data[i].fillStyle.get(context);
+				context.fill();
+			}
+		}
+	};
+
+	PieChart.prototype.serialize = function(recursive)
+	{
+		var data = Object2D.prototype.serialize.call(this, recursive);
+
+		data.radius = this.radius;
+		data.lineWidth = this.lineWidth;
+		data.startAngle = this.startAngle;
+		data.endAngle = this.endAngle;
+
+		return data;
+	};
+
+	PieChart.prototype.parse = function(data, root)
+	{
+		Object2D.prototype.parse.call(this, data, root);
+
+		this.radius = data.radius;
+		this.lineWidth = data.lineWidth;
+		this.startAngle = data.startAngle;
+		this.endAngle = data.endAngle;
+	};
+
 	/**
 	 * Pattern style represents an opaque object describing a pattern, based on an image, a canvas, or a video.
 	 *
@@ -6104,6 +6224,7 @@
 	exports.Object2D = Object2D;
 	exports.Pattern = Pattern;
 	exports.PatternStyle = PatternStyle;
+	exports.PieChart = PieChart;
 	exports.Pointer = Pointer;
 	exports.QuadraticCurve = QuadraticCurve;
 	exports.RadialGradientStyle = RadialGradientStyle;

+ 5 - 1
examples/charts.html

@@ -39,7 +39,6 @@
 		graph.box.min.set(-500, -50);
 		graph.box.max.set(500, 50);
 		graph.position.set(300, 400);
-		graph.strokeStyle = null;
 		graph.draggable = true;
 		group.add(graph);
 
@@ -76,6 +75,11 @@
 			graph.data.push((Math.cos(i / 3) + 1) * 3);
 		}
 
+		var graph = new Escher.PieChart();
+		graph.position.set(500, 1000);
+		graph.draggable = true;
+		group.add(graph);
+
 		var viewport = new Escher.Viewport(canvas);
 
 		var renderer = new Escher.Renderer(canvas);

+ 0 - 1
examples/physics.html

@@ -101,7 +101,6 @@
 		var renderer = new Escher.Renderer(canvas);
 		renderer.createRenderLoop(group, viewport, function()
 		{
-
 			plane.position.y = -planeBody.position[1];
 			
 			// Move physics bodies forward in time

+ 1 - 0
source/Escher.js

@@ -28,6 +28,7 @@ export {Graph} from "./objects/chart/Graph.js";
 export {ScatterGraph} from "./objects/chart/ScatterGraph.js";
 export {BarGraph} from "./objects/chart/BarGraph.js";
 export {Gauge} from "./objects/chart/Gauge.js";
+export {PieChart} from "./objects/chart/PieChart.js";
 
 export {Style} from "./objects/style/Style.js";
 export {ColorStyle} from "./objects/style/ColorStyle.js";

+ 124 - 0
source/objects/chart/PieChart.js

@@ -0,0 +1,124 @@
+import {Object2D} from "../../Object2D.js";
+import { ColorStyle } from "../style/ColorStyle.js";
+
+/**
+ * Pie chart represents a set of data in a pie like chart graph.
+ * 
+ * The values are drawn in porportion relative to their sum.
+ *
+ * @class
+ * @extends {Object2D}
+ */
+function PieChart()
+{
+	Object2D.call(this);
+
+	/**
+	 * Data to be displayed on the pie chart. Each element should store a value and a stroke/fill styles.
+	 * 
+	 * Each element should use the following structure {value: 0.0, fillStyle: ..., strokestyle: ...}.
+	 */
+	this.data = [
+		{value: 10, fillStyle: new ColorStyle("#FD5748"), strokeStyle: new ColorStyle("#AAAAAA")},
+		{value: 15, fillStyle: new ColorStyle("#23AB48"), strokeStyle: new ColorStyle("#AAAAAA")},
+		{value: 5, fillStyle: new ColorStyle("#6285F8"), strokeStyle: new ColorStyle("#AAAAAA")}
+	];
+
+	/**
+	 * Radius of the pie chart object.
+	 *
+	 * @type {number}
+	 */
+	this.radius = 50;
+
+	/**
+	 * The line width of each pie chart section.
+	 *
+	 * @type {number}
+	 */
+	this.lineWidth = 1.0;
+
+	/**
+	 * Start angle of the pie chart.
+	 *
+	 * @type {number}
+	 */
+	this.startAngle = 0;
+
+	/**
+	 * End angle of the pie chart.
+	 *
+	 * @type {number}
+	 */
+	this.endAngle = 2 * Math.PI;
+}
+
+PieChart.prototype = Object.create(Object2D.prototype);
+PieChart.prototype.constructor = PieChart;
+PieChart.prototype.type = "PieChart";
+Object2D.register(PieChart, "PieChart");
+
+PieChart.prototype.isInside = function(point)
+{
+	return point.length() <= this.radius;
+};
+
+PieChart.prototype.draw = function(context)
+{
+	var sum = 0;
+	for(var i = 0; i < this.data.length; i++)
+	{
+		sum += this.data[i].value;
+	}
+
+	context.lineWidth = this.lineWidth;
+	
+	var angleRange = this.endAngle - this.startAngle;
+	var angle = this.startAngle;
+
+	for(var i = 0; i < this.data.length; i++)
+	{
+		var section = angleRange * (this.data[i].value / sum);
+		
+		context.beginPath();
+		context.arc(0, 0, this.radius, angle, section);
+		
+		angle += section;
+
+		if(this.data[i].strokeStyle)
+		{
+			context.strokeStyle = this.data[i].strokeStyle.get(context);
+			context.stroke();
+		}
+
+		if(this.data[i].fillStyle)
+		{
+			context.fillStyle = this.data[i].fillStyle.get(context);
+			context.fill();
+		}
+	}
+};
+
+PieChart.prototype.serialize = function(recursive)
+{
+	var data = Object2D.prototype.serialize.call(this, recursive);
+
+	data.radius = this.radius;
+	data.lineWidth = this.lineWidth;
+	data.startAngle = this.startAngle;
+	data.endAngle = this.endAngle;
+
+	return data;
+};
+
+PieChart.prototype.parse = function(data, root)
+{
+	Object2D.prototype.parse.call(this, data, root);
+
+	this.radius = data.radius;
+	this.lineWidth = data.lineWidth;
+	this.startAngle = data.startAngle;
+	this.endAngle = data.endAngle;
+};
+
+export {PieChart};