⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 barchartlegend.as

📁 FLASH实现的bar chart组件
💻 AS
📖 第 1 页 / 共 2 页
字号:
		// Finally ready to plot values
		// ----------------------------
		
		var lineThickness:Number = 3;
		var dropShadowColor:Number = 0x666666;
		var dropShadowAlpha:Number = 50;
		var dropShadowOffset:Number = 2;
		
		var lastPlotX:Number;
		var lastPlotY:Number;
		
		for (var i:Number=0; i<__lines.length; i++)
		{
			var tempLine:Object = __lines[i];
			trace("dot in this "+tempLine.points.toString());
			var linePoints:Array = tempLine.points;
			var lineLabel:String = tempLine.label;
			var tempLineColor:Number = tempLine.color;
			
			
			
			// Loop thru points to plot - DROP SHADOW THIS TIME
			
			//vAxis_mc.lineStyle(lineThickness, dropShadowColor, dropShadowAlpha);
			//define a mc container to hold the whole line, so we can control it in later time.
			var containerName = "line_"+i;
			var lineContainer = vAxis_mc.createEmptyObject(containerName,1000+i);
			lineContainer.lineStyle(lineThickness, dropShadowColor, dropShadowAlpha);
			//for (var j:Number=0; j<linePoints.length; j++)
			//{
				//var tempPlotValue:Number = linePoints[j];
				
				// Prepare layout values - reference hPoints position
				//var tempPlotX:Number = __hPoints[j].hAxisX;
				//var tempPlotY:Number = getPlotY(tempPlotValue);
				/*
				if (j!=0)
				{
					// Actually draw drop shadow if not first item
					
					// vAxis_mc.moveTo(lastPlotX, lastPlotY + dropShadowOffset);
					
					vAxis_mc.lineTo(tempPlotX, tempPlotY + dropShadowOffset);
				}
				else
				{
					// Move to starting position for dropshadow
					
					vAxis_mc.moveTo(tempPlotX, tempPlotY + dropShadowOffset);
					
				}
				*/
				//lastPlotX = tempPlotX;
				//lastPlotY = tempPlotY;
			//}
			
			// Loop thru points to plot - ACTUAL LINE THIS TIME
			
			vAxis_mc.lineStyle(lineThickness, tempLineColor, 100);
			for (var j:Number=0; j<linePoints.length; j++)
			{
				var tempPlotValue:Number = linePoints[j];
				
				
				// Prepare layout values - reference hPoints position
				var tempPlotX:Number = __hPoints[j].hAxisX;
				var tempColumnName:String = __hPoints[j].label;
				var tempPlotY:Number = getPlotY(tempPlotValue);
				
				
				
				
				//Draw the bar rectangle according to the xAxis_Step, itemCount [The items count inner a x axis section]
				var w:Number = width;
				w -= 100;
				var numMarkers:Number = __hPoints.length;
				var sectionW:Number = w / (numMarkers - 1);
				var barSpace:Number = sectionW/(itemCount*2+1);	//keep the space half of the bar's width
				var barWidth = (sectionW-(itemCount+1)*barSpace)/itemCount;
				//barWidth = (barWidth > 16)? 16 : barWidth;
				trace("value:"+__hPoints.length+"------"+sectionW+"barWidth"+ barWidth+"itemCount:" + itemCount);
				
				
				var startX = tempPlotX + (i+1)*barSpace + i*barWidth;
				var layoutY:Number = topPadding;
				var startY:Number = Math.round(layoutY - 0.5);
				trace(startX+","+startY +","+barWidth );
				
				/*Modified by Roland Zhu 
				if (j!=0)
				{
					// Actually draw line if not first item
					
					// vAxis_mc.moveTo(lastPlotX, lastPlotY);

					vAxis_mc.lineTo(startX+barWidth, tempPlotY);
				}
				else
				{
					// Move to starting position for line
					vAxis_mc.moveTo(startX, tempPlotY);
				}
				*/
				
				// -----------------
				// Add mouseover box
				// -----------------
				
				var mouseOverBoxName:String = "mouseoverbox_" + i + "_" + j + "_mc";
				trace("The var mc name in the vaxis_mc"+vAxis_mc["mouseoverbox_" + i + "_" + j + "_mc"]._name);
				var mouseOverBox:MovieClip =vAxis_mc.createEmptyObject(mouseOverBoxName, 10000+i*linePoints.length +j);
				
				//draw the viewable bar, but it has not the mouse event upon it
				drawThickBox(lineContainer,startX, Math.round(height - hAxis_mc._height) - 5,startX+barWidth,tempPlotY,tempLineColor,0,1,0);
				
				/*Modified by Roland Zhu
				Put the bar instance into a container, and define the mouse event upon it, in later time, we will control its visible
				*/
				mouseOverBox.createEmptyObject("hover_mc", 1);
				drawThickBox(mouseOverBox.hover_mc,startX, Math.round(height - hAxis_mc._height) - 5,startX+barWidth,tempPlotY,tempLineColor,0,0x00AAFF,0,0);
				
				
				drawThickBox(mouseOverBox.hover_mc,startX-2,tempPlotY-2,startX+2,tempPlotY+2,0x00AAFF,0,1,0);
				drawThickBox(mouseOverBox.hover_mc,startX+barWidth-2,tempPlotY-2,startX+barWidth+2,tempPlotY+2,0x00AAFF,0,1,0);
				drawThickBox(mouseOverBox.hover_mc,startX-2,Math.round(height - hAxis_mc._height) - 5-2,startX+2,Math.round(height - hAxis_mc._height) - 5+2,0x00AAFF,0,1,0);
				drawThickBox(mouseOverBox.hover_mc,startX+barWidth-2,Math.round(height - hAxis_mc._height) - 5-2,startX+barWidth+2,Math.round(height - hAxis_mc._height) - 5+2,0x00AAFF,0,1,0);
				mouseOverBox.hover_mc._alpha =0;	//hide the hover_mc in init time,only show it as the mouse roll over it
				/*
				
				mouseOverBox.hover_mc.createTextField("value_txt", 3, startX-2, tempPlotY-16, 0, 0);
				var vf:TextField = mouseOverBox.hover_mc.value_txt;
				vf.selectable = false;
				//vf.html = true;
				vf.embedFonts= true;
				vf.text =   numberPrefix + tempPlotValue + numberPostfix ;
				vf.setTextFormat(titleFormatBold);
				vf._width = vf.textWidth + 5;
				vf._height = vf.textHeight + 5;
				*/
				
				mouseOverBox._x = _parent._x;	
				mouseOverBox._y = _parent._y;	//keep the bar's relative postion equal to the v axis
				
				
				mouseOverBox.createEmptyObject("content_mc", this.getNextHighestDepth());
				//mouseOverBox.hover_mc.useHandCursor = false;
				mouseOverBox.content_mc._visible = false;
				
				// Invisible hoverbox
				//drawRoundRect(mouseOverBox.hover_mc, 8, 8, 5, 5, 0, 0xFFD400, 100);	//modified by Roland Zhu to show the vertex upon the line.
				
				// Tooltip hover dot
				var mouseOverDotDiameter:Number = 10;
				mouseOverBox.content_mc.lineStyle(1, tempLineColor, 100);
				//drawRoundRect(mouseOverBox.content_mc, 5, 5, 10, 10, 5, 0xFFFFFF, 100);
				//mouseOverBox.content_mc.lineStyle(0, tempLineColor, 0);
				//drawRoundRect(mouseOverBox.content_mc, 8, 8, 4, 4, 2, tempLineColor, 100);
				
				// Tooltip message
				var mouseOverMessage:String = (lineLabel!=undefined && lineLabel != "") ? "<b>" + lineLabel + "</b><br>" : "";
				mouseOverMessage += __hTitle +": "+ tempColumnName + "<br>" + __vTitle +": "+  tempPlotValue + numberPostfix;
								
				mouseOverBox.content_mc.createTextField("label_txt", 2, 2, -2, 0, 0);
				var tf:TextField = mouseOverBox.content_mc.label_txt;
				tf.selectable = false;
				tf.html = true;
				tf.multiline = true;
				tf.htmlText = "<font face='Verdana' size='10'>" + mouseOverMessage + "</font>";
				tf._width = tf.textWidth + 5;
				tf._height = tf.textHeight + 5;
				tf._y -= tf._height;
				
				var o:MovieClip = mouseOverBox.content_mc.createEmptyObject("labelbg_mc", 1);
				o._x = tf._x - 2;
				o._y = tf._y - 1;
				o.lineStyle(1, tempLineColor, 100);
				drawRoundRect(o, 0, 0, tf._width + 4, tf._height + 3, 0, 0xFFFFFF, 80); // bg
				o.lineStyle(0, tempLineColor, 0);
				drawRoundRect(o, 1, tf._height + 3.5, tf._width+4.5, 1, 0, 0x666666, 50); // inner shad horiz
				drawRoundRect(o, tf._width + 4.5, 1, 1, tf._height+2.5, 0, 0x666666, 50); // inner shad vert
				drawRoundRect(o, 2, tf._height + 4.5, tf._width+4.5, 1, 0, 0x666666, 20); // outer shad horiz
				drawRoundRect(o, tf._width + 5.5, 2, 1, tf._height+2.5, 0, 0x666666, 20); // outer shad vert
				
				//Added by Roland Zhu
				mouseOverBox.content_mc._x = startX ;	//put the popup pane at the center-top of the current bar relatively
				mouseOverBox.content_mc._y = tempPlotY -3;
				/*Added by Roland Zhu 2006.9.19
				 *Control the coordinate of the float pane when it is near from the stage boundary
				 */
				 if(startX + mouseOverBox.content_mc._width > this.width){	//this is the last point, then move the position of the float pane to make it may show in the stage area.
					trace("The original postion"+mouseOverBox.content_mc._x);
					var offset = mouseOverBox.content_mc._width;
					mouseOverBox.content_mc._x -=  offset;
					
					if(mouseOverBox.content_mc._x < 0) { 
						mouseOverBox.content_mc._x = 20;	//keep the left border inner the visible area
						
					}
					
				 }
				 
				// Mouse events
				mouseOverBox.hover_mc.onRollOver = function():Void
				{
					trace("Mouse roll over the hoverMc");
					this._parent.content_mc._visible = true;
					this._alpha = 100;
				}
				mouseOverBox.hover_mc.onRollOut = mouseOverBox.hover_mc.onDragOut = function():Void
				{
					trace("Mouse roll out the hoverMc");
					this._parent.content_mc._visible = false;
					this._alpha = 0;
					
					
				}
				
				// -- Done with mouseoverbox --
				
				// Update lastPlot values
				lastPlotX = tempPlotX;
				lastPlotY = tempPlotY;
			}
		}
		
	}
	
	function convertLineObj(obj:Object):Array
	{
		// Create reference object
		var referenceObj:Object = {};
		for (var i:Number=0; i<__hPoints.length; i++) {
			var tempItem:Object = __hPoints[i];
			referenceObj[tempItem.id] = i;
		}
		// Create ordered array
		var returnArray:Array = [];
		for (var i:String in obj)
		{
			var tempOrder = referenceObj[i];
			var tempVal:Number = obj[i];
			returnArray[tempOrder] = tempVal;
		}
		return returnArray;
	}
	
	//function plotBar(xScale,yScale,backColor,lineColor,barColor){
	function plotBar(bar:Object,label:String,color:Number,count:Number):Void{
		var plotValues:Array = convertLineObj(bar);
		var labelValue:String = label; //label!=undefined ? label : "(None)";
		var colorValue:Number;
		itemCount = count;
		trace("itemcount in plotBar function:"+itemCount);
		if (color!=undefined)
		{
			colorValue = color;
		}
		else
		{
			var colorIndex:Number;
			if (__defaultColors.length > __lines.length)
			{
				colorIndex = __lines.length;
			}
			else
			{
				colorIndex = __lines.length - (Math.floor(__lines.length / __defaultColors.length) * __defaultColors.length);
			}
			colorValue = __defaultColors[colorIndex];
		}
		
		__lines.push({points:plotValues, label:labelValue, color:colorValue});
		
		// Tell to draw in future
		invalidate();
	}
		
	function drawLegend(o:MovieClip, pad:Number, spacing:Number,legendWidth:Number):Void
	{
		var padding:Number = pad!=undefined ? pad : 5;
		//var spacing:Number = 100;
		var layoutY:Number = 0;
		__spacing = spacing;
		// Remove previous legend remains
		
		o.clear();
		
		for (var i:String in o)
		{
			if (i.substr(0, 13)=="legend_label_")
			{
				o[i].removeTextField();
			}
		}
		
		// Loop through lines and draw legend
		
		for (var i:Number=0; i<__lines.length +1; i++)	//appended a special button "check all" button
		{
			// Prepare vars
			var tempLine:Object = __lines[i];
			var tempLabel:String = tempLine.label;
			var tempColor:Number = tempLine.color;
			
			/*
			// Draw box
			drawRoundRect(o, padding, layoutY, 10, 15, 0, tempColor, 100);
			
			// Draw label
			var labelName:String = "legend_label_" + i;
			o.createTextField(labelName, o.getNextHighestDepth(), padding + 15, layoutY, 0, 10);
			var t:TextField = o[labelName];
			t.selectable = false;
			t.autoSize = "left";
			t.setNewTextFormat(labelFormatBold);
			t.text = tempLabel;
			*/
			
			/*Added by Roland Zhu 2006.8.10
			 *Create the click-able legend, and using these buttons to control the correstponded bar or line show the emphasized view
			 */
			 //create the button (simpleButton) instance and set its properties to show as a legend
			 var tpBtn = o.createClassObject(SimpleButton,"lgd_"+i,o.getNextHighestDepth(),null);
			tpBtn.toggle = true;
			tpBtn.icon = "";
			tpBtn.setStyle("textIndent",17);
			tpBtn._x = padding + 15;
			tpBtn._y = layoutY;
			tpBtn.setStyle("fontSize",11);
			//tpBtn.setStyle("fontWeight","bold");
			//tpBtn.setStyle("color",0x428ECE);
			tpBtn.setStyle("color",0x000000);
			tpBtn.setStyle("borderColor",0x000000);
			tpBtn.setStyle("highlightColor",0xcccccc);
			//tpBtn.setStyle("textDecoration","underline");
			tpBtn.setState(true);	//set clicked state when initialization
			
			//define the event handler for the click event to listen which legen-button was released
			var chartMc =this;
			tpBtn.clickHandler = function() { chartMc.legendPress(o,this) };
			 
			 if(i != __lines.length){
				tpBtn.setLabel(tempLabel);
				tpBtn.setStyle("buttonColor",tempColor);
			 }
			 else{	//the spcial button which was not created by data
				tpBtn.setLabel("Hide All"); 
			 }
			 //tpBtn.fui._width = __spacing;	//fix the bug that the label can not show the whole text inner it.
			
			//
			// Increment layoutY
			//layoutY += spacing;
			//Modified by Roland Zhu Indrement padding, so the legend icon will be put horizontally
			if(padding + __spacing < legendWidth-50){
				padding += __spacing;
			}
			else{	//jump to next 'line'
				layoutY += 20;
				//reset the padding
				padding = pad!=undefined ? pad : 5;
			}
		}
	}
	
	function legendPress(o:MovieClip,legendBtn:SimpleButton):Void{
		//get the index of the released legend-button, and control the bar which is belongs to this line
		
		var index = legendBtn._name.substr(legendBtn._name.indexOf("_")+1,legendBtn._name.length);
		var state = legendBtn.getState();	//get the released button's state, so then set the bars(lines) according to it
		trace("You clicked the"+index);
		var globalState:Boolean = true;	//the variable to caculate the global state of all the legend button, so it can decide the show/hide all button 
		//Todo: control all the bar within this line
		for (var i:Number=0; i<__lines.length+1; i++)
		{
			var tempLine:Object = __lines[i];
			var linePoints:Array = tempLine.points;
			//after looping is end, then refresh the show/hide all button state
			if(i == __lines.length){
					o["lgd_"+__lines.length].setState(globalState);
					var tpLabelTxt = (globalState) ? "Hide All" : "Show All";
					o["lgd_"+__lines.length].setLabel(tpLabelTxt);
			}
			
			if(Number(index) == __lines.length)	{ 	// the "show/Hide all" button's behaviour
				//change other button's state according to the "check all" button's state
				
				o["lgd_"+i].setState(state);
				vAxis_mc["line_" +i]._visible = state;	//show\hide all lines
				
				for (var j:Number=0; j<linePoints.length; j++)
				{
					vAxis_mc["mouseoverbox_" + i + "_" + j + "_mc"].hover_mc._visible = state;
				}
				
			}
			else{
				//caculate the global state by 'and' operator
				globalState = globalState && o["lgd_"+i].getState();
				if(i!= Number(index)) continue;	//kill the unwanted path when loop through the legend button
				vAxis_mc["line_" +  index ]._visible = state;	//show\hide this line
				 
				for (var j:Number=0; j<linePoints.length; j++)
				{
					trace("The name of dummy"+vAxis_mc["mouseoverbox_" + i + "_" + j + "_mc"]._visible);
					vAxis_mc["mouseoverbox_" + i + "_" + j + "_mc"].hover_mc._visible = state;
				
				}
				
				
			}
			
			
			
			//refresh the show/hide all button's label
			if(Number(index) == __lines.length)	{ 	// the "show/Hide all" button's behaviour		
					
					if(state)
						legendBtn.setLabel("Hide All");
						
					else
						legendBtn.setLabel("Show All");
				//legendBtn.setStyle("textIndent",17);
				//legendBtn.fui._width = legendBtn.fui.textWidth+18;	//fix the bug that the label can not show the whole text inner it.
				//legendBtn.fui.autoSize = "left";
			}
			
		}
	}
	
	/*
	 *Added By Roland Zhu 2006.8.14
	 */
	 function refreshState(buttonIndex:Number):Void{
		 trace("The button index is changed"+ buttonIndex);
		
		 this["lgd_"+buttonIndex].setState(false);	//close this button when found all the hover mc has been hided.
		 //this["lgd_"+buttonIndex].refresh();
	 }
	 
	function drawChart():Void
	{
		drawHAxis();
		drawVAxis();
	}
	
	function toDeg(rad:Number):Number
	{
		// 2PI RAD = 360deg
		var deg:Number = (180 / Math.PI) * rad;
		return deg;
	}
	
	






}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -