Can we draw graph in unity

hi to all

is there any option in unity to draw a graph of given values, using GUI functions or any other function to do it.

thanks in advance

unityboy

No, you need to implement your own, likely basing on the line renderer

line renderer means using fuction to draw 2D line on iphone screen?? what funciton we use for this. thanks for reply

There is no 2D on the iphone, all you do is 3D using real geometry.

Please check the manual and script reference on the line renderer and check the component in the components - particle menu

thanks

Here’s some code that I used to draw a graph on a 2D texture. I attached this script to the 2D texture (the graph paper) that the graph is drawn on. There’s some unused code/variables because I adapted this code from a more complex graph-drawing routine. This only draws one set of data with one symbol. It assumes that your background texture already has grid lines, axes, labels, etc. drawn on it. It just plots points scaled to fit the graph. And since this was a one-off, I hard coded some of the variables, which you could set on the fly if needed. Also, this was my first attempt at Unity code, adapted from Lingo code, so it may not be the best way to do this in Unity. But it works. HTH.

var pBlankMember: Texture2D;  // the blank "paper", assigned in Inspector
var pMember: Texture2D;  // the "paper" to draw on, assigned in Inspector
var tMember: Texture2D;  // the symbol to draw, assigned in Inspector

var pXMin: float; // x axis min value
var pXMax: float; // x axis max value
var pYMin: float; // y axis min value
var pYMax: float; // y axis max value
var pXOffset: float;  // x axis origin offset
var pYOffset: float;  // y axis origin offset
var pPlotXRange = new Array();  // min/max
var pPlotYRange = new Array();  // min/max
var pPlotXScale: float;  // size of the bitmap
var pPlotYScale: float;  // size of the bitmap
var pNewXScale: float;  // adjusted scale
var pNewYScale: float;  // adjusted scale
var pDot: String;  // for graphs with more than one symbol
var pNumDots: int;  // number of symbols in graph
var pCurrentDot: int;  // always draws dots in order
var halfWidth: float;  // half size of symbol
var halfHeight: float;  // half size of symbol

var dRect: Rect;
static var gHCenter: float;
var dotColors: Color[];
var xx: int;
var yy: int;

//----------------------------------------------

function InitGraph () {
	
	pXMin = 0.0;
	pXMax = 0.575;
	pYMin = 0.0;
	pYMax = 0.575;
	pXOffset = 0.0;
	pYOffset = 0.0;

	SetXRange(pXMin, pXMax);
	SetYRange(pYMin, pYMax);
  
	pPlotXScale = pMember.width - pXOffset;
	pPlotYScale = pMember.height - pYOffset;
	SetupScale();
  
	pCurrentDot = 1;
	dotColors = pBlankMember.GetPixels(0);
	pMember.SetPixels(dotColors, 0);
	pMember.Apply();
	renderer.material.mainTexture = pMember;
}

//----------------------------------------------

function DrawGraph (theData: Array, theDot: String, numDots: int) {
  
	dotColors = pBlankMember.GetPixels(0);
	pMember.SetPixels(dotColors, 0);
	
	pDot = theDot;
	pNumDots = numDots;
	pCurrentDot = 1;
	SetupScale();
	for (var i=0; i<theData.length; i++) {
		PlotPoint(theData[i].x, theData[i].y);
	}
	pMember.Apply();	
	renderer.material.mainTexture = pMember;
}

//----------------------------------------------

function PlotPoint (xx, yy: float) {
	  
	yy = pPlotYScale - Mathf.Round((yy * pNewYScale) - (pPlotYRange[0] * pNewYScale));
  
	xx = Mathf.Round((xx * pNewXScale) - (pPlotXRange[0] * pNewXScale)) + pXOffset;
	gHCenter = xx;
	    
	halfWidth = Mathf.Round(tMember.width / 2.0) ;
	halfHeight = Mathf.Round(tMember.height / 2.0);
	dRect = Rect(xx - halfWidth, yy - halfHeight, tMember.width, tMember.height);
	
	dotColors = tMember.GetPixels(0);
	pMember.SetPixels(xx - halfWidth, yy - halfHeight, tMember.width, tMember.height, dotColors, 0); 
	
	pCurrentDot++;
	if (pCurrentDot > pNumDots) {
		pCurrentDot = 1;
	}  
}

//----------------------------------------------

function SetXRange (tMin, tMax: float) {  
	pPlotXRange = [tMin, tMax];  
}

//----------------------------------------------

function SetYRange (tMin, tMax: float) {  
	pPlotYRange = [tMin, tMax];  
}

//----------------------------------------------
// must call before plotting first point if anything has changed since InitGraph

function SetupScale () {
	pNewXScale = pPlotXScale/(pPlotXRange[1] - pPlotXRange[0]);
	pNewYScale = pPlotYScale/(pPlotYRange[1] - pPlotYRange[0]);
}

//----------------------------------------------

hi jhon B , could yo pl help me out where i have to place this code and what things i have to add in?
can yo explain in detail pl

Thanks
Arun

yes plz, if u tell us exactly how to use ur script.

when i copy paste it on the javaScript editor, it give me this error :

BCE0019: ‘x’ is not a member of ‘Object’.
BCE0019: ‘y’ is not a member of ‘Object’.
BCE0051: Operator ‘*’ cannot be used with a left hand side of type ‘Object’ and a right hand side of type ‘float’.

and a lot of other errors!! …

i found this through the forum, i don’t know if it will help .

http://forum.unity3d.com/threads/35717-Telestrator-style-overlay