首页 \ 问答 \ 在D3.js中绘制实时时间序列时的奇怪x值(Weird x-values when plotting live time series in D3.js)

在D3.js中绘制实时时间序列时的奇怪x值(Weird x-values when plotting live time series in D3.js)

我正在尝试绘制过去2小时的时间序列,只是一堆带有时间戳和值的点。 例如{time: 1234234553, value: 34}

当一个新点到达时,我将它推到列表data的末尾

并重新列出列表中的所有内容:

function redraw() 
{
    var TS = new Date().getTime(); // current time stamp
    var T = (60*60*2*1000); // last two hours

    chart.selectAll("rect")
     .data(data)
   .enter().append("rect")
     .attr("x", function(d, i)   { return x(d.time - TS + T); })
     .attr("y", function(d)      { return H - y(d.value); })
     .attr("width", function(d)  { return 2; })
     .attr("height", function(d) { return y(d.value); });
}

但不幸的是,该点出现在图的左侧而不是右侧,当我在Firebug中查看SVG的源时,该条目有一个奇怪的x值:

<rect x="function (){}" y="354.9553019925952" width="2" height="45.044698007404804"/>

有人可以帮忙吗? 我是否需要删除所有绘制的数据并重新绘制它?

附录:

如果你需要看x()和y()

var T = (60*60*2*1000); // 1 hurs

var x = d3.scale.linear()
        .domain([0, T])
        .range([0, W]);

var y = d3.scale.linear()
        .domain([d3.min(data, function(d) { return d.value; }), 10])
        .range([0, H]);

I'm trying to plot a time series for the last 2 hours, just a bunch of points with a timestamp and a value. e.g. {time: 1234234553, value: 34}

When a new point arrives I push it onto the end of a list data

and replot everything in the list:

function redraw() 
{
    var TS = new Date().getTime(); // current time stamp
    var T = (60*60*2*1000); // last two hours

    chart.selectAll("rect")
     .data(data)
   .enter().append("rect")
     .attr("x", function(d, i)   { return x(d.time - TS + T); })
     .attr("y", function(d)      { return H - y(d.value); })
     .attr("width", function(d)  { return 2; })
     .attr("height", function(d) { return y(d.value); });
}

But unforunately the point appears on the left of the plot instead of the right and when I look at the source in Firebug for the SVG the entry has a weird x value:

<rect x="function (){}" y="354.9553019925952" width="2" height="45.044698007404804"/>

Can anyone help? Do I need to remove all the plotted data and re-plot it?

Appendix:

In case you need to see x() and y()

var T = (60*60*2*1000); // 1 hurs

var x = d3.scale.linear()
        .domain([0, T])
        .range([0, W]);

var y = d3.scale.linear()
        .domain([d3.min(data, function(d) { return d.value; }), 10])
        .range([0, H]);

原文:https://stackoverflow.com/questions/10405495
更新时间:2022-05-28 16:05

最满意答案

由于您希望表单中的输入值显示为af

表格已成功提交

  1. 在您的数据属性中添加属性,如下所示:

    data(){
                return{
                    showFormValues = false;
                }
            }   
    
  2. 在表单下方的段落标记中添加一个带有输入值的div,并仅在表格成功地使用usimg v-show显示div,如下所示:

    <div v-show="showFormValues">
        <p>date: {{ScheduleInterviewForm.schedule_datetime}}</p>
        <p>type: {{ScheduleInterviewForm.interview_type_id}}</p>
    </div>  
    
  3. 现在在成功部分然后阻止你的表单提交点击方法设置showFormValues = true的值,如下所示:

    ScheduleInterview(form, hrcId) {
        var that = this;
        this.$refs[form].validate((valid) => {
          if (valid) {
            // AJAX: Create HrRequest
            axios.post('/ajax/schedule_interview', {
              interviewTypeId: this.ScheduleInterviewForm.interview_type_id,
              scheduleDatetime: this.ScheduleInterviewForm.schedule_datetime,
              hrcId
            })
            .then(function(res) {
                 that.interviewScheduled = true;
               //show the input form values on succesful form submission
                 that.showFormValues = true;
              setTimeout(() => that.interviewScheduled = false, 3000);
              console.log(res);
              // that.candidates = res.data.candidates;
            })
            .catch(function(err) {
              console.log(err);
            });
          } else {
            return false;
          }
        });
      }, 
    

Since you want the inputed values in the form show up af

ter the form is successfully submitted

  1. Add a property in your data property as below:

    data(){
                return{
                    showFormValues = false;
                }
            }   
    
  2. Add a div with the inputed values in paragraph tags below the form and show the div only if form is sucessfully sunbmitted usimg v-show as below:

    <div v-show="showFormValues">
        <p>date: {{ScheduleInterviewForm.schedule_datetime}}</p>
        <p>type: {{ScheduleInterviewForm.interview_type_id}}</p>
    </div>  
    
  3. Now in the success part then block of your form submittion click method set the value of showFormValues = true like this:

    ScheduleInterview(form, hrcId) {
        var that = this;
        this.$refs[form].validate((valid) => {
          if (valid) {
            // AJAX: Create HrRequest
            axios.post('/ajax/schedule_interview', {
              interviewTypeId: this.ScheduleInterviewForm.interview_type_id,
              scheduleDatetime: this.ScheduleInterviewForm.schedule_datetime,
              hrcId
            })
            .then(function(res) {
                 that.interviewScheduled = true;
               //show the input form values on succesful form submission
                 that.showFormValues = true;
              setTimeout(() => that.interviewScheduled = false, 3000);
              console.log(res);
              // that.candidates = res.data.candidates;
            })
            .catch(function(err) {
              console.log(err);
            });
          } else {
            return false;
          }
        });
      }, 
    

相关问答

更多
  • 你正在寻找道具 您的html元素看起来像 。 然后,您可以通过this.dataToPass在您的vue实例中访问它。 阅读文档,如果传递字符串或表达式(例如来自另一个vue实例),则会有所不同。 在你的例子中:
  • 首先,您必须定义表单操作和方法,如: