Class DonutChart


public class DonutChart extends Chart
A ring of slices, or a pie, with setInnerRadius(float) at 0.

The chart draws its first visible series as the ring: one slice per value, named by the chart's labels and colored by palette slot, because in a donut it is the slices that are the categories. A second series is left undrawn rather than drawn as a second ring; two rings on one chart is a chart nobody can read.

The hole is a real widget slot. setCenter(Widget) puts a widget in the middle (a total, an icon, a button), laid out in the largest square that fits inside the ring, and it takes input like any other widget: a button in the hole is clickable, and the chart's own hover stops at the ring.

Negative values are ignored: they have no share of a whole. Hiding a slice from the legend animates it closed and gives its angle back to the others, because the shares are recomputed from what is still shown.


 DonutChart chart = new DonutChart();
 chart.setLabels("Direct", "Search", "Social", "Mail");
 chart.addSeries(ChartSeries.of("Traffic", 42, 31, 18, 9));
 chart.setCenter(new Label("100%").setRole(Label.Role.TITLE));
 
  • Constructor Details

    • DonutChart

      public DonutChart()
  • Method Details

    • of

      public static DonutChart of(List<String> labels, double... values)
      A donut over labels showing values.
    • innerRadius

      public float innerRadius()
      The hole's diameter as a fraction of the ring's.
    • setInnerRadius

      public DonutChart setInnerRadius(float fraction)
      Sets the hole as a fraction of the outer radius, in [0, 0.95]. 0 draws a pie; the default 0.62 leaves a hole big enough for a headline number.
    • startAngle

      public float startAngle()
      Where the first slice starts, in degrees clockwise from three o'clock.
    • setStartAngle

      public DonutChart setStartAngle(float degrees)
      Sets the first slice's starting angle in degrees; -90 (the default) is noon.
    • sliceGap

      public float sliceGap()
      Points of surface between two slices.
    • setSliceGap

      public DonutChart setSliceGap(float points)
      Sets the gap between slices in logical points (0 makes them touch).
    • setSliceColors

      public DonutChart setSliceColors(Color... colors)
      Pins the slice colors, in slice order; a shorter array leaves the rest to the palette, and null hands all of them back to it.
    • sliceColor

      public Color sliceColor(int index)
      The color of slice index: its own, or its palette slot.
    • center

      public Widget center()
      The widget in the hole, or null.
    • setCenter

      public DonutChart setCenter(Widget widget)
      Puts a widget in the hole. It is laid out centred in the largest square that fits inside the ring and is a normal child in every other way: it takes input, it can be a button, and it is measured against that square rather than against the chart. null empties the hole.
    • isSliceVisible

      public boolean isSliceVisible(int index)
      Whether slice index is currently drawn.
    • setSliceVisible

      public DonutChart setSliceVisible(int index, boolean visible)
      Hides or shows one slice; the rest of the ring closes over it, or opens for it.
    • onLayout

      protected void onLayout()
      Description copied from class: Widget
      Containers position children here (measure + Widget.layoutBox(float, float, float, float) per child).
      Overrides:
      onLayout in class Widget
    • paintContent

      protected void paintContent(Canvas canvas, float x, float y, float w, float h)
      Description copied from class: Chart
      Paints the marks inside the region left after the title and the legend, in the chart's own coordinates. Grid, axes and everything data-shaped belong here.
      Specified by:
      paintContent in class Chart
    • pickAt

      protected ChartPoint pickAt(float localX, float localY)
      Description copied from class: Chart
      The datum under a point in the chart's local coordinates, or null when the pointer is not over the marks. Called for hover and for clicks, so it decides both.
      Specified by:
      pickAt in class Chart
    • onHoverChanged

      protected void onHoverChanged(ChartPoint picked)
      Description copied from class: Chart
      Called when the datum under the pointer changes, null when there is none: the hook for a chart that animates something on hover. The application-facing callback is Chart.onPointHover(Consumer); this is for subclasses.
      Overrides:
      onHoverChanged in class Chart
    • tooltipRows

      protected List<ChartPoint> tooltipRows(ChartPoint picked)
      Description copied from class: Chart
      The rows a tooltip shows for picked. The default is Chart.TooltipMode.INDEX-aware: every visible series at the same index, or just the picked datum in Chart.TooltipMode.POINT.
      Overrides:
      tooltipRows in class Chart
    • tooltipTitle

      protected String tooltipTitle(ChartPoint picked)
      The series names the ring as a whole ("Traffic"); the row names the slice.
      Overrides:
      tooltipTitle in class Chart
    • tooltipRowName

      protected String tooltipRowName(ChartPoint row)
      Description copied from class: Chart
      The name a tooltip row carries where reading starts, the series name by default.
      Overrides:
      tooltipRowName in class Chart
    • tooltipRowValue

      protected String tooltipRowValue(ChartPoint row)
      Description copied from class: Chart
      The value a tooltip row carries where reading ends.
      Overrides:
      tooltipRowValue in class Chart
    • tooltipRowColor

      protected Color tooltipRowColor(ChartPoint row)
      Description copied from class: Chart
      The swatch color for a tooltip row.
      Overrides:
      tooltipRowColor in class Chart
    • legendEntries

      protected List<Chart.LegendEntry> legendEntries()
      Description copied from class: Chart
      One entry per series, in palette order. Overridden by charts whose legend is not that.
      Overrides:
      legendEntries in class Chart
    • toggleLegendEntry

      protected void toggleLegendEntry(int index)
      Description copied from class: Chart
      Hides or shows what legend entry index stands for.
      Overrides:
      toggleLegendEntry in class Chart