Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add default theme to the Theme companion object #74

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Changes since last release]

## [0.5.x] - 2018-??-??
- added a low priority default Theme so this cumbersome import is only necessary when desired
- added a ExplicitImplicit trait to help library authors catch unintentional implicit theme's

## [0.5.0] - 2018-09-21
### Added
- `ComponentGroup` for combining plot components
Expand Down
2 changes: 0 additions & 2 deletions docs/src/main/tut/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ and take a look at it.
```scala
import com.cibo.evilplot._
import com.cibo.evilplot.plot._
import com.cibo.evilplot.plot.aesthetics.DefaultTheme._
import com.cibo.evilplot.numeric.Point

val data = Seq.tabulate(100) { i =>
Expand Down Expand Up @@ -77,7 +76,6 @@ some labels, so our audience knows what we're talking about. That's easy as well
<div class="col-md-6" markdown="1">
```scala
import com.cibo.evilplot.plot._
import com.cibo.evilplot.plot.aesthetics.DefaultTheme._
import com.cibo.evilplot.numeric.Point

val data = Seq.tabulate(100) { i =>
Expand Down
4 changes: 0 additions & 4 deletions docs/src/main/tut/plot-catalog.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ A pairs plot can be built by combining `ScatterPlot` and `Histogram` plots with
```scala
import com.cibo.evilplot.numeric.Point
import com.cibo.evilplot.plot._
import com.cibo.evilplot.plot.aesthetics.DefaultTheme._
import scala.util.Random

val labels = Vector("a", "b", "c", "d")
Expand Down Expand Up @@ -332,7 +331,6 @@ A `FunctionPlot` can be used to build density plots.
import com.cibo.evilplot.colors.Color
import com.cibo.evilplot.numeric.Bounds
import com.cibo.evilplot.plot._
import com.cibo.evilplot.plot.aesthetics.DefaultTheme._
import com.cibo.evilplot.plot.renderers.PathRenderer
import scala.util.Random

Expand Down Expand Up @@ -379,7 +377,6 @@ Overlay(
import com.cibo.evilplot.colors.HTMLNamedColors.{green, red}
import com.cibo.evilplot.geometry.Extent
import com.cibo.evilplot.plot._
import com.cibo.evilplot.plot.aesthetics.DefaultTheme._
import com.cibo.evilplot.plot.renderers.BarRenderer
import scala.util.Random

Expand Down Expand Up @@ -408,7 +405,6 @@ Overlay(
```scala
import com.cibo.evilplot.numeric.Point
import com.cibo.evilplot.plot._
import com.cibo.evilplot.plot.aesthetics.DefaultTheme._
import scala.util.Random

val data = Seq.fill(100) {
Expand Down
1 change: 0 additions & 1 deletion docs/src/main/tut/plots.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ A `PointRenderer` tells your plot how to draw the data. When we don't pass one i
import com.cibo.evilplot.numeric.Point
import com.cibo.evilplot.plot._
import com.cibo.evilplot.plot.renderers.PointRenderer
import com.cibo.evilplot.plot.aesthetics.DefaultTheme._
import scala.util.Random

val qualities = Seq("good", "bad")
Expand Down
1 change: 0 additions & 1 deletion docs/src/main/tut/render-context.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Started page using `CanvasRenderContext`.
```scala
import com.cibo.evilplot.geometry.{CanvasRenderContext, Extent}
import com.cibo.evilplot.plot._
import com.cibo.evilplot.plot.aesthetics.DefaultTheme._
import com.cibo.evilplot.numeric.Point
import org.scalajs.dom

Expand Down
18 changes: 9 additions & 9 deletions shared/src/main/scala/com/cibo/evilplot/plot/BarChart.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ final case class Bar(
)
}

object Bar {
object Bar extends ExplicitImplicits{
def apply(value: Double)(implicit theme: Theme): Bar = Bar(Seq(value), 0, theme.colors.stream)
def apply(value: Double, cluster: Int)(implicit theme: Theme): Bar =
Bar(Seq(value), cluster = cluster, theme.colors.stream)
}

object BarChart {
object BarChart extends ExplicitImplicits{

val defaultBoundBuffer: Double = 0.1

Expand Down Expand Up @@ -151,9 +151,9 @@ object BarChart {
spacing: Option[Double] = None,
boundBuffer: Option[Double] = None
)(implicit theme: Theme): Plot = {
val barRenderer = BarRenderer.default(color)
val bars = values.map(Bar(_))
custom(bars, Some(barRenderer), spacing, None, boundBuffer)
val barRenderer = BarRenderer.default(color)(theme)
val bars = values.map(Bar(_)(theme))
custom(bars, Some(barRenderer), spacing, None, boundBuffer)(theme)
}

/** Create a bar chart where bars are divided into clusters. */
Expand Down Expand Up @@ -195,7 +195,7 @@ object BarChart {
Some(barRenderer),
spacing,
Some(clusterSpacing.getOrElse(theme.elements.clusterSpacing)),
boundBuffer)
boundBuffer)(theme)
}

/** Create a stacked bar chart.
Expand All @@ -220,7 +220,7 @@ object BarChart {
val bars = values.map { stack =>
Bar(stack, colors = colorStream, labels = barLabels, cluster = 0)
}
custom(bars, Some(barRenderer), spacing, None, boundBuffer)
custom(bars, Some(barRenderer), spacing, None, boundBuffer)(theme)
}

/** Create a clustered bar chart of stacked bars.
Expand Down Expand Up @@ -261,7 +261,7 @@ object BarChart {
Some(barRenderer),
spacing,
Some(clusterSpacing.getOrElse(theme.elements.clusterSpacing)),
boundBuffer)
boundBuffer)(theme)
}

/** Create a custom bar chart.
Expand Down Expand Up @@ -292,7 +292,7 @@ object BarChart {
ybounds,
BarChartRenderer(
bars,
barRenderer.getOrElse(BarRenderer.default()),
barRenderer.getOrElse(BarRenderer.default()(theme)),
spacing.getOrElse(theme.elements.barSpacing),
clusterSpacing
)
Expand Down
14 changes: 7 additions & 7 deletions shared/src/main/scala/com/cibo/evilplot/plot/BoxPlot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ final case class BoxPlotRenderer(
pointRenderer: PointRenderer,
spacing: Double,
clusterSpacing: Option[Double]
) extends PlotRenderer {
) extends PlotRenderer with ExplicitImplicits{

private val isClustered = clusterSpacing.isDefined
private val clusterPadding = clusterSpacing.getOrElse(spacing)
Expand Down Expand Up @@ -106,7 +106,7 @@ final case class BoxPlotRenderer(
override def legendContext: LegendContext = boxRenderer.legendContext
}

object BoxPlot {
object BoxPlot extends ExplicitImplicits{

/** Create box plots for a sequence of distributions.
*
Expand Down Expand Up @@ -141,7 +141,7 @@ object BoxPlot {
boundBuffer,
boxRenderer,
pointRenderer
)
)(theme)
}


Expand Down Expand Up @@ -185,7 +185,7 @@ object BoxPlot {
boundBuffer,
boxRenderer,
pointRenderer
)
)(theme)
}

private def makePlot(
Expand All @@ -210,8 +210,8 @@ object BoxPlot {
ybounds,
BoxPlotRenderer(
boxContexts,
boxRenderer.getOrElse(BoxRenderer.default()),
pointRenderer.getOrElse(PointRenderer.default()),
boxRenderer.getOrElse(BoxRenderer.default()(theme)),
pointRenderer.getOrElse(PointRenderer.default()(theme)),
spacing.getOrElse(theme.elements.boxSpacing),
clusterSpacing
)
Expand All @@ -236,6 +236,6 @@ object BoxPlot {
spacing: Option[Double] = None,
boundBuffer: Option[Double] = None
)(implicit theme: Theme): Plot = {
apply(data, quantiles, spacing, boundBuffer, Some(boxRenderer), Some(pointRenderer))
apply(data, quantiles, spacing, boundBuffer, Some(boxRenderer), Some(pointRenderer))(theme)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2018, CiBO Technologies, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.cibo.evilplot.plot

import com.cibo.evilplot.plot.aesthetics.{Theme, DefaultTheme}

trait ExplicitImplicits{
implicit val defaultTheme: Theme = DefaultTheme.defaultTheme
}
2 changes: 1 addition & 1 deletion shared/src/main/scala/com/cibo/evilplot/plot/Facets.scala
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ object Facets {
row.zipWithIndex.map {
case (subplot, xIndex) =>
val x = xIndex * innerExtent.width
subplot.render(innerExtent).translate(x = x, y = y)
subplot.render(innerExtent)(theme).translate(x = x, y = y)
}.group
}.group
}
Expand Down
12 changes: 6 additions & 6 deletions shared/src/main/scala/com/cibo/evilplot/plot/FunctionPlot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import com.cibo.evilplot.plot.aesthetics.Theme
import com.cibo.evilplot.plot.components.FunctionPlotLine
import com.cibo.evilplot.plot.renderers.{PathRenderer, PointRenderer}

object FunctionPlot {
object FunctionPlot extends ExplicitImplicits{
val defaultBounds: Bounds = Bounds(0, 1)
val defaultNumPoints: Int = 800

Expand Down Expand Up @@ -66,7 +66,7 @@ object FunctionPlot {
pointRenderer.orElse(Some(PointRenderer.empty())),
pathRenderer,
xBoundBuffer,
yBoundBuffer)
yBoundBuffer)(theme)
}

/** Plot a function using a name for the legend.
Expand All @@ -85,8 +85,8 @@ object FunctionPlot {
xBoundBuffer: Option[Double] = None,
yBoundBuffer: Option[Double] = None
)(implicit theme: Theme): Plot = {
val renderer = Some(PathRenderer.named(name, color, strokeWidth))
apply(function, xbounds, numPoints, renderer, None, xBoundBuffer, yBoundBuffer)
val renderer = Some(PathRenderer.named(name, color, strokeWidth)(theme))
apply(function, xbounds, numPoints, renderer, None, xBoundBuffer, yBoundBuffer)(theme)
}

/** Plot a function using a name for the legend.
Expand All @@ -104,7 +104,7 @@ object FunctionPlot {
strokeWidth: Option[Double],
xBoundBuffer: Option[Double],
yBoundBuffer: Option[Double])(implicit theme: Theme): Plot = {
val renderer = Some(PathRenderer.default(strokeWidth, Some(color), label))
apply(function, xbounds, numPoints, renderer, None, xBoundBuffer, yBoundBuffer)
val renderer = Some(PathRenderer.default(strokeWidth, Some(color), label)(theme))
apply(function, xbounds, numPoints, renderer, None, xBoundBuffer, yBoundBuffer)(theme)
}
}
10 changes: 5 additions & 5 deletions shared/src/main/scala/com/cibo/evilplot/plot/Heatmap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import com.cibo.evilplot.numeric.Bounds
import com.cibo.evilplot.plot.aesthetics.Theme
import com.cibo.evilplot.plot.renderers.PlotRenderer

object Heatmap {
object Heatmap extends ExplicitImplicits{

val defaultColorCount: Int = 10

Expand Down Expand Up @@ -80,7 +80,7 @@ object Heatmap {
ybounds = ybounds,
xfixed = true,
yfixed = true,
renderer = HeatmapRenderer(data, colorBar)
renderer = HeatmapRenderer(data, colorBar)(theme)
)
}

Expand All @@ -99,7 +99,7 @@ object Heatmap {
val minValue = flattenedData.reduceOption[Double](math.min).getOrElse(0.0)
val maxValue = flattenedData.reduceOption[Double](math.max).getOrElse(0.0)
val colorBar = ScaledColorBar(colorStream.take(colorCount), minValue, maxValue)
apply(data, colorBar)
apply(data, colorBar)(theme)
}

def apply(data: Seq[Seq[Double]],
Expand All @@ -108,9 +108,9 @@ object Heatmap {
val minValue = flattenedData.reduceOption[Double](math.min).getOrElse(0.0)
val maxValue = flattenedData.reduceOption[Double](math.max).getOrElse(0.0)
val useColoring = coloring.getOrElse(theme.colors.continuousColoring)
val colorFunc = useColoring(flattenedData)
val colorFunc = useColoring(flattenedData)(theme)
val colorBar = ScaledColorBar(flattenedData.map(point => colorFunc.apply(point)), minValue, maxValue)
apply(data, colorBar)
apply(data, colorBar)(theme)

}
}
6 changes: 3 additions & 3 deletions shared/src/main/scala/com/cibo/evilplot/plot/Histogram.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import com.cibo.evilplot.numeric.{Bounds, Point}
import com.cibo.evilplot.plot.aesthetics.Theme
import com.cibo.evilplot.plot.renderers.{BarRenderer, PlotRenderer}

object Histogram {
object Histogram extends ExplicitImplicits{

val defaultBinCount: Int = 20

Expand Down Expand Up @@ -128,7 +128,7 @@ object Histogram {
val clippedY = math.min(point.y * yscale, plot.ybounds.max)
val y = ytransformer(clippedY)
val barWidth = math.max(xtransformer(point.x + binWidth) - x - spacing, 0)
val bar = Bar(clippedY)
val bar = Bar(clippedY)(theme)
val barHeight = yintercept - y
barRenderer.render(plot, Extent(barWidth, barHeight), bar).translate(x = x, y = y)
}.group
Expand Down Expand Up @@ -173,7 +173,7 @@ object Histogram {
ybounds = Bounds(0, maxY * (1.0 + boundBuffer.getOrElse(theme.elements.boundBuffer))),
renderer = HistogramRenderer(
values,
barRenderer.getOrElse(BarRenderer.default()),
barRenderer.getOrElse(BarRenderer.default()(theme)),
bins,
spacing.getOrElse(theme.elements.barSpacing),
boundBuffer.getOrElse(theme.elements.boundBuffer),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ case class LegendContext(
}
}

object LegendContext {
object LegendContext extends ExplicitImplicits{
def empty: LegendContext = LegendContext()

def single(
Expand Down
Loading