Skip to content

Commit 64f1c4d

Browse files
jeffvandykedanvk
authored andcommitted
Added pixelRatio option to override canvas upscaling. Resolves danvk#876, test included. (danvk#877)
* Added pixelRatio option, using where appropriate. Falls back to old behavior if not specified. Added to options reference, Added to Dygraph.prototype.resizeElements_, Added to rangeSelector.prototype.updateVisibility. * Fixed bug in range-selector pixelRatio implementation, tests pass * added test for the pixelRatio option, properly run. First fails without change, then passes after change. * Added pull request suggestions Fix typo in docs, documented a float type, Simplified method of reading option. * Added note about pixelRatio affecting resolution
1 parent f6e73ea commit 64f1c4d

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

auto_tests/tests/hidpi.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,36 @@ it('testDoesntCreateScrollbars', function() {
4545
assert.equal(sw, document.body.scrollWidth);
4646
});
4747

48+
it('should be influenced by options.pixelRatio', function () {
49+
var graph = document.getElementById("graph");
50+
51+
// make sure devicePixelRatio is still setup to not 1.
52+
assert(devicePixelRatio > 1.5, 'devicePixelRatio is not much greater than 1.');
53+
54+
var data = "X,Y\n" +
55+
"0,-1\n" +
56+
"1,0\n" +
57+
"2,1\n" +
58+
"3,0\n";
59+
60+
// first try a default one
61+
var g1 = new Dygraph(graph, data, {});
62+
var area1 = g1.getArea();
63+
64+
var g2 = new Dygraph(graph, data, { pixelRatio: 1 });
65+
var area2 = g2.getArea();
66+
67+
var g3 = new Dygraph(graph, data, { pixelRatio: 3 });
68+
var area3 = g3.getArea();
69+
70+
assert.deepEqual(area1, area2, 'areas 1 and 2 are not the same');
71+
assert.deepEqual(area2, area3, 'areas 2 and 3 are not the same');
72+
73+
assert.notEqual(g1.canvas_.width, g2.canvas_.width,
74+
'Expected, for devicePixelRatio != 1, '
75+
+ 'that setting options.pixelRatio would change the canvas width');
76+
assert.equal(g2.canvas_.width * 3, g3.canvas_.width,
77+
'Expected that pixelRatio of 3 vs 1 would triple the canvas width.');
78+
});
4879

4980
});

src/dygraph-options-reference.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,12 @@ OPTIONS_REFERENCE = // <JSON>
299299
"type": "integer",
300300
"description": "Width, in pixels, of the chart. If the container div has been explicitly sized, this will be ignored."
301301
},
302+
"pixelRatio": {
303+
"default": "(devicePixelRatio / context.backingStoreRatio)",
304+
"labels": ["Overall display"],
305+
"type": "float",
306+
"description": "Overrides the pixel ratio scaling factor for the canvas's 2d context. Ordinarily, this is set to the devicePixelRatio / (context.backingStoreRatio || 1), so on mobile devices, where the devicePixelRatio can be somewhere around 3, performance can be improved by overriding this value to something less precise, like 1, at the expense of resolution."
307+
},
302308
"interactionModel": {
303309
"default": "...",
304310
"labels": ["Interactive Elements"],

src/dygraph.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,9 @@ Dygraph.prototype.resizeElements_ = function() {
861861
this.graphDiv.style.width = this.width_ + "px";
862862
this.graphDiv.style.height = this.height_ + "px";
863863

864-
var canvasScale = utils.getContextPixelRatio(this.canvas_ctx_);
864+
var pixelRatioOption = this.getNumericOption('pixelRatio')
865+
866+
var canvasScale = pixelRatioOption || utils.getContextPixelRatio(this.canvas_ctx_);
865867
this.canvas_.width = this.width_ * canvasScale;
866868
this.canvas_.height = this.height_ * canvasScale;
867869
this.canvas_.style.width = this.width_ + "px"; // for IE
@@ -870,7 +872,7 @@ Dygraph.prototype.resizeElements_ = function() {
870872
this.canvas_ctx_.scale(canvasScale, canvasScale);
871873
}
872874

873-
var hiddenScale = utils.getContextPixelRatio(this.hidden_ctx_);
875+
var hiddenScale = pixelRatioOption || utils.getContextPixelRatio(this.hidden_ctx_);
874876
this.hidden_.width = this.width_ * hiddenScale;
875877
this.hidden_.height = this.height_ * hiddenScale;
876878
this.hidden_.style.width = this.width_ + "px"; // for IE

src/plugins/range-selector.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ rangeSelector.prototype.updateVisibility_ = function() {
161161
* Resizes the range selector.
162162
*/
163163
rangeSelector.prototype.resize_ = function() {
164-
function setElementRect(canvas, context, rect) {
165-
var canvasScale = utils.getContextPixelRatio(context);
164+
function setElementRect(canvas, context, rect, pixelRatioOption) {
165+
var canvasScale = pixelRatioOption || utils.getContextPixelRatio(context);
166166

167167
canvas.style.top = rect.y + 'px';
168168
canvas.style.left = rect.x + 'px';
@@ -189,8 +189,9 @@ rangeSelector.prototype.resize_ = function() {
189189
h: this.getOption_('rangeSelectorHeight')
190190
};
191191

192-
setElementRect(this.bgcanvas_, this.bgcanvas_ctx_, this.canvasRect_);
193-
setElementRect(this.fgcanvas_, this.fgcanvas_ctx_, this.canvasRect_);
192+
var pixelRatioOption = this.dygraph_.getNumericOption('pixelRatio');
193+
setElementRect(this.bgcanvas_, this.bgcanvas_ctx_, this.canvasRect_, pixelRatioOption);
194+
setElementRect(this.fgcanvas_, this.fgcanvas_ctx_, this.canvasRect_, pixelRatioOption);
194195
};
195196

196197
/**

0 commit comments

Comments
 (0)