/* 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) def apply[A,B](xys:Seq[(A,B)])(implicit bx:Bound.Boundable[A], by:Bound.Boundable[B]) = { val xbound = Bound.find(xys.map{_._1}).require("Bound does not exist for x axis to Plot") val ybound = Bound.find(xys.map{_._2}).require("Bound does not exist for y axis to Plot") val scale = Scale(xys:_*) PlotFunction(scale.apply, xbound*1.1, ybound*1.1) } def println[A,B](xys:Seq[(A,B)])(implicit aBound:Bound.Boundable[A],bBound:Bound.Boundable[B]):Unit = { val func = Scale(xys:_*) val xBound = Bound.find(xys.map{_._1}).get val yBound = Bound.find(xys.map{_._2}).get*1.1 System.out.println(Plot(func.apply, xBound, yBound).ascii('*')) } } 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, size:Vec = Vec(100,30)):String = { val ydomain = Bound(0,size.y.toInt) val yscale = yrange --> ydomain val spaces = Vector.fill(ydomain.max)(' ') val rows = xrange.take(size.x.toInt).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" } }