Commit 815f7440 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

barchart proper padding, cleaning up

parent 602fc98d
Loading
Loading
Loading
Loading

barchart.cc

deleted100644 → 0
+0 −188
Original line number Diff line number Diff line
/**
 * This file is part of the "FnordMetric" project
 *   Copyright (c) 2011-2014 Paul Asmuth, Google Inc.
 *
 * FnordMetric is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License v3.0. You should have received a
 * copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <assert.h>
#include "barchart.h"
#include "canvas.h"
#include "domain.h"
#include "rendertarget.h"

/**
 * todo:
 *  - labels inside/outside
 */
namespace fnordmetric {
namespace ui {

/*
void BarChart::addSeries(Series2D<double, double>* series) {
  series_colors_.emplace_back(seriesColor(series));

  for (const auto& point : series->getData()) {
    const auto& x_val = util::format::numberToHuman(std::get<0>(point)); // FIXPAUL
    const auto& y_val = std::get<1>(point);

    BarData* bar_data = nullptr;

    for (auto& candidate : data_) {
      if (candidate.x == x_val) {
        bar_data = &candidate;
      }
    }

    if (bar_data == nullptr) {
      data_.emplace_back();
      bar_data = &data_.back();
      bar_data->x = x_val;
    }

    if (bar_data->ys.size() < num_series_ + 1) {
      for (int i = bar_data->ys.size(); i < num_series_; ++i) {
        bar_data->ys.emplace_back(0, 0);
      }

      bar_data->ys.emplace_back(0, y_val);
    }
  }

  num_series_++;
}

void BarChart::addSeries(Series3D<std::string, double, double>* series) {
  series_colors_.emplace_back(seriesColor(series));

  for (const auto& point : series->getData()) {
    const auto& x_val = std::get<0>(point);
    const auto& y_val = std::get<1>(point);
    const auto& z_val = std::get<2>(point);

    BarData* bar_data = nullptr;

    for (auto& candidate : data_) {
      if (candidate.x == x_val) {
        bar_data = &candidate;
      }
    }

    if (bar_data == nullptr) {
      data_.emplace_back();
      bar_data = &data_.back();
      bar_data->x = x_val;
    }

    if (bar_data->ys.size() < num_series_ + 1) {
      for (int i = bar_data->ys.size(); i < num_series_; ++i) {
        bar_data->ys.emplace_back(0, 0);
      }

      bar_data->ys.emplace_back(y_val, z_val);
    }
  }

  num_series_++;
}
*/

/*
template <typename TX, typename TY>
NumericalDomain* BarChart<TX, TY>::newYDomain() const {
  double y_domain_min = 0.0f;
  double y_domain_max = 0.0f;

  if (stacked_) {
    for (const auto& group : data_) {
      int sum = 0;
      for (const auto& y : group.ys) {
        sum += y.second;
      }

      if (sum > 0 && sum > y_domain_max) {
        y_domain_max = sum;
      }
    }
  } else {
    for (const auto& group : data_) {
      for (const auto& y : group.ys) {
        if (y.first < y_domain_min) {
          y_domain_min = y.first;
        }
        if (y.first > y_domain_max) {
          y_domain_max = y.first;
        }
        if (y.second < y_domain_min) {
          y_domain_min = y.second;
        }
        if (y.second > y_domain_max) {
          y_domain_max = y.second;
        }
      }
    }
  }

  if (y_domain_max > 0) {
    y_domain_max *= 1.1;
  }

  if (y_domain_max < 0) {
    y_domain_max *= 0.9;
  }

  if (y_domain_min > 0) {
    y_domain_min *= 0.9;
  }

  if (y_domain_min < 0) {
    y_domain_min *= 1.1;
  }

  return new NumericalDomain(y_domain_min, y_domain_max);
}
*/

/*
AxisDefinition* BarChart::newLabelAxis(AxisDefinition::kPosition position)
    const {
  auto axis = canvas_->addAxis(position);
  axis->addTick(0.0f);

  auto bar_width = (1.0 / data_.size()) * (1.0f - kBarPadding);
  auto bar_padding = (1.0 / data_.size()) * (kBarPadding * 0.5f);
  bar_width -= bar_padding / data_.size() * 2;

  auto draw_x = bar_padding;
  for (int i = 0; i < data_.size(); ++i) {
    draw_x += bar_padding;

    switch (orientation_) {
      case O_VERTICAL:
        axis->addLabel(draw_x + bar_width * 0.5f, data_[i].x);
        break;
      case O_HORIZONTAL:
        axis->addLabel(
            draw_x + bar_width * 0.5f,
            data_[data_.size() - 1 - i].x);
        break;
    }

    draw_x += bar_width + bar_padding;
    if (i < data_.size() - 1) {
      axis->addTick(draw_x);
    }
  }

  axis->addTick(1.0f);
  return axis;
}
*/


}
}
+8 −60
Original line number Diff line number Diff line
@@ -47,7 +47,8 @@ public:
    O_HORIZONTAL
  };

  constexpr static const double kBarPadding = 0.2f; // FIXPAUL make configurable
  constexpr static const double kBarPadding = 0.3f; // FIXPAUL make configurable
  constexpr static const double kBarPaddingInner = 0.2f; // FIXPAUL make configurable

  /**
   * Create a new bar chart with an explicit y domain
@@ -216,7 +217,6 @@ void BarChart3D<TX, TY, TZ>::renderHorizontalBars(
  auto x_domain = x_domain_.getAs<Domain<TX>>();
  auto y_domain = y_domain_.getAs<Domain<TY>>();

  printf("BARS: %i\n", data_.getData().size());
  for (const auto& bar : data_.getData()) {
    auto x = x_domain->scaleRange(bar.x.value());

@@ -237,69 +237,17 @@ void BarChart3D<TX, TY, TZ>::renderHorizontalBars(
      auto dy = viewport->paddingTop() +
          (1.0 - x.first) * viewport->innerHeight() - dh;

      if (stacked_) {
      } else {
        double bar_padding = 0.3;
        dy += dh * bar_padding * 0.5;
        dh *= (1.0 - bar_padding);
        dh /= data_.seriesCount();
      dy += dh * kBarPadding * 0.5;
      dh *= (1.0 - kBarPadding);

        for (int i = 0; i < n; ++i) {
          dy += dh;
        }
      if (!stacked_) {
        dh /= data_.seriesCount();
        dy += dh * n + (dh * kBarPaddingInner * 0.5);
        dh *= (1.0 - kBarPaddingInner);
      }

      target->drawRect(dx, dy, dw, dh, "#000000", "bar");
    }


    /* stacked */
    /*else if (stacked_) {
      double y_min = 0.0f;
      double y_max = 0.0f;
      for (int i = 0; i < bar.ys.size(); i++) {
        auto& y_val = bar.ys[i];
        y_max += y_val.second - y_val.first;
        auto draw_x = padding_left + y_domain->scale(y_min) * inner_width;
        auto draw_width = y_domain->scale(y_max - y_min) * inner_width;

        target->drawRect(
            draw_x,
            draw_y,
            draw_width,
            draw_height,
            series_colors_[i],
            "bar");

        y_min += y_val.second - y_val.first;
      }
    }*/

    /* multi series unstacked */
    /*else {
      auto draw_y_multi = draw_y;
      auto draw_height_multi = draw_height / num_series_;
      for (int i = 0; i < bar.ys.size(); i++) {
        //auto y_min = y_domain->scale(bar.ys[i].first);
        //auto y_min = y_domain->scale(bar.ys[i].first);
        double y_min = 0;
        auto y_max = y_domain->scale(bar.ys[i].second);
        auto draw_x = padding_left + y_min * inner_width;
        auto draw_width = (y_max - y_min) * inner_width;

        target->drawRect(
            draw_x,
            draw_y_multi,
            draw_width,
            draw_height_multi * (1.0f - kBarPadding * 0.5f),
            series_colors_[i],
            "bar");

        draw_y_multi += (draw_height_multi * (1.0f + kBarPadding * 0.5f));
      }
    }*/

    //draw_y += bar_height + bar_padding;
  }
}

+0 −3
Original line number Diff line number Diff line
@@ -50,11 +50,8 @@ public:
        joined->ys.emplace_back(nullptr, nullptr);
      }

      printf("pointisze: %i\n", joined->ys.size());
      if (joined->ys.size() < num_series_ + 1) {

        joined->ys.emplace_back(y_val, z_val);
        printf("pointisze: %i\n", joined->ys.size());
      }
    }