
.13.e3.source-code.plot.scala Maven / Gradle / Ivy
/*
Copyright 2010 Aaron J. Radke
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cc.drx
object Plot{
// [x] 1) function with all all specified
// [ ] 2) function with auto y bounds
// [ ] 3) y data only, auto y range, and downsample/clobber to fit within 100px in chars along auto x range to length of group
def apply[A,B](f: A => B, xrange:Bound[A], yrange:Bound[B])(implicit bx:Bound.Boundable[A], by:Bound.Boundable[B]) =
PlotFunction(f,xrange,yrange)
}
case class PlotFunction[A,B](f: A => B, xrange:Bound[A], yrange:Bound[B])(implicit bx:Bound.Boundable[A], by:Bound.Boundable[B]){
override def toString = ascii('*')
def ascii(mark:Char):String = {
val ydomain = Bound(0,30)
val yscale = yrange --> ydomain
val spaces = Vector.fill(ydomain.max)(' ')
val rows = xrange take 100 map {x =>
val y = yscale(f(x))
spaces.updated(y, mark).reverse
}
val table = rows.transpose.map{_.mkString}.mkString("\n")
f"${yrange.max}%-100s\n$table\n${xrange.max}%100s"
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy