Skip to content

Commit c18dc21

Browse files
committed
Add some options for D3-based chart to set padding , and not have y-axis numbers.
Alsoadded double-clicking on y-axis ticks/line to toggle log/linear.
1 parent 2cfedc1 commit c18dc21

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

d3_resources/SpectrumChartD3.js

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ SpectrumChartD3 = function(elem, options) {
6060
if( (typeof this.options.compactXAxis) !== 'boolean' ) this.options.compactXAxis = false;
6161
if( (typeof this.options.adjustYAxisPadding) !== 'boolean' ) this.options.adjustYAxisPadding = true;
6262
if( (typeof this.options.wheelScrollYAxis) !== 'boolean' ) this.options.wheelScrollYAxis = true;
63-
63+
if( (typeof this.options.noYAxisNumbers) !== 'boolean' ) this.options.noYAxisNumbers = false;
6464

6565
if(typeof options.animationDuration !== 'number' || options.animationDuration < 0) this.options.animationDuration = 1000;
6666
this.options.showAnimation = (typeof options.showAnimation == 'boolean' && this.options.animationDuration > 0) ? options.showAnimation : false;
@@ -106,8 +106,7 @@ SpectrumChartD3 = function(elem, options) {
106106
this.options.backgroundSubtract = (typeof options.backgroundSubtract == 'boolean') ? options.backgroundSubtract : false;
107107
this.options.allowDragRoiExtent = (typeof options.allowDragRoiExtent == 'boolean') ? options.allowDragRoiExtent : true;
108108
this.options.showSliderCloseBtn = (typeof options.showSliderCloseBtn == 'boolean') ? options.showSliderCloseBtn : false;
109-
110-
109+
111110

112111
self.options.spectrumLineWidth = (typeof options.spectrumLineWidth == 'number' && options.spectrumLineWidth>0 && options.spectrumLineWidth < 15) ? options.spectrumLineWidth : 1.0;
113112

@@ -135,8 +134,7 @@ SpectrumChartD3 = function(elem, options) {
135134
// A value of 0 is horizontal, a value of -90 is vertical (i.e. up-and-down). Only tested [0,-90]
136135
self.options.peakLabelRotation = (typeof options.peakLabelRotation == 'number') ? options.peakLabelRotation : 0;
137136
self.options.logYAxisMin = ((typeof options.logYAxisMin == 'number') && (options.logYAxisMin > 0)) ? options.logYAxisMin : 0.1;
138-
139-
137+
140138
self.setLocalizations( {}, true );//Set default localization strings
141139

142140
this.padding = {
@@ -427,7 +425,14 @@ SpectrumChartD3 = function(elem, options) {
427425
})
428426
.on("mouseout", function() {
429427
d3.select(this).selectAll('text').style("font-weight", null);
430-
});
428+
})
429+
.on("dblclick", function(){ //toggle between linear and log when user double-clicks axis
430+
d3.event.preventDefault();
431+
d3.event.stopPropagation();
432+
const ytype = (self.options.yscale === "log") ? "lin" : "log";
433+
self.setYAxisType( (self.options.yscale === "log") ? "lin" : "log" );
434+
self.WtEmit(self.chart.id, {name: 'yAxisTypeChanged'}, ytype );
435+
});
431436

432437
this.xAxis = d3.svg.axis().scale(this.xScale)
433438
.orient("bottom")
@@ -4972,6 +4977,8 @@ SpectrumChartD3.prototype.yticks = function() {
49724977
.forEach(function(e){ticks.push({value:e,major:true,text:formatYNumber(e)});});
49734978
}
49744979

4980+
if( this.options.noYAxisNumbers )
4981+
ticks.forEach( function(obj){obj["text"] = null; } );
49754982

49764983
return ticks;
49774984
}
@@ -5051,10 +5058,30 @@ SpectrumChartD3.prototype.setAdjustYAxisPadding = function( adjust, pad ) {
50515058
this.handleResize( false );
50525059
}
50535060

5061+
5062+
SpectrumChartD3.prototype.setNoYAxisNumbers = function(d) {
5063+
if( typeof d === "boolean" )
5064+
this.options.noYAxisNumbers = d;
5065+
this.handleResize( false );
5066+
}
5067+
50545068
SpectrumChartD3.prototype.setWheelScrollYAxis = function(d) {
50555069
this.options.wheelScrollYAxis = d;
50565070
}
50575071

5072+
SpectrumChartD3.prototype.setChartPadding = function(d) {
5073+
if( !d )
5074+
return;
5075+
const allowed_keys = [ "top", "right", "bottom", "left", "titlePad", "xTitlePad", "labelPad", "label", "sliderChart" ];
5076+
for( const key in d )
5077+
{
5078+
if( Object.hasOwnProperty.call(d, key) && allowed_keys.includes(key) && (typeof d[key] === "number") )
5079+
this.padding[key] = d[key];
5080+
}
5081+
this.handleResize( false );
5082+
}
5083+
5084+
50585085

50595086
/**
50605087
* -------------- Y-axis Scale Functions --------------

0 commit comments

Comments
 (0)