9

我正在使用 morris.js 图表和引导滑块。

我想要做什么:
如果滑块被移动,我想在图表中滑块值的位置插入一条事件线。没什么特别的。

问题:
设置事件后图表没有重绘。插入新数据时,图表会自动重绘。所以我尝试通过再次将现有数据传递给图表来刷新(检查语法是否正确),如下所示:

chart.setData(chart.options.data);

这是有效的!新事件被绘制到图表中。不幸的是,由于图表正在重绘所有数据,因此性能很差。

文档说有chart.redraw(). 它只是不适合我。也许你们中的某个人可以找出原因。

只需运行代码片段并切换单选按钮来测试这两种方法,您就会发现问题所在。

// Chart creation - not interesting
var chart = new Morris.Line({
  element: 'chart',
  ymin: 0,
  ymax: 100,
  gridIntegers: true,
  parseTime:false,
  // Will be set below
  data: [  ],
  xkey: 'position',
  ykeys: ['value'],
  labels: ['Value'],
  hideHover: "always",
  pointSize: 0,
  continuousLine: false,
  goalStrokeWidth: 3,
  axes: true,
  grid: true,
  numLines: 6,
  eventLineColors: [ "red" ],
  smooth: true
});

// Inserting some values
var values = [101]
for(i = 1; i <= 100; i += 1) {
    var x = 100/(Math.pow((i-50), 2)+4);
    values.push({ position: i, value: x });
}
chart.setData(values);

// Create slider
$('#slider').slider({
  tooltip: 'never',
  tooltip_position:'left',
  height: 300,
  length: 300,
});

// If the slider moves, I want to set its value as an event-line
$("#slider").on("slideStop", function(slideEvt) {
    chart.options.events = [ slideEvt.value ];

    // Check inserting-data VS chart.redraw()
    // Both should work... chart.redraw() isnt.
    if(document.getElementById('insert').checked)
        chart.setData(chart.options.data);
    else if(document.getElementById('redraw').checked)
        chart.redraw();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script src="http://cdn.oesmith.co.uk/morris-0.5.1.min.js"></script>
<link href="http://cdn.oesmith.co.uk/morris-0.5.1.css" rel="stylesheet"/>

<!-- Bootstrap slider -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-slider/9.7.2/bootstrap-slider.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-slider/9.7.2/css/bootstrap-slider.min.css">

<div id="chart" style="height: 150px;width:500px;"></div>
<form action="">
    <input checked="true" id="insert" type="radio" name="refreshMethod"> Insert (working)<br>
    <input id="redraw" type="radio" name="refreshMethod"> Redraw (not working)<br>
</form>
<input id="slider" type="text" data-slider-min="0" data-slider-max="100" data-slider-step="1" data-slider-value="50"/>

我感谢这里的每一个帮助。非常感谢!

重要编辑:
只要我可以设置赏金(+1 小时),我就会这样做。这个问题的一个非常重要的附加部分是如何将滑块绑定到单个图表事件而不会滞后。目前使用slide事件而不是slideStop.

4

1 回答 1

2

它不起作用,因为chart.events当您调用chart.redraw(). 此属性setData在行内设置this.events = this.options.events

要纠正这一点:在您的代码中使用chart.events = [val]而不是chart.options.events = [val];

就个人而言,我会同时设置它们以防setData再次调用,但这取决于你。

所以在你的演示中:

...
else if(document.getElementById('redraw').checked) {
    chart.events = [ slideEvt.value ];
    chart.redraw();
}

编辑:

添加了一个chart.quickRedraw功能。这从重绘函数中删除了不必要的重新计算(请参阅注释掉的代码以了解现在排除的内容):

chart.quickRedraw = function() {
      this.raphael.clear();
      //this._calc();
      this.drawGrid();
      //this.drawGoals();
      this.drawEvents();
      this.draw();      
};

演示已更新为第三个选项。

// Chart creation - not interesting
var chart = new Morris.Line({
  element: 'chart',
  ymin: 0,
  ymax: 100,
  gridIntegers: true,
  parseTime:false,
  // Will be set below
  data: [  ],
  xkey: 'position',
  ykeys: ['value'],
  labels: ['Value'],
  hideHover: "always",
  pointSize: 0,
  continuousLine: false,
  goalStrokeWidth: 3,
  axes: true,
  grid: true,
  numLines: 6,
  eventLineColors: [ "red" ],
  smooth: true
});

// Inserting some values
var values = [101]
for(i = 1; i <= 100; i += 1) {
    var x = 100/(Math.pow((i-50), 2)+4);
    values.push({ position: i, value: x });
}
chart.setData(values);

// Create slider
$('#slider').slider({
  tooltip: 'never',
  tooltip_position:'left',
  height: 300,
  length: 300,
});

// If the slider moves, I want to set its value as an event-line
$("#slider").on("slideStop", function(slideEvt) {
    chart.options.events = [ slideEvt.value ];

    // Check inserting-data VS chart.redraw()
    // Both should work... chart.redraw() isnt.
    if(document.getElementById('insert').checked)
        chart.setData(chart.options.data);
    else if(document.getElementById('redraw').checked) {
        chart.events = [ slideEvt.value ];
        chart.redraw();
    } else if(document.getElementById('quickerRedraw').checked) {
        chart.events = [ slideEvt.value ];
        chart.quickRedraw();
    }
});

chart.quickRedraw = function() {
      this.raphael.clear();
      //this._calc();
      this.drawGrid();
      //this.drawGoals();
      this.drawEvents();
      this.draw();
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script src="http://cdn.oesmith.co.uk/morris-0.5.1.min.js"></script>
<link href="http://cdn.oesmith.co.uk/morris-0.5.1.css" rel="stylesheet"/>

<!-- Bootstrap slider -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-slider/9.7.2/bootstrap-slider.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-slider/9.7.2/css/bootstrap-slider.min.css">

<div id="chart" style="height: 150px;width:500px;"></div>
<form action="">
    <input checked="true" id="insert" type="radio" name="refreshMethod"> Insert (working)<br>
    <input id="redraw" type="radio" name="refreshMethod"> Redraw (working)<br>
    <input id="quickerRedraw" type="radio" name="refreshMethod"> Quicker Redraw<br>
</form>
<input id="slider" type="text" data-slider-min="0" data-slider-max="100" data-slider-step="1" data-slider-value="50"/>

于 2017-04-06T11:59:35.287 回答