Commit 7982f0d1 authored by h2b's avatar h2b

Chart arguments restructured.

parent 78adbc32
......@@ -17,8 +17,7 @@
*/
package de.h2b.scala.lib.simgraf.shapes
import de.h2b.scala.lib.simgraf.{ Point, World }
import de.h2b.scala.lib.simgraf.ColorIterator
import de.h2b.scala.lib.simgraf.{ Color, ColorIterator, Point, World }
/**
* A bar chart as shape.
......@@ -26,11 +25,10 @@ import de.h2b.scala.lib.simgraf.ColorIterator
* Each bar extends from `0` to `h` on the y axis, where `h` is its given
* height.
*
* Each bar is displayed in a different color ''excluding'' the active color.
*
* @constructor
*
* @param heights sequence of bar heights
* @param colors sequence of colors to iterate over for each bar (cycling)
*
* @param enc enclosing for this chart; defaults to the full world if not
* explicitly or otherwise implicitly given
......@@ -38,9 +36,11 @@ import de.h2b.scala.lib.simgraf.ColorIterator
* @since 1.4.0
* @author h2b
*/
case class BarChart (val heights: Double*)
case class BarChart (val heights: Seq[Double], val colors: Seq[Color])
(implicit val enc: Enclosing) extends Drawable with Fillable {
private val iterator = ColorIterator.over(colors: _*)
def draw (w: World): Unit =
display(w, (p1: Point, p2: Point) Rectangle(p1, p2).draw(w))
......@@ -50,12 +50,11 @@ case class BarChart (val heights: Double*)
private def display (w: World, f: (Point, Point) Unit): Unit = {
if (heights.isEmpty) return
val activeColor = w.activeColor
val colorIterator = ColorIterator.excluding(activeColor)
val (wl, wr, wb, wt) = enc.lrbt(w)
val barWidth = (wr-wl)/heights.size
var x = wl
for (h heights) {
w.activeColor = colorIterator.next()
w.activeColor = iterator.next()
f(Point(x, 0), Point(x+barWidth, h))
x += barWidth
}
......
......@@ -17,19 +17,17 @@
*/
package de.h2b.scala.lib.simgraf.shapes
import de.h2b.scala.lib.simgraf.{ Point, World }
import de.h2b.scala.lib.simgraf.ColorIterator
import de.h2b.scala.lib.simgraf.{ Color, ColorIterator, Point, World }
/**
* A pie chart as shape.
*
* The pie is centred at `(0 ,0)` in the enclosing world coordinate system.
*
* Each segment is displayed in a different color ''excluding'' the active color.
*
* @constructor
*
* @param weights sequence of segment weights
* @param colors sequence of colors to iterate over for each bar (cycling)
*
* @param enc enclosing for this chart; defaults to the full world if not
* explicitly or otherwise implicitly given
......@@ -37,9 +35,11 @@ import de.h2b.scala.lib.simgraf.ColorIterator
* @since 1.4.0
* @author h2b
*/
case class PieChart (val weights: Double*)
case class PieChart (val weights: Seq[Double], val colors: Seq[Color])
(implicit val enc: Enclosing) extends Drawable with Fillable {
private val iterator = ColorIterator.over(colors: _*)
def draw (w: World): Unit =
display(w, (alpha1: Double, alpha2: Double)
Arc(Point(0,0), width(w), height(w), alpha1, alpha2).draw(w))
......@@ -61,7 +61,6 @@ case class PieChart (val weights: Double*)
private def display (w: World, f: (Double, Double) Unit): Unit = {
if (weights.isEmpty) return
val activeColor = w.activeColor
val colorIterator = ColorIterator.excluding(activeColor)
val sum = weights.sum
val angles = weights map { _*2*Math.PI/sum }
val r1 = width(w)/2
......@@ -70,7 +69,7 @@ case class PieChart (val weights: Double*)
var alpha = 0.0
//Lines are for arc drawing and do not disturb arc filling
for (a angles) {
w.activeColor = colorIterator.next()
w.activeColor = iterator.next()
w.draw(Line(p0, Point(r1*Math.cos(alpha), r2*Math.sin(alpha))))
f(alpha, a)
alpha += a
......
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