Commit 472912be authored by Paul Asmuth's avatar Paul Asmuth
Browse files

linechart: add 'labels' property

parent 16a2bf04
Loading
Loading
Loading
Loading
+34 −2
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ namespace plot {
namespace lines {

static const double kDefaultLineWidthPT = 2;
static const double kDefaultLabelPaddingEM = 0.8;

PlotLinesConfig::PlotLinesConfig() {}

@@ -61,7 +62,7 @@ ReturnCode draw_lines(
  auto x = domain_translate(domain_x, config.xs);
  auto y = domain_translate(domain_y, config.ys);

  // draw line
  /* draw line */
  {
    Path path;

@@ -85,7 +86,7 @@ ReturnCode draw_lines(
    strokePath(layer, clip, path, style);
  }

  // draw points
  /* draw points */
  auto point_size = config.point_size;
  if (point_size > 0) {
    FillStyle style;
@@ -102,6 +103,33 @@ ReturnCode draw_lines(
    }
  }

  /* draw labels */
  for (size_t i = 0; i < config.xs.size(); ++i) {
    if (i >= config.labels.size()) {
      break;
    }

    const auto& label_text = config.labels[i];
    auto label_padding = measure_or(
        config.label_padding,
        from_em(kDefaultLabelPaddingEM, config.label_font_size));

    Point p(
        clip.x + x[i] * clip.w,
        clip.y + (1.0 - y[i]) * clip.h - label_padding);

    TextStyle style;
    style.font = config.label_font;
    style.color = config.label_color;
    style.font_size = config.label_font_size;

    auto ax = HAlign::CENTER;
    auto ay = VAlign::BOTTOM;
    if (auto rc = drawTextLabel(label_text, p, ax, ay, style, layer); rc != OK) {
      return rc;
    }
  }

  return OK;
}

@@ -115,11 +143,15 @@ ReturnCode configure(const plist::Property& prop, const Document& doc, PlotConfi
  PlotLinesConfig series;
  series.line_color = color;
  series.point_color = color;
  series.label_font = doc.font_sans;
  series.label_font_size = doc.font_size;
  series.label_color = doc.text_color; // FIXME: lighten by 20%

  static const ParserDefinitions pdefs = {
    {"xs", std::bind(&configure_series, std::placeholders::_1, &series.xs)},
    {"ys", std::bind(&configure_series, std::placeholders::_1, &series.ys)},
    {"title", std::bind(&configure_string, std::placeholders::_1, &series.title)},
    {"labels", std::bind(&configure_series, std::placeholders::_1, &series.labels)},
    {
      "color",
      configure_multiprop({
+5 −0
Original line number Diff line number Diff line
@@ -48,6 +48,11 @@ struct PlotLinesConfig {
  Series xs;
  Series ys;
  std::string title;
  Series labels;
  FontInfo label_font;
  Measure label_padding;
  Measure label_font_size;
  Color label_color;
  Measure line_width;
  Color line_color;
  Measure point_size;
+13 −0
Original line number Diff line number Diff line
month,temperature
Jan,3.9,3.9°C
Feb,4.2,4.2°C
Mar,5.7,5.7°C
Apr,8.5,8.5°C
May,11.9,11.9°C
Jun,15.2,15.2°C
Jul,17.0,17.0°C
Aug,16.6,16.6°C
Sep,14.2,14.2°C
Oct,10.3,10.3°C
Nov,6.6,6.6°C
Dec,4.8,4.8°C
+19 −0
Original line number Diff line number Diff line
plot {
  axis-top: off;
  axis-right: off;
  margin-right: 2.4em;

  ymin: -10;
  ymax: 32;
  xdomain: categorical;
  xdomain-format: string;

  series {
    title: "London";
    xs: csv(examples/data/city_temperatures_london.csv, 0);
    ys: csv(examples/data/city_temperatures_london.csv, 1);
    labels: csv(examples/data/city_temperatures_london.csv, 2);
    point-size: 3.3pt;
  }

}
+0 −13
Original line number Diff line number Diff line
IMPORT TABLE city_temperatures
   FROM 'csv:examples/data/city_temperatures.csv?headers=true';

DRAW LINECHART WITH
   AXIS LEFT
   AXIS BOTTOM
   LABELS
   YDOMAIN -10, 40
   LEGEND TOP LEFT INSIDE;

SELECT city AS series, month AS x, temperature AS y, temperature + "°C" as label, "circle" as pointstyle
   FROM city_temperatures
   WHERE city = "Berlin";
Loading