Browse Source

Adding metadata handling to HueChart.

Marcus McLaughlin 15 năm trước cách đây
mục cha
commit
7d88aae6fd

+ 27 - 12
desktop/core/static/js/Source/Hue/HueChart.Box.js

@@ -105,7 +105,7 @@ HueChart.Box = new Class({
 				this.highlighted_index = -1;
 				//Build initial list of data series
 				//Initialize data object
-				if (this.hasData()) this.initializeData();
+				if (this.hasData()) this.initializeData(this.options.metadata);
 				//Create tip to show if showPointValue is true.
 				if (this.options.showPointValue) {
 						this.tip = new HueChart.Tips(this.element, {
@@ -154,16 +154,22 @@ HueChart.Box = new Class({
 			return this.getData().getLength() > 1;
 		},
 
-		initializeData: function() {
-				this.series = [];
-				//Initialize dataSeries
-				dataObjs = this.getData(true).getObjects();
-				//Iterate through the data objects to create a list of the data series.
-				dataObjs.each(function(obj) {
-					Hash.each(obj, function(value, key) {
-						if (!this.series.contains(key) && key != this.options.xProperty) this.series.push(key);
+		initializeData: function(metadata) {
+				if (metadata) {
+					 this.metadata = metadata;
+					 this.series = Hash.getKeys(metadata);
+				} else {
+					delete this.metadata;
+					this.series = [];
+					//Initialize dataSeries
+					dataObjs = this.getData(true).getObjects();
+					//Iterate through the data objects to create a list of the data series.
+					dataObjs.each(function(obj) {
+						Hash.each(obj, function(value, key) {
+							if (!this.series.contains(key) && key != this.options.xProperty) this.series.push(key);
+						}.bind(this));
 					}.bind(this));
-				}.bind(this));
+				}
 				if(this.options.dates.x) {
 						//If the xProperty is a date property, prepare the dates for sorting
 						//Change the xProperty to the new property designed for sorting dates
@@ -176,7 +182,16 @@ HueChart.Box = new Class({
 						this.xProperty = this.options.xProperty;
 						this.getData(true).sortByProperty(this.xProperty);
 				}
-
+		},
+		
+		//If series is not defined, returns true if there is metadata defined for the chart, false otherwise.
+		//If series is defined, returns true if there is metadata defined for the chart and within that metadata there is an entry for the series in question.
+		hasMetadata: function(series) {
+			var hasMeta = $defined(this.metadata);
+			if ($defined(series)) {
+				hasMeta = hasMeta & Hash.has(this.metadata, series);
+			}
+			return hasMeta;
 		},
 
 		//Add data -- redefined to add series to the array of series
@@ -348,7 +363,7 @@ HueChart.Box = new Class({
 				var invertedYValue = scale.invert(y);
 				//Since range will be inverted, the array goes from greatest to least initially.
 				var invertedYRange = [scale.invert(y + yBuffer), scale.invert(y - yBuffer)];
-				var yValue = this.yValueReverse(invertedYValue); 
+				var yValue = this.yValueReverse(invertedYValue);
 				//Convert the inverted yRange to a non-inverted yRange.
 				var yRange = invertedYRange.map(function(value) { return this.yValueReverse(value); }.bind(this));
 				return yRange;

+ 14 - 0
desktop/core/static/js/Source/Hue/HueChart.Line.js

@@ -39,8 +39,21 @@ HueChart.Line = new Class({
 		addGraph: function(vis) {
 				//In effort to maintain same data structure for lines and stacked charts, iterating
 				//through the series and adding a line using each series for its data points.
+				var valueToPV = {'continuous': 'linear', 'discrete':'step-after'};
+				//Get interpolation from metadata and convert to ProtoVis value
+				var getInterpolation = function(series) {
+					var interpolation = 'linear';
+					if (this.hasMetadata(series)) {
+						metaValue = this.metadata[series]['interpolation'];
+						if ($defined(metaValue)) interpolation = valueToPV[metaValue];
+					}
+					return interpolation;
+				}.bind(this);
+
+				var hasMetadata = $defined(this.metadata);
 				for (var itemIndex = 0; itemIndex < this.series.length; itemIndex++) {
 						var series = this.series[itemIndex];
+						var interpolation = getInterpolation(series);
 						var dataSeries = this.getData(true).getDataSeries(series);
 						//Add a line to the visualization, connecting points produced from the data object.
 						var visLine = vis.add(pv.Line)
@@ -62,6 +75,7 @@ HueChart.Line = new Class({
 												return this.yScale(d[this.series[itemIndex]]);
 										}.bind(this);
 								}.bind(this)(itemIndex))
+								.interpolate(interpolation)
 								//Make the line's width 3 pixels.
 								.lineWidth(this.options.lineWidth);
 						if (this.options.dot.size) {

+ 2 - 2
desktop/core/static/js/Source/Hue/HueChart.js

@@ -156,10 +156,10 @@ HueChart = new Class({
 		},
 		
 		//Change full data object
-		setData: function(data) {
+		setData: function(data, metadata) {
 				this.data = new HueChart.Data(data);
 				delete this.currentData;
-				if (this.hasData() && this.initializeData) this.initializeData();
+				if (this.hasData() && this.initializeData) this.initializeData(metadata);
 		},
 
 		//Set the currentData, which is the data currently being displayed, assuming it is different from the base data.  Should be a subset of the main data object.