-->

Disable resize of brush on range chart from focus

2019-09-10 09:23发布

问题:

Please note - there is a solution for part of this problem by Gordon Woodhull in Disable brush on range chart before selecting a scale from the dropdown/on page load(dc.js,d3.js)

In addition,there is a partial solution at the end this question.

Furthermore there are two fiddles:

1) https://jsfiddle.net/dani2011/4dsjjdan/1/

2) https://jsfiddle.net/dani2011/uzg48yk7/1/ (with partial solution)

Need to disable resize of the brush on range chart (timeSlider) by dragging the line within the focus charts (bitChart,bitChart2). As Gordon Woodhull suggested (Disable brush resize (DC.js, D3.js) try to enable only pan without zoom.

Current behavior:

1)

Dragging the line on bitChart2 (focus chart) pans the brush until the borders of the timeChart. Once reaching the borders,the brush shrinks. The other focus chart (bitChart) resizes the brush of the range chart during drag of its line.

2)

When selecting a span for the brush from the dropdown only the .on('zoomed', function (chart, filter) { of bitChart2 is loaded and not the .on("zoomed"... of bitChart.

Print screens from the console:

a) Selecting scale from the dropdown

b) Dragging line on bitChart:

c) Dragging line on bitChart2:

3) For both bitChart and bitChart2 the value of scale is 1 and the position
is 0,0:

.on('zoomed', function (chart, filter) {
  //var zoom = d3.behavior.zoom()
  // .translate([0, 0])
  //.scale(1).scaleExtent([1, 1])
  var zoom = d3.behavior.zoom()
  var scale = zoom.scale(); var position = zoom.translate();

js file

The following implementations did not solve the issue:

**option 1**
   bitChart.on('zoomed', function (chart, filter) {
   d3.behavior.zoom().on("zoom", null);//doesn't stop zoom
   //event needs svg element(tried different options),doesn't work
 d3.behavior.zoom().scale(1).scaleExtent([1,1]).translate([0,0]).event(chart.select('g.stack_0')))

**option 2**
   //Applied on timeslider,bitChart,bitChart2 to eliminate zoom and   
   //maintain pan
  .zoomScale([1, 1])//dc.js
  //And also
  .on('zoomed', function (chart, filter) {
  bitChart.zoomScale([1, 1]);
  //Nothing pans with
  chart.focus(chart.xOriginalDomain())

**option 3**
  bitChart.on('zoomed', function (chart, filter) {
  var svg = d3.select("body")
      .append("svg")
      .call(d3.behavior.zoom().on("zoom", function () {
       svg.attr("transform", "translate(" + d3.event.translate + ")" +"   
       scale(" + 1 + ")")
      }))
     //.append("g")



 **option 4**
      .on('zoomed', function (chart, filter) {
        var chartBody = chart.select('g.stack _0');
        var path = chartBody.selectAll('path.line').data([extra_data]);
        path.enter().append('path').attr({
            class: 'line',
        });
        path.attr('transform', 'translate(0,50)');



**option 5**

    bitChart.on('zoomed', function (chart, filter) {
    var zoom = d3.behavior.zoom()
   .scaleExtent([1, 1])
    chart.select('g.stack _0').call(zoom);
    zoom.scale(1);
    zoom.event(chart.select('g.stack _0'));

  **option 6**
   bitChart.on('zoomed', function (chart, filter) {
   svg.call(d3.behavior.zoom().scale(1));

   **option 7**
   var min_zoom = 1;
   var max_zoom = 1;
   var svg = d3.select("body").append("svg");
   var zoom = d3.behavior.zoom().scaleExtent([min_zoom, max_zoom])
   bitChart.on('zoomed', function (chart, filter) {
   svg.call(zoom);

My fiddle:

https://jsfiddle.net/dani2011/4dsjjdan/1/ was forked from https://jsfiddle.net/gordonwoodhull/272xrsat/9/.

When selecting span from the dropdown and clicking on the range chart,The range chart (timeSlider) acts strange on the fiddle, but behaves as expected when run it in my environment. Please note in this fiddle that bitChart2 pans the brush as expected.The resize of the brush when reaching the edge happens in my enviroment. bitChart still resizes the brush.

A partial solution:

To enable multi focus charts on a single range chart as in https://github.com/dc-js/dc.js/blob/master/web/examples/multi-focus.html written by Gordon Woodhull.Used the focus chart which worked properly in my code (bitChart2) as the main reference chart:

bitChart2.focusCharts = function (chartlist) {
    if (!arguments.length) {
        return this._focusCharts;
    }
    this._focusCharts = chartlist; // only needed to support the getter above
    this.on('filtered', function (range_chart) {
        if (!range_chart.filter()) {
            dc.events.trigger(function () {
                chartlist.forEach(function(focus_chart) {
                    focus_chart.x().domain(focus_chart.xOriginalDomain());
                });
            });
        } else chartlist.forEach(function(focus_chart) {
            if (!rangesEqual(range_chart.filter(), focus_chart.filter())) {
                dc.events.trigger(function () {
                    focus_chart.focus(range_chart.filter());
                });
            }
        });
    });
    return this;
};
bitChart2.focusCharts([bitChart]);

My second fiddle:

https://jsfiddle.net/dani2011/uzg48yk7/1/ was forked from https://jsfiddle.net/gordonwoodhull/272xrsat/9/.

1) When clicking on the range chart in the fiddle it does not function properly, but works in my environment.

2) The brush does not resize at the edges of the range chart in the fiddle as it does in my environment

3) It does show in the fiddle that the whole range chart is selected when panning/clicking on the lines in the focus charts and when clicking in the range chart

4) It does show in the fiddle that after selecting the brush span from the dropdown, panning the lines in the focus charts moves the brush properly on the range chart.

5) It does show in the fiddle that dragging the brush on the range chart is possible again in no span is selected from the dropdown

Still needs to solve:

1) When reaching the ends of the range chart (timeSlider) the brush resizes

solved by updating versions to be the same as the version of the external resources in the fiddle https://jsfiddle.net/gordonwoodhull/272xrsat/9/. Thank you Gordon!

2) Before selecting a scale from the dropdown:

a) When panning /translating the line of the focus charts(bitChart,bitChart2) the brush resizes

b) It is possible again to drag the brush on the range chart

Any help would be appreciated !

标签: d3.js dc.js