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

feat: enable chart grid enable option #761

Merged
merged 12 commits into from
Sep 10, 2020
37 changes: 30 additions & 7 deletions packages/core/src/components/axes/grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,34 @@ export class Grid extends Component {
backdrop: any;

render(animate = true) {
const isXGridEnabled = Tools.getProperty(
this.model.getOptions(),
"grid",
"x",
"enabled"
);
const isYGridEnabled = Tools.getProperty(
this.model.getOptions(),
"grid",
"y",
"enabled"
);

if (!isXGridEnabled && !isYGridEnabled) {
return;
}
// Draw the backdrop
this.drawBackdrop();
DOMUtils.appendOrSelect(this.backdrop, "g.x.grid");
DOMUtils.appendOrSelect(this.backdrop, "g.y.grid");
this.drawBackdrop(isXGridEnabled, isYGridEnabled);

this.drawXGrid(animate);
this.drawYGrid(animate);
if (isXGridEnabled) {
DOMUtils.appendOrSelect(this.backdrop, "g.x.grid");
this.drawXGrid(animate);
}

if (isYGridEnabled) {
DOMUtils.appendOrSelect(this.backdrop, "g.y.grid");
this.drawYGrid(animate);
}
}

drawXGrid(animate: boolean) {
Expand Down Expand Up @@ -184,7 +205,7 @@ export class Grid extends Component {
return xGridlines;
}

drawBackdrop() {
drawBackdrop(isXGridEnabled, isYGridEnabled) {
const svg = this.parent;

const mainXScale = this.services.cartesianScales.getMainXScale();
Expand All @@ -197,7 +218,9 @@ export class Grid extends Component {
this.backdrop = DOMUtils.appendOrSelect(svg, "svg.chart-grid-backdrop");
const backdropRect = DOMUtils.appendOrSelect(
this.backdrop,
"rect.chart-grid-backdrop"
isXGridEnabled || isYGridEnabled
? "rect.chart-grid-backdrop.stroked"
: "rect.chart-grid-backdrop"
);

this.backdrop
Expand Down
16 changes: 15 additions & 1 deletion packages/core/src/components/axes/ruler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ export class Ruler extends Component {
domainValue: number;
originalData: any;
}[];
isXGridEnabled = Tools.getProperty(
this.model.getOptions(),
"grid",
"x",
"enabled"
);
isYGridEnabled = Tools.getProperty(
this.model.getOptions(),
"grid",
"y",
"enabled"
);

render() {
this.drawBackdrop();
Expand Down Expand Up @@ -231,7 +243,9 @@ export class Ruler extends Component {
this.backdrop = DOMUtils.appendOrSelect(svg, "svg.chart-grid-backdrop");
const backdropRect = DOMUtils.appendOrSelect(
this.backdrop,
"rect.chart-grid-backdrop"
this.isXGridEnabled || this.isYGridEnabled
? "rect.chart-grid-backdrop.stroked"
: "rect.chart-grid-backdrop"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like stroke should be a separate configurable no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

圖片

A chart with grid but without stroke looks kind of weird. So I guess we would always want to add stroke when there is grid being enabled. And when there is no grid being enabled, we can let the user setup the option to disable stroke? What do you think?

With stroke.
圖片

Without stroke.
圖片

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually I think stroke might be something we'd always want to keep unless overriden

@jeanservaas could you please chime in?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @theiliad and @jeanservaas,
There is the situation when we don't want to show the stroke.
圖片

);

this.backdrop
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,13 @@ const legend: LegendOptions = {
*/
export const grid: GridOptions = {
x: {
// set enable to false will not draw grid and stroke of grid backdrop
enabled: true,
numberOfTicks: 15
},
y: {
// set enable to false will not draw grid and stroke of grid backdrop
enabled: true,
numberOfTicks: 5
}
};
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/interfaces/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ export interface ThresholdOptions {

export interface GridOptions {
y?: {
enabled?: boolean;
numberOfTicks?: number;
};
x?: {
enabled?: boolean;
numberOfTicks?: number;
};
}
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/styles/components/_grid.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
} @else {
fill: $ui-background;
}
}
rect.chart-grid-backdrop.stroked {
stroke: $ui-03;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need this extra classname?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason why we need chart-grid-backdrop-no-stroke classname is because we don't want to apply stroke around the chart when there is no grid line.


Expand Down