New topic: 

EDIT: Line Chart

<http://forums.realsoftware.com/viewtopic.php?t=37074>

         Page 1 of 1
   [ 4 posts ]                 Previous topic | Next topic          Author  
Message        Sky Stream          Post subject: EDIT: Line ChartPosted: Thu 
Jan 13, 2011 9:04 pm                         
Joined: Sun Nov 23, 2008 12:27 am
Posts: 206
Location: New Jersey                I am working on my next chart which is a 
line chart and I am debating the best way to handle plotting multiple lines. 
The only way I can think of would have to use an array within an array to draw 
the line on the chart.

For example, lets say I have 2 lines with the following coordinate:

line 1: 100, 90, 80, 70, 60
line 2: 50, 40, 30, 20, 10

now, when I want to pass these coordinates to the method that will draw the 
graph, I was thinking about passing it as a string as 
"100,90,80,70,60|50,40,30,20,10"

now, the question is... is the below code the best way to handle this process?

Code:  dim s as string = "100,90,80,70,60|50,40,30,20,10"
  dim firstPass() as String = split(s,"|")
  
  dim firstCount, secondCount as Integer
  dim secondPass() As String
  
  for firstCount = 0 to firstPass.Ubound
  secondPass = split(firstPass(FirstCount), ",")
  
  for secondCount = 0 to secondPass.Ubound
  // this is where I would plot the coordinates for the line chart
  MsgBox secondPass(secondCount)
  
  next
  
  next


I wasnt sure if I was complicating things more than I need to.      
_________________
- Rich

RealStudio 2010 R5 Enterprise
Dell Latitude E6400 running Windows XP SP3 / Mac Pro, Macbook Pro & Mac Mini 
running OS X 10.6.4      

    Last edited by Sky Stream on Fri Jan 14, 2011 3:47 am, edited 1 time in 
total.   
                             Top                 mauitom          Post subject: 
Re: Using an array with an arrayPosted: Fri Jan 14, 2011 2:21 am                
                 
Joined: Thu Feb 16, 2006 10:04 pm
Posts: 69                I don't understand why you're showing 5 points for the 
line, it only takes 4 

I would wrap each of your LineObects in to a class. Each of these would have 
the end points of the line, a color, a pen width, etc. And it would have the 
ability to draw itself. You'd have a controller class of sorts that would hold 
an array of these objects, looping through the array and call the draw method 
on each line object.

This is one way to do it, there really is no one best way to code in my 
opinion, only the best way it makes sense to you.      
_________________
Thomas C.
REAL.basic Blog
http://bigdaddysurf.com/blog/  
                             Top                Sky Stream          Post 
subject: Re: Using an array with an arrayPosted: Fri Jan 14, 2011 3:39 am       
                  
Joined: Sun Nov 23, 2008 12:27 am
Posts: 206
Location: New Jersey                Thank you Thomas for your reply...

I didn't give the bigger picture of what I am working on which is why the 5 
points confused you  Basically, the 5 points are X coordinates only. The Y 
coordinates are calculated based upon width of the graph divided by total 
number of items.

Below is is a working version of the line chart I have been working on:

This can go in the open of a Canvas
Code:  dim lineChart as new rhChart
  dim dataItem() As Double, dataLabel() as String
  
  dataItem = array(10.0,50.0,100.0,75.0)
  dataLabel = array("2006","2007","2008","2009")
  
  // Chart Width, Chart Height, Chart Title, Minimum Scale, Maximium Scale, 
Number Of Gridlines, Data Item Array, Data Label Array, Data Color
  me.Backdrop = lineChart.lineChart(me.Width, me.Height,"Line Chart", 0,100,4, 
dataItem, dataLabel,&c7F007F)


I create a class called rhChart with:

Method Name: lineChart
Parameters: picWidth As Integer, picHeight As Integer, chartTitle As String, 
minScale As Double, maxScale As Double, scaleSpacing as Double, dataItem() as 
Double, dataLabel() as String, dataColor As Color
Return Type: Picture

Code:  // Draw Picture Width & Height
  dim pic as new Picture (picWidth, picHeight, 32)
  
  // Chart Title
  pic.Graphics.Bold = true
  pic.Graphics.DrawString(chartTitle,((picWidth/2) - 
(pic.Graphics.StringWidth(chartTitle)/2)),20)
  
  // Draw Y-Axis, X-Axis, Scale Markers, Scale Label
  dim yPosition, scaleHeight, rowCount, rowIncrement, scaleIncrement, 
rangeIncrement as Double
  
  pic.Graphics.Bold = false
  rangeIncrement = (maxScale - minScale) / scaleSpacing
  scaleHeight = (picHeight-30)
  
  if InStr(str(rangeIncrement),".") = 0 then
  yPosition = 40
  
  else
  yPosition = 60
  
  end if
  
  for rowCount = 0 to scaleSpacing
  pic.Graphics.DrawLine(yPosition,scaleHeight,picWidth-30,scaleHeight)
  pic.Graphics.DrawString(str(Ceil(scaleIncrement*100)/100), 10, 
(scaleHeight+5))
  
  if rowCount = scaleSpacing then
  pic.Graphics.DrawLine(yPosition,scaleHeight,yPosition,picHeight-30)
  
  end if
  
  scaleIncrement = scaleIncrement + rangeIncrement
  rowIncrement = rowIncrement + ((picHeight-60) / scaleSpacing)
  scaleHeight = ((picHeight-30) - rowIncrement)
  
  next
  
  // Draw Line Graph
  dim startPosition, endPosition, startLineHeight, endLineHeight, 
startLineConvert, endLineConvert, lineSpacing, lineCount as Double
  
  lineSpacing = ((picWidth-75) / (dataItem.Ubound+1))
  startPosition = yPosition + (lineSpacing/2)
  
  for lineCount = 0 to dataItem.Ubound
  if lineCount <> dataItem.Ubound then
  
  startLineConvert = ((dataItem(lineCount) - minScale) * (picHeight-60)) / 
(maxScale - minScale)
  endLineConvert = ((dataItem(lineCount+1) - minScale) * (picHeight-60)) / 
(maxScale - minScale)
  startLineHeight = ((((picHeight-60) * startLineConvert) / (picHeight-60)) * 
-1) + (picHeight-30)
  endLineHeight = ((((picHeight-60) * endLineConvert) / (picHeight-60)) * -1) + 
(picHeight-30)
  
  endPosition = startPosition + (lineSpacing)
  
  pic.Graphics.ForeColor = dataColor
  pic.Graphics.PenWidth = 5
  pic.Graphics.DrawLine(startPosition,startLineHeight,endPosition,endLineHeight)
  
  end if
  
  pic.Graphics.ForeColor = &c000000
  pic.Graphics.PenWidth = 1
  
pic.Graphics.DrawString(dataLabel(lineCount),(startPosition-(pic.Graphics.StringWidth(dataLabel(lineCount))/2)),(picHeight-10))
  
pic.Graphics.DrawLine(startPosition,(picHeight-30),startPosition,(picHeight-25))
  
  startPosition = startPosition + (lineSpacing)
  
  next
  
  // Return Picture
  Return pic

  
Now.. this will go ahead and draw a line to the 4 X coordinates in the dataItem 
Variable. What I was attempting to solve and I should probably clarify is to 
draw a completely separate line that is not attached to the first. For example, 
lets say I wanted to chart Rainbows & Ponies sightings, each of them would have 
their own line. In order to do this, I took a look at the google chart api and 
I saw that they where passing this information as a string such as "50 30 50 10 
| 40 50 20 10" and using | as the split. Then, I assumed that they break it 
down to a usable format in which they can start plotting the lines.

I just wants sure if what I was thinking above was the best way to do it or if 
I was creating more work for myself


Oh.. and if anyone needs a line chart.. there you go     
_________________
- Rich

RealStudio 2010 R5 Enterprise
Dell Latitude E6400 running Windows XP SP3 / Mac Pro, Macbook Pro & Mac Mini 
running OS X 10.6.4      

    Last edited by Sky Stream on Fri Jan 14, 2011 4:03 am, edited 1 time in 
total.   
                             Top                 jefftullin          Post 
subject: Re: EDIT: Line ChartPosted: Fri Jan 14, 2011 4:03 am                   
              
Joined: Wed Nov 15, 2006 3:50 pm
Posts: 2066
Location: England                Wouldnt polygons etc be more useful here?
if you create Object2D items, and group them, then you only need to scale the 
group and have it draw itself...   
                             Top             Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 4 posts ]      
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

[email protected]

Reply via email to