简体   繁体   English

在Google Charts中使用条形图创建时间轴?

[英]Creating a timeline with a bar chart in Google Charts?

I would like to create a timeline with Google charts to show the duration, repetition, and overlap of events. 我想用Google图表创建一个时间轴,以显示事件的持续时间,重复次数和重叠次数。 Each row should be a new event and the columns should be increments of time. 每行应该是一个新事件,列应该是时间增量。

I've been looking over the Google Charts docs , however, and it doesn't look like what I want is possible - to not only have the length of a horizontal bar be ranged, but to have multiple ranged bars in the same row (non-overlapping). 但是,我一直在查看Google Charts文档 ,但看起来并不太可能-不仅要对水平条的长度进行范围调整,还要在同一行中包含多个范围条(非重叠)。

Is this possible with Chrome or another free chart library? Chrome或其他免费图表库是否有可能?

edit: I would like to produce a chart similar to the one below 编辑:我想产生一个类似于下面的图表 在此输入图像描述

I was 100% confident I could recreate this in Google Visualization, but I fell a wee bit short due to an odd behavior I can't figure out (the red line marking the date). 我有100%的自信我可以在谷歌可视化中重新创建这个,但由于我无法弄清楚的奇怪行为(标记日期的红线),我感觉有点短暂。 If you can solve that issue, the rest works great and looks like this: 如果您可以解决该问题,那么其余的效果很好,如下所示:

示例图像

Here is the code: 这是代码:

function drawVisualization() {
  // Create and populate the data table.
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Assignment');
  data.addColumn('date', 'Dummy');
  data.addColumn('date', 'Current Assignment');
  data.addColumn('date', 'Dummy');
  data.addColumn('date', 'Next Assignment');
  data.addRows([
    ['Uranus', new Date(1298246400000), {v:new Date(26092800000), f:'2/21-12/20'}, new Date(0), {v:new Date(0), f:'1/0-1/0'}],
    ['Thomas Lamonthezie', new Date(1297036800000), {v:new Date(6393600000), f:'2/7-4/22'}, new Date(0), {v:new Date(0), f:'1/0-1/0'}],
    ['Shuo Li', new Date(0), {v:new Date(0), f:'1/0-1/0'}, new Date(1303689600000), {v:new Date(2937600000), f:'4/25-5/29'}],
    ['Saturne', new Date(1298246400000), {v:new Date(4579200000), f:'2/21-4/15'}, new Date(0), {v:new Date(0), f:'1/0-1/0'}],
    ['Roger Erwin', new Date(1301875200000), {v:new Date(1555200000), f:'4/4-4/22'}, new Date(259200000), {v:new Date(2160000000), f:'4/25-5/20'}],
    ['Rick Maverick', new Date(1302652800000), {v:new Date(2592000000), f:'4/13-5/13'}, new Date(0), {v:new Date(0), f:'1/0-1/0'}],
  ]);

  //Define date ranges for the chart
  var dateMin = new Date(2010,11,1);
  var dateMax = new Date(2012,0,1);
  var dateToday = new Date(2012,3,15);


  // Create and draw the visualization.
  new google.visualization.BarChart(document.getElementById('visualization')).
    draw(data,
         {width:600, height:400,
          isStacked: true,
          series: [{color: 'transparent'},{},{color: 'transparent'},{}],
          hAxis: {
            viewWindow: {
              max: dateMax,
              min: dateMin,
            },
            baseline: dateToday,
            baselineColor: 'red',
          },
         }
        );
}

Basically, you create 4 series: 基本上,您创建4个系列:

  1. Dummy series (time until the first assignment) 假系列(第一次转让的时间)
  2. Duration of the first assignment 第一次作业的持续时间
  3. Dummy series (time between the first and second assignment) 虚拟系列(第一次和第二次分配之间的时间)
  4. Duration of the second assignment 第二次作业的持续时间

Then you set series 1 and 3 to be invisible. 然后将系列1和3设置为不可见。

For the data, you have to do some date math (I did it in Excel, apologies), but basically you just get the duration by subtracting the start/end date for each assignment, and set the dummy as the start date of the second assignment minus time until the first assignment plus the duration of the first assignment. 对于数据,您必须执行一些日期数学运算(我在Excel中就这样做了,很抱歉),但是基本上您只是通过减去每个作业的开始/结束日期来获得持续时间,并将虚拟对象设置为第二个的开始日期作业减去第一次作业的时间加上第一次作业的持续时间。

I used the {value: x, format: y} trick to make the mouseover work properly. 我使用{value: x, format: y}技巧使鼠标悬停正常工作。 You can automate this (again, I did the data stuff in excel as my javascript would have taken significantly longer). 你可以自动化这个(再次,我在excel中做了数据,因为我的javascript会花费更长的时间)。

Then the settings set the minimum date, the maximum date, turn the series to be the proper colors, and set the baseline to red. 然后,设置将设置最小日期,最大日期,将系列设置为适当的颜色,并将基线设置为红色。 Unfortunately, for reasons beyond my understanding, the baseline no matter the date set wants to live on the left side of the chart. 不幸的是,由于我理解的原因,无论日期设置如何,基线都希望生活在图表的左侧。 If you can find a way around that, you have your chart. 如果您能找到解决方法,则可以找到图表。

your dateToday is out of range change to change var dateToday = new Date(2011,7,1), another workaround i create a extra column, that will be my datetoday and is set to new Date(2011,6,1) 你的dateToday超出范围更改以更改var dateToday = new Date(2011,7,1),另一个解决方法我创建了一个额外的列,这将是我的datetoday并设置为新的Date(2011,6,1)

data.addColumn('string', 'Assignment');
data.addColumn('date', 'Dummy');
data.addColumn('date', 'Current Assignment');
data.addColumn('date', 'Dummy');
data.addColumn('date', 'Next Assignment');
data.addColumn('date', 'Date of Report');
data.addRows([
['Uranus', new Date(1298246400000), {v:new Date(26092800000), f:'2/21-12/20'}, new Date(0), {v:new Date(0), f:'1/0-1/0'},new Date(2011,6,1) ],
['Thomas Lamonthezie', new Date(1297036800000), {v:new Date(6393600000), f:'2/7-4/22'}, new Date(0), {v:new Date(0), f:'1/0-1/0'},new Date(2011,6,1) ],
['Shuo Li', new Date(0), {v:new Date(0), f:'1/0-1/0'}, new Date(1303689600000), {v:new Date(2937600000), f:'4/25-5/29'},new Date(2011,6,1)],
['Saturne', new Date(1298246400000), {v:new Date(4579200000), f:'2/21-4/15'}, new Date(0), {v:new Date(0), f:'1/0-1/0'},new Date(2011,6,1)],
['Roger Erwin', new Date(1301875200000), {v:new Date(1555200000), f:'4/4-4/22'}, new Date(259200000), {v:new Date(2160000000), f:'4/25-5/20'},new Date(2011,6,1)],
['Rick Maverick', new Date(1302652800000), {v:new Date(2592000000), f:'4/13-5/13'}, new Date(0), {v:new Date(0), f:'1/0-1/0'},new Date(2011,6,1)],
]);

change the series 改变系列

series: [{color: 'transparent'},{},{color: 'transparent'},{}]

to

series: [{color: 'transparent'},{},{color: 'transparent'},{},{type: "line"}]

and i use google playground to replicate. 我使用谷歌操场复制。

One question your dates are timestamp? 一个问题,您的日期是时间戳?

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

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