Skip to content

Commit 3606ae9

Browse files
authored
Merge pull request #466 from xychang/bar_chart_fix
Enable bar chart full-bar display at beginning and end
2 parents 9364aaf + a5a2e25 commit 3606ae9

5 files changed

Lines changed: 31 additions & 3 deletions

File tree

docs/InputParameters.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ These key-value pairs should be placed under the key `bar`.
103103
| Key | Description | Number of Values | Default |
104104
|:--------|:-------|:-----------:|:------|
105105
| `barColor` | Color of bars in the chart | 1~NT | #69b3a2 |
106+
| `xAxisPadding` | Padding to ensure bars are fully displayed | 1~2 | null |
106107
| `yAxisLocation` | Corresponding y-axis for the dataset (left\|right) | 1~NT | left |
107108

108109
### Parameters for a Summary

examples/TestBarChart.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ endDate: 2021-01-05
99
bar:
1010
title: Weight Log
1111
yAxisLabel: Weight
12+
xAxisPadding: 12h
1213
yAxisUnit: kg
1314
yMin: 0
1415
barColor: darkolivegreen
@@ -23,6 +24,7 @@ endDate: 2021-01-31
2324
bar:
2425
title: Weight Log
2526
yAxisLabel: Weight
27+
xAxisPadding: 12h
2628
yAxisUnit: kg
2729
yMin: 0
2830
barColor: brown
@@ -37,6 +39,7 @@ endDate: 2021-01-21
3739
bar:
3840
title: Sin Wave
3941
yAxisLabel: Value
42+
xAxisPadding: 12h
4043
barColor: yellow, red, green
4144
```
4245

@@ -49,6 +52,7 @@ endDate: 2021-01-05
4952
bar:
5053
title: Sin Square Wave
5154
yAxisLabel: Value
55+
xAxisPadding: 12h
5256
yMin: 0
5357
barColor: yellow, red, green, blue, orange, white
5458
```
@@ -64,6 +68,7 @@ stack: true
6468
bar:
6569
title: Sin Square Wave (Stacked)
6670
yAxisLabel: Value
71+
xAxisPadding: 12h
6772
yMin: 0
6873
barColor: yellow, red, green, blue, orange, black
6974
```

src/data.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,11 +791,13 @@ export class LineInfo extends CommonChartInfo {
791791
export class BarInfo extends CommonChartInfo {
792792
barColor: string[];
793793
yAxisLocation: string[];
794+
xAxisPadding: string;
794795

795796
constructor() {
796797
super();
797798
this.barColor = []; // #69b3a2
798799
this.yAxisLocation = []; // left, for each target
800+
this.xAxisPadding = null; // the string will be converted to Duration (a month is not nesscesary to 30 days)
799801
}
800802

801803
public GetGraphType() {

src/parsing.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,6 +1836,14 @@ export function getRenderInfoFromYaml(
18361836
bar.yAxisLocation = retYAxisLocation;
18371837
// console.log(bar.yAxisLocation);
18381838

1839+
// xAxisPadding
1840+
let retXAxisPadding = getStringFromInput(
1841+
yamlBar?.xAxisPadding,
1842+
null
1843+
);
1844+
bar.xAxisPadding = retXAxisPadding;
1845+
// console.log(bar.xAxisPadding);
1846+
18391847
renderInfo.bar.push(bar);
18401848
} // bar related parameters
18411849
// console.log(renderInfo.bar);

src/rendering.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,18 @@ function renderXAxis(
307307

308308
let datasets = renderInfo.datasets;
309309
let xDomain = d3.extent(datasets.getDates());
310+
if (chartInfo instanceof BarInfo && chartInfo.xAxisPadding !== null) {
311+
let xAxisPaddingDuration = helper.parseDurationString(
312+
chartInfo.xAxisPadding
313+
);
314+
if (xAxisPaddingDuration !== null) {
315+
xDomain = [
316+
xDomain[0].clone().subtract(xAxisPaddingDuration.asHours(), 'hours'),
317+
xDomain[1].clone().add(xAxisPaddingDuration.asHours(), 'hours'),
318+
];
319+
}
320+
}
321+
// console.log(xDomain);
310322
let xScale = d3
311323
.scaleTime()
312324
.domain(xDomain)
@@ -954,7 +966,7 @@ function renderBar(
954966
.enter()
955967
.append("rect")
956968
.attr("x", function (p: DataPoint, i: number) {
957-
if (i === 0) {
969+
if (i === 0 && barInfo.xAxisPadding === null) {
958970
let portionVisible = currentDiaplayInd + 1 - totalDiaplaySet / 2.0;
959971
if (portionVisible < 1.0) {
960972
return (
@@ -975,15 +987,15 @@ function renderBar(
975987
return yScale(Math.max(p.value, 0));
976988
})
977989
.attr("width", function (p: DataPoint, i: number) {
978-
if (i === 0) {
990+
if (i === 0 && barInfo.xAxisPadding === null) {
979991
let portionVisible = currentDiaplayInd + 1 - totalDiaplaySet / 2.0;
980992
if (portionVisible < 0.0) {
981993
return 0.0;
982994
} else if (portionVisible < 1.0) {
983995
return barWidth * portionVisible;
984996
}
985997
return barWidth;
986-
} else if (i === dataset.getLength() - 1) {
998+
} else if (i === dataset.getLength() - 1 && barInfo.xAxisPadding === null) {
987999
let portionVisible =
9881000
1.0 - (currentDiaplayInd + 1 - totalDiaplaySet / 2.0);
9891001
if (portionVisible < 0.0) {

0 commit comments

Comments
 (0)