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

[IMP] charts: limit trending line degree range #5022

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/components/side_panel/chart/chart_with_axis/design_panel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Component, useState } from "@odoo/owl";
import { getColorsPalette, getNthColor, setColorAlpha, toHex } from "../../../../helpers";
import { getColorsPalette, getNthColor, range, setColorAlpha, toHex } from "../../../../helpers";
import { CHART_AXIS_CHOICES, getDefinedAxis } from "../../../../helpers/figures/charts";
import { _t } from "../../../../translation";
import { ChartJSRuntime } from "../../../../types/chart";
import {
ChartWithAxisDefinition,
Color,
Expand Down Expand Up @@ -76,6 +77,10 @@ export class ChartWithAxisDesignPanel<P extends Props = Props> extends Component
return this.props.definition.dataSets.map((d, i) => d.label ?? `${ChartTerms.Series} ${i + 1}`);
}

getPolynomialDegrees(): number[] {
return range(1, this.getMaxPolynomialDegree() + 1);
}

updateSerieEditor(ev) {
const chartId = this.props.figureId;
const selectedIndex = ev.target.selectedIndex;
Expand Down Expand Up @@ -205,12 +210,7 @@ export class ChartWithAxisDesignPanel<P extends Props = Props> extends Component

onChangePolynomialDegree(ev: InputEvent) {
const element = ev.target as HTMLInputElement;
const order = parseInt(element.value || "1");
if (order < 2) {
element.value = `${this.getTrendLineConfiguration()?.order ?? 2}`;
return;
}
this.updateTrendLineValue({ order });
this.updateTrendLineValue({ order: parseInt(element.value) });
}

getTrendLineColor() {
Expand All @@ -235,4 +235,9 @@ export class ChartWithAxisDesignPanel<P extends Props = Props> extends Component
};
this.props.updateChart(this.props.figureId, { dataSets });
}

getMaxPolynomialDegree() {
const runtime = this.env.model.getters.getChartRuntime(this.props.figureId) as ChartJSRuntime;
return Math.min(10, runtime.chartJsConfig.data.datasets[this.state.index].data.length - 1);
}
}
15 changes: 9 additions & 6 deletions src/components/side_panel/chart/chart_with_axis/design_panel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,16 @@
</div>
<div class="w-50 ms-3" t-if="trendType === 'polynomial'">
<span class="o-section-subtitle">Degree</span>
<input
<select
t-att-value="trend.order"
type="number"
class="w-100 o-input trend-order-input"
t-on-change="this.onChangePolynomialDegree"
min="1"
/>
class="o-input trend-order-input"
t-on-change="this.onChangePolynomialDegree">
<t t-foreach="getPolynomialDegrees()" t-as="degree" t-key="degree">
<option t-att-value="degree">
<t t-esc="degree"/>
</option>
</t>
</select>
</div>
</div>
<div class="d-flex align-items-center">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,16 @@
</div>
<div class="w-50 ms-3" t-if="trendType === 'polynomial'">
<span class="o-section-subtitle">Degree</span>
<input
<select
t-att-value="trend.order"
type="number"
class="w-100 o-input trend-order-input"
t-on-change="this.onChangePolynomialDegree"
min="1"
/>
class="o-input trend-order-input"
t-on-change="this.onChangePolynomialDegree">
<t t-foreach="getPolynomialDegrees()" t-as="degree" t-key="degree">
<option t-att-value="degree">
<t t-esc="degree"/>
</option>
</t>
</select>
</div>
</div>
<div class="d-flex align-items-center">
Expand Down
25 changes: 25 additions & 0 deletions tests/figures/chart/charts_component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1733,6 +1733,31 @@ describe("charts", () => {
}
);

test.each(["bar", "line", "scatter", "combo"] as const)(
"Polynome degree choices are limited by the number of points",
async (type: "bar" | "line" | "scatter" | "combo") => {
createChart(
model,
{
dataSets: [
{ dataRange: "B1:B5", trend: { type: "polynomial", order: 3, display: true } },
],
labelRange: "A1:A5",
type,
dataSetsHaveTitle: false,
},
chartId,
sheetId
);
await mountChartSidePanel(chartId);
await openChartDesignSidePanel(model, env, fixture, chartId);

const selectElement = fixture.querySelector(".trend-order-input") as HTMLSelectElement;
const optionValues = [...selectElement.options].map((o) => o.value);
expect(optionValues).toEqual(["1", "2", "3", "4"]);
}
);

test.each(["bar", "line", "scatter", "combo"] as const)(
"Can change trend line color",
async (type: "bar" | "line" | "scatter" | "combo") => {
Expand Down