Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grouped sets of trends #34

Open
ceejayoz opened this issue Mar 29, 2023 · 2 comments
Open

grouped sets of trends #34

ceejayoz opened this issue Mar 29, 2023 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@ceejayoz
Copy link

We have a need to group the trend by a particular column (for use in a stacked bar chart). I extended the class to permit a ->groupBy('type') on the Trend definition; the resulting trend is an array of trend data for each value of the type field. Is there any interest in working this up for inclusion in the package?

<?php

use Flowframe\Trend\Trend;
use Illuminate\Support\Collection;

class GroupedTrend extends Trend
{
    public string $groupColumn;

    public function groupBy(string $groupColumn): self
    {
        $this->groupColumn = $groupColumn;

        return $this;
    }

    public function aggregate(string $column, string $aggregate): Collection
    {
        $values = $this->builder
            ->toBase()
            ->selectRaw("
                {$this->getSqlDate()} as {$this->dateAlias},
                {$aggregate}({$column}) as aggregate,
                {$this->groupColumn} as group
            ")
            ->whereBetween($this->dateColumn, [$this->start, $this->end])
            ->groupBy($this->dateAlias, $this->groupColumn)
            ->orderBy($this->dateAlias)
            ->get();

        return $values->groupBy('group')
            ->map(fn($group) => $this->mapValuesToDates($group));
    }
}
@Larsklopstra
Copy link
Member

Interesting, I might have an idea how to tackle this. Will look into it

@Larsklopstra Larsklopstra added the enhancement New feature or request label May 4, 2023
@Larsklopstra Larsklopstra self-assigned this May 4, 2023
@vpuentem
Copy link

up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants