Skip to content

Commit 94b242a

Browse files
author
Jelte Lagendijk
committed
Add responsive width/height ratio, resolves responsive issue where the height is very small
1 parent 140dc22 commit 94b242a

File tree

10 files changed

+51
-8
lines changed

10 files changed

+51
-8
lines changed

src/ChartJS/widgets/BarChart/BarChart.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
<category>Appearance</category>
3838
<description>Initial height of the chart canvas, will be ignored when responsive property is set to 'Yes'</description>
3939
</property>
40+
<property key="responsiveRatio" type="integer" required="true" defaultValue="0">
41+
<caption>Responsive w/h ratio (%)</caption>
42+
<category>Appearance</category>
43+
<description>Responsive is set to 'true': Responsive width height ratio default value is 0, which means it will set the height of the chart the same height as the container. If you set this value to a certain number, it will create a chart where the height is a percentage of the width. So 100 means the height will be 100% of the width (a square)</description>
44+
</property>
4045
<property key="labelFont" type="string" required="true" defaultValue="Helvetica Neue">
4146
<caption>Font</caption>
4247
<category>Appearance</category>

src/ChartJS/widgets/Core.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ define([
5151
// Template path
5252
templateString: _chartJSTemplate,
5353

54+
// Set in modeler
55+
responsiveRatio: 0,
56+
5457
// Internal variables
5558
_chartJS: null,
5659
_chart: null,
@@ -142,8 +145,10 @@ define([
142145

143146
if (this._handle !== null) {
144147
mx.data.unsubscribe(this._handle);
148+
this._handle = null;
145149
}
146-
if(this._mxObj) {
150+
151+
if (this._mxObj) {
147152
this._handle = mx.data.subscribe({
148153
guid: this._mxObj.getGuid(),
149154
callback: lang.hitch(this, this._loadData)
@@ -155,9 +160,8 @@ define([
155160
} else {
156161
domStyle.set(this.domNode, "display", "none");
157162
}
158-
if (typeof callback !== "undefined") {
159-
callback();
160-
}
163+
164+
mendix.lang.nullExec(callback);
161165
},
162166

163167
_loadData: function () {
@@ -285,16 +289,20 @@ define([
285289
var position = domGeom.position(this.domNode.parentElement, false);
286290
domAttr.set(this.canvasNode, "id", "canvasid_" + this.id);
287291

288-
logger.debug(this.id + ".createCtx", position);
289-
290292
if (position.w > 0 && this.responsive) {
291293
this.canvasNode.width = position.w;
292294
} else {
293295
this.canvasNode.width = this.width;
294296
}
295297

296-
if (position.h > 0 && this.responsive) {
297-
this.canvasNode.height = position.h;
298+
if (this.responsive) {
299+
if (this.responsiveRatio > 0) {
300+
this.canvasNode.height = Math.round(this.canvasNode.width * (this.responsiveRatio / 100));
301+
} else if (position.h > 0) {
302+
this.canvasNode.height = position.h;
303+
} else {
304+
this.canvasNode.height = this.height;
305+
}
298306
} else {
299307
this.canvasNode.height = this.height;
300308
}

src/ChartJS/widgets/DoughnutChart/DoughnutChart.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
<category>Appearance</category>
1818
<description>Initial height of the chart canvas, will be ignored when responsive property is set to 'Yes'</description>
1919
</property>
20+
<property key="responsiveRatio" type="integer" required="true" defaultValue="0">
21+
<caption>Responsive w/h ratio (%)</caption>
22+
<category>Appearance</category>
23+
<description>Responsive is set to 'true': Responsive width height ratio default value is 0, which means it will set the height of the chart the same height as the container. If you set this value to a certain number, it will create a chart where the height is a percentage of the width. So 100 means the height will be 100% of the width (a square)</description>
24+
</property>
2025
<property key="labelFont" type="string" required="true" defaultValue="Helvetica Neue">
2126
<caption>Font</caption>
2227
<category>Appearance</category>

src/ChartJS/widgets/LineChart/LineChart.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444
<category>Appearance</category>
4545
<description>Initial height of the chart canvas, will be ignored when responsive property is set to 'Yes'</description>
4646
</property>
47+
<property key="responsiveRatio" type="integer" required="true" defaultValue="0">
48+
<caption>Responsive w/h ratio (%)</caption>
49+
<category>Appearance</category>
50+
<description>Responsive is set to 'true': Responsive width height ratio default value is 0, which means it will set the height of the chart the same height as the container. If you set this value to a certain number, it will create a chart where the height is a percentage of the width. So 100 means the height will be 100% of the width (a square)</description>
51+
</property>
4752
<property key="labelFont" type="string" required="true" defaultValue="Helvetica Neue">
4853
<caption>Font</caption>
4954
<category>Appearance</category>

src/ChartJS/widgets/PieChart/PieChart.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
<category>Appearance</category>
2121
<description>Initial height of the chart canvas, will be ignored when responsive property is set to 'Yes'</description>
2222
</property>
23+
<property key="responsiveRatio" type="integer" required="true" defaultValue="0">
24+
<caption>Responsive w/h ratio (%)</caption>
25+
<category>Appearance</category>
26+
<description>Responsive is set to 'true': Responsive width height ratio default value is 0, which means it will set the height of the chart the same height as the container. If you set this value to a certain number, it will create a chart where the height is a percentage of the width. So 100 means the height will be 100% of the width (a square)</description>
27+
</property>
2328
<property key="labelFont" type="string" required="true" defaultValue="Helvetica Neue">
2429
<caption>Font</caption>
2530
<category>Appearance</category>

src/ChartJS/widgets/PolarChart/PolarChart.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
<category>Appearance</category>
1818
<description>Initial height of the chart canvas, will be ignored when responsive property is set to 'Yes'</description>
1919
</property>
20+
<property key="responsiveRatio" type="integer" required="true" defaultValue="0">
21+
<caption>Responsive w/h ratio (%)</caption>
22+
<category>Appearance</category>
23+
<description>Responsive is set to 'true': Responsive width height ratio default value is 0, which means it will set the height of the chart the same height as the container. If you set this value to a certain number, it will create a chart where the height is a percentage of the width. So 100 means the height will be 100% of the width (a square)</description>
24+
</property>
2025
<property key="labelFont" type="string" required="true" defaultValue="Helvetica Neue">
2126
<caption>Font</caption>
2227
<category>Appearance</category>

src/ChartJS/widgets/RadarChart/RadarChart.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
<category>Appearance</category>
1818
<description>Initial height of the chart canvas, will be ignored when responsive property is set to 'Yes'</description>
1919
</property>
20+
<property key="responsiveRatio" type="integer" required="true" defaultValue="0">
21+
<caption>Responsive w/h ratio (%)</caption>
22+
<category>Appearance</category>
23+
<description>Responsive is set to 'true': Responsive width height ratio default value is 0, which means it will set the height of the chart the same height as the container. If you set this value to a certain number, it will create a chart where the height is a percentage of the width. So 100 means the height will be 100% of the width (a square)</description>
24+
</property>
2025
<property key="labelFont" type="string" required="true" defaultValue="Helvetica Neue">
2126
<caption>Font</caption>
2227
<category>Appearance</category>

src/ChartJS/widgets/StackedBarChart/StackedBarChart.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
<category>Appearance</category>
1818
<description>Initial height of the chart canvas, will be ignored when responsive property is set to 'Yes'</description>
1919
</property>
20+
<property key="responsiveRatio" type="integer" required="true" defaultValue="0">
21+
<caption>Responsive w/h ratio (%)</caption>
22+
<category>Appearance</category>
23+
<description>Responsive is set to 'true': Responsive width height ratio default value is 0, which means it will set the height of the chart the same height as the container. If you set this value to a certain number, it will create a chart where the height is a percentage of the width. So 100 means the height will be 100% of the width (a square)</description>
24+
</property>
2025
<property key="xLabel" type="string" required="false" defaultValue="">
2126
<caption>X-axis label</caption>
2227
<category>Appearance</category>

test/[Test] ChartJS.mpr

18 KB
Binary file not shown.

test/widgets/ChartJS.mpk

1.33 KB
Binary file not shown.

0 commit comments

Comments
 (0)