This repository has been archived by the owner on Nov 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd3-brush.html
171 lines (137 loc) · 4.79 KB
/
d3-brush.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<!--obtained map projection script https://bl.ocks.org/mbostock/3711652 -->
<!--http://stage-155.brilliant-data.net/webdataconnectors/maps.html-->
<!DOCTYPE html>
<html>
<head>
<!-- adapated from https://bl.ocks.org/mbostock/4062045 -->
<meta charset="utf-8">
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
background: #021617;
height: 95px;
position: relative;
width:1420px;
overflow: hidden;
}
.area {
fill: #00aa7f;
clip-path: url(#clip);
}
.zoom {
cursor: move;
fill: none;
pointer-events: all;
}
.hidden {
opacity: 0.0;
}
.selection {
fill: #283839;
fill-opacity: 0.5;
}
.axis .tick text {
font-family: Arial, Helvetica, sans-serif;
fill: white;
stroke: none;
}
</style>
</head>
<body onload="appApi.initEditor();">
<svg width="1420" height="100"></svg>
<input type="text" id = "htext" class="hidden" value="" />
<script src="https://d3js.org/d3.v4.min.js"></script>
<!-- IMPORTANT! Replace tableau.server to your Tableau server domain -->
<script src="http://tableau.server/javascripts/api/tableau-2.1.1.min.js"></script>
<script src="./d3brush_integration.js"></script>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script type='text/javascript' src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="tableauLib.js"></script>
<script>
var TBLib = new TableauLib();
var svg = d3.select("svg"),
margin2 = {top: 0, right: 20, bottom: 30, left: 40},
width = +svg.attr("width") - margin2.left - margin2.right,
height2 = +svg.attr("height") - margin2.top - margin2.bottom;
var parseDate = d3.timeParse("%Y-%m-%d %H:%M:%S");
var x2 = d3.scaleTime().range([0, width]),
y2 = d3.scaleLinear().range([height2, 0]);
var xAxis2 = d3.axisBottom(x2);
var brush = d3.brushX()
.extent([[0, 0], [width, height2]])
.on("end", brushed); //brush removed because tableau cannot keep up
var area2 = d3.area()
.curve(d3.curveMonotoneX)
.x(function(d) { ; return x2(parseDate(d.date));})
.y0(height2)
.y1(function(d) { return y2(+d.price); });
var context = svg.append("g")
.attr("class", "context")
.attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");
function property (v) {
return function (_) {
if(!arguments.length)
return v;
v = _;
return this;
};
}
function drawBrush(tableauData) {
var d3Data = new Array;
for (j = 0, len = tableauData.length; j < len; j++) {
d3Data[j] = {
// date : tableauData[j]["Order Date"],
date : tableauData[j][xAxisField],
//price : tableauData[j]["AGG(SalesRollingAverage)"]
price : parseFloat(tableauData[j][yAxisField]),
}
}
//uncomment to see what this looks like, we need it to be a javascript array of data
x2.domain(d3.extent(d3Data, function(d) { return parseDate(d.date); }));
y2.domain([0, d3.max(d3Data, function(d) { return +d.price; })]);
context.append("path")
.datum(d3Data)
.attr("class", "area")
.attr("d", area2);
context.append("g")
.attr("class", "axis axis--x")
.attr("font-family", "sans-serif")
.attr("font-size", 18)
.attr("transform", "translate(0," + height2 + ")")
.style("stroke", "#f5f5f5")
.call(xAxis2);
context.append("g")
.attr("class", "brush")
.call(brush)
.call(brush.move, x2.range());
}
function brushed() {
if (d3.event.sourceEvent && d3.event.sourceEvent.type === "zoom") return; // ignore brush-by-zoom
var s = d3.event.selection || x2.range();
var filts = s.map(x2.invert, x2);
//x2.domain(s.map(x2.invert, x2));
//this log is to check on the filts array and make sure it has valid dates
//console.log(filts);
//getCurrentWorksheet().applyRangeFilterAsync("Order Date", {min: filts[0], max: filts[1]},tableauSoftware.FilterUpdateType.REPLACE);
sheet = parent.parent.tableau.VizManager.getVizs()[0].getWorkbook().getActiveSheet();
if(sheet.getSheetType() === 'worksheet')
{
sheet.applyRangeFilterAsync(xAxisField, {min: filts[0], max: filts[1]},tableauSoftware.FilterUpdateType.REPLACE);
}
else
{
worksheetArray = sheet.getWorksheets();
for(var i =0; i < worksheetArray.length; i++)
{
worksheetArray[i].applyRangeFilterAsync(xAxisField, {min: filts[0], max: filts[1]},tableauSoftware.FilterUpdateType.REPLACE);
}
}
//this is where we need to call the filter function to update the tableau view
//need to update this for date range filter
//property.call(vizFilter("Order Date",d.date,"REPLACE"));
}
</script>
</body>
</html>