Commit 766e091e authored by Paul Asmuth's avatar Paul Asmuth
Browse files

support non-consecutive groups

parent 2d6b9bf7
Loading
Loading
Loading
Loading
+13 −10
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "data_model.h"
#include <assert.h>
#include <iostream>

namespace plotfx {
@@ -79,18 +80,20 @@ Value value_from_float(double v) {

std::vector<DataGroup> series_group(const Series& data) {
  std::vector<DataGroup> groups;
  std::unordered_map<Value, size_t> group_map;

  for (size_t idx = 0; idx < data.size(); ) {
  for (size_t idx = 0; idx < data.size(); ++idx) {
    size_t group_idx = group_map[data[idx]];
    if (!group_idx) {
      DataGroup g;
      g.key = data[idx];
    g.begin = idx;
    g.end = idx;

    while (g.end < data.size() && data[g.end] == data[g.begin]) {
      g.end = ++idx;
      groups.emplace_back(g);
      group_idx = groups.size();
      group_map[data[idx]] = group_idx;
    }

    groups.emplace_back(g);
    assert(group_idx > 0);
    groups[group_idx - 1].index.push_back(idx);
  }

  return groups;
+1 −2
Original line number Diff line number Diff line
@@ -46,8 +46,7 @@ struct DataContext {

struct DataGroup {
  Value key;
  size_t begin;
  size_t end;
  std::vector<size_t> index;
};

std::vector<DataGroup> series_group(const Series& data);
+8 −13
Original line number Diff line number Diff line
@@ -53,21 +53,16 @@ std::vector<Color> series_to_colors(
}

std::vector<Color> groups_to_colors(
    size_t count,
    const std::vector<DataGroup>& groups,
    const ColorScheme& palette) {
  auto max_idx =
      std::max_element(
          groups.begin(),
          groups.end(),
          [] (const DataGroup& a, const DataGroup& b) { return a.end < b.end; })
      ->end;

  std::vector<Color> colors(max_idx);
  for (size_t i = 0; i < groups.size(); ++i) {
    std::fill(
        colors.begin() + groups[i].begin,
        colors.begin() + groups[i].end,
        palette.get(i));
  std::vector<Color> colors(count);
  for (size_t gi = 0; gi < groups.size(); ++gi) {
    for (auto i : groups[gi].index) {
      if (i < colors.size()) {
        colors[i] = palette.get(gi);
      }
    }
  }

  return colors;
+1 −0
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ std::vector<Color> series_to_colors(
    const ColorScheme& palette);

std::vector<Color> groups_to_colors(
    size_t count,
    const std::vector<DataGroup>& groups,
    const ColorScheme& palette);

+11 −10
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include <numeric>
#include "plot_area.h"
#include <plotfx.h>
#include <graphics/path.h>
@@ -51,20 +52,20 @@ ReturnCode draw(
  for (const auto& group : config.groups) {
    Path path;

    for (size_t i = group.begin; i < group.end; ++i) {
    for (auto i : group.index) {
      auto sx = clip.x + config.x[i] * clip.w;
      auto sy = clip.y + (1.0 - config.y1[i]) * clip.h;

      if (i == group.begin) {
      if (i == group.index[0]) {
        path.moveTo(sx, sy);
      } else {
        path.lineTo(sx, sy);
      }
    }

    for (size_t i = group.end; i > group.begin; --i) {
      auto sx = clip.x + config.x[i - 1] * clip.w;
      auto sy = clip.y + (1.0 - config.y2[i - 1]) * clip.h;
    for (auto i = group.index.rbegin(); i != group.index.rend(); ++i) {
      auto sx = clip.x + config.x[*i] * clip.w;
      auto sy = clip.y + (1.0 - config.y2[*i]) * clip.h;
      path.lineTo(sx, sy);
    }

@@ -73,7 +74,7 @@ ReturnCode draw(
    FillStyle style;
    style.color = config.colors.empty()
        ? Color{}
        : config.colors[group.begin % config.colors.size()];
        : config.colors[group.index[0]];

    fillPath(layer, clip, path, style);
  }
@@ -93,7 +94,7 @@ ReturnCode build_legend(
    li.title = g.key.empty() ? title : g.key;
    li.color = config.colors.empty()
        ? Color{}
        : config.colors[g.begin % config.colors.size()];
        : config.colors[g.index[0] % config.colors.size()];

    legend_items.items.emplace_back(li);
  }
@@ -176,8 +177,8 @@ ReturnCode configure(
    config->groups = plotfx::series_group(*data_group);
  } else {
    DataGroup g;
    g.begin = 0;
    g.end = data_x->size();
    g.index = std::vector<size_t>(data_x->size());
    std::iota(g.index.begin(), g.index.end(), 0);
    config->groups.emplace_back(g);
  }

@@ -193,7 +194,7 @@ ReturnCode configure(
  config->colors = fallback(
      color,
      series_to_colors(colors, color_domain, color_palette),
      groups_to_colors(config->groups, color_palette));
      groups_to_colors(data_x->size(), config->groups, color_palette));

  /* build legend items */
  if (auto rc = build_legend(*config, title, legend_key, legend); !rc) {
Loading