Commit 1655e12b authored by h2b's avatar h2b

Pie-chart shape added.

Not really working yet.
parent c9b87750
......@@ -49,11 +49,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(activeColor)
val (wl, wr, wb, wt) = enc.lrbt(w)
val barWidth = (wr-wl)/heights.size
var x = wl
val activeColor = w.activeColor
val colorIterator = ColorIterator(activeColor)
for (h heights) {
w.activeColor = colorIterator.next()
f(Point(x, 0), Point(x+barWidth, h))
......
/*
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 }
import de.h2b.scala.lib.simgraf.ColorIterator
/**
* 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 enc enclosing for this chart; defaults to the full world if not
* explicitly or otherwise implicitly given
*
* @since 1.4.0
* @author h2b
*/
case class PieChart (val weights: Double*)
(implicit val enc: Enclosing) extends Drawable with Fillable {
def draw (w: World): Unit =
display(w, (alpha1: Double, alpha2: Double)
Arc(Point(0,0), width(w), height(w), alpha1, alpha2).draw(w))
def fill (w: World): Unit =
display(w, (alpha1: Double, alpha2: Double)
Arc(Point(0,0), width(w), height(w), alpha1, alpha2).fill(w))
private def width (w: World): Double = {
val (wl, wr, wb, wt) = enc.lrbt(w)
wr-wl
}
private def height (w: World): Double = {
val (wl, wr, wb, wt) = enc.lrbt(w)
wt-wb
}
private def display (w: World, f: (Double, Double) Unit): Unit = {
if (weights.isEmpty) return
val activeColor = w.activeColor
val colorIterator = ColorIterator(activeColor)
val sum = weights.sum
val angles = weights map { _*2*Math.PI/sum }
var alpha = 0.0
for (a angles) {
w.activeColor = colorIterator.next()
f(alpha, a)
alpha += a
}
w.activeColor = activeColor
}
}
......@@ -26,31 +26,47 @@ import de.h2b.scala.lib.simgraf.shapes._
object ChartDemo extends App {
private val p1 = Point(0,-30)
private val p2 = Point(100,100)
private val p0 = p1+(p2-p1)*0.5
private val p1Bar = Point(0,-30)
private val p2Bar = Point(100,100)
private val w = 200
private val h = 200
private val p1Pie = Point(-100,-100)
private val p2Pie = Point(100,100)
private val iterator = GridLayout.onScreen(1, 4).iterator
private def next () = iterator.next().fit(w, h)
private def next () = iterator.next().fit(200, 200)
private def newDefaultWorld (title: String) =
World(Rectangle(p1, p2))(next(), title)
private def newDefaultBarWorld (title: String) =
World(Rectangle(p1Bar, p2Bar))(next(), title)
private def newDefaultPieWorld (title: String) =
World(Rectangle(p1Pie, p2Pie))(next(), title)
{
val world = newDefaultWorld("Bar Chart Drawed")
val world = newDefaultBarWorld("Bar Chart Drawed")
world.clear(Color.Black)
world.activeColor = Color.Black
world.draw(BarChart(30, 60, 20, 80, 50))
}
{
val world = newDefaultWorld("Bar Chart Filled")
val world = newDefaultBarWorld("Bar Chart Filled")
world.clear(Color.Black)
world.activeColor = Color.Black
world.fill(BarChart(30, 60, 20, 80, 50))
}
{
val world = newDefaultPieWorld("Pie Chart Drawed")
world.clear(Color.Black)
world.activeColor = Color.Black
world.draw(PieChart(30, 60, 20, 80, 50))
}
{
val world = newDefaultPieWorld("Pie Chart Filled")
world.clear(Color.Black)
world.activeColor = Color.Black
world.fill(PieChart(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