Skip to content

Radial/polar histograms #41

Open
Open

Description

@mredaelli

Hi! Is it possible to draw something like this
http://datavizproject.com/data-type/radical-histogram/
with evilplot?

Activity

  1. mredaelli commented on Jul 21, 2018

    @mredaelli
    Author

    I was looking around the code to try my hands at implementing this.
    Can you confirm my impression that in order to do it in a clean way one would need to modify the Extent class, which looks very invasive?

  2. triton11 commented on Jul 23, 2018

    @triton11
    Contributor

    One potential solution you could try is using a modified pie chart renderer:

    case class PieChartRenderer(
        data: Seq[(Drawable, Double)],
        colors: Seq[Color]
    ) extends PlotRenderer {
    def render(plot: Plot, plotExtent: Extent)(implicit theme: Theme): Drawable = {
          val radius = math.min(plotExtent.width, plotExtent.height) / 2
          val highestNumber = data.map(_._2).max
          val d = data.flatMap(x => Seq((x._1,x._2), (x._1,x._2)))
          val total: Double = d.map(_._2).length
          val totalRotations = d.tail.scanLeft(d.head._2 * 360 / total) {
            case (acc, (_, _)) =>
              acc + 360.0 / total
          } :+ 360.0
          val moreColors = colors.flatMap(c => Seq(c, HTMLNamedColors.white))
          val wedges = d.zip(totalRotations).zip(moreColors).map {
            case (((_, value), totalRotation), color) =>
              if (color == HTMLNamedColors.white) {
                Wedge(totalRotation, radius + 5).filled(color)
              }
              else {
                val xoffset = highestNumber - value * radius/highestNumber
                val yoffset = highestNumber - value * radius/highestNumber
                Wedge(totalRotation, value * radius/highestNumber)
                  .translate(x = radius + xoffset, y = radius + yoffset).filled(color)
              }
          }
          wedges.reverse.group
        }
    }

    This renders a pie chart with equal radius "slices" with a radius based on their value:

    PieChart(Seq(("a",1),("b",2),("c",3),("d", 4),("e",1)))

    screen shot 2018-07-23 at 4 30 18 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions