Saltar al contenido
CrossFly es 100% open sourceSíguenos en GitHub
CrossFlyCrossFly
Empezar
Todos los ejemplos
RecetasGráficos102 líneas · 1 archivos

Charts

autoThemeColors themes the axes, grid and background — but NOT the series. A series left at colour 0 falls back to a library grey, and becomes the one thing on screen ignoring your brand.

Qué muestra esto

Four charts, and the one line that stops them looking foreign.

Which chart is a question about the reader, not the data. Over time is a line; compared side by side is bars; share of a whole is a donut; a trend inside a card is a mini bar chart with no axes at all, because axes on a two-centimetre chart are decoration.

Ejecutarlo en tu máquina

Cada ejemplo es una Window normal, así que el lanzador solo necesita su nombre.

gradlew lwjgl3:run --args="classname dev.cf.recipe.R16Charts"
Editarlo

Java se compila antes de ejecutarse, así que el navegador no puede reconstruir una edición por su cuenta: el código que ves aquí se convirtió en JavaScript con TeaVM antes de publicarse. Copia el archivo a un proyecto y funciona sin cambios en las cuatro plataformas; el comando de arriba es toda la preparación.

Ejecutándose aquíCrossFly 0.1.32
Iniciando la aplicación…

Compilado a JavaScript por TeaVM: es el mismo código, no un vídeo de él. · Todos los ejemplos

Qué usa

Tipos del framework que toca este ejemplo. Los enlazados tienen página de referencia.

CardChartViewColorUtilsComboChartComponentDonutChartLineChartMiniBarChartPalColorRelativeStylesWidgets

El código

Completo, sin recortes. Es el archivo, no un fragmento.

Crudo
dev/cf/recipe/R16Charts.java
package dev.cf.recipe;

import dev.cf.Tones;

import dev.crossfly.ui.*;
import dev.crossfly.ui.chart.*;
import dev.crossfly.ui.color.*;
import dev.crossfly.ui.color.ColorUtils;
import dev.crossfly.ui.graphics.*;
import dev.crossfly.ui.layout.*;
import dev.crossfly.ui.style.*;

import static dev.crossfly.ui.Widgets.*;

/**
 * Four charts, and the one line that stops them looking foreign.
 *
 * <p>Which chart is a question about the reader, not the data. <b>Over time</b> is a line;
 * <b>compared side by side</b> is bars; <b>share of a whole</b> is a donut; <b>a trend inside a
 * card</b> is a mini bar chart with no axes at all, because axes on a two-centimetre chart are
 * decoration.</p>
 */
public class R16Charts extends Recipe
{
   private static final String[] MONTH = { "Jan", "Feb", "Mar", "Apr", "May", "Jun" };
   private static final double[] REVENUE = { 4200, 5100, 4800, 6300, 7100, 6800 };
   private static final double[] ORDERS = { 31, 38, 35, 46, 52, 49 };

   public String title()
   {
      return "Charts";
   }

   public String note()
   {
      return "autoThemeColors themes the axes, grid and background — but NOT the series. A series"
         + " left at colour 0 falls back to a library grey, and becomes the one thing on screen"
         + " ignoring your brand.";
   }

   public void build(Component body) throws Exception
   {
      int brand = Styles.primaryColor.color();
      // The second series is a ROLE, not the brand dimmed towards the text colour. Two measures on
      // one chart have to be told apart at a glance, and the old shade came out grey-blue — the
      // reader had to find the legend to know which line was which. Tertiary sits sixty degrees
      // off the seed and is derived from it, so it is distinct without being unrelated.
      int shade = Tones.highlight().color();

      head(body, 0xE8E5, "Over time", Tones.accent());
      LineChart line = new LineChart();
      line.categories(MONTH);
      line.series(new ChartView.Series("Revenue", REVENUE, brand));
      line.area(true);
      line.smooth(true);
      chart(body, line);

      head(body, 0xE26B, "Side by side", Tones.accent());
      ComboChart combo = new ComboChart();
      combo.categories(MONTH);
      combo.addSeries(new ChartView.Series("Revenue", REVENUE, brand).bar());
      combo.addSeries(new ChartView.Series("Orders", ORDERS, shade).line());
      chart(body, combo);

      head(body, 0xE6C4, "Share of a whole", Tones.accent());
      DonutChart donut = new DonutChart();
      // One hue in steps, not four unrelated colours: the question is "how much of it", and a
      // legend the reader has to learn is a worse answer than a ramp they can just see.
      String[] channel = { "Field", "Web", "Phone", "Partner" };
      double[] share = { 46, 28, 17, 9 };
      for (int i = 0; i < channel.length; i++)
         donut.slice(channel[i], share[i],
            ColorUtils.lerpColor(brand, Styles.backColor.color(), i * .21f));
      donut.hole(.62f);
      chart(body, donut);

      head(body, 0xE873, "Inside a card", Tones.accent());
      Card c = new Card();
      c.add(LAY_AUTO, label("Last six months", 1f, true, Tones.accent(), Position.START));
      MiniBarChart mini = new MiniBarChart().values(REVENUE)
         .categories(MONTH)
         .highlight(REVENUE.length - 1);
      mini.getStyle()
         .autoHeight(Relative.ofFont(5f));
      c.add(LAY_AUTO, outgap(mini, 0, .4f, 0, 0));
      body.add(LAY_AUTO, c);
   }

   private void chart(Component body, ChartView v) throws Exception
   {
      v.autoThemeColors(true);
      v.getStyle()
         .autoHeight(Relative.ofFont(12f));
      body.add(LAY_AUTO, outgap(v, 0, 0, 0, 1.2f));
   }

   private void head(Component body, int icone, String t, PalColor tom) throws Exception
   {
      body.add(LAY_AUTO, Tones.head(icone, t, tom));
   }
}
Para una IA

Versiones de esta página legibles por máquina, para que un modelo lea el estante en vez de adivinar la API.

Este ejemplo, en JSONEste ejemplo, en Markdown
AnteriorA header that collapsesSiguienteA list too big to build