Commit 9f7a956d authored by h2b's avatar h2b

Basic bar-chart shape added.

parent 14935d8a
/*
SimGraf - A Simple Scala Graphics Library
Copyright 2016-2018 Hans-Hermann Bode
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 de.h2b.scala.lib.simgraf.shapes
import de.h2b.scala.lib.simgraf.{ Point, World }
/**
* @since 1.4.0
* @author h2b
*/
case class BarChart (val heights: Double*)
(implicit val enc: Enclosing) extends Drawable with Fillable {
def draw (w: World): Unit =
display(w, (p1: Point, p2: Point) Rectangle(p1, p2).draw(w))
def fill (w: World): Unit =
display(w, (p1: Point, p2: Point) Rectangle(p1, p2).fill(w))
private def display (w: World, f: (Point, Point) Unit): Unit = {
if (heights.isEmpty) return
val (wl, wr, wb, wt) = enc.lrbt(w)
val barWidth = (wr-wl)/heights.size
var x = wl
for (h heights) {
f(Point(x, wb), Point(x+barWidth, h))
x += barWidth
}
}
}
/*
SimGraf - A Simple Scala Graphics Library
Copyright 2016-2018 Hans-Hermann Bode
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 de.h2b.scala.lib.simgraf.demo
import scala.math.{ Pi, sin }
import scala.util.Random
import de.h2b.scala.lib.simgraf.{ Color, Pixel, Point, Screen, World }
import de.h2b.scala.lib.simgraf.layout.GridLayout
import de.h2b.scala.lib.simgraf.shapes._
object ChartDemo extends App {
private val p1 = Point(0,0)
private val p2 = Point(100,100)
private val p0 = p1+(p2-p1)*0.5
private val w = 200
private val h = 200
private val iterator = GridLayout.onScreen(1, 4).iterator
private def next () = iterator.next().fit(w, h)
private def newDefaultWorld (title: String) =
World(Rectangle(p1, p2))(next(), title)
{
val world = newDefaultWorld("Bar Chart Drawed")
world.clear(Color.Black)
world.activeColor = Color.Red
world.draw(BarChart(30, 60, 20, 80, 50))
}
{
val world = newDefaultWorld("Bar Chart Filled")
world.clear(Color.Black)
world.activeColor = Color.Red
world.fill(BarChart(30, 60, 20, 80, 50))
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment