简体   繁体   English

plot javascript 交叉过滤器维度的各个方面(使用 d3 和交叉过滤器的数量与时间)

[英]plot individual aspects of a javascript crossfilter dimension (quantity vs time using d3 and crossfilter)

I am trying to plot a line graph of some quantity, in this case, "bananas", vs Time using D3 and crossfilter.我正在尝试 plot 一些数量的折线图,在本例中为“香蕉”,与使用 D3 和交叉过滤器的时间对比。 The dimension aspect of the plot does not seem to reflect the dimension values that I am inputting, though is possible that this is a conceptual issue with crossfilter, as I am new to using it. plot 的尺寸方面似乎没有反映我输入的尺寸值,尽管这可能是交叉过滤器的概念问题,因为我是新手。 below is the script that I am using and the output plot, as you can see the y-axis runs 0-6 and there are 11 unique inputs for banana and time.下面是我正在使用的脚本和 output plot,您可以看到 y 轴运行 0-6,香蕉和时间有 11 个独特的输入。 Other examples that I have looked at for this type of plot appear to plot the equivalent to the quantity aspect of bananas (which runs from 8 to 668 in this case), so I was hoping for some clarity on what exactly is on my Y-axis and how to re-write my code such that it is actually plotting the number of bananas consumed as a function of time.我为这种类型的 plot 看到的其他示例在 plot 看来相当于香蕉的数量方面(在这种情况下从 8 到 668),所以我希望能弄清楚我的 Y-轴以及如何重写我的代码,以便它实际上将消耗的香蕉数量绘制为时间的 function。

    //declare new variable, chart and select the dc chart type 
    var chart =     new dc.LineChart("#lineChart");
//insert the data you wish to plot 
     var data1 = [ 
     {"bananas": 22.28876521596685, "Timestamp":'2021-01-19 17:06:50.239197'},
     {"bananas": 25.8395210863092,  "Timestamp": '2021-01-19 17:16:52.181954'},
     {"bananas": 23.11122495696,  "Timestamp": '2021-01-20 17:21:52.181954'},
      {"bananas": 28.8395210863092,  "Timestamp": '2021-01-21 17:26:52.181954'},
      {"bananas": 38.32323232232,  "Timestamp": '2021-01-21 17:26:52.181954'},
      {"bananas": 248.8395210863092,  "Timestamp": '2021-01-21 17:26:52.181954'},
      {"bananas": 8.122345566789, "Timestamp": '2021-01-21 17:26:52.181954'},
      {"bananas": 9.8395210863092,  "Timestamp": '2021-01-21 17:26:52.181954'},
      {"bananas": 58.86904272742, "Timestamp": '2021-01-21 17:26:52.181954'},
      {"bananas": 668.642100932,  "Timestamp": '2021-01-22 17:26:52.181954'},
      {"bananas": 68.8395210863092,  "Timestamp": '2021-01-21 18:26:52.181954'},
     ];

// convert the timestamp data into something that js can understand 
    data1.forEach(function (d) {
        d.Timestamp = new Date(d.Timestamp);
    }); 
    //log the updated values to the console (troubleshooting)
    console.log(data1)


    // set crossfilter with first dataset
    var xfilter = crossfilter(data1),
   // var all = xfilter.groupAll();

        //Define the Dimensions 
            bananasDim = xfilter.dimension(function (d){return +d.bananas;})
            console.log(bananasDim.top(Infinity)) 
            //this prints out the entire data1 json in the console 
            TimeDim = xfilter.dimension(function (d){return d.Timestamp;});     
            
       //Define the Group elements 
         bananasGroup= bananasDim.group()
         TimeGroup=TimeDim.group()
         console.log(bananasGroup)
         console.log(TimeGroup)

    function render_plots(){
 
         chart
            .width(768)
            .height(480)
            .x(d3.scaleTime().domain([0,200])) //this defines a range of 0 to 200
            // but we use elasticX so its not really relevant 
            .curve(d3.curveLinear)
            .renderArea(true)
            .brushOn(false)
            .renderDataPoints(true)
            .clipPadding(10)
            .elasticX(true)
            .elasticY(true)
            .dimension(bananasDim)
            .group(TimeGroup);

        dc.renderAll();
    }

    render_plots();

香蕉与时间情节

Thanks for including a complete working example with your question.感谢您在问题中包含完整的工作示例。

That looks like a plot of counts by time.这看起来像是按时间计数的 plot。 If you want to sum the bananas, you could use如果你想总结香蕉,你可以使用

     TimeGroup=TimeDim.group().reduceSum(d => d.bananas)

Additionally, for a specific chart, the group should usually be instantiated from the same dimension:此外,对于特定图表,组通常应从相同维度实例化:

        .dimension(TimeDim)

This is because the group will be used for fetching the data and the dimension will be used for filtering.这是因为组将用于获取数据,而维度将用于过滤。

You will probably also want to set the resolution so that times which are not exactly the same will aggregate.您可能还需要设置分辨率,以便聚合不完全相同的时间。 For example you could do:例如你可以这样做:

        TimeDim = xfilter.dimension(function (d){return d3.timeHour(d.Timestamp);});     
      // ...
      chart
        .xUnits(d3.timeHours)

to round to hours (similar for days).四舍五入到小时(几天类似)。

香蕉/小时截图

demo fiddle演示小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM