- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个包含以下内容的数据表:
id,day,proj,col1,col2,subtype,time
1,Nov 28,projectA,1050,880,foo,17481
2,Nov 28,projectA,1050,880,bar,16098
3,Nov 28,projectA,1080,40,foo,13509
4,Nov 28,projectA,1080,40,bar,9031
但想创建一个新的旋转 DataView 包含:
id,day,proj,col1,col2,foo,bar
1,Nov 28,projectA,1050,880,17481,16098
3,Nov 28,projectA,1080,40,13509,9031
然后我想为其创建一个堆叠的 columnChart。
查询语言中有一个 pivot 子句,但如何对 DataTable 中已有的数据进行 pivot?
最佳答案
手动。
可以看到this example在 asgallant 的 jsfiddle 上。这使用数据 View 来完成任务。
google.load('visualization', '1', {packages: ['table']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'A');
data.addColumn('string', 'B');
data.addColumn('number', 'C');
data.addRows([
[1, 'foo', 6],
[2, 'foo', 2],
[3, 'foo', 1],
[4, 'foo', 3],
[1, 'bar', 7],
[2, 'bar', 3],
[1, 'baz', 8],
[2, 'baz', 4]
]);
var table1 = new google.visualization.Table(document.getElementById('table1'));
table1.draw(data, {});
/* manually pivot the data table
* set column A as the first column in the view,
* then we have to separate out the C values into their own columns
* according to the value of B, using a DataView with calculated columns
*/
var view = new google.visualization.DataView(data);
view.setColumns([0, {
type: 'number',
label: 'foo',
calc: function (dt, row) {
// return values of C only for the rows where B = "foo"
return (dt.getValue(row, 1) == 'foo') ? dt.getValue(row, 2) : null;
}
}, {
type: 'number',
label: 'bar',
calc: function (dt, row) {
// return values of C only for the rows where B = "bar"
return (dt.getValue(row, 1) == 'bar') ? dt.getValue(row, 2) : null;
}
}, {
type: 'number',
label: 'baz',
calc: function (dt, row) {
// return values of C only for the rows where B = "baz"
return (dt.getValue(row, 1) == 'baz') ? dt.getValue(row, 2) : null;
}
}]);
// next, we group the view on column A, which gets us the pivoted data
var pivotedData = google.visualization.data.group(view, [0], [{
column: 1,
type: 'number',
label: view.getColumnLabel(1),
aggregation: google.visualization.data.sum
}, {
column: 2,
type: 'number',
label: view.getColumnLabel(2),
aggregation: google.visualization.data.sum
}, {
column: 3,
type: 'number',
label: view.getColumnLabel(3),
aggregation: google.visualization.data.sum
}]);
var table2 = new google.visualization.Table(document.getElementById('table2'));
table2.draw(pivotedData, {});
}
或者,您可以手动进行。
var data = new google.visualization.DataTable();
data.addColumn('string', 'First Column Title');
var baseline = chartData.getValue(chartData.getNumberOfRows() - 1, 15);
for (var i = 0; i < chartData.getNumberOfRows(); i++) {
data.addColumn('number', chartData.getFormattedValue(i, 0));
};
for (var j = 0; j < chartData.getNumberOfColumns() - 2; j++) {
data.addRow();
data.setValue(j, 0, chartData.getColumnLabel(j + 1));
for (var i = 0; i < chartData.getNumberOfRows(); i++) {
data.setValue(j, i + 1, chartData.getValue(i, j+1));
};
};
关于javascript - 从现有的谷歌图表 DataTable 对象创建数据透视表对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13610893/
我正在尝试使用透视投影描绘一个立方体,但我得到的只是一个正方形的角。正方形的面设置在原点并向正方向扩展。使用 glOrtho 我可以设置坐标系,但我在使用 glPerspective 做同样的事情时遇
SELECT j.departure, stopDepartures.* FROM journey j JOIN journey_day ON journey_day.journey = j.id J
我确实需要一些帮助来了解如何根据相似的值对表格进行透视。 day | startDate ----------------------- Monday | 09:00 Monday |
我有以下数据框 df = pd.DataFrame({ '1': ['Mon (07/08)','Sales', '2'], '2': ['Mon (07/0
dummy_df = pd.DataFrame({ 'accnt' : [101, 102, 103, 104, 101, 102, 103, 104, 101, 102, 103, 104,
public class MainActivity extends Activity { LinearLayout rotator; protected void onCreate(Bundle sa
我正在尝试通过 PHP 更改 ImageMagick 中 Plane2Cylinder 失真的视角。 为了帮助解释我在寻找什么,我制作了这张图: 您可以看到红色 block 的下部比顶部的半径更大,就
我有一个像这样的简单查询.. USE AdventureWorks; GO SELECT DaysToManufacture, AVG(StandardCost) AS AverageCost FRO
我希望我可以更改架构,但我受制于它,假设我有以下表格 JanDataTable FebDataTable MarDataTable ProductsTable 其中前三个表有 ID 和 Amount
我正在将我们的一个旧应用程序从 vb6 更新到 c#,在此过程中必须重新创建原始程序员设计的自定义控件。该控件简单地获取对象的尺寸,矩形或圆锥形,并在 3D 中放置对象的轮廓草图(我认为在技术上是 2
我一直在尝试在 MySQL 中对表进行透视(将行移动到列)。我知道 mysql 没有枢纽功能,所以我认为需要联合,但不是 100% 确定。我有三列,user_id、option_id 和 Questi
我正在尝试旋转像这样创建的 mysql 表 'CREATE TABLE `fundreturns` ( `Timestamp` datetime NOT NULL, `FundName` varcha
提前感谢任何对此提供帮助的人。我知道我以前做过这件事,没有太多痛苦,但似乎找不到解决方案 我的数据库看起来像这样: `tbl_user: ---------- id ( pkey )
我正在尝试开发 X 轴方向的卡片翻转动画。截至目前,div 现在只需使用 rotateX() 方法进行旋转。我试过对上层 div 使用透视属性,而不是工作它扭曲了我的 div 结构。因为,这只是一个工
我有一个带有 CSS3 透视图的 DIV 元素。 DIV 包含 2 个子 DIV,其中之一在 z 轴上有平移。这应该会导致一个 DIV 在另一个前面,因此后面的那个应该被挡住。 然而,这些 DIV 的
大家好,我有一张这样的 map http://sinanisler.com/demo/map/ 如您所见,有一些树,但不是真正的视角,我想要这个 http://sinanisler.com/demo/
我有以下代码将快照拍摄到帧缓冲区。我验证了帧缓冲区工作正常并且相机正确地面向对象。我曾经正确地完成图片,但它是基于错误的代码,使用了错误的截锥体。所以我决定重新开始(使用截锥体)。 物体以中间为中心,
我正在尝试将求和列添加到透视数据框,但不断收到数据解析错误。 mydata = [{'amount': 3200, 'close_date':'2013-03-31', 'customer': 'Cu
我正在尝试将一些 groupby/crosstabbing 逻辑应用于用户定义对象的 IEnumerable 列表,并且想知道是否有人可以帮助我。我坚持使用现有的(相当烦人的)对象模型来工作,但无论如
我想使用一个 CALayer 创建如下图所示的效果 - 而不是通过拆分图像、对两半进行透视变换然后将它们并排放置。 可以使用 CoreImage 以任何方式完成吗? 或者,有人可以使用 OpenGL
我是一名优秀的程序员,十分优秀!